diff --git a/libs/client/package.json b/libs/client/package.json index e372661..4c5ed4e 100644 --- a/libs/client/package.json +++ b/libs/client/package.json @@ -1,7 +1,7 @@ { "name": "@fal-ai/serverless-client", "description": "The fal serverless JS/TS client", - "version": "0.3.2", + "version": "0.4.0", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/function.ts b/libs/client/src/function.ts index dca7c25..72ac996 100644 --- a/libs/client/src/function.ts +++ b/libs/client/src/function.ts @@ -9,7 +9,7 @@ import { isUUIDv4, isValidUrl } from './utils'; */ type RunOptions = { /** - * The path to the function, if any. Defaults to `/`. + * The path to the function, if any. Defaults to ``. */ readonly path?: string; @@ -126,19 +126,24 @@ export async function subscribe( if (options.onEnqueue) { options.onEnqueue(requestId); } + const path = options.path ?? ''; return new Promise((resolve, reject) => { let timeoutId: ReturnType; const pollInterval = options.pollInterval ?? 1000; const poll = async () => { try { - const requestStatus = await queue.status(id, requestId, options.logs ?? false); + const requestStatus = await queue.status(id, { + requestId, + logs: options.logs ?? false, + path, + }); if (options.onQueueUpdate) { options.onQueueUpdate(requestStatus); } if (requestStatus.status === 'COMPLETED') { clearTimeout(timeoutId); try { - const result = await queue.result(id, requestId); + const result = await queue.result(id, { requestId, path }); resolve(result); } catch (error) { reject(error); @@ -179,6 +184,26 @@ type QueueSubscribeOptions = { /** * If `true`, the response will include the logs for the request. + * Defaults to `false`. + */ + logs?: boolean; +}; + +type BaseQueueOptions = { + /** + * The unique identifier for the enqueued request. + */ + requestId: string; + /** + * The path to the function, if any. Defaults to ``. + */ + path?: string; +}; + +type QueueStatusOptions = BaseQueueOptions & { + /** + * If `true`, the response will include the logs for the request. + * Defaults to `false`. */ logs?: boolean; }; @@ -201,20 +226,19 @@ interface Queue { * Retrieves the status of a specific request in the queue. * * @param id - The ID or URL of the function web endpoint. - * @param requestId - The unique identifier for the enqueued request. - * @param logs - If `true`, the response will include the logs for the request. + * @param options - Options to configure how the request is run. * @returns A promise that resolves to the status of the request. */ - status(id: string, requestId: string, logs: boolean): Promise; + status(id: string, options: QueueStatusOptions): Promise; /** * Retrieves the result of a specific request from the queue. * * @param id - The ID or URL of the function web endpoint. - * @param requestId - The unique identifier for the enqueued request. + * @param options - Options to configure how the request is run. * @returns A promise that resolves to the result of the request. */ - result(id: string, requestId: string): Promise; + result(id: string, options: BaseQueueOptions): Promise; /** * @deprecated Use `fal.subscribe` instead. @@ -235,21 +259,32 @@ export const queue: Queue = { id: string, options: RunOptions ): Promise { - return run(id, { ...options, method: 'post', path: '/fal/queue/submit/' }); + const path = options.path ?? ''; + return run(id, { + ...options, + method: 'post', + path: '/fal/queue/submit' + path, + }); }, - async status(id: string, requestId: string, logs = false): Promise { + async status( + id: string, + { requestId, logs = false, path = '' }: QueueStatusOptions + ): Promise { return run(id, { method: 'get', - path: `/fal/queue/requests/${requestId}/status`, + path: `/fal/queue/requests/${requestId}/status${path}`, input: { logs: logs ? '1' : '0', }, }); }, - async result(id: string, requestId: string): Promise { + async result( + id: string, + { requestId, path = '' }: BaseQueueOptions + ): Promise { return run(id, { method: 'get', - path: `/fal/queue/requests/${requestId}/response`, + path: `/fal/queue/requests/${requestId}/response${path}`, }); }, subscribe,