import {z} from 'zod' export type CreateApiV0WebhooksWebhookIdActionsTestArgs = {'webhook-id': string}; export type CreateApiV0WebhooksWebhookIdActionsTestOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV0WebhooksWebhookIdActionsTestResponse = z.union([z.object({"webhook-request-status": z.literal("default"), "updated-at": z.string()}), z.object({"webhook-request-status": z.literal("complete"), "updated-at": z.string(), "webhook-http-status": z.number().int(), "webhook-error-code": z.enum(["webhook-timeout", "webhook-unknown-host", "webhook-unknown-error", "webhook-internal-error", "webhook-invalid-response"])})]); 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 }; /** * Send a test event to the webhook * * POST /v0/webhooks/{webhook-id}/actions/test */ export const createApiV0WebhooksWebhookIdActionsTest = async (args: CreateApiV0WebhooksWebhookIdActionsTestArgs, options?: CreateApiV0WebhooksWebhookIdActionsTestOptions) => { const {baseUrl = "https://api.griffin.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("Authorization", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v0/webhooks/${encodeURIComponent(String(args['webhook-id']))}/actions/test${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV0WebhooksWebhookIdActionsTestResponse, await res.json()) };