import {z} from 'zod' export type GetApiApiV1GootenMappingsArgs = {recipeId: string, ids: string}; export type GetApiApiV1GootenMappingsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const productsApiResponseProductsVariantsGootenMappingItemsPreconfigurations = z.object({space_id: z.string(), url: z.string(), space_desc: z.string().nullable().optional(), il: z.unknown()}); export const gootenMappingItem = z.object({product_id: z.number().int(), product_variant_sku: z.string(), preconfigurations: z.array(productsApiResponseProductsVariantsGootenMappingItemsPreconfigurations)}); export const productsApiResponseProductsVariantsGootenMappingImages = z.object({id: z.string().nullable().optional(), url: z.string(), index: z.number().int(), description: z.string().nullable().optional()}); export const gootenMapping = z.object({id: z.number().int(), sku: z.string(), items: z.array(gootenMappingItem), images: z.array(productsApiResponseProductsVariantsGootenMappingImages), preview_url: z.string().nullable(), neck_tag_image_url: z.string().nullable(), neck_tag_id: z.number().int().nullable(), orientation: z.string().nullable(), has_main_mockup: z.boolean().nullable()}); export const gootenMappingsAPIResponse = z.object({gooten_mappings: z.array(gootenMapping)}); 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 gooten mappings by id's * * GET /api/v/1/gooten_mappings */ export const getApiApiV1GootenMappings = async (args: GetApiApiV1GootenMappingsArgs, options: GetApiApiV1GootenMappingsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.recipeId != null) query.append("recipeId", String(args.recipeId)) if (args.ids != null) query.append("ids", String(args.ids)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v/1/gooten_mappings${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(gootenMappingsAPIResponse, await res.json()) };