Skip to content

Commit

Permalink
feat(client): webhook url support on queue submit (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Nov 10, 2023
1 parent 0a18c7e commit 7f2bb5e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libs/client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client",
"version": "0.5.1",
"version": "0.5.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
27 changes: 22 additions & 5 deletions libs/client/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ type QueueSubscribeOptions = {
logs?: boolean;
};

/**
* Options for submitting a request to the queue.
*/
type SubmitOptions<Input> = RunOptions<Input> & {
/**
* The URL to send a webhook notification to when the request is completed.
* @see WebHookResponse
*/
webhookUrl?: string;
};

type BaseQueueOptions = {
/**
* The unique identifier for the enqueued request.
Expand Down Expand Up @@ -184,7 +195,10 @@ interface Queue {
* @param options - Options to configure how the request is run.
* @returns A promise that resolves to the result of enqueuing the request.
*/
submit<Input>(id: string, options: RunOptions<Input>): Promise<EnqueueResult>;
submit<Input>(
id: string,
options: SubmitOptions<Input>
): Promise<EnqueueResult>;

/**
* Retrieves the status of a specific request in the queue.
Expand Down Expand Up @@ -221,13 +235,16 @@ interface Queue {
export const queue: Queue = {
async submit<Input>(
id: string,
options: RunOptions<Input>
options: SubmitOptions<Input>
): Promise<EnqueueResult> {
const path = options.path ?? '';
const { webhookUrl, path = '', ...runOptions } = options;
const query = webhookUrl
? '?' + new URLSearchParams({ fal_webhook: webhookUrl }).toString()
: '';
return run(id, {
...options,
...runOptions,
method: 'post',
path: '/fal/queue/submit' + path,
path: '/fal/queue/submit' + path + query,
});
},
async status(
Expand Down
6 changes: 5 additions & 1 deletion libs/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export { withMiddleware, withProxy } from './middleware';
export type { RequestMiddleware } from './middleware';
export { ApiError, ValidationError } from './response';
export type { ResponseHandler } from './response';
export type { QueueStatus, ValidationErrorInfo } from './types';
export type {
QueueStatus,
ValidationErrorInfo,
WebHookResponse,
} from './types';
29 changes: 29 additions & 0 deletions libs/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,32 @@ export type ValidationErrorInfo = {
loc: Array<string | number>;
type: string;
};

/**
* Represents the response from a WebHook request.
* This is a union type that varies based on the `status` property.
*
* @template Payload - The type of the payload in the response. It defaults to `any`,
* allowing for flexibility in specifying the structure of the payload.
*/
export type WebHookResponse<Payload = any> =
| {
/** Indicates a successful response. */
status: 'OK';
/** The payload of the response, structure determined by the Payload type. */
payload: Payload;
/** Error is never present in a successful response. */
error: never;
/** The unique identifier for the request. */
request_id: string;
}
| {
/** Indicates an unsuccessful response. */
status: 'ERROR';
/** The payload of the response, structure determined by the Payload type. */
payload: Payload;
/** Description of the error that occurred. */
error: string;
/** The unique identifier for the request. */
request_id: string;
};

0 comments on commit 7f2bb5e

Please sign in to comment.