import {z} from 'zod' export type GetApiBundlesSellerIdBundleIdArgs = {sellerId: number, bundleId: number}; export type GetApiBundlesSellerIdBundleIdOptions = {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 }; /** * Get details of bundle by bundleId * * GET /bundles/{sellerId}/{bundleId} */ export const getApiBundlesSellerIdBundleId = async (args: GetApiBundlesSellerIdBundleIdArgs, options?: GetApiBundlesSellerIdBundleIdOptions) => { const {baseUrl = "https://api.quivo.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/bundles/${encodeURIComponent(String(args.sellerId))}/${encodeURIComponent(String(args.bundleId))}${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(bundleGetDetail, await res.json()) };