import {z} from 'zod' export type UpdateWebhookRequest = {url?: (string | null) | undefined, subscription_events?: Array | undefined, active?: (boolean | null) | undefined}; export type PatchApiWebhooksWebhookIdArgs = {'Authorization-Signature': string, webhook_id: string, body: UpdateWebhookRequest}; export type PatchApiWebhooksWebhookIdOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookResponse = z.object({id: z.string(), url: z.string(), webhook_secret: z.string().optional(), subscription_events: z.array(z.string()).optional(), active: 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 Webhook (partial) * * PATCH /webhooks/{webhook_id} */ export const patchApiWebhooksWebhookId = async (args: PatchApiWebhooksWebhookIdArgs, options?: PatchApiWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.coinvoyage.io/v3", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Authorization-Signature'] != null) headers.set("Authorization-Signature", String(args['Authorization-Signature'])) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.webhook_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(webhookResponse, await res.json()) };