import {z} from 'zod' export type PublishProductOptionValue = {position: number, value: string}; export type PublishProductImage = {id?: string | undefined, index: number, url?: (string | null) | undefined, base_64?: (string | null) | undefined}; export type PublishApiRequestProductVariantsGootenMappingImages = {id?: (string | null) | undefined, index: number, url: string}; export type PublishApiRequestProductVariantsGootenMappingItemsPreconfigurations = {space_id: string, url: string, image_id?: (number | null) | undefined, space_desc?: (string | null) | undefined, il: unknown}; export type PublishApiRequestProductVariantsGootenMappingItems = {product_id: number, product_variant_sku: string, preconfigurations: Array}; export type PublishGootenMapping = {description?: string | undefined, neck_tag_id?: (number | null) | undefined, images?: (Array | null) | undefined, name?: string | undefined, orientation?: (string | null) | undefined, neck_tag_image_url?: (string | null) | undefined, preview_url?: (string | null) | undefined, has_main_mockup?: (boolean | null) | undefined, sku: string, items: Array}; export type PublishProductVariant = {option_values?: Array | undefined, customer_price: number | null, images?: Array | undefined, weight_unit?: ('oz' | 'lb' | 'kg' | 'g') | undefined, max_cost?: (number | null) | undefined, sku: string | null, compare_at_price?: (number | null) | undefined, weight?: (number | null) | undefined, id?: number | undefined, position?: number | undefined, gooten_mapping: PublishGootenMapping}; export type PublishProductCollection = {id: string, name: string | null}; export type PublishProductOption = {position: number, name: string}; export type PublishApiRequestProduct = {tags?: (Array | null) | undefined, variants: Array, personalize?: (boolean | null) | undefined, images?: Array | undefined, name: string, type?: (string | null) | undefined, collections?: (Array | null) | undefined, options?: Array | undefined, description_html: string | null}; export type PublishApiRequestTasksOptions = {store_id?: (number | null) | undefined, publish?: (boolean | null) | undefined, update_preview?: (boolean | null) | undefined}; export type PublishProductTask = {name: string, options?: PublishApiRequestTasksOptions | undefined, overrides: unknown}; export type PublishAPIRequest = {product: PublishApiRequestProduct, tasks: Array}; export type CreateApiApiV1PublishArgs = {recipeId: string, body: PublishAPIRequest}; export type CreateApiApiV1PublishOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiApiV1PublishResponse = z.unknown(); 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 }; /** * Publish Products Data * * POST /api/v/1/publish */ export const createApiApiV1Publish = async (args: CreateApiApiV1PublishArgs, options: CreateApiApiV1PublishOptions) => { const {baseUrl, 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.recipeId != null) query.append("recipeId", String(args.recipeId)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v/1/publish${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(createApiApiV1PublishResponse, await res.json()) };