import {z} from 'zod' export type GetApiV1InventoryArgs = {pageIndex?: number | undefined, numResultsPerPage?: number | undefined, search?: (string | null) | undefined, ids?: (string | null) | undefined, skus?: (string | null) | undefined, semanticSearch?: (string | null) | undefined, externalStoreLocationId?: (string | null) | undefined, externalProductId?: (string | null) | undefined}; export type GetApiV1InventoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const locationDataOutputSerializer = z.object({aisle: z.string().nullable().optional(), bay: z.string().nullable().optional(), shelf: z.string().nullable().optional()}); export const storeInventoryWeightedItemInfoOutputSerializer = z.object({valueCentsPerMeasurementUnit: z.number().int().nullable().optional()}); export const storeInventoryDetailsOutputSerializer = z.object({weightedItemInfo: storeInventoryWeightedItemInfoOutputSerializer.nullable().optional(), taxPercentage: z.number().nullable().optional()}); export const inventoryDiscountOutputSerializer = z.object({valueCents: z.number().int(), startTime: z.string(), endTime: z.string()}); export const inventoryProviderConfigOutputSerializer = z.object({provider: z.string(), serviceType: z.string(), valueCents: z.number().int(), currency: z.string(), details: storeInventoryDetailsOutputSerializer.nullable().optional(), available: z.boolean().nullable().optional(), discount: inventoryDiscountOutputSerializer.nullable().optional(), taxPercentage: z.number().nullable().optional()}); export const storeInventoryOutputSerializer = z.object({id: z.string(), productId: z.string(), externalProductId: z.string().nullable().optional(), storeLocationId: z.string(), externalStoreLocationId: z.string().nullable().optional(), quantity: z.number().int(), valueCents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), available: z.boolean(), location: locationDataOutputSerializer.nullable().optional(), details: storeInventoryDetailsOutputSerializer.nullable().optional(), providerConfigs: z.array(inventoryProviderConfigOutputSerializer).nullable().optional()}); export const inventoryPaginationOutput = z.object({inventory: z.array(storeInventoryOutputSerializer), totalResults: z.number().int()}); 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 }; /** * Get inventory * * GET /v1/inventory */ export const getApiV1Inventory = async (args: GetApiV1InventoryArgs, options?: GetApiV1InventoryOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageIndex != null) query.append("pageIndex", String(args.pageIndex)) if (args.numResultsPerPage != null) query.append("numResultsPerPage", String(args.numResultsPerPage)) if (args.search != null) query.append("search", String(args.search)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.skus != null) query.append("skus", String(args.skus)) if (args.semanticSearch != null) query.append("semanticSearch", String(args.semanticSearch)) if (args.externalStoreLocationId != null) query.append("externalStoreLocationId", String(args.externalStoreLocationId)) if (args.externalProductId != null) query.append("externalProductId", String(args.externalProductId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/inventory${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(inventoryPaginationOutput, await res.json()) };