import {z} from 'zod' export type Status = 'draft' | 'pending_approval' | 'rejected' | 'pending_active' | 'active' | 'inactive' | 'problem'; export type GetApiApiV1ProductsArgs = {style_key?: (string | null) | undefined, sku?: (string | null) | undefined, status?: (Status | null) | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiApiV1ProductsOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const status = z.enum(["draft", "pending_approval", "rejected", "pending_active", "active", "inactive", "problem"]); export const localizedDescriptionSchema = z.record(z.string(), z.string()); export const textileMaterialSchema = z.object({material_id: z.number().int().gte(1), fraction: z.number().int()}); export const materialCompositionTextileSchema = z.object({cluster_id: z.number().int().gte(1), components: z.array(textileMaterialSchema)}); export const nonTextileMaterialSchema = z.object({material_id: z.number().int().gte(1)}); export const materialCompositionNonTextileSchema = z.object({cluster_id: z.number().int().gte(1), components: z.array(nonTextileMaterialSchema)}); export const priceSchema = z.object({country_code: z.string(), retail_price: z.number().gt(0).lte(1000000), sale_price: z.number().gt(0).lte(1000000).nullable().optional()}); export const getProductItemSchema = z.object({style_key: z.string().nullable(), status: status, sku: z.string(), ean: z.string().nullable(), name: z.string().nullable(), description: z.string().nullable(), descriptions: localizedDescriptionSchema.nullable(), color: z.number().int().nullable(), size: z.number().int().nullable(), second_size: z.number().int().nullable(), brand: z.number().int(), category: z.number().int(), quantity: z.number().int().nullable(), quantity_fbm: z.number().int().nullable(), weight: z.number().int().nullable(), countries: z.array(z.string()).nullable(), country_of_origin: z.string().nullable(), attributes: z.array(z.number().int()), material_composition_textile: z.array(materialCompositionTextileSchema).nullable().optional(), material_composition_non_textile: z.array(materialCompositionNonTextileSchema).nullable().optional(), prices: z.array(priceSchema), images: z.array(z.string())}); export const pagination = z.object({per_page: z.number().int().gt(0), page: z.number().int().gt(0).nullable().optional(), pages: z.number().int().gte(0).nullable().optional(), total: z.number().int().gte(0).nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional(), next_cursor: z.string().nullable().optional()}); export const getProductsResponseSchema = z.object({items: z.array(getProductItemSchema), pagination: pagination}); 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 }; /** * Get Products * * GET /api/v1/products/ */ export const getApiApiV1Products = async (args: GetApiApiV1ProductsArgs, options: GetApiApiV1ProductsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.style_key != null) query.append("style_key", String(args.style_key)) if (args.sku != null) query.append("sku", String(args.sku)) if (args.status != null) query.append("status", String(args.status)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getProductsResponseSchema, await res.json()) };