import {z} from 'zod' export type AcquisitionType = 'loan' | 'lease'; export type Id = number; export type NullableId = number | null; export type CreateApiAcquisitionsArgs = {body: {acquisition_type: AcquisitionType, vehicle_id: Id, vendor_id?: NullableId | undefined, lender_id?: NullableId | undefined, monthly_cost?: number | undefined, capitalized_cost?: number | undefined, down_payment_amount?: number | undefined, residual_value?: number | undefined, purchase_price?: number | undefined, acquisition_date?: string | undefined, acquisition_end_date?: string | undefined, first_payment_date?: string | undefined, number_of_payments?: number | undefined, mileage_cap?: number | undefined, loan_amount?: number | undefined, mileage_charge?: number | undefined, annual_percentage_rate?: number | undefined, reference_number?: string | undefined, notes?: string | undefined}}; export type CreateApiAcquisitionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const acquisitionType = z.enum(["loan", "lease"]); export const nullableId = z.number().int().gte(1).nullable(); export const acquisition = z.object({id: id.optional(), vehicle_id: id, acquisition_type: acquisitionType, vendor_id: nullableId, lender_id: nullableId, first_payment_date: z.string().nullable(), number_of_payments: z.number().int().nullable(), mileage_cap: z.number().int().nullable(), annual_percentage_rate: z.number().nullable(), reference_number: z.string().nullable(), notes: z.string().nullable(), loan_amount: z.number().nullable(), down_payment_amount: z.number().nullable(), monthly_cost: z.number().nullable(), capitalized_cost: z.number().nullable(), residual_value: z.number().nullable(), purchase_price: z.number().nullable(), mileage_charge: z.number().nullable(), acquisition_date: z.string().nullable(), acquisition_end_date: z.string().nullable().optional(), vehicle: z.object({}).nullable(), lender: z.object({}).nullable(), vendor: z.object({}).nullable()}); 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 Acquisition * * POST /acquisitions */ export const createApiAcquisitions = async (args: CreateApiAcquisitionsArgs, options?: CreateApiAcquisitionsOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = 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) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/acquisitions${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(acquisition, await res.json()) };