import {z} from 'zod' export type DeleteApiV2DlqArgs = {dlqIds?: Array | undefined, cursor?: string | undefined, messageId?: string | undefined, url?: Array | undefined, topicName?: Array | undefined, scheduleId?: Array | undefined, queueName?: Array | undefined, fromDate?: number | undefined, toDate?: number | undefined, responseStatus?: Array | undefined, callerIp?: Array | undefined, label?: Array | undefined, flowControlKey?: Array | undefined, count?: number | undefined}; export type DeleteApiV2DlqOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const deleteApiV2DlqResponse = z.object({cursor: z.string().optional(), deleted: z.number().int().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 }; /** * Bulk Delete DLQ messages * * DELETE /v2/dlq * * Also accepts auth: bearerAuthQuery */ export const deleteApiV2Dlq = async (args: DeleteApiV2DlqArgs, options?: DeleteApiV2DlqOptions) => { const {baseUrl = "https://qstash-eu-central-1.upstash.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.dlqIds ?? []) query.append("dlqIds", String(item)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.messageId != null) query.append("messageId", String(args.messageId)) for (const item of args.url ?? []) query.append("url", String(item)) for (const item of args.topicName ?? []) query.append("topicName", String(item)) for (const item of args.scheduleId ?? []) query.append("scheduleId", String(item)) for (const item of args.queueName ?? []) query.append("queueName", String(item)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) for (const item of args.responseStatus ?? []) query.append("responseStatus", String(item)) for (const item of args.callerIp ?? []) query.append("callerIp", String(item)) for (const item of args.label ?? []) query.append("label", String(item)) for (const item of args.flowControlKey ?? []) query.append("flowControlKey", String(item)) if (args.count != null) query.append("count", String(args.count)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/dlq${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(deleteApiV2DlqResponse, await res.json()) };