import {z} from 'zod' export type GetApiWebhooksUuidArgs = {uuid: unknown}; export type GetApiWebhooksUuidOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const messageResource = z.object({message: z.string(), reason: z.string(), details: z.record(z.string(), z.string()).optional()}); export const webhookEventType = z.string(); export const webhookResources = z.object({post_url: z.string(), credentials: z.object({username: z.string().optional(), password: z.string().optional()}).optional(), events_filter_list: z.array(webhookEventType)}); export const webhookDefStatus = z.object({state: z.string().optional(), message_list: z.array(messageResource).optional(), name: z.string(), resources: webhookResources, description: z.string().optional()}); export const webhook = z.object({name: z.string().max(64), resources: webhookResources, description: z.string().max(1000).optional()}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const webhookMetadata = z.object({last_update_time: z.string().optional(), kind: z.string(), uuid: z.string().optional(), spec_version: z.number().int().optional(), creation_time: z.string().optional(), spec_hash: z.string().optional(), should_force_translate: z.boolean().optional(), owner_reference: userReference.optional(), categories: z.record(z.string(), z.string()).optional(), name: z.string().max(64).optional()}); export const webhookIntentResponse = z.object({status: webhookDefStatus.optional(), spec: webhook.optional(), api_version: z.string(), metadata: webhookMetadata}); 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 a Webhook * * GET /webhooks/{uuid} */ export const getApiWebhooksUuid = async (args: GetApiWebhooksUuidArgs, options: GetApiWebhooksUuidOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks/${encodeURIComponent(String(args.uuid))}${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(webhookIntentResponse, await res.json()) };