import {z} from 'zod' export type ISO3 = string; export type WebhookCreateBody = {url: string, passport_filter?: Array | undefined, destination_filter?: Array | undefined}; export type CreateApiApiV1VisaWebhooksArgs = {body: WebhookCreateBody}; export type CreateApiApiV1VisaWebhooksOptions = {baseUrl?: string | undefined, apiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const iSO3 = z.string().min(3).max(3); export const webhookWithSecret = z.object({id: z.number().int().optional(), url: z.string().optional(), passport_filter: z.array(iSO3).nullable().optional(), destination_filter: z.array(iSO3).nullable().optional(), active: z.boolean().optional(), created_at: z.string().optional(), last_triggered_at: z.string().nullable().optional(), failures: z.number().int().optional(), secret: z.string().optional()}); export const createApiApiV1VisaWebhooksResponse = z.object({webhook: webhookWithSecret.optional(), message: 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 }; /** * Create a webhook (Business+) * * POST /api/v1/visa/webhooks * * Also accepts auth: apiKeyQuery */ export const createApiApiV1VisaWebhooks = async (args: CreateApiApiV1VisaWebhooksArgs, options?: CreateApiApiV1VisaWebhooksOptions) => { const {baseUrl = "https://visa.orizn.app", fetch: fetchFn = fetch, apiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyHeader !== undefined) headers.set("x-api-key", apiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/visa/webhooks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiApiV1VisaWebhooksResponse, await res.json()) };