import {z} from 'zod' export type GetApiV1WebhooksWebhookIdArgs = {webhookId: number}; export type GetApiV1WebhooksWebhookIdOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const templateArgDef = z.object({type: z.enum(["string", "boolean", "number", "time", "(list string)", "(list number)"]).min(1), description: z.string().optional(), title: z.string().min(1), ui: z.object({}), key: z.string().optional(), picklistID: z.number().int().optional(), restrictedByPicklist: z.boolean().optional()}); export const webhook = z.object({id: z.number().int(), created: z.string(), modified: z.string(), applicationIds: z.array(z.number().int()), title: z.string(), description: z.string().optional(), draft: z.boolean(), verb: z.enum(["POST", "PUT", "GET", "DELETE", "PATCH"]), url: z.string(), headers: z.array(z.string()), payload: z.string().optional(), params: z.array(templateArgDef), enabled: z.boolean(), authenticationId: z.number().int().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 }; /** * Get webhook * * GET /v1/webhooks/{webhookId} * * Also accepts auth: management_key */ export const getApiV1WebhooksWebhookId = async (args: GetApiV1WebhooksWebhookIdArgs, options?: GetApiV1WebhooksWebhookIdOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(webhook, await res.json()) };