import {z} from 'zod' export type GetApiApiV2WebhooksArgs = {'page[before]'?: string | undefined, 'page[after]'?: string | undefined, 'page[size]'?: number | undefined, 'filter[name_contains]'?: string | undefined, 'filter[status]'?: ('active' | 'inactive') | undefined, sort?: ('name' | 'status') | undefined}; export type GetApiApiV2WebhooksOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const basicAuthAuthentication = z.object({type: z.literal("basic_auth"), data: z.object({username: z.string(), password: z.string().optional()}).optional()}); export const bearerTokenAuthentication = z.object({type: z.literal("bearer_token"), data: z.object({token: z.string().optional()}).optional()}); export const apiKeyAuthentication = z.object({type: z.literal("api_key"), data: z.object({name: z.string(), value: z.string().optional()}).optional(), add_position: z.enum(["header", "query_string"])}); export const webhookSigningSecret = z.object({algorithm: z.string().optional(), secret: z.string().optional()}); export const webhook = z.object({id: z.string().optional(), name: z.string(), description: z.string().optional(), endpoint: z.string(), http_method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]), request_format: z.enum(["json", "xml", "form_encoded"]), status: z.enum(["active", "inactive"]), subscriptions: z.array(z.string()).optional(), authentication: z.union([basicAuthAuthentication, bearerTokenAuthentication, apiKeyAuthentication]).optional(), custom_headers: z.record(z.string(), z.string()).optional(), signing_secret: webhookSigningSecret.optional(), external_source: z.object({name: z.string().optional(), version: z.string().optional()}).optional(), created_at: z.string().optional(), created_by: z.string().optional(), updated_at: z.string().optional(), updated_by: z.string().optional()}); export const paginationMeta = z.object({has_more: z.boolean().optional(), after_cursor: z.string().optional(), before_cursor: z.string().optional(), count: z.number().int().optional()}); export const paginationLinks = z.object({prev: z.string().optional(), next: z.string().optional()}); export const webhookListResponse = z.object({webhooks: z.array(webhook).optional(), meta: paginationMeta.optional(), links: paginationLinks.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 Webhooks * * GET /api/v2/webhooks */ export const getApiApiV2Webhooks = async (args: GetApiApiV2WebhooksArgs, options?: GetApiApiV2WebhooksOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['filter[name_contains]'] != null) query.append("filter[name_contains]", String(args['filter[name_contains]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/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(webhookListResponse, await res.json()) };