import {z} from 'zod' export type CreateApiV2SchedulesDestinationArgs = {destination: string, 'Upstash-Cron': string, 'Upstash-Schedule-Id'?: string | undefined, 'Content-Type'?: string | undefined, 'Upstash-Method'?: ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE') | undefined, 'Upstash-Timeout'?: string | undefined, 'Upstash-Retries'?: number | undefined, 'Upstash-Retry-Delay'?: string | undefined, 'Upstash-Delay'?: string | undefined, 'Upstash-Forward-*'?: string | undefined, 'Upstash-Callback'?: string | undefined, 'Upstash-Callback-Forward-*'?: string | undefined, 'Upstash-Callback-*'?: string | undefined, 'Upstash-Failure-Callback'?: string | undefined, 'Upstash-Failure-Callback-Forward-*'?: string | undefined, 'Upstash-Failure-Callback-*'?: string | undefined, 'Upstash-Redact-Fields'?: string | undefined, 'Upstash-Label'?: string | undefined, body: Record}; export type CreateApiV2SchedulesDestinationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2SchedulesDestinationResponse = z.object({scheduleId: z.string().optional()}); 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 a Schedule * * POST /v2/schedules/{destination} * * Also accepts auth: bearerAuthQuery */ export const createApiV2SchedulesDestination = async (args: CreateApiV2SchedulesDestinationArgs, options?: CreateApiV2SchedulesDestinationOptions) => { const {baseUrl = "https://qstash-eu-central-1.upstash.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Upstash-Cron'] != null) headers.set("Upstash-Cron", String(args['Upstash-Cron'])) if (args['Upstash-Schedule-Id'] != null) headers.set("Upstash-Schedule-Id", String(args['Upstash-Schedule-Id'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args['Upstash-Method'] != null) headers.set("Upstash-Method", String(args['Upstash-Method'])) if (args['Upstash-Timeout'] != null) headers.set("Upstash-Timeout", String(args['Upstash-Timeout'])) if (args['Upstash-Retries'] != null) headers.set("Upstash-Retries", String(args['Upstash-Retries'])) if (args['Upstash-Retry-Delay'] != null) headers.set("Upstash-Retry-Delay", String(args['Upstash-Retry-Delay'])) if (args['Upstash-Delay'] != null) headers.set("Upstash-Delay", String(args['Upstash-Delay'])) if (args['Upstash-Forward-*'] != null) headers.set("Upstash-Forward-*", String(args['Upstash-Forward-*'])) if (args['Upstash-Callback'] != null) headers.set("Upstash-Callback", String(args['Upstash-Callback'])) if (args['Upstash-Callback-Forward-*'] != null) headers.set("Upstash-Callback-Forward-*", String(args['Upstash-Callback-Forward-*'])) if (args['Upstash-Callback-*'] != null) headers.set("Upstash-Callback-*", String(args['Upstash-Callback-*'])) if (args['Upstash-Failure-Callback'] != null) headers.set("Upstash-Failure-Callback", String(args['Upstash-Failure-Callback'])) if (args['Upstash-Failure-Callback-Forward-*'] != null) headers.set("Upstash-Failure-Callback-Forward-*", String(args['Upstash-Failure-Callback-Forward-*'])) if (args['Upstash-Failure-Callback-*'] != null) headers.set("Upstash-Failure-Callback-*", String(args['Upstash-Failure-Callback-*'])) if (args['Upstash-Redact-Fields'] != null) headers.set("Upstash-Redact-Fields", String(args['Upstash-Redact-Fields'])) if (args['Upstash-Label'] != null) headers.set("Upstash-Label", String(args['Upstash-Label'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/schedules/${encodeURIComponent(String(args.destination))}${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(createApiV2SchedulesDestinationResponse, await res.json()) };