import {z} from 'zod' export type WebhookUpdateInput = {account_id: string, name?: string | undefined, state?: ('ACTIVE' | 'INACTIVE') | undefined, url?: string | undefined, api_key?: string | undefined, secret?: string | undefined, hmac_client_secret?: string | undefined, oauth2_authentication_url?: string | undefined, oauth2_client_id?: string | undefined, oauth2_client_secret?: string | undefined, oauth2_grant_type?: string | undefined, oauth2_scope?: string | undefined, oauth2_authorization_name?: string | undefined, oauth2_include_client_id?: boolean | undefined, enrollment_triggers?: (Array<'ENROLL' | 'UNENROLL'>) | undefined, payment_triggers?: (Array<'AUTHORIZE' | 'CANCEL' | 'CAPTURE' | 'CHARGEBACK' | 'PRECHARGEBACK' | 'PURCHASE' | 'REFUND' | 'VERIFY'>) | undefined, report_triggers?: (Array<'CREATE' | 'UPDATE'>) | undefined, subscription_triggers?: (Array<'ACTIVE' | 'CANCEL' | 'CLOSE_TO_RENEWAL' | 'COMPLETE' | 'CREATE' | 'PAUSE' | 'RESUME'>) | undefined, onboarding_triggers?: (Array<'BLOCKED' | 'CANCELLED' | 'CREATED' | 'DECLINED' | 'ERROR' | 'EXPIRED' | 'INACTIVE' | 'PENDING' | 'SUCCEEDED' | 'TRANSFERRED' | 'UNBLOCKED'>) | undefined, renewal_days?: number | undefined, clear_renewal_days?: boolean | undefined}; export type PatchApiWebhooksWebhookIdArgs = {webhook_id: string, body: WebhookUpdateInput}; export type PatchApiWebhooksWebhookIdOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const webhook = z.object({id: z.string().optional(), account_id: z.string().optional(), name: z.string().optional(), state: z.enum(["ACTIVE", "INACTIVE"]).optional(), url: z.string().optional(), api_key: z.string().optional(), secret: z.string().optional(), hmac_client_secret: z.string().optional(), oauth2_authentication_url: z.string().optional(), oauth2_client_secret: z.string().optional(), oauth2_client_id: z.string().optional(), oauth2_grant_type: z.string().optional(), oauth2_scope: z.string().optional(), oauth2_authorization_name: z.string().optional(), oauth2_include_client_id: z.boolean().optional(), enrollment_triggers: z.array(z.string()).optional(), payment_triggers: z.array(z.string()).optional(), report_triggers: z.array(z.string()).optional(), subscription_triggers: z.array(z.string()).optional(), onboarding_triggers: z.array(z.string()).optional(), renewal_days: z.unknown(), created_at: z.string().optional(), updated_at: z.string().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 /webhooks/{webhook_id} */ export const patchApiWebhooksWebhookId = async (args: PatchApiWebhooksWebhookIdArgs, options?: PatchApiWebhooksWebhookIdOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) 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(webhook, await res.json()) };