import {z} from 'zod' export type QueueTypeName = {type: 'task' | 'custom', name: string}; export type BulkActionFilter = {status?: ('PENDING_VERSION' | 'QUEUED' | 'EXECUTING' | 'REATTEMPTING' | 'FROZEN' | 'COMPLETED' | 'CANCELED' | 'FAILED' | 'CRASHED' | 'INTERRUPTED' | 'SYSTEM_FAILURE' | Array<'PENDING_VERSION' | 'QUEUED' | 'EXECUTING' | 'REATTEMPTING' | 'FROZEN' | 'COMPLETED' | 'CANCELED' | 'FAILED' | 'CRASHED' | 'INTERRUPTED' | 'SYSTEM_FAILURE'>) | undefined, taskIdentifier?: (string | Array) | undefined, version?: (string | Array) | undefined, from?: (string | number) | undefined, to?: (string | number) | undefined, period?: string | undefined, bulkAction?: string | undefined, tag?: (string | Array) | undefined, schedule?: string | undefined, isTest?: boolean | undefined, batch?: string | undefined, queue?: (QueueTypeName | Array) | undefined, machine?: (string | Array) | undefined, region?: (string | Array) | undefined}; export type CreateBulkActionRequestBody = {action: 'cancel', filter: BulkActionFilter, name?: string | undefined} | {action: 'cancel', runIds: Array, name?: string | undefined} | {action: 'replay', filter: BulkActionFilter, name?: string | undefined, targetRegion?: string | undefined} | {action: 'replay', runIds: Array, name?: string | undefined, targetRegion?: string | undefined}; export type CreateApiApiV1BulkActionsArgs = {body: CreateBulkActionRequestBody}; export type CreateApiApiV1BulkActionsOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createBulkActionResponse = z.object({id: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create bulk action * * POST /api/v1/bulk-actions */ export const createApiApiV1BulkActions = async (args: CreateApiApiV1BulkActionsArgs, options?: CreateApiApiV1BulkActionsOptions) => { const {baseUrl = "https://api.trigger.dev", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/bulk-actions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createBulkActionResponse, await res.json()) };