import {z} from 'zod' export type AuthEnum = 'APIKey' | 'Token'; export type PrefixEnum = 'Basic' | 'Bearer' | 'JWT'; export type AuthModel = {type?: AuthEnum | undefined, value: string, prefix?: (PrefixEnum | null) | undefined}; export type WebhookUpdateRequestDTO = {url?: (string | null) | undefined, auth?: (AuthModel | null) | undefined}; export type UpdateApiWebhookHookIdArgs = {hook_id: string, body: WebhookUpdateRequestDTO}; export type UpdateApiWebhookHookIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const v2PackagesEnterpriseSchemasWebhookWebhookEnumResourceEnum = z.enum(["vehicle_debt", "vehicle_debt_batch", "checkout", "autoradar", "payment_recurrence", "documents", "monitoring"]); export const authEnum = z.enum(["APIKey", "Token"]); export const prefixEnum = z.enum(["Basic", "Bearer", "JWT"]); export const authModel = z.object({type: authEnum.optional(), value: z.string(), prefix: prefixEnum.nullable().optional()}); export const webhookResponseDTOInput = z.object({id: z.string().nullable(), url: z.string(), resource: v2PackagesEnterpriseSchemasWebhookWebhookEnumResourceEnum.optional(), auth: authModel.nullable().optional(), 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 }; /** * Atualizar Um Webhook * * PUT /webhook/{hook_id}/ */ export const updateApiWebhookHookId = async (args: UpdateApiWebhookHookIdArgs, options?: UpdateApiWebhookHookIdOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", 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(/\/+$/, '')}/webhook/${encodeURIComponent(String(args.hook_id))}/${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(webhookResponseDTOInput, await res.json()) };