import {z} from 'zod' export type GetApiV2WebhooksArgs = {agent_id?: string | undefined}; export type GetApiV2WebhooksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataOptions = z.object({include_transcript: z.boolean().optional(), include_recording_url: z.boolean().optional()}); export const customHeaders = z.record(z.string(), z.string()); export const webhookResponse = z.object({id: z.string().optional(), agent_id: z.string().optional(), name: z.string().optional(), url: z.string().optional(), secret: z.string().optional(), events: z.array(z.string()).optional(), data_options: dataOptions.optional(), custom_headers: customHeaders.optional(), is_active: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const webhookListResponse = z.object({webhooks: z.array(webhookResponse), total_count: z.number().int()}); export const getApiV2WebhooksResponse = z.object({data: webhookListResponse}); 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 }; /** * Lists all webhooks for your company. Optionally filter by agent_id. * * GET /v2/webhooks */ export const getApiV2Webhooks = async (args: GetApiV2WebhooksArgs, options?: GetApiV2WebhooksOptions) => { const {baseUrl = "https://api.samora.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/webhooks${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(getApiV2WebhooksResponse, await res.json()) };