import {z} from 'zod' export type LocationDataInputSerializer = {aisle?: (string | null) | undefined, bay?: (string | null) | undefined, shelf?: (string | null) | undefined}; export type StoreInventoryWeightedItemInfoInputSerializer = {valueCentsPerMeasurementUnit?: (number | null) | undefined}; export type StoreInventoryDetailsInputSerializer = {weightedItemInfo?: (StoreInventoryWeightedItemInfoInputSerializer | null) | undefined, taxPercentage?: (number | null) | undefined}; export type InventoryDiscountInputSerializer = {valueCents: number, startTime: string, endTime: string}; export type InventoryProviderConfigInputSerializer = {provider: string, serviceType: string, valueCents: number, currency: string, details?: (StoreInventoryDetailsInputSerializer | null) | undefined, available?: (boolean | null) | undefined, discount?: (InventoryDiscountInputSerializer | null) | undefined, taxPercentage?: (number | null) | undefined}; export type StoreInventoryInputSerializer = {externalProductId?: (string | null) | undefined, externalStoreLocationId?: (string | null) | undefined, quantity?: (number | null) | undefined, valueCents?: (number | null) | undefined, currency?: (string | null) | undefined, available: boolean, location?: (LocationDataInputSerializer | null) | undefined, details?: (StoreInventoryDetailsInputSerializer | null) | undefined, providerConfigs?: (Array | null) | undefined}; export type StoreInventoryListInputSerializer = {inventory: Array}; export type CreateApiV1InventoryArgs = {body: StoreInventoryListInputSerializer}; export type CreateApiV1InventoryOptions = {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 storeInventoryUpsertRowSerializer = z.object({id: z.string().nullable(), productId: z.string().nullable(), externalProductId: z.string().nullable(), storeLocationId: z.string().nullable(), externalStoreLocationId: z.string().nullable(), quantity: z.number().int().nullable(), valueCents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), available: z.boolean().nullable(), location: locationDataOutputSerializer.nullable().optional(), details: storeInventoryDetailsOutputSerializer.nullable().optional(), providerConfigs: z.array(inventoryProviderConfigOutputSerializer).nullable().optional(), errorCode: z.string().nullable(), errorMessage: z.string().nullable()}); export const storeInventoryListOutputSerializer = z.array(storeInventoryUpsertRowSerializer); 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 or update inventory * * POST /v1/inventory */ export const createApiV1Inventory = async (args: CreateApiV1InventoryArgs, options?: CreateApiV1InventoryOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/inventory${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(storeInventoryListOutputSerializer, await res.json()) };