import {z} from 'zod' export type GetApiWebhooksWebhookIdArgs = {webhookId: string}; export type GetApiWebhooksWebhookIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const httpsCallbackSchema = z.object({auth: z.object({bearer: z.object({token: z.string().optional()}).optional()}).nullable(), url: z.string()}); export const webhookKindSchema = z.enum(["redo", "loop"]); export const webhookTopicSchema = z.literal("return"); export const webhookReadSchema = z.object({callback: httpsCallbackSchema, createdAt: z.string(), externalId: z.string().nullable().optional(), id: z.string().optional(), kind: webhookKindSchema.optional(), topic: webhookTopicSchema, updatedAt: z.string()}); export const getApiWebhooksWebhookIdResponse = z.object({webhook: webhookReadSchema}); 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 }; /** * Webhook * * GET /webhooks/{webhookId} */ export const getApiWebhooksWebhookId = async (args: GetApiWebhooksWebhookIdArgs, options?: GetApiWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.getredo.com/v2.2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.webhookId))}${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(getApiWebhooksWebhookIdResponse, await res.json()) };