import {z} from 'zod' export type UpdateApiV3VariantArgs = {id: string, body: {productId: number, name?: string | undefined, price?: number | undefined, cost?: number | undefined, onHand?: number | undefined, displayColor?: string | undefined, primaryId?: string | undefined, secondaryId?: string | undefined, properties?: Record | undefined}}; export type UpdateApiV3VariantOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const updateApiV3VariantResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), clientNumber: z.string().optional(), description: z.string().optional(), taxCode: z.string().optional(), active: z.boolean().optional(), modified: z.string().optional(), created: z.string().optional(), deleted: z.boolean().optional(), taxable: z.boolean().optional(), tags: z.array(z.object({})).optional(), minPrice: z.string().optional(), maxPrice: z.string().optional(), availableCount: z.number().int().optional(), quantitySold: z.number().int().optional(), collectionName: z.string().optional(), displayImageDefault: z.boolean().optional()}); 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 }; /** * Update Variant * * PUT /v3/variant */ export const updateApiV3Variant = async (args: UpdateApiV3VariantArgs, options?: UpdateApiV3VariantOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/variant${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV3VariantResponse, await res.json()) };