import {z} from 'zod' export type CreateApiProductmanagerImportproductcompetitionArgs = {body: {data: {importStrategy: 'INSERT' | 'UPDATE', products: Array<{sku: string, label?: string | undefined, price?: number | undefined, currency?: string | undefined, competitor?: string | undefined, competitorSku?: string | undefined, competitionType?: string | undefined, country?: string | undefined, priceUnit?: number | undefined, unitOfMeasure?: string | undefined, infoDate?: string | undefined, additionalInfo1?: number | undefined, additionalInfo2?: number | undefined, additionalInfo4?: string | undefined}>}}}; export type CreateApiProductmanagerImportproductcompetitionOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiProductmanagerImportproductcompetitionResponse = z.object({response: z.object({node: z.string().min(1).optional(), data: z.object({updated: z.number().int().optional(), inserted: z.number().int().optional(), error: z.number().int().optional(), errorRecords: z.array(z.object({record: z.object({sku: z.string().optional(), label: z.string().optional(), price: z.number().optional(), currency: z.string().optional(), competitor: z.string().optional(), competitorSku: z.string().optional(), competitionType: z.string().optional(), country: z.string().optional(), comments: z.string().optional(), source: z.string().optional(), priceType: z.string().optional(), priceUnit: z.number().optional(), unitOfMeasure: z.string().optional(), infoDate: z.string().optional(), additionalInfo1: z.number().optional(), additionalInfo2: z.number().optional(), additionalInfo3: z.string().optional(), additionalInfo4: z.string().optional()}).optional(), errorMessages: z.array(z.string()).optional()})).optional()}).optional(), status: z.number().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 }; /** * Import Competition Data * * POST /productmanager.importproductcompetition * * Also accepts auth: X-PriceFx-jwt */ export const createApiProductmanagerImportproductcompetition = async (args: CreateApiProductmanagerImportproductcompetitionArgs, options?: CreateApiProductmanagerImportproductcompetitionOptions) => { const {baseUrl = "https://companynode.pricefx.com/pricefx/companypartition", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/productmanager.importproductcompetition${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(createApiProductmanagerImportproductcompetitionResponse, await res.json()) };