import {z} from 'zod' export type PatchedWebhookData = {url?: string | undefined, description?: (string | null) | undefined, enabled_events?: (Array<'all' | 'shipment_purchased' | 'shipment_cancelled' | 'shipment_fulfilled' | 'shipment_out_for_delivery' | 'shipment_needs_attention' | 'shipment_delivery_failed' | 'tracker_created' | 'tracker_updated' | 'order_created' | 'order_updated' | 'order_fulfilled' | 'order_cancelled' | 'order_delivered' | 'batch_queued' | 'batch_failed' | 'batch_running' | 'batch_completed'>) | undefined, disabled?: (boolean | null) | undefined}; export type PatchApiV1WebhooksIdArgs = {id: string, body: PatchedWebhookData}; export type PatchApiV1WebhooksIdOptions = {baseUrl: string, TokenBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({id: z.string().optional(), url: z.string(), description: z.string().nullable().optional(), enabled_events: z.array(z.enum(["all", "shipment_purchased", "shipment_cancelled", "shipment_fulfilled", "shipment_out_for_delivery", "shipment_needs_attention", "shipment_delivery_failed", "tracker_created", "tracker_updated", "order_created", "order_updated", "order_fulfilled", "order_cancelled", "order_delivered", "batch_queued", "batch_failed", "batch_running", "batch_completed"])), disabled: z.boolean().nullable().optional(), object_type: z.string().optional(), last_event_at: z.string().nullable().optional(), secret: z.string(), test_mode: z.boolean()}); 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 }; /** * Update a webhook * * PATCH /v1/webhooks/{id} * * Also accepts auth: Token, OAuth2, JWT */ export const patchApiV1WebhooksId = async (args: PatchApiV1WebhooksIdArgs, options: PatchApiV1WebhooksIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, TokenBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${TokenBasic.username}:${TokenBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/webhooks/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(webhook, await res.json()) };