import {z} from 'zod' export type Model128 = Array; export type Model129 = Array; export type Model130 = {prescriptions?: Model128 | undefined, insurance_token?: Model129 | undefined, metadata?: string | undefined, patient_token: string, notes?: string | undefined, ship_to_state?: string | undefined, patient_payment_type?: ('cash' | 'insurance') | undefined}; export type CreateApiV1CopayRequestArgs = {body: Model130}; export type CreateApiV1CopayRequestOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const model131 = z.object({prescription_token: z.string().optional(), copay_request_prescription_token: z.string().optional(), status: z.string().optional()}); export const copayRequestPrescriptionsTokens = z.array(model131); export const model132 = z.object({copay_request_token: z.string().optional(), copay_request_prescriptions_tokens: copayRequestPrescriptionsTokens.optional()}); export const model133 = z.object({request_id: z.string().optional(), timestamp: z.number().int().optional(), status: z.string().optional(), details: model132.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 Copay Request * * POST /v1/copay_request */ export const createApiV1CopayRequest = async (args: CreateApiV1CopayRequestArgs, options?: CreateApiV1CopayRequestOptions) => { 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/copay_request${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(model133, await res.json()) };