import {z} from 'zod' export type GetApiV1AccountsAccountIdWebhooksArgs = {accountId: string, endpoint_id?: string | undefined, event?: string | undefined, delivered?: ('true' | 'false') | undefined, from?: number | undefined, to?: number | undefined, page?: number | undefined, 'per-page'?: number | undefined}; export type GetApiV1AccountsAccountIdWebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookDispatch = z.object({resource: z.string().optional(), id: z.string().optional(), event: z.string().optional(), activity_id: z.number().int().optional(), endpoint_id: z.string().nullable().optional(), endpoint: z.string().nullable().optional(), payload: z.object({}).nullable().optional(), delivered: z.boolean().optional(), webhook_id: z.string().nullable().optional(), attempt: z.number().int().nullable().optional(), kind: z.enum(["scheduled", "probe", "manual"]).optional(), will_retry: z.boolean().optional(), duration_ms: z.number().int().nullable().optional(), http_status: z.number().int().nullable().optional(), response_body: z.string().nullable().optional(), error: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiV1AccountsAccountIdWebhooksResponse = z.object({status: z.number().int().optional(), message: z.string().optional(), data: z.array(webhookDispatch).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 }; /** * List webhook deliveries * * GET /v1/accounts/{accountId}/webhooks * * Also accepts auth: apiKeyAuth */ export const getApiV1AccountsAccountIdWebhooks = async (args: GetApiV1AccountsAccountIdWebhooksArgs, options?: GetApiV1AccountsAccountIdWebhooksOptions) => { const {baseUrl = "https://api.assinafy.com.br", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.endpoint_id != null) query.append("endpoint_id", String(args.endpoint_id)) if (args.event != null) query.append("event", String(args.event)) if (args.delivered != null) query.append("delivered", String(args.delivered)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.page != null) query.append("page", String(args.page)) if (args['per-page'] != null) query.append("per-page", String(args['per-page'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}/webhooks${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(getApiV1AccountsAccountIdWebhooksResponse, await res.json()) };