Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): webhook url support on queue submit #26

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -25,7 +25,7 @@
response_url: string;
};

export function isQueueStatus(obj: any): obj is QueueStatus {

Check warning on line 28 in libs/client/src/types.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return obj && obj.status && obj.response_url;
}

Expand All @@ -34,3 +34,32 @@
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> =

Check warning on line 45 in libs/client/src/types.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
| {
/** 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;
};
Loading