import {z} from 'zod' export type StringI18nDetail = {value?: string | undefined, language?: ('DE' | 'EN' | 'ES' | 'PT' | 'FR' | 'IT' | 'ZH') | undefined}; export type WeightDetail = {value?: number | undefined, unit?: ('KG' | 'G' | 'LB' | 'OZ') | undefined}; export type MonetaryValueDetail = {currencyCode?: string | undefined, value?: number | undefined}; export type BundleItemDetail = {id?: number | undefined, quantity: number, unitCustomsValue?: number | undefined, articleId: number, articleSku?: string | undefined, deletedAt?: string | undefined}; export type BundlePost = {sku: string, deletedAt?: string | undefined, alternativeSkus?: Array | undefined, name?: StringI18nDetail | undefined, grossWeight?: WeightDetail | undefined, netWeight?: WeightDetail | undefined, customsValue?: MonetaryValueDetail | undefined, bundleItems?: Array | undefined, alternativeNames?: Array | undefined, alternativeCustomsValues?: Array | undefined}; export type CreateApiBundlesSellerIdArgs = {sellerId: number, body: BundlePost}; export type CreateApiBundlesSellerIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const stringI18nDetail = z.object({value: z.string().max(255).optional(), language: z.enum(["DE", "EN", "ES", "PT", "FR", "IT", "ZH"]).optional()}); export const weightDetail = z.object({value: z.number().optional(), unit: z.enum(["KG", "G", "LB", "OZ"]).optional()}); export const monetaryValueDetail = z.object({currencyCode: z.string().optional(), value: z.number().optional()}); export const bundleItemDetail = z.object({id: z.number().int().optional(), quantity: z.number().int().gte(1), unitCustomsValue: z.number().optional(), articleId: z.number().int(), articleSku: z.string().optional(), deletedAt: z.string().optional()}); export const bundleGetDetail = z.object({sku: z.string().min(1).max(50), deletedAt: z.string().optional(), alternativeSkus: z.array(z.string()).optional(), name: stringI18nDetail.optional(), grossWeight: weightDetail.optional(), netWeight: weightDetail.optional(), customsValue: monetaryValueDetail.optional(), bundleItems: z.array(bundleItemDetail).optional(), alternativeNames: z.array(stringI18nDetail).optional(), alternativeCustomsValues: z.array(monetaryValueDetail).optional(), id: z.number().int().optional(), created: z.string().optional(), lastModified: z.string().optional(), sellerId: z.number().int()}); 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 }; /** * Create a new bundle * * POST /bundles/{sellerId} */ export const createApiBundlesSellerId = async (args: CreateApiBundlesSellerIdArgs, options?: CreateApiBundlesSellerIdOptions) => { 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(/\/+$/, '')}/bundles/${encodeURIComponent(String(args.sellerId))}${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(bundleGetDetail, await res.json()) };