feat: introduce priority for subscribe and submit

This commit is contained in:
Ruslan Kuprieiev 2024-10-21 22:22:24 +03:00
parent 83e21ef9ca
commit 56e398d230
2 changed files with 19 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import {
} from "./types"; } from "./types";
import { parseEndpointId } from "./utils"; import { parseEndpointId } from "./utils";
export type QueuePriority = "low" | "normal";
export type QueueStatusSubscriptionOptions = QueueStatusOptions & export type QueueStatusSubscriptionOptions = QueueStatusOptions &
Omit<QueueSubscribeOptions, "onEnqueue" | "webhookUrl">; Omit<QueueSubscribeOptions, "onEnqueue" | "webhookUrl">;
@ -71,6 +72,12 @@ export type QueueSubscribeOptions = {
* @see WebHookResponse * @see WebHookResponse
*/ */
webhookUrl?: string; webhookUrl?: string;
/**
* The priority of the request. It defaults to `normal`.
* @see QueuePriority
*/
priority?: QueuePriority;
} & ( } & (
| { | {
mode?: "polling"; mode?: "polling";
@ -102,6 +109,12 @@ export type SubmitOptions<Input> = RunOptions<Input> & {
* @see WebHookResponse * @see WebHookResponse
*/ */
webhookUrl?: string; webhookUrl?: string;
/**
* The priority of the request. It defaults to `normal`.
* @see QueuePriority
*/
priority?: QueuePriority;
}; };
type BaseQueueOptions = { type BaseQueueOptions = {
@ -216,7 +229,7 @@ export const createQueueClient = ({
endpointId: string, endpointId: string,
options: SubmitOptions<Input>, options: SubmitOptions<Input>,
): Promise<InQueueQueueStatus> { ): Promise<InQueueQueueStatus> {
const { webhookUrl, ...runOptions } = options; const { webhookUrl, priority, ...runOptions } = options;
const input = options.input const input = options.input
? await storage.transformInput(options.input) ? await storage.transformInput(options.input)
: undefined; : undefined;
@ -227,6 +240,9 @@ export const createQueueClient = ({
subdomain: "queue", subdomain: "queue",
query: webhookUrl ? { fal_webhook: webhookUrl } : undefined, query: webhookUrl ? { fal_webhook: webhookUrl } : undefined,
}), }),
headers: {
"x-fal-queue-priority": priority ?? "normal",
},
input: input as Input, input: input as Input,
config, config,
}); });

View File

@ -18,6 +18,7 @@ type RequestParams<Input = any> = {
input?: Input; input?: Input;
config: RequiredConfig; config: RequiredConfig;
options?: RequestOptions & RequestInit; options?: RequestOptions & RequestInit;
headers?: Record<string, string>;
}; };
export async function dispatchRequest<Input, Output>( export async function dispatchRequest<Input, Output>(
@ -39,6 +40,7 @@ export async function dispatchRequest<Input, Output>(
const { method, url, headers } = await requestMiddleware({ const { method, url, headers } = await requestMiddleware({
method: (params.method ?? options.method ?? "post").toUpperCase(), method: (params.method ?? options.method ?? "post").toUpperCase(),
url: targetUrl, url: targetUrl,
headers: params.headers,
}); });
const authHeader = credentials ? { Authorization: `Key ${credentials}` } : {}; const authHeader = credentials ? { Authorization: `Key ${credentials}` } : {};
const requestHeaders = { const requestHeaders = {