import {z} from 'zod' export type Model138 = {name: string, street1: string, city: string, state: string, zip: string}; export type Prescriber = {prescriber_type?: ('OD' | 'PMHNP' | 'MBBS' | 'DNP' | 'D.O.' | 'DPT' | 'D.D.S.' | 'F.N.P.' | 'CPP' | 'N.D.' | 'P.A.' | 'APNP' | 'V.M.D.' | 'RPh' | 'APRN' | 'DC APC' | 'PsyD' | 'CRNA' | 'PharmD' | 'B.V.M.S.' | 'WHNP' | 'PhD' | 'ARNP' | 'PT' | 'CRNP' | 'DPM' | 'CNP' | 'N.P.' | 'CDH' | 'D.M.D.' | 'D.V.M.' | 'M.D.' | 'CNM') | undefined, first_name: string, last_name: string, address: Model138, dea?: string | undefined, npi: string}; export type TransferFrom = {name: string, dea?: string | undefined, pharmacist: string, id_number: string, address: Model138}; export type TransferTo = {name: string, dea?: string | undefined, pharmacist: string, id_number?: string | undefined}; export type Model139 = {medication_name: string, quantity_written: string, quantity_dispensed?: string | undefined, medication_sig: string, written_date: string, expiration_date?: string | undefined, last_fill_date?: string | undefined, first_fill_date?: string | undefined, number: string, direct_transfer_url?: string | undefined, can_substitute: boolean, prescribed_ndc?: string | undefined, days_supply?: number | undefined, icd10_code?: string | undefined, notes?: string | undefined, annotation?: string | undefined, diagnosis?: string | undefined, quantity_transferred?: string | undefined, original_refills?: number | undefined}; export type CreateDirectTransfer = {metadata?: string | undefined, prescriber: Prescriber, patient_token?: string | undefined, transfer_from: TransferFrom, transfer_to: TransferTo, prescription: Model139, source?: string | undefined}; export type CreateApiV1DirectTransferArgs = {body: CreateDirectTransfer}; export type CreateApiV1DirectTransferOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const model140 = z.object({direct_transfer_id: z.string().optional()}); export const model141 = z.object({request_id: z.string().optional(), timestamp: z.number().int().optional(), status: z.string().optional(), details: model140.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 Direct Transfer * * POST /v1/direct_transfer */ export const createApiV1DirectTransfer = async (args: CreateApiV1DirectTransferArgs, options?: CreateApiV1DirectTransferOptions) => { const {baseUrl = "https://api.truepill.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/direct_transfer${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(model141, await res.json()) };