import {z} from 'zod' export type PartnerWebhook = {id?: string | undefined, entity?: 'webhook' | undefined, owner_id?: string | undefined, owner_type?: string | undefined, url?: string | undefined, alert_email?: string | undefined, secret?: string | undefined, secret_exists?: boolean | undefined, active?: boolean | undefined, events?: {[key: string]: boolean} | undefined, service?: string | undefined, context?: Array | undefined, disabled_at?: number | undefined, created_at?: number | undefined, updated_at?: number | undefined}; export type PatchApiV2AccountsAccountIdWebhooksWebhookIdArgs = {account_id: string, webhook_id: string, body: PartnerWebhook}; export type PatchApiV2AccountsAccountIdWebhooksWebhookIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const partnerWebhook = z.object({id: z.string().optional(), entity: z.literal("webhook").optional(), owner_id: z.string().optional(), owner_type: z.string().optional(), url: z.string().optional(), alert_email: z.string().optional(), secret: z.string().optional(), secret_exists: z.boolean().optional(), active: z.boolean().optional(), events: z.record(z.string(), z.boolean()).optional(), service: z.string().optional(), context: z.array(z.string()).optional(), disabled_at: z.number().int().optional(), created_at: z.number().int().optional(), updated_at: 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 }; /** * Update a webhook * * PATCH /v2/accounts/{account_id}/webhooks/{webhook_id} */ export const patchApiV2AccountsAccountIdWebhooksWebhookId = async (args: PatchApiV2AccountsAccountIdWebhooksWebhookIdArgs, options?: PatchApiV2AccountsAccountIdWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.razorpay.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.account_id))}/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(partnerWebhook, await res.json()) };