import {z} from 'zod' export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type OnlyId = {id?: string | undefined}; export type CustomAttribute = {attributeDefinitionId?: string | undefined, booleanValue?: boolean | undefined, dateValue?: number | undefined, entityId?: string | undefined, entityReferences?: Array | undefined, numberValue?: unknown | undefined, selectedValueId?: string | undefined, selectedValues?: Array | undefined, stringValue?: string | undefined}; export type NestedStoragePlace = {id?: string | undefined, version?: string | undefined, active?: boolean | undefined, barcode?: string | undefined, blockedForResupply?: boolean | undefined, blockedForResupplyReasonId?: string | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, customerId?: string | undefined, fieldNumber?: number | undefined, lastModifiedDate?: number | undefined, levelNumber?: number | undefined, name: string, pickSequence?: number | undefined, storagePlaceSizeId?: string | undefined, storagePlaceType: 'ASSEMBLY' | 'BLOCKED' | 'BUFFER' | 'CONSOLIDATION' | 'INBOUND' | 'PICKING' | 'STOCK' | 'UNIVERSAL' | 'UNIVERSAL_WITHOUT_PICKING'}; export type Shelf = {id?: string | undefined, version?: string | undefined, active?: boolean | undefined, createdDate?: number | undefined, lastModifiedDate?: number | undefined, shortIdentifier: string, storageLocationId: string, storagePlaces: Array}; export type CreateApiShelfArgs = {dryRun?: boolean | undefined, body: Shelf}; export type CreateApiShelfOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const onlyId = z.object({id: z.string().optional()}); export const customAttribute = z.object({attributeDefinitionId: z.string().optional(), booleanValue: z.boolean().optional(), dateValue: z.number().int().optional(), entityId: z.string().optional(), entityReferences: z.array(entityReference).optional(), numberValue: z.string().optional(), selectedValueId: z.string().optional(), selectedValues: z.array(onlyId).optional(), stringValue: z.string().optional()}); export const nestedStoragePlace = z.object({id: z.string().optional(), version: z.string().optional(), active: z.boolean().optional(), barcode: z.string().optional(), blockedForResupply: z.boolean().optional(), blockedForResupplyReasonId: z.string().optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), customerId: z.string().optional(), fieldNumber: z.number().int().optional(), lastModifiedDate: z.number().int().optional(), levelNumber: z.number().int().optional(), name: z.string(), pickSequence: z.number().int().optional(), storagePlaceSizeId: z.string().optional(), storagePlaceType: z.enum(["ASSEMBLY", "BLOCKED", "BUFFER", "CONSOLIDATION", "INBOUND", "PICKING", "STOCK", "UNIVERSAL", "UNIVERSAL_WITHOUT_PICKING"])}); export const shelf = z.object({id: z.string().optional(), version: z.string().optional(), active: z.boolean().optional(), createdDate: z.number().int().optional(), lastModifiedDate: z.number().int().optional(), shortIdentifier: z.string(), storageLocationId: z.string(), storagePlaces: z.array(nestedStoragePlace)}); 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 a shelf * * POST /shelf */ export const createApiShelf = async (args: CreateApiShelfArgs, options: CreateApiShelfOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/shelf${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(shelf, await res.json()) };