import {z} from 'zod' export type SortAscDescDefaultDesc = 'asc' | 'desc'; export type GetApiPurchaseOrdersNumberPurchaseOrderLineItemsArgs = {number: string, start_cursor?: string | undefined, per_page?: number | undefined, sort?: ({id?: SortAscDescDefaultDesc | undefined}) | undefined}; export type GetApiPurchaseOrdersNumberPurchaseOrderLineItemsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, accountToken?: string | undefined, fetch?: typeof fetch | undefined}; export const sortAscDesc = z.enum(["asc", "desc"]); export const purchaseOrderLineItem = z.object({id: z.number().int().gte(1).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), part_id: z.number().int().gte(1), quantity: z.string().nullable(), total_quantity_received: z.string().nullable(), part_number: z.string().nullable().optional(), unit_cost: z.number().nullable(), subtotal: z.number().nullable().optional()}); export const getApiPurchaseOrdersNumberPurchaseOrderLineItemsResponse = z.object({start_cursor: z.string().nullable().optional(), next_cursor: z.string().nullable().optional(), per_page: z.number().int().optional(), estimated_remaining_count: z.number().int().optional(), filtered_by: z.array(z.object({})).optional(), sorted_by: z.array(z.object({id: sortAscDesc.optional()})).optional(), records: z.array(purchaseOrderLineItem).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 }; /** * List Purchase Order Line Items * * GET /purchase_orders/{number}/purchase_order_line_items */ export const getApiPurchaseOrdersNumberPurchaseOrderLineItems = async (args: GetApiPurchaseOrdersNumberPurchaseOrderLineItemsArgs, options?: GetApiPurchaseOrdersNumberPurchaseOrderLineItemsOptions) => { const {baseUrl = "https://secure.fleetio.com/api", fetch: fetchFn = fetch, apiKey, accountToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_cursor != null) query.append("start_cursor", String(args.start_cursor)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort != null) query.append("sort", JSON.stringify(args.sort)) if (apiKey !== undefined) headers.set("Authorization", apiKey) if (accountToken !== undefined) headers.set("Account-Token", accountToken) const url = `${baseUrl.replace(/\/+$/, '')}/purchase_orders/${encodeURIComponent(String(args.number))}/purchase_order_line_items${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPurchaseOrdersNumberPurchaseOrderLineItemsResponse, await res.json()) };