import {z} from 'zod' export type DateField = {date?: string | undefined, op?: ('GT' | 'GTE' | 'LT' | 'LTE' | 'EQ') | undefined}; export type LastModifiedDate = {from?: DateField | undefined, to?: DateField | undefined} | null; export type SortField = {field?: string | undefined, desc?: boolean | undefined} | null; export type FetchHierarchyRequest = {gtin?: (Array | null) | undefined, ipGln?: (unknown | null) | undefined, lastModifiedDate?: LastModifiedDate | undefined, sortFields?: (Array | null) | undefined, searchAfter?: (Array | null> | null) | undefined}; export type CreateApiV1ProductHierarchyArgs = {appid: string, hashcode: string, timestamp: string, pageSize: number, gln: unknown, body: FetchHierarchyRequest}; export type CreateApiV1ProductHierarchyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const itemFetchHierarchy: z.ZodType = z.object({parentGtin: z.string().optional(), gtin: z.string().optional(), quantity: z.string().optional(), children: z.array(z.lazy(() => itemFetchHierarchy)).optional()}); export const hierarchyDetails = z.object({gtin: z.string().optional(), informationProviderGLN: z.string().optional(), targetMarket: z.string().optional(), hierarchy: z.array(itemFetchHierarchy).optional()}); export const hierarchyFetchResult = z.object({hierarchies: z.array(hierarchyDetails).optional(), message: z.string().optional(), searchAfter: z.array(z.object({})).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 }; /** * Fetch item hierarchy details. * * POST /V1/product/hierarchy */ export const createApiV1ProductHierarchy = async (args: CreateApiV1ProductHierarchyArgs, options?: CreateApiV1ProductHierarchyOptions) => { const {baseUrl = "https://content1-api.1worldsync.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.appid != null) headers.set("appid", String(args.appid)) if (args.hashcode != null) headers.set("hashcode", String(args.hashcode)) if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.gln != null) headers.set("gln", String(args.gln)) const url = `${baseUrl.replace(/\/+$/, '')}/V1/product/hierarchy${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(hierarchyFetchResult, await res.json()) };