import {z} from 'zod' export type GetApiAccountsAccountIdWebhooksArgs = {page_size?: number | undefined, start_after?: string | undefined, end_before?: string | undefined}; export type GetApiAccountsAccountIdWebhooksOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookID = z.string(); export const webhookURL = z.string(); export const webhookEventType = z.enum(["target-group-updated", "quota-fill-registered", "session-updated"]); export const webhookEventTypes = z.array(webhookEventType); export const webhookCompression = z.enum(["gzip", "null"]).nullable(); export const webhookCreatedAt = z.string(); export const webhookUpdatedAt = z.string(); export const webhook = z.object({id: webhookID.optional(), url: webhookURL.optional(), events: webhookEventTypes.optional(), compression: webhookCompression.optional(), created_at: webhookCreatedAt.optional(), updated_at: webhookUpdatedAt.optional()}); export const webhooksList = z.object({webhooks: z.array(webhook).optional(), has_more: z.boolean().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 }; /** * Lists all webhooks for an account * * GET /accounts/{account_id}/webhooks */ export const getApiAccountsAccountIdWebhooks = async (args: GetApiAccountsAccountIdWebhooksArgs, options?: GetApiAccountsAccountIdWebhooksOptions) => { const {baseUrl = "https://api.cint.com/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.start_after != null) query.append("start_after", String(args.start_after)) if (args.end_before != null) query.append("end_before", String(args.end_before)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/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(webhooksList, await res.json()) };