import {z} from 'zod' export type Gender = 'M' | 'F' | 'O'; export type Age = number; export type Dob = string; export type CreateApiAssessmentApiV1InitArgs = {'client-id': string, locale?: ('en' | 'hi' | 'kn') | undefined, body: {user_info: {gender: Gender, age: Age, dob?: Dob | undefined} | {gender: Gender, dob: Dob, age?: Age | undefined}, workflow_id: number, practitioner_uuid?: string | undefined, patient_uuid?: string | undefined, unique_identifier: string, transaction_id?: string | undefined}}; export type CreateApiAssessmentApiV1InitOptions = {baseUrl?: string | undefined, authApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const aSInit200 = z.object({assessment_id: z.string()}); 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 }; /** * Step 1. Initialization * * POST /assessment/api/v1/init/ */ export const createApiAssessmentApiV1Init = async (args: CreateApiAssessmentApiV1InitArgs, options?: CreateApiAssessmentApiV1InitOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, authApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['client-id'] != null) headers.set("client-id", String(args['client-id'])) if (args.locale != null) headers.set("locale", String(args.locale)) if (authApiKey !== undefined) headers.set('authorization', `Bearer ${authApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/assessment/api/v1/init/${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(aSInit200, await res.json()) };