import {z} from 'zod' export type DataOptions = {include_transcript?: boolean | undefined, include_recording_url?: boolean | undefined}; export type CustomHeaders = {[key: string]: string}; export type CreateWebhookRequest = {agent_id: string, name: string, url: string, events: Array<'CALL_STARTED' | 'CALL_FINISHED' | 'CALL_FAILED'>, data_options?: DataOptions | undefined, custom_headers?: CustomHeaders | undefined}; export type CreateApiV2WebhooksArgs = {body: CreateWebhookRequest}; export type CreateApiV2WebhooksOptions = {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 createApiV2WebhooksResponse = z.object({message: z.string(), data: webhookResponse}); 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 }; /** * Creates a new webhook to receive call event notifications. * * POST /v2/webhooks */ export const createApiV2Webhooks = async (args: CreateApiV2WebhooksArgs, options?: CreateApiV2WebhooksOptions) => { const {baseUrl = "https://api.samora.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2WebhooksResponse, await res.json()) };