import {z} from 'zod' export type Id = number; export type CustomFields = Record | null; export type CreateApiExpenseEntriesArgs = {body: {vehicle_id: Id, expense_entry_type_id: Id, vendor_id?: Id | undefined, total_amount_cents: number, occurred_at: string, notes?: string | undefined, custom_fields?: CustomFields | undefined, documents_attributes?: (Array<{name?: string | undefined, file_url?: string | undefined, file_mime_type?: string | undefined, file_name?: string | undefined, file_size?: number | undefined}>) | undefined, images_attributes?: (Array<{name?: string | undefined, file_url?: string | undefined, file_mime_type?: string | undefined, file_name?: string | undefined, file_size?: number | undefined}>) | undefined}}; export type CreateApiExpenseEntriesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const id = z.number().int().gte(1); export const createdAt = z.string(); export const updatedAt = z.string(); export const nullableId = z.number().int().gte(1).nullable(); export const customFields = z.object({}).nullable(); export const expenseEntryCreated = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), account_id: z.number().int(), notes: z.string().nullable().optional(), expense_entry_type_id: id, occurred_at: z.string(), vehicle_id: z.number().int(), vendor_id: nullableId, custom_fields: customFields, billable_id: z.number().int().nullable().optional(), billable_type: z.literal("Acquisition").nullable().optional(), bulk_watch_record: z.boolean().optional(), comments_count: z.number().int().gte(0), documents_count: z.number().int().gte(0), images_count: z.number().int().gte(0), import_id: z.number().int().nullable().optional(), is_sample: z.boolean().optional(), recurring_expense_entry_id: z.number().int().nullable().optional(), skip_automatic_watchers_callbacks: z.boolean().optional(), total_amount_cents: z.string(), vehicle_archived_at: z.string().nullable().optional(), watchable_options: 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 Expense Entry * * POST /expense_entries */ export const createApiExpenseEntries = async (args: CreateApiExpenseEntriesArgs, options?: CreateApiExpenseEntriesOptions) => { 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(/\/+$/, '')}/expense_entries${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(expenseEntryCreated, await res.json()) };