import {z} from 'zod' export type CreateDocumentRequest = {session_id: string, document_id?: string | undefined, template_id?: string | undefined, type?: ('context' | 'transcript' | 'custom' | 'notes' | 'integration') | undefined, document_name?: string | undefined, status?: string | undefined, errors?: Array | undefined, warnings?: Array | undefined, usage_information?: {[key: string]: unknown} | undefined}; export type CreateApiVoiceApiV1DocumentsArgs = {body: CreateDocumentRequest}; export type CreateApiVoiceApiV1DocumentsOptions = {baseUrl?: string | undefined, auth?: string | undefined, fetch?: typeof fetch | undefined}; export const document = z.object({document_id: z.string().optional(), session_id: z.string().optional(), template_id: z.string().optional(), document_name: z.string().optional(), type: z.string().optional(), document_type: z.string().optional(), status: z.string().optional(), errors: z.array(z.unknown()).optional(), warnings: z.array(z.unknown()).optional(), usage_information: z.record(z.string(), z.unknown()).optional(), presigned_url: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), publish: z.record(z.string(), z.unknown()).optional()}); export const documentEnvelopeResponse = z.object({status: z.string().optional(), data: document.optional()}); 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 }; /** * Create or Update Document * * POST /voice/api/v1/documents */ export const createApiVoiceApiV1Documents = async (args: CreateApiVoiceApiV1DocumentsArgs, options?: CreateApiVoiceApiV1DocumentsOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (auth !== undefined) headers.set('authorization', `Bearer ${auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/voice/api/v1/documents${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(documentEnvelopeResponse, await res.json()) };