import {z} from 'zod' export type ID = string; export type PagingParameters = {after?: string | undefined, before?: string | undefined, size?: number | undefined}; export type GetApiAccountsAccountIdWebhooksDeliveriesArgs = {account_id: ID, page?: PagingParameters | undefined}; export type GetApiAccountsAccountIdWebhooksDeliveriesOptions = {baseUrl?: string | undefined, oauth?: string | undefined, fetch?: typeof fetch | undefined}; export const iD = z.string(); export const dateTime = z.string(); export const webhookDeliveryLog = z.object({attempt_number: z.number().int().gte(1), event_id: z.string(), failure_message: z.string().nullable().optional(), id: iD, inserted_at: dateTime, status_code: z.string(), subscription_id: iD}); export const pagination = z.object({next: z.string().nullable().optional(), page_size: z.number().int().optional(), prev: z.string().nullable().optional()}); export const webhookDeliveryLogList = z.object({deliveries: z.array(webhookDeliveryLog), pagination: pagination}); 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 }; /** * List webhook delivery logs * * GET /accounts/{account_id}/webhooks/deliveries */ export const getApiAccountsAccountIdWebhooksDeliveries = async (args: GetApiAccountsAccountIdWebhooksDeliveriesArgs, options?: GetApiAccountsAccountIdWebhooksDeliveriesOptions) => { const {baseUrl = "https://api.brale.xyz", fetch: fetchFn = fetch, oauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", JSON.stringify(args.page)) if (oauth !== undefined) headers.set('authorization', `Bearer ${oauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/webhooks/deliveries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webhookDeliveryLogList, await res.json()) };