import {z} from 'zod' export type LabelListParameters = string; export type Id = number; export type PurchaseOrderNumber = number; export type DiscountType = 'fixed' | 'percentage' | null; export type TaxType = 'fixed' | 'percentage' | null; export type CustomFields = Record | null; export type CreateApiPurchaseOrdersArgs = {body: {description?: (string | null) | undefined, label_list?: LabelListParameters | undefined, vendor_id?: Id | undefined, part_location_id?: number | undefined, number: PurchaseOrderNumber, discount_type?: DiscountType | undefined, discount_percentage?: number | undefined, discount?: number | undefined, shipping?: number | undefined, tax_1_percentage?: number | undefined, tax_1?: number | undefined, tax_1_type?: TaxType | undefined, tax_2_percentage?: number | undefined, tax_2?: number | undefined, tax_2_type?: TaxType | undefined, custom_fields?: CustomFields | undefined, purchase_order_line_items_attributes?: (Array<{part_id?: number | undefined, quantity?: number | undefined, unit_cost?: number | undefined}>) | undefined}}; export type CreateApiPurchaseOrdersOptions = {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 discountType = z.enum(["fixed", "percentage"]).nullable(); export const taxType = z.enum(["fixed", "percentage"]).nullable(); export const purchaseOrderNumber = z.number().int(); export const labelList = z.array(z.string().max(255)); export const purchaseOrderState = z.enum(["draft", "submitted_for_approval", "rejected", "approved", "purchased", "received_partial", "received_full", "closed"]).nullable(); export const customFields = z.object({}).nullable(); export const nullableId = z.number().int().gte(1).nullable(); export const purchaseOrderCreated = z.object({id: id, created_at: createdAt.optional(), updated_at: updatedAt.optional(), discount_type: discountType, discount_percentage: z.number().nullable(), discount_cents: z.number().int().nullable().optional(), tax_1_type: taxType, tax_1_percentage: z.number().nullable(), tax_1_cents: z.number().int().nullable().optional(), tax_2_type: taxType, tax_2_percentage: z.number().nullable(), tax_2_cents: z.number().int().nullable().optional(), shipping_cents: z.number().int().nullable().optional(), account_id: id, number: purchaseOrderNumber, description: z.string().nullable(), label_list: labelList, submitted_for_approval_at: z.string().nullable(), rejected_at: z.string().nullable(), approved_at: z.string().nullable(), purchased_at: z.string().nullable(), received_partial_at: z.string().nullable(), received_full_at: z.string().nullable(), closed_at: z.string().nullable(), state: purchaseOrderState, custom_fields: customFields, part_location_id: z.number().int().gte(1).nullable(), vendor_id: z.number().int().gte(1).nullable(), purchase_order_status_id: nullableId.optional(), created_by_id: z.number().int().gte(1).nullable().optional(), rejected_by_id: z.number().int().gte(1).nullable().optional(), submitted_for_approval_by_id: z.number().int().gte(1).nullable().optional(), approved_by_id: z.number().int().gte(1).nullable().optional(), purchased_by_id: z.number().int().gte(1).nullable().optional(), received_partial_by_id: z.number().int().gte(1).nullable().optional(), received_full_by_id: z.number().int().gte(1).nullable().optional(), closed_by_id: z.number().int().gte(1).nullable().optional(), documents_count: z.number().int().nullable(), comments_count: z.number().int().nullable(), subtotal_cents: z.number().int().nullable().optional(), total_amount_cents: z.number().int().nullable().optional(), watchers_count: z.number().int().optional(), watchable_options: z.object({}).optional(), bulk_watch_record: z.boolean().optional(), skip_automatic_watchers_callbacks: z.boolean().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 Purchase Orders * * POST /purchase_orders */ export const createApiPurchaseOrders = async (args: CreateApiPurchaseOrdersArgs, options?: CreateApiPurchaseOrdersOptions) => { 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(/\/+$/, '')}/purchase_orders${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(purchaseOrderCreated, await res.json()) };