import {z} from 'zod' export type GetApiV1InventoryItemArgs = {'search.inventory_item_id'?: Array | undefined, 'search.customer_id'?: Array | undefined, 'search.identifier'?: Array | undefined, 'search.inventory_type_id'?: Array | undefined, 'search.status'?: ('AVAILABLE' | 'UNAVAILABLE' | 'ASSIGNED') | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1InventoryItemOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsFieldValue = z.object({field_id: z.number().int().optional(), label: z.string().optional(), value: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const revAppModelsInventoryItem = z.object({inventory_item_id: z.number().int().optional(), inventory_type_id: z.number().int().optional(), identifier: z.string().max(255).optional(), customer_id: z.number().int().optional(), created_date: z.string().optional(), created_by: z.number().int().optional(), status: z.enum(["AVAILABLE", "UNAVAILABLE", "ASSIGNED"]).optional(), inventory_item_unavailable_reason_id: z.number().int().optional(), note: z.string().optional(), fields: z.array(revAppModelsFieldValue).optional(), links: z.array(revAppComponentsLink).optional()}); export const revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsInventoryItem = z.object({ok: z.boolean().optional(), has_more: z.boolean().optional(), record_count: z.number().int().optional(), records: z.array(revAppModelsInventoryItem).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 }; /** * Search inventory items * * GET /v1/InventoryItem */ export const getApiV1InventoryItem = async (args: GetApiV1InventoryItemArgs, options?: GetApiV1InventoryItemOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.inventory_item_id'] ?? []) query.append("search.inventory_item_id", String(item)) for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) for (const item of args['search.identifier'] ?? []) query.append("search.identifier", String(item)) for (const item of args['search.inventory_type_id'] ?? []) query.append("search.inventory_type_id", String(item)) if (args['search.status'] != null) query.append("search.status", String(args['search.status'])) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/InventoryItem${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(revWebAPIModelsGetOrSearchOfRevAppSearchModelsResponseOfRevAppModelsInventoryItem, await res.json()) };