import {z} from 'zod' export type OffsetDateTimeRangeFilter = {endDateTime?: string | undefined, endDateTimeInclusive?: boolean | undefined, startDateTime?: string | undefined, startDateTimeInclusive?: boolean | undefined}; export type InventoryIdentifier = {description?: string | undefined, type?: ('DEPARTMENT' | 'SUB_DEPARTMENT' | 'CATEGORY' | 'GROUP' | 'FAMILY' | 'GENDER' | 'VENDOR' | 'MANUFACTURER' | 'PROMOTION' | 'SECTOR' | 'DIVISION' | 'CLASS' | 'SUB_CLASS' | 'CODE' | 'SEASON') | undefined, value?: string | undefined}; export type Currency = 'USD' | 'CAD' | 'MXN' | 'EUR'; export type MonetaryValue = {amount?: number | undefined, currency?: Currency | undefined}; export type MonetaryValueRangeFilter = {maximumValue?: MonetaryValue | undefined, maximumValueInclusive?: boolean | undefined, minimumValue?: MonetaryValue | undefined, minimumValueInclusive?: boolean | undefined}; export type BasicInventoryItemSearchCriteria = {consumeByDateTimeRange?: OffsetDateTimeRangeFilter | undefined, hazardClasses?: Array | undefined, ids?: Array | undefined, inventoryIdentifiers?: Array | undefined, manufacturedDateTimeRange?: OffsetDateTimeRangeFilter | undefined, orderIds?: Array | undefined, packagedDateTimeRange?: OffsetDateTimeRangeFilter | undefined, perUnitCost?: MonetaryValueRangeFilter | undefined, perUnitRetailValue?: MonetaryValueRangeFilter | undefined, perishable?: boolean | undefined, searchText?: string | undefined, sellByDateTimeRange?: OffsetDateTimeRangeFilter | undefined, shipmentIds?: Array | undefined, skus?: Array | undefined, taxable?: boolean | undefined, upcs?: Array | undefined}; export type CreateApiApiV4InventoryItemsSearchArgs = {page?: number | undefined, size?: number | undefined, sort?: string | undefined, body: BasicInventoryItemSearchCriteria}; export type CreateApiApiV4InventoryItemsSearchOptions = {baseUrl?: string | undefined, oAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({pageNumber: z.number().int().optional(), pageSize: z.number().int().optional(), total: z.number().int().optional()}); export const cubicDimension = z.object({height: z.number().optional(), length: z.number().optional(), width: z.number().optional()}); export const weight = z.object({weight: z.number().optional(), weightUnit: z.enum(["LB", "KG"]).optional()}); export const dimensionalWeight = z.object({cubicDimension: cubicDimension.optional(), lengthUnit: z.enum(["IN", "CM", "FT", "M"]).optional(), packageType: z.enum(["BAG", "BALE", "BOX", "BUCKET", "BUNDLE", "CAN", "CARTON", "CASE", "COIL", "CRATE", "CYLINDER", "DRUM", "PAIL", "PLT", "PIECES", "REEL", "ROLL", "SKID", "TOTE", "TUBE", "EACH", "FLAT", "LOOSE"]).optional(), quantity: z.number().int().optional(), weight: weight.optional()}); export const inventoryIdentifier = z.object({description: z.string().optional(), type: z.enum(["DEPARTMENT", "SUB_DEPARTMENT", "CATEGORY", "GROUP", "FAMILY", "GENDER", "VENDOR", "MANUFACTURER", "PROMOTION", "SECTOR", "DIVISION", "CLASS", "SUB_CLASS", "CODE", "SEASON"]).optional(), value: z.string().optional()}); export const lineItemHazmatDetail = z.object({hazardClass: z.string().optional(), identificationNumber: z.string().optional(), packingGroup: z.enum(["I", "II", "III", "NONE"]).optional(), properShippingName: z.string().optional()}); export const currency = z.enum(["USD", "CAD", "MXN", "EUR"]); export const monetaryValue = z.object({amount: z.number().optional(), currency: currency.optional()}); export const offsetDateTimeWindow = z.object({endDateTime: z.string().optional(), startDateTime: z.string().optional()}); export const inventoryItem = z.object({consumeByDateTime: z.string().optional(), description: z.string().optional(), externalID: z.string().optional(), id: z.string().optional(), inventoryDimensionalWeight: dimensionalWeight.optional(), inventoryIdentifiers: z.array(inventoryIdentifier).optional(), lineItemHazmatDetail: lineItemHazmatDetail.optional(), manufacturedDateTime: z.string().optional(), orderIds: z.array(z.string()).optional(), packagedDateTime: z.string().optional(), perUnitCost: monetaryValue.optional(), perUnitRetailValue: monetaryValue.optional(), perishable: z.boolean().optional(), promotionalDateTimeWindow: offsetDateTimeWindow.optional(), sellByDateTime: z.string().optional(), serialNumber: z.string().optional(), shipmentIds: z.array(z.string()).optional(), stockKeepingUnit: z.string().optional(), taxable: z.boolean().optional(), universalProductCode: z.string().optional()}); export const inventoryItemPage = z.object({paginationInfo: paginationInfo.optional(), results: z.array(inventoryItem).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 for items. * * POST /api/v4/inventory/items/search */ export const createApiApiV4InventoryItemsSearch = async (args: CreateApiApiV4InventoryItemsSearchArgs, options?: CreateApiApiV4InventoryItemsSearchOptions) => { const {baseUrl = "https://na12.api.project44.com", fetch: fetchFn = fetch, oAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", String(args.sort)) if (oAuth2 !== undefined) headers.set('authorization', `Bearer ${oAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/inventory/items/search${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(inventoryItemPage, await res.json()) };