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 FetchFields = {include?: Array | undefined, exclude?: Array | undefined} | null; export type ItemFetchRequest = {gtins?: Array | undefined, pullHierarchy?: (boolean | null) | undefined, gtin?: (Array | null) | undefined, ipGln?: (unknown | null) | undefined, targetMarket?: (string | null) | undefined, lastModifiedDate?: LastModifiedDate | undefined, brandName?: (string | null) | undefined, productType?: (string | null) | undefined, isConsumerUnit?: (string | null) | undefined, gpcCode?: (string | null) | undefined, upcCode?: (string | null) | undefined, sortFields?: (Array | null) | undefined, fields?: FetchFields | undefined, searchAfter?: (Array | null> | null) | undefined}; export type CreateApiV1ProductCountArgs = {appid: string, hashcode: string, timestamp: string, gln: unknown, body: ItemFetchRequest}; export type CreateApiV1ProductCountOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const itemCount = z.object({count: z.number().int().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 }; /** * This method will return product count for a given search criteria. * * POST /V1/product/count */ export const createApiV1ProductCount = async (args: CreateApiV1ProductCountArgs, options?: CreateApiV1ProductCountOptions) => { 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.gln != null) headers.set("gln", String(args.gln)) const url = `${baseUrl.replace(/\/+$/, '')}/V1/product/count${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(itemCount, await res.json()) };