import {z} from 'zod' export type CommonsCareContextIdentifier = {cc_ref?: string | undefined, patient_ref?: string | undefined}; export type CommonsHipIdentifier = {id?: string | undefined, name?: string | undefined}; export type ConsentRequesterIden = {system?: string | undefined, type?: string | undefined, value?: string | undefined}; export type ConsentRequester = {identifier?: ConsentRequesterIden | undefined, name?: string | undefined}; export type ConsentEkaHiuIdentifier = {clinic_id?: string | undefined, d_oid?: string | undefined, requester?: ConsentRequester | undefined}; export type CommonsEkaPatientReference = {health_id?: string | undefined, oid?: string | undefined}; export type CommonsPeriodWithExpiry = {expiry?: string | undefined, from?: string | undefined, to?: string | undefined}; export type ConsentCreateConsentRequest = {appointment_id?: string | undefined, care_contexts?: Array | undefined, hip_identifier?: CommonsHipIdentifier | undefined, hiu?: ConsentEkaHiuIdentifier | undefined, patient?: CommonsEkaPatientReference | undefined, period?: CommonsPeriodWithExpiry | undefined, purpose?: ('Self Requested' | 'Care management' | 'Public Health' | 'Disease Specific Health Research') | undefined, record_types?: Array | undefined}; export type CreateApiAbdmV1ConsentsCreateArgs = {'X-Pt-Id'?: string | undefined, 'X-Partner-Pt-Id'?: string | undefined, 'X-Hip-Id'?: string | undefined, body: ConsentCreateConsentRequest}; export type CreateApiAbdmV1ConsentsCreateOptions = {baseUrl?: string | undefined, authApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const consentCreateConsentResponse = z.object({consent_init_id: z.string().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 }; /** * POST /abdm/v1/consents/create */ export const createApiAbdmV1ConsentsCreate = async (args: CreateApiAbdmV1ConsentsCreateArgs, options?: CreateApiAbdmV1ConsentsCreateOptions) => { 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['X-Pt-Id'] != null) headers.set("X-Pt-Id", String(args['X-Pt-Id'])) if (args['X-Partner-Pt-Id'] != null) headers.set("X-Partner-Pt-Id", String(args['X-Partner-Pt-Id'])) if (args['X-Hip-Id'] != null) headers.set("X-Hip-Id", String(args['X-Hip-Id'])) if (authApiKey !== undefined) headers.set('authorization', `Bearer ${authApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/abdm/v1/consents/create${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(consentCreateConsentResponse, await res.json()) };