import {z} from 'zod' export type LocalizedDescriptionSchema = {[key: string]: string}; export type TextileMaterialSchema = {material_id: number, fraction: number}; export type MaterialCompositionTextileSchema = {cluster_id: number, components: Array}; export type NonTextileMaterialSchema = {material_id: number}; export type MaterialCompositionNonTextileSchema = {cluster_id: number, components: Array}; export type PriceSchema = {country_code: string, retail_price: number, sale_price?: (number | null) | undefined}; export type UpsertProductItemSchema = {style_key: string, sku: string, ean?: (string | null) | undefined, hs_code?: (string | null) | undefined, name?: (string | null) | undefined, description?: (string | null) | undefined, descriptions?: (LocalizedDescriptionSchema | null) | undefined, color: number, size: number | null, second_size?: (number | null) | undefined, brand: number, category: number, quantity?: (number | null) | undefined, weight: number, countries?: (Array | null) | undefined, country_of_origin: string, attributes: Array, material_composition_textile?: (Array | null) | undefined, material_composition_non_textile?: (Array | null) | undefined, prices: Array, images: Array}; export type UpsertProductSchema = {items: Array}; export type CreateApiApiV1ProductsArgs = {body: UpsertProductSchema}; export type CreateApiApiV1ProductsOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchRequestResponseSchema = z.object({batchRequestId: z.string()}); 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 }; /** * Upsert Product * * POST /api/v1/products/ */ export const createApiApiV1Products = async (args: CreateApiApiV1ProductsArgs, options: CreateApiApiV1ProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/products/${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(batchRequestResponseSchema, await res.json()) };