import {z} from 'zod' export type GetApiV2WebhooksWebhookIdArgs = {webhook_id: string}; export type GetApiV2WebhooksWebhookIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookScopeType = z.enum(["tenant", "organization", "partner"]); export const webhookScope = z.object({type: webhookScopeType, id: z.string()}); export const payloadFormat = z.enum(["json", "stringified_json"]); export const webhookStatus = z.enum(["active", "disabled"]); export const requestStatus = z.enum(["success", "miss", "failure"]); export const v2ResponseContext = z.object({correlation_id: z.string(), request_status: requestStatus, time_taken_in_seconds: z.number()}); export const webhookResponse = z.object({webhook_id: z.string(), scope: webhookScope, name: z.string(), description: z.string().optional(), webhook_url: z.string(), headers: z.array(z.string()).optional(), payload_format: payloadFormat.optional(), status: webhookStatus, created_at: z.string(), last_modified_time: z.string(), legacy: z.boolean().optional(), response_context: v2ResponseContext}); 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 * * GET /v2/webhooks/{webhook_id} * * Also accepts auth: APIKeyHeader */ export const getApiV2WebhooksWebhookId = async (args: GetApiV2WebhooksWebhookIdArgs, options?: GetApiV2WebhooksWebhookIdOptions) => { const {baseUrl = "https://api.explorium.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("api_key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks/${encodeURIComponent(String(args.webhook_id))}${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(webhookResponse, await res.json()) };