Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
htunnicliff committed Oct 11, 2024
1 parent c28db26 commit 6bf2616
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 113 deletions.
7 changes: 7 additions & 0 deletions biome.json → biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
"recommended": true,
"style": {
"noNonNullAssertion": "off"
},
// Remove these once remedied:
"suspicious": {
"noExplicitAny": "warn"
},
"complexity": {
"noBannedTypes": "warn"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const knownCapabilities = {
SearchSnippet: "urn:ietf:params:jmap:mail",
Identity: "urn:ietf:params:jmap:submission",
EmailSubmission: "urn:ietf:params:jmap:submission",
VacationResponse: "urn:ietf:params:jmap:vacationresponse",
VacationResponse: "urn:ietf:params:jmap:vacationresponse"
};

/**
Expand All @@ -25,7 +25,7 @@ const entityMatcher = /^(\w+)\//;
*/
export function getCapabilitiesForMethodCalls({
methodNames,
availableCapabilities,
availableCapabilities
}: {
methodNames: Iterable<string>;
availableCapabilities: ReadonlyMap<string, string>;
Expand Down
90 changes: 44 additions & 46 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import {
getCapabilitiesForMethodCalls,
knownCapabilities,
knownCapabilities
} from "./capabilities.ts";
import {
expandURITemplate,
getErrorFromInvocation,
getResultsForMethodCalls,
getResultsForMethodCalls
} from "./helpers.ts";
import {
WithRefValues,
WithoutRefValues,
buildRequestsFromDrafts,
type DraftsProxy,
type InvocationDraft,
type WithRefValues,
type WithoutRefValues,
buildRequestsFromDrafts
} from "./request-drafts.ts";
import {
type GetArgs,
type GetResponseData,
type LocalInvocation,
type Meta,
type Methods,
type ProxyAPI,
type RequestOptions,
import type {
GetArgs,
GetResponseData,
LocalInvocation,
Meta,
Methods,
ProxyAPI,
RequestOptions
} from "./types/contracts.ts";
import type * as JMAP from "./types/jmap.ts";
import type * as JMAPMail from "./types/jmap-mail.ts";
import type * as JMAP from "./types/jmap.ts";

export type ClientConfig = {
/**
Expand Down Expand Up @@ -75,7 +75,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {

this.capabilities = new Map<string, string>([
...Object.entries(config.customCapabilities ?? {}),
...Object.entries(knownCapabilities),
...Object.entries(knownCapabilities)
]);

this.session = JamClient.loadSession(config.sessionUrl, this.authHeader);
Expand All @@ -91,9 +91,9 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
return fetch(sessionUrl, {
headers: {
Authorization: authHeader,
Accept: "application/json",
Accept: "application/json"
},
cache: "no-cache",
cache: "no-cache"
}).then((res) => res.json());
}

Expand All @@ -111,7 +111,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const {
using = [],
fetchInit,
createdIds: createdIdsInput,
createdIds: createdIdsInput
} = options ?? {};

// Assemble method call
Expand All @@ -122,12 +122,12 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
using: [
...getCapabilitiesForMethodCalls({
methodNames: [method],
availableCapabilities: this.capabilities,
availableCapabilities: this.capabilities
}),
...using,
...using
],
methodCalls: [invocation],
createdIds: createdIdsInput,
createdIds: createdIdsInput
};

// Ensure session is loaded (if not already)
Expand All @@ -139,10 +139,10 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
headers: {
Authorization: this.authHeader,
Accept: "application/json",
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(body),
...fetchInit,
...fetchInit
});

// Handle 4xx-5xx errors
Expand All @@ -160,7 +160,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const {
methodResponses: [methodResponse],
sessionState,
createdIds,
createdIds
} = (await response.json()) as JMAP.Response<[JMAP.Invocation<Data>]>;

const error = getErrorFromInvocation(methodResponse);
Expand All @@ -173,8 +173,8 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
{
sessionState,
createdIds,
response,
},
response
}
];
}

Expand Down Expand Up @@ -206,12 +206,12 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
using: [
...getCapabilitiesForMethodCalls({
methodNames,
availableCapabilities: this.capabilities,
availableCapabilities: this.capabilities
}),
...using,
...using
],
methodCalls,
createdIds: createdIdsInput,
createdIds: createdIdsInput
};

// Ensure session is loaded (if not already)
Expand All @@ -223,10 +223,10 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
headers: {
Authorization: this.authHeader,
Accept: "application/json",
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(body),
...fetchInit,
...fetchInit
});

// Handle 4xx-5xx errors
Expand All @@ -249,7 +249,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const meta: Meta = {
sessionState,
createdIds,
response,
response
};

const errors = methodResponses
Expand All @@ -263,7 +263,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
return [
// @ts-expect-error TODO
getResultsForMethodCalls(methodResponses, { returnErrors: false }),
meta,
meta
];
}

Expand All @@ -285,7 +285,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const { uploadUrl } = await this.session;

const params: JMAP.BlobUploadParams = {
accountId,
accountId
};

const url = expandURITemplate(uploadUrl, params);
Expand All @@ -295,18 +295,17 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
method: "POST",
headers: {
Authorization: this.authHeader,
Accept: "application/json",
Accept: "application/json"
},
body,
...fetchInit,
...fetchInit
});

if (!response.ok) {
if (response.headers.get("Content-Type")?.includes("json")) {
throw (await response.json()) as JMAP.ProblemDetails;
} else {
throw await response.text();
}
throw await response.text();
}

return (await response.json()) as JMAP.BlobUploadResponse;
Expand All @@ -333,7 +332,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
accountId: options.accountId,
blobId: options.blobId,
type: options.mimeType,
name: options.fileName,
name: options.fileName
};

const url = expandURITemplate(downloadUrl, params);
Expand All @@ -342,17 +341,16 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: this.authHeader,
Authorization: this.authHeader
},
...fetchInit,
...fetchInit
});

if (!response.ok) {
if (response.headers.get("Content-Type")?.includes("json")) {
throw (await response.json()) as JMAP.ProblemDetails;
} else {
throw await response.text();
}
throw await response.text();
}

return response;
Expand All @@ -372,7 +370,7 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {
const params: JMAP.EventSourceArguments = {
types: options.types === "*" ? "*" : options.types.join(","),
closeafter: options.closeafter ?? "no",
ping: `${options.ping}`,
ping: `${options.ping}`
};

const { eventSourceUrl } = await this.session;
Expand Down Expand Up @@ -411,9 +409,9 @@ export class JamClient<Config extends ClientConfig = ClientConfig> {

return this.request([method, args], options);
};
},
}
}
),
)
});
}

Expand Down
21 changes: 10 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function expandURITemplate(
let expanded = template;

for (const [key, value] of Object.entries(params)) {
const parameter = "{" + key + "}";
const parameter = `{${key}}`;
if (!expanded.includes(parameter)) {
throw new Error(`Template "${template}" is missing parameter: ${key}`);
}
Expand Down Expand Up @@ -58,18 +58,17 @@ export function getResultsForMethodCalls(
id,
{
data: null,
error: data as ProblemDetails,
},
];
} else {
return [
id,
{
data,
error: null,
},
error: data as ProblemDetails
}
];
}
return [
id,
{
data,
error: null
}
];
})
);
}
Expand Down
18 changes: 9 additions & 9 deletions src/request-drafts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ExcludeValue, IncludeValue } from "./helpers.ts";
import { LocalInvocation, Methods, Requests } from "./types/contracts.ts";
import type { LocalInvocation, Methods, Requests } from "./types/contracts.ts";
import type {
Invocation,
ExtendedJSONPointer,
ResultReference,
Invocation,
ResultReference
} from "./types/jmap.ts";

/**
Expand Down Expand Up @@ -43,8 +43,8 @@ export class InvocationDraft<I = unknown> {
return {
[r]: {
path,
invocation: this.#invocation,
},
invocation: this.#invocation
}
};
}

Expand Down Expand Up @@ -92,8 +92,8 @@ export class InvocationDraft<I = unknown> {
{
name: invocation[0], // Ref method
resultOf: invocationToId.get(invocation)!, // Ref ID
path,
} satisfies ResultReference,
path
} satisfies ResultReference
];
}

Expand Down Expand Up @@ -145,9 +145,9 @@ export function buildRequestsFromDrafts<

return new InvocationDraft(invocation);
};
},
}
}
),
)
});

// Create invocation drafts
Expand Down
Loading

0 comments on commit 6bf2616

Please sign in to comment.