import {z} from 'zod' export type ServiceEntryLineItemType = 'ServiceEntryServiceTaskLineItem' | 'ServiceEntryLaborLineItem' | 'ServiceEntryPartLineItem' | 'ServiceEntryTaxLineItem' | 'ServiceEntryFeeLineItem' | 'ServiceEntryTireLineItem' | 'ServiceEntryFreeTextLineItem'; export type Id = number; export type CreateApiServiceEntriesServiceEntryIdServiceEntryLineItemsArgs = {service_entry_id: string, body: {type?: ServiceEntryLineItemType | undefined, service_task_id?: Id | undefined, description?: string | undefined, issue_ids?: Array | undefined, labor_cost?: number | undefined, parts_cost?: number | undefined, subtotal?: number | undefined}}; export type CreateApiServiceEntriesServiceEntryIdServiceEntryLineItemsOptions = {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 serviceEntryLineItemV2Created = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), account_id: id.optional(), title: z.string().nullable(), description: z.string().nullable(), position: z.number().int().nullable(), linking_source: z.enum(["manual", "automatic_algorithm", "service_code_mapper"]).nullable(), vehicle_id: id, service_entry_line_item_id: z.number().int().nullable(), service_reminder_id: z.number().int().nullable(), service_entry_id: id, service_task_id: id, service_code_id: nullableId, import_id: nullableId.optional(), labor_cost_cents: z.number().int().optional(), parts_cost_cents: z.number().int().optional(), subtotal_cents: z.number().int().optional(), vmrs_system_id: nullableId.optional(), vmrs_assembly_id: nullableId.optional(), vmrs_component_id: nullableId.optional(), vmrs_reason_for_repair_id: nullableId.optional(), vmrs_system_group_id: nullableId.optional()}).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 Service Entry Line Item * * POST /service_entries/{service_entry_id}/service_entry_line_items */ export const createApiServiceEntriesServiceEntryIdServiceEntryLineItems = async (args: CreateApiServiceEntriesServiceEntryIdServiceEntryLineItemsArgs, options?: CreateApiServiceEntriesServiceEntryIdServiceEntryLineItemsOptions) => { 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(/\/+$/, '')}/service_entries/${encodeURIComponent(String(args.service_entry_id))}/service_entry_line_items${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(serviceEntryLineItemV2Created, await res.json()) };