import {z} from 'zod' export type CreateApiV2PublishDestinationArgs = {destination: string, 'Content-Type'?: string | undefined, 'Upstash-Forward-*'?: 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-Not-Before'?: number | undefined, 'Upstash-Label'?: string | undefined, 'Upstash-Flow-Control-Key'?: string | undefined, 'Upstash-Flow-Control-Value'?: string | undefined, 'Upstash-Deduplication-Id'?: string | undefined, 'Upstash-Content-Based-Deduplication'?: ('true' | 'false') | 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, body: Record}; export type CreateApiV2PublishDestinationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const publishResponse = z.object({messageId: z.string().optional(), deduplicated: z.boolean().optional()}); export const publishToUrlGroupResponse = z.object({messageId: z.string().optional(), deduplicated: z.boolean().optional(), url: z.string().optional()}); export const createApiV2PublishDestinationResponse = z.union([publishResponse, z.object({messages: z.array(publishToUrlGroupResponse).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 }; /** * Publish a Message * * POST /v2/publish/{destination} * * Also accepts auth: bearerAuthQuery */ export const createApiV2PublishDestination = async (args: CreateApiV2PublishDestinationArgs, options?: CreateApiV2PublishDestinationOptions) => { 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['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args['Upstash-Forward-*'] != null) headers.set("Upstash-Forward-*", String(args['Upstash-Forward-*'])) 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-Not-Before'] != null) headers.set("Upstash-Not-Before", String(args['Upstash-Not-Before'])) if (args['Upstash-Label'] != null) headers.set("Upstash-Label", String(args['Upstash-Label'])) if (args['Upstash-Flow-Control-Key'] != null) headers.set("Upstash-Flow-Control-Key", String(args['Upstash-Flow-Control-Key'])) if (args['Upstash-Flow-Control-Value'] != null) headers.set("Upstash-Flow-Control-Value", String(args['Upstash-Flow-Control-Value'])) if (args['Upstash-Deduplication-Id'] != null) headers.set("Upstash-Deduplication-Id", String(args['Upstash-Deduplication-Id'])) if (args['Upstash-Content-Based-Deduplication'] != null) headers.set("Upstash-Content-Based-Deduplication", String(args['Upstash-Content-Based-Deduplication'])) 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/publish/${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(createApiV2PublishDestinationResponse, await res.json()) };