import {z} from 'zod' export type WeightDetail = {value?: number | undefined, unit?: ('KG' | 'G' | 'LB' | 'OZ') | undefined}; export type LengthMeasurementDetail = {value?: number | undefined, unit?: ('M' | 'DM' | 'CM' | 'MM' | 'IN') | undefined}; export type PackagingUnitDetail = {type?: ('PALLET' | 'CARTON') | undefined, barcode?: string | undefined, piecesPerPackagingUnit?: number | undefined, grossWeight?: WeightDetail | undefined, netWeight?: WeightDetail | undefined, length?: LengthMeasurementDetail | undefined, width?: LengthMeasurementDetail | undefined, height?: LengthMeasurementDetail | undefined}; export type ArticleImagePost = {id: string, packagingUnit?: PackagingUnitDetail | undefined}; export type CreateApiArticlesSellerIdArticleIdImagesArgs = {sellerId: number, articleId: number, body: ArticleImagePost}; export type CreateApiArticlesSellerIdArticleIdImagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({link: z.string().optional()}); export const weightDetail = z.object({value: z.number().optional(), unit: z.enum(["KG", "G", "LB", "OZ"]).optional()}); export const lengthMeasurementDetail = z.object({value: z.number().optional(), unit: z.enum(["M", "DM", "CM", "MM", "IN"]).optional()}); export const packagingUnitDetail = z.object({type: z.enum(["PALLET", "CARTON"]).optional(), barcode: z.string().optional(), piecesPerPackagingUnit: z.number().int().optional(), grossWeight: weightDetail.optional(), netWeight: weightDetail.optional(), length: lengthMeasurementDetail.optional(), width: lengthMeasurementDetail.optional(), height: lengthMeasurementDetail.optional()}); export const articleImageGet = z.object({id: z.string().optional(), url: link.optional(), packagingUnit: packagingUnitDetail.optional(), source: 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 }; /** * Stores the article image information * * POST /articles/{sellerId}/{articleId}/images */ export const createApiArticlesSellerIdArticleIdImages = async (args: CreateApiArticlesSellerIdArticleIdImagesArgs, options?: CreateApiArticlesSellerIdArticleIdImagesOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/articles/${encodeURIComponent(String(args.sellerId))}/${encodeURIComponent(String(args.articleId))}/images${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(articleImageGet, await res.json()) };