import {z} from 'zod' export type GetApiV3WebhooksArgs = {top?: number | undefined, skip?: number | undefined}; export type GetApiV3WebhooksOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3WebhooksResponse = z.object({items: z.array(z.object({id: z.number().int().optional(), eventType: z.string().optional(), url: z.string().optional(), scope: z.enum(["personal", "team", "organization"]).optional(), enabled: z.boolean().optional(), createdAt: z.string().optional(), payloadConfig: z.object({includeEmailUrl: z.boolean().optional(), includeEmailText: z.boolean().optional(), includeProspectCustomFields: z.boolean().optional()}).optional()})).optional(), hasMore: 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 }; /** * List webhook subscriptions * * GET /v3/webhooks */ export const getApiV3Webhooks = async (args: GetApiV3WebhooksArgs, options?: GetApiV3WebhooksOptions) => { const {baseUrl = "https://api.reply.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.top != null) query.append("top", String(args.top)) if (args.skip != null) query.append("skip", String(args.skip)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/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(getApiV3WebhooksResponse, await res.json()) };