import {z} from 'zod' export type UpdateApiV2WebhooksWebhookNanoidArgs = {webhook_nanoid: string, body: {url?: string | undefined, events?: Array | undefined, secret?: string | undefined, is_active?: boolean | undefined, name?: string | undefined, description?: string | undefined}}; export type UpdateApiV2WebhooksWebhookNanoidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2WebhooksWebhookNanoidResponse = z.object({success: z.literal(true), data: z.object({webhook_nanoid: z.string().min(15).max(15), webhook_version: z.enum(["v1", "v2"]), url: z.string(), events: z.array(z.string()), is_active: z.boolean(), name: z.string().nullable(), description: z.string().nullable()})}); 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 (v1/v2 cannot be changed; register a new endpoint to switch version) * * PUT /v2/webhooks/{webhook_nanoid} */ export const updateApiV2WebhooksWebhookNanoid = async (args: UpdateApiV2WebhooksWebhookNanoidArgs, options?: UpdateApiV2WebhooksWebhookNanoidOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks/${encodeURIComponent(String(args.webhook_nanoid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV2WebhooksWebhookNanoidResponse, await res.json()) };