Skip to content

Commit

Permalink
Fix slow types
Browse files Browse the repository at this point in the history
  • Loading branch information
htunnicliff committed May 2, 2024
1 parent 1a1c66d commit c3fdb36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
19 changes: 11 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getResultsForMethodCalls,
} from "./helpers.ts";
import {
WithRevValues,
WithRefValues,
WithoutRefValues,
buildRequestsFromDrafts,
type DraftsProxy,
Expand Down Expand Up @@ -84,7 +84,10 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
/**
* Retrieve fresh session data
*/
static async loadSession(sessionUrl: string, authHeader: string) {
static async loadSession(
sessionUrl: string,
authHeader: string
): Promise<JMAP.Session> {
return fetch(sessionUrl, {
headers: {
Authorization: authHeader,
Expand Down Expand Up @@ -185,7 +188,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
[
{
[MethodId in keyof Returning]: Returning[MethodId] extends InvocationDraft<
infer Inv extends [Methods, WithRevValues<Record<string, any>>]
infer Inv extends [Methods, WithRefValues<Record<string, any>>]
>
? GetResponseData<Inv[0], WithoutRefValues<Inv[1]>>
: never;
Expand Down Expand Up @@ -267,7 +270,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
/**
* Get the ID of the primary mail account for the current session
*/
async getPrimaryAccount() {
async getPrimaryAccount(): Promise<string> {
return (await this.session).primaryAccounts?.["urn:ietf:params:jmap:mail"];
}

Expand All @@ -278,7 +281,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
accountId: JMAP.BlobUploadParams["accountId"],
body: BodyInit,
fetchInit: RequestInit = {}
) {
): Promise<JMAP.BlobUploadResponse> {
const { uploadUrl } = await this.session;

const params: JMAP.BlobUploadParams = {
Expand Down Expand Up @@ -323,7 +326,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
fileName: JMAP.BlobDownloadParams["name"];
},
fetchInit: RequestInit = {}
) {
): Promise<Response> {
const { downloadUrl } = await this.session;

const params: JMAP.BlobDownloadParams = {
Expand Down Expand Up @@ -365,7 +368,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
types: "*" | Array<JMAPMail.Entity>;
ping: number;
closeafter?: JMAP.EventSourceArguments["closeafter"];
}) {
}): Promise<EventSource> {
const params: JMAP.EventSourceArguments = {
types: options.types === "*" ? "*" : options.types.join(","),
closeafter: options.closeafter ?? "no",
Expand Down Expand Up @@ -396,7 +399,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
* });
* ```
*/
get api() {
get api(): ProxyAPI {
return new Proxy<ProxyAPI>({} as ProxyAPI, {
get: (_, entity: string) =>
new Proxy(
Expand Down
24 changes: 16 additions & 8 deletions src/request-drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import type { ExcludeValue, IncludeValue } from "./helpers.ts";
import { LocalInvocation, Methods, Requests } from "./types/contracts.ts";
import type { Invocation, JSONPointer, ResultReference } from "./types/jmap.ts";

export type Ref<I = unknown> = ReturnType<InvocationDraft<I>["$ref"]>;

export type WithRevValues<T> = IncludeValue<T, Ref>;

export type WithoutRefValues<T> = ExcludeValue<T, Ref>;

/**
* Symbol used to identify arguments that need to be transformed
* into JMAP result references
*/
const r = Symbol("Result Reference");

export type Ref<I = unknown> = {
[r]: {
path: `/${string}`;
invocation: I;
};
};

export type WithRefValues<T> = IncludeValue<T, Ref>;

export type WithoutRefValues<T> = ExcludeValue<T, Ref>;

/**
* These instances represent partially-formed method calls
* used within `requestMany`. They are transformed into standard
Expand All @@ -30,7 +35,7 @@ export class InvocationDraft<I = unknown> {
* Create a result reference that points to the result
* of a previous invocation.
*/
$ref(path: JSONPointer) {
$ref(path: JSONPointer): Ref<I> {
return {
[r]: {
path,
Expand All @@ -53,7 +58,10 @@ export class InvocationDraft<I = unknown> {
*/
static createInvocationsFromDrafts<T extends Record<string, InvocationDraft>>(
drafts: T
) {
): {
methodCalls: Invocation[];
methodNames: Set<string>;
} {
// Track method names
const methodNames = new Set<string>();

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import type { HeaderParsedForm } from "./types/jmap-mail.ts";
export function headerField<
Name extends string,
Form extends keyof HeaderParsedForm
>(name: Name, form: Form) {
>(name: Name, form: Form): `header:${Name}:as${Form}` {
return `header:${name}:as${form}` as const;
}

export function allHeaderFields<
Name extends string,
Form extends keyof HeaderParsedForm
>(name: Name, form: Form) {
>(name: Name, form: Form): `header:${Name}:as${Form}:all` {
const header = headerField(name, form);

return `${header}:all` as const;
Expand Down

0 comments on commit c3fdb36

Please sign in to comment.