From c3fdb3638eea0c7ecd6616f108c5e00b1775cf78 Mon Sep 17 00:00:00 2001 From: Hunter Tunnicliff Date: Thu, 2 May 2024 16:33:19 -0700 Subject: [PATCH] Fix slow types --- src/client.ts | 19 +++++++++++-------- src/request-drafts.ts | 24 ++++++++++++++++-------- src/utils.ts | 4 ++-- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/client.ts b/src/client.ts index 94b055c..212e013 100644 --- a/src/client.ts +++ b/src/client.ts @@ -8,7 +8,7 @@ import { getResultsForMethodCalls, } from "./helpers.ts"; import { - WithRevValues, + WithRefValues, WithoutRefValues, buildRequestsFromDrafts, type DraftsProxy, @@ -84,7 +84,10 @@ export class JamClient { /** * Retrieve fresh session data */ - static async loadSession(sessionUrl: string, authHeader: string) { + static async loadSession( + sessionUrl: string, + authHeader: string + ): Promise { return fetch(sessionUrl, { headers: { Authorization: authHeader, @@ -185,7 +188,7 @@ export class JamClient { [ { [MethodId in keyof Returning]: Returning[MethodId] extends InvocationDraft< - infer Inv extends [Methods, WithRevValues>] + infer Inv extends [Methods, WithRefValues>] > ? GetResponseData> : never; @@ -267,7 +270,7 @@ export class JamClient { /** * Get the ID of the primary mail account for the current session */ - async getPrimaryAccount() { + async getPrimaryAccount(): Promise { return (await this.session).primaryAccounts?.["urn:ietf:params:jmap:mail"]; } @@ -278,7 +281,7 @@ export class JamClient { accountId: JMAP.BlobUploadParams["accountId"], body: BodyInit, fetchInit: RequestInit = {} - ) { + ): Promise { const { uploadUrl } = await this.session; const params: JMAP.BlobUploadParams = { @@ -323,7 +326,7 @@ export class JamClient { fileName: JMAP.BlobDownloadParams["name"]; }, fetchInit: RequestInit = {} - ) { + ): Promise { const { downloadUrl } = await this.session; const params: JMAP.BlobDownloadParams = { @@ -365,7 +368,7 @@ export class JamClient { types: "*" | Array; ping: number; closeafter?: JMAP.EventSourceArguments["closeafter"]; - }) { + }): Promise { const params: JMAP.EventSourceArguments = { types: options.types === "*" ? "*" : options.types.join(","), closeafter: options.closeafter ?? "no", @@ -396,7 +399,7 @@ export class JamClient { * }); * ``` */ - get api() { + get api(): ProxyAPI { return new Proxy({} as ProxyAPI, { get: (_, entity: string) => new Proxy( diff --git a/src/request-drafts.ts b/src/request-drafts.ts index 251698c..4c609a8 100644 --- a/src/request-drafts.ts +++ b/src/request-drafts.ts @@ -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 = ReturnType["$ref"]>; - -export type WithRevValues = IncludeValue; - -export type WithoutRefValues = ExcludeValue; - /** * Symbol used to identify arguments that need to be transformed * into JMAP result references */ const r = Symbol("Result Reference"); +export type Ref = { + [r]: { + path: `/${string}`; + invocation: I; + }; +}; + +export type WithRefValues = IncludeValue; + +export type WithoutRefValues = ExcludeValue; + /** * These instances represent partially-formed method calls * used within `requestMany`. They are transformed into standard @@ -30,7 +35,7 @@ export class InvocationDraft { * Create a result reference that points to the result * of a previous invocation. */ - $ref(path: JSONPointer) { + $ref(path: JSONPointer): Ref { return { [r]: { path, @@ -53,7 +58,10 @@ export class InvocationDraft { */ static createInvocationsFromDrafts>( drafts: T - ) { + ): { + methodCalls: Invocation[]; + methodNames: Set; + } { // Track method names const methodNames = new Set(); diff --git a/src/utils.ts b/src/utils.ts index 0e9672a..83099ae 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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;