import {z} from 'zod' export type CreateApiMessagesMessageIdCancelArgs = {message_id: string}; export type CreateApiMessagesMessageIdCancelOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const messageStatus = z.enum(["CANCELED", "CLICKED", "DELAYED", "DELIVERED", "DIGESTED", "ENQUEUED", "FILTERED", "OPENED", "ROUTED", "SENT", "SIMULATED", "THROTTLED", "UNDELIVERABLE", "UNMAPPED", "UNROUTABLE"]); export const reason = z.enum(["BOUNCED", "FAILED", "FILTERED", "NO_CHANNELS", "NO_PROVIDERS", "OPT_IN_REQUIRED", "PROVIDER_ERROR", "UNPUBLISHED", "UNSUBSCRIBED"]); export const messageDetails = z.object({id: z.string(), status: messageStatus, enqueued: z.number().int(), sent: z.number().int().optional(), delivered: z.number().int().optional(), opened: z.number().int().optional(), clicked: z.number().int().optional(), recipient: z.string(), event: z.string(), notification: z.string(), error: z.string().nullable().optional(), reason: reason.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 }; /** * Cancel message * * POST /messages/{message_id}/cancel */ export const createApiMessagesMessageIdCancel = async (args: CreateApiMessagesMessageIdCancelArgs, options?: CreateApiMessagesMessageIdCancelOptions) => { const {baseUrl = "https://api.courier.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/messages/${encodeURIComponent(String(args.message_id))}/cancel${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(messageDetails, await res.json()) };