diff --git a/src/backend.ts b/src/backend.ts index cd2f1778..9e2b2b65 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -154,7 +154,7 @@ export interface Backend { args: Parameters, scheduleFor: Date, discardAfter: Date | undefined, - metadata: { [key: string]: string } | undefined + metadata: { [key: string]: string } | undefined, ): Promise; getExecution(id: string): Promise; getExecutionResult(id: string): Promise; @@ -162,15 +162,15 @@ export interface Backend { reRunExecution(id: string): Promise; rescheduleExecution( id: string, - scheduleFor: Date + scheduleFor: Date, ): Promise; listExecutions( page?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise; listExecutionAttempts( id: string, page?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise; } diff --git a/src/backend/local.ts b/src/backend/local.ts index 3d2506a9..17f25acc 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -86,7 +86,7 @@ const banner = ` function paginate( page: PageRequest | undefined, - records: Map + records: Map, ): PageResult { let edges = Array.from(records.keys()).reverse(); let hasNextPage: boolean = false; @@ -137,7 +137,7 @@ function paginate( function isExecutionMatchFilter( filters: ExecutionFilters | undefined, - execution: InternalExecution + execution: InternalExecution, ): boolean { if ( filters?.states && @@ -163,13 +163,13 @@ function isExecutionMatchFilter( if (filters?.metadata && filters.metadata.length > 0 && execution.metadata) { const metadataFilters = filters.metadata.filter( - (mdFilter) => mdFilter.values.length > 0 + (mdFilter) => mdFilter.values.length > 0, ); if ( !metadataFilters.some((mdFilter) => mdFilter.values.some( - (value) => execution.metadata[mdFilter.key] === value - ) + (value) => execution.metadata[mdFilter.key] === value, + ), ) ) { return false; @@ -264,7 +264,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); return execution; - } + }, ); if (shouldDiscard) { @@ -320,7 +320,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); execution.errorCode = errorCode; return execution; - } + }, ); }; @@ -342,7 +342,7 @@ export async function enqueue( args: Parameters, scheduleFor: Date, discardAfter: Date | undefined, - metadata: { [key: string]: string } | undefined + metadata: { [key: string]: string } | undefined, ): Promise { let functionId = functionIdMapping.get(func.__metadata.name); if (functionId === undefined) { @@ -396,7 +396,7 @@ export async function getExecutionResult(id: string): Promise { export async function cancelExecution( id: string, - force: boolean + force: boolean, ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -407,7 +407,7 @@ export async function cancelExecution( switch (execution.state) { case "aborting": throw new ExecutionAbortingAlreadyInProgress( - "aborting execution already in progress" + "aborting execution already in progress", ); case "created": execution.state = "cancelled"; @@ -417,7 +417,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state` + `cannot cancel execution in "${execution.state}" state`, ); } } else { @@ -427,7 +427,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state` + `cannot cancel execution in "${execution.state}" state`, ); } } @@ -441,7 +441,7 @@ export async function cancelExecution( export async function rescheduleExecution( id: string, - scheduleFor: Date + scheduleFor: Date, ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -460,7 +460,7 @@ export async function rescheduleExecution( } export async function reRunExecution( - id: string + id: string, ): Promise { const execution = await executionsStore.get(id); if (execution === undefined) @@ -487,7 +487,7 @@ export async function reRunExecution( export async function listExecutions( pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); @@ -505,7 +505,7 @@ export async function listExecutions( export async function listExecutionAttempts( id: string, pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); diff --git a/src/backend/local/kv.ts b/src/backend/local/kv.ts index 74429aa7..12d9a65e 100644 --- a/src/backend/local/kv.ts +++ b/src/backend/local/kv.ts @@ -33,7 +33,7 @@ export class KV { async transaction( key: string, - setFunc: (value: T) => Promise + setFunc: (value: T) => Promise, ): Promise { const cur = (await this.get(key)) as T; const updated = await setFunc(cur); diff --git a/src/backend/remote.ts b/src/backend/remote.ts index a158564f..d2d7810f 100644 --- a/src/backend/remote.ts +++ b/src/backend/remote.ts @@ -153,7 +153,7 @@ export async function enqueue( args: Parameters, scheduleFor: Date, discardAfter: Date | undefined, - metadata: { [key: string]: string } | undefined + metadata: { [key: string]: string } | undefined, ): Promise { const httpClient = newClientFromEnv(); const request: CreateExecutionRequest = { @@ -167,7 +167,7 @@ export async function enqueue( const { status, response } = await httpClient( "PUT", "/public/v2/executions", - stringify(request) + stringify(request), ); if (status === 201) return newExecution(response.data); @@ -175,7 +175,7 @@ export async function enqueue( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } @@ -183,7 +183,7 @@ export async function getExecution(id: string): Promise { const httpClient = newClientFromEnv(); const { status, response } = await httpClient( "GET", - `/public/v2/executions/${id}` + `/public/v2/executions/${id}`, ); if (status === 200) return newExecution(response.data); @@ -193,7 +193,7 @@ export async function getExecution(id: string): Promise { throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } @@ -201,7 +201,7 @@ export async function getExecutionResult(id: string): Promise { const httpClient = newClientFromEnv(); const { status, response } = await httpClient( "GET", - `/public/v2/executions/${id}/results` + `/public/v2/executions/${id}/results`, ); if (status === 200) return response; @@ -213,20 +213,20 @@ export async function getExecutionResult(id: string): Promise { throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } export async function cancelExecution( id: string, - force: boolean + force: boolean, ): Promise { const httpClient = newClientFromEnv(); const request: CancelExecutionRequest = { force: force }; const { status, response } = await httpClient( "POST", `/public/v2/executions/${id}/cancellation`, - stringify(request) + stringify(request), ); if (status === 200) return newExecution(response.data); @@ -240,13 +240,13 @@ export async function cancelExecution( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } export async function rescheduleExecution( id: string, - scheduleFor: Date + scheduleFor: Date, ): Promise { const httpClient = newClientFromEnv(); const request: RescheduleExecutionRequest = { @@ -255,7 +255,7 @@ export async function rescheduleExecution( const { status, response } = await httpClient( "PATCH", `/public/v2/executions/${id}/schedule`, - stringify(request) + stringify(request), ); if (status === 200) return newExecution(response.data); @@ -267,19 +267,19 @@ export async function rescheduleExecution( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } export async function reRunExecution( - id: string + id: string, ): Promise { const httpClient = newClientFromEnv(); const request: ReRunExecutionRequest = {}; const { status, response } = await httpClient( "POST", `/public/v2/executions/${id}/reruns`, - stringify(request) + stringify(request), ); if (status === 200) return newExecution(response.data); @@ -289,13 +289,13 @@ export async function reRunExecution( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } export async function listExecutions( pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const httpClient = newClientFromEnv(); const request: ListExecutionsRequest = { @@ -315,7 +315,7 @@ export async function listExecutions( const { status, response } = await httpClient( "POST", `/public/v2/executions`, - stringify(request) + stringify(request), ); if (status === 200) { @@ -336,14 +336,14 @@ export async function listExecutions( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } export async function listExecutionAttempts( id: string, pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const httpClient = newClientFromEnv(); const request: ListExecutionAttemptsRequest = { @@ -363,7 +363,7 @@ export async function listExecutionAttempts( const { status, response } = await httpClient( "POST", `/public/v2/executions/${id}/attempts`, - stringify(request) + stringify(request), ); if (status === 200) { @@ -385,6 +385,6 @@ export async function listExecutionAttempts( throw new DeferError( `backend responds with "${status}" and message "${ (response as any).message - }"` + }"`, ); } diff --git a/src/backend/remote/httpClient.ts b/src/backend/remote/httpClient.ts index 76646390..4da9592c 100644 --- a/src/backend/remote/httpClient.ts +++ b/src/backend/remote/httpClient.ts @@ -19,7 +19,7 @@ import VERSION from "../../version.js"; export type HTTPClient = ( method: string, path: string, - body?: string | null + body?: string | null, ) => Promise<{ status: number; response: T }>; const basicAuth = (username: string, password: string) => { @@ -37,12 +37,12 @@ export class ClientError extends DeferError { export function makeHTTPClient( apiEndpoint: string, accessToken: string, - clientOptions?: RequestInit + clientOptions?: RequestInit, ): HTTPClient { return async ( method: string, path: string, - body: string | null = null + body: string | null = null, ): Promise<{ status: number; response: T }> => { let endpoint; diff --git a/src/index.ts b/src/index.ts index 681dd8f7..ef97ee9b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,7 +103,7 @@ function defaultRetryPolicy(): RetryPolicy { } function parseRetryPolicy( - options?: DeferredFunctionConfiguration + options?: DeferredFunctionConfiguration, ): RetryPolicy { const retryPolicy: RetryPolicy = defaultRetryPolicy(); switch (typeof options?.retry) { @@ -183,7 +183,7 @@ async function enqueue( args, scheduleFor, discardAfter, - metadata + metadata, ); info("execution enqueued", { @@ -203,7 +203,7 @@ async function enqueue( */ export function defer( fn: F, - config?: DeferredFunctionConfiguration + config?: DeferredFunctionConfiguration, ): DeferredFunction { const wrapped: DeferredFunction = async ( ...args: Parameters @@ -232,7 +232,7 @@ export function defer( defer.cron = function ( fn: F, cronExpr: string, - config?: DeferredFunctionConfiguration + config?: DeferredFunctionConfiguration, ): DeferredFunction { const wrapped: DeferredFunction = async ( ...args: Parameters @@ -262,7 +262,7 @@ defer.cron = function ( */ export function delay( fn: DeferredFunction, - delay: Duration | Date + delay: Duration | Date, ): DeferredFunction { return assignOptions(fn, { delay }); } @@ -277,7 +277,7 @@ export function delay( */ export function addMetadata( fn: DeferredFunction, - metadata: ExecutionMetadata + metadata: ExecutionMetadata, ): DeferredFunction { return assignOptions(fn, { metadata }); } @@ -292,7 +292,7 @@ export function addMetadata( */ export function discardAfter( fn: DeferredFunction, - value: Duration | Date + value: Duration | Date, ): DeferredFunction { return assignOptions(fn, { discardAfter: value }); } @@ -306,7 +306,7 @@ export function discardAfter( */ export function assignOptions( fn: DeferredFunction, - options: ExecutionOptions + options: ExecutionOptions, ): DeferredFunction { const wrapped: DeferredFunction = async ( ...args: Parameters @@ -358,7 +358,7 @@ export async function getExecutionResult(id: string): Promise { */ export async function cancelExecution( id: string, - force: boolean = false + force: boolean = false, ): Promise { return backend.cancelExecution(id, force); } @@ -375,7 +375,7 @@ export async function cancelExecution( */ export async function rescheduleExecution( id: string, - value?: Duration | Date | undefined + value?: Duration | Date | undefined, ): Promise { const now = new Date(); let scheduleFor: Date; @@ -400,7 +400,7 @@ export async function rescheduleExecution( * @returns {Promise} */ export async function reRunExecution( - id: string + id: string, ): Promise { return backend.reRunExecution(id); } @@ -415,10 +415,10 @@ export async function reRunExecution( * @returns {Promise} */ export async function getExecutionTries( - id: string + id: string, ): Promise { warn( - `"getExecutionTries" is deprecated and will be removed in future versions. Please use "listExecutionAttempts" instead.` + `"getExecutionTries" is deprecated and will be removed in future versions. Please use "listExecutionAttempts" instead.`, ); return listExecutionAttempts(id); } @@ -436,7 +436,7 @@ export async function getExecutionTries( export async function listExecutionAttempts( id: string, page?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { return backend.listExecutionAttempts(id, page, filters); } @@ -451,7 +451,7 @@ export async function listExecutionAttempts( */ export async function listExecutions( page?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { return backend.listExecutions(page, filters); } @@ -468,7 +468,7 @@ export async function listExecutions( * @returns {Promise>} */ export function awaitResult>( - fn: DeferredFunction + fn: DeferredFunction, ): (...args: Parameters) => Promise> { return async function (...args: Parameters): Promise> { const enqueueResponse = await enqueue(fn, ...args); @@ -482,7 +482,7 @@ export function awaitResult>( case "failed": { const result = await getExecutionResult(enqueueResponse.id); let error = new DeferError( - `execution ${enqueueResponse.id} has failed` + `execution ${enqueueResponse.id} has failed`, ); if (result?.message) { error = new DeferError(result.message); @@ -498,7 +498,7 @@ export function awaitResult>( case "cancelled": case "discarded": throw new DeferError( - `execution "${enqueueResponse.id}" was "${response.state}"` + `execution "${enqueueResponse.id}" was "${response.state}"`, ); default: await sleep(jitter(i) * 1000); diff --git a/src/next/asNextRoute.ts b/src/next/asNextRoute.ts index 2a11b2e8..41ea3438 100644 --- a/src/next/asNextRoute.ts +++ b/src/next/asNextRoute.ts @@ -13,7 +13,7 @@ export interface DeferNextRoute { interface Options< F extends (...args: any) => Promise, - R = ReturnType extends Promise ? RR : ReturnType + R = ReturnType extends Promise ? RR : ReturnType, > { proxy?: (request: NextRequest) => Promise>; } @@ -22,7 +22,7 @@ const ResponseJSON = Response.json; export function asNextRoute Promise>( deferFn: DeferredFunction, - options?: Options + options?: Options, ): DeferNextRoute { return { GetHandler: async (request: NextRequest) => { @@ -42,21 +42,21 @@ export function asNextRoute Promise>( { id, error: e.toString() }, { status: 500, - } + }, ); } else { return ResponseJSON( { id, error: "Unexpected error." }, { status: 500, - } + }, ); } } } else { return ResponseJSON( { error: "missing `id` query parameter from `useDeferRoute()`" }, - { status: 400 } + { status: 400 }, ); } }, @@ -76,7 +76,7 @@ export function asNextRoute Promise>( options?.proxy ? "- check your `proxy()`" : "" }`, }, - { status: 400 } + { status: 400 }, ); } }, diff --git a/src/next/useDeferRoute.ts b/src/next/useDeferRoute.ts index 670e9e32..ee809e90 100644 --- a/src/next/useDeferRoute.ts +++ b/src/next/useDeferRoute.ts @@ -9,7 +9,7 @@ export type UseDeferRoute = [ loading: boolean; result: R; error?: Error | undefined; - } + }, ]; // const [uploadFile, { loading, result: fileName, error }] = useDeferRoute("/api/upload", { refreshInterval: 1000 }); @@ -22,14 +22,14 @@ export const useDeferRoute = < : any, HP extends boolean = false, R = ReturnType extends Promise ? RR : ReturnType, - A extends any[] = Parameters + A extends any[] = Parameters, >( routePath: string, { refreshInterval }: { hasProxy: HP; refreshInterval: number } = { refreshInterval: 500, // @ts-expect-error to refine hasProxy: false, - } + }, ): UseDeferRoute => { const [status, setStatus] = useState(); const [result, setResult] = useState(); @@ -70,10 +70,10 @@ export const useDeferRoute = < const data = await result.json(); intervalRef.current = setInterval( pollExecution(data.id), - Math.max(500, refreshInterval) + Math.max(500, refreshInterval), ) as unknown as number; }, - [pollExecution, refreshInterval] + [pollExecution, refreshInterval], ); return [