import {z} from 'zod' export type CreateApiUpdateCLTArgs = {body: {data?: ({typedId: string, configuration: {columns?: ({sku?: ({name?: (string | null) | undefined, fieldFormatType?: (string | null) | undefined, canEdit?: boolean | undefined}) | undefined}) | undefined}}) | undefined, oldValues: {version: number, typedId: string, uniqueName?: string | undefined, label?: string | undefined, formulaName?: string | undefined, allocationFormulaName?: string | undefined, validationStateElementName?: string | undefined, sheetName?: string | undefined, createDate?: string | undefined, createdBy?: number | undefined, lastUpdateDate?: string | undefined, lastUpdateBy?: number | undefined}, operationType: string, textMatchStyle?: string | undefined}}; export type CreateApiUpdateCLTOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiUpdateCLTResponse = z.object({response: z.object({node: z.string().optional(), data: z.array(z.object({version: z.number().int().optional(), typedId: z.string().optional(), uniqueName: z.string().optional(), label: z.string().optional(), formulaName: z.string().optional(), allocationFormulaName: z.string().optional(), validationStateElementName: z.string().optional(), sheetName: z.string().optional(), configuration: z.object({columns: z.object({sku: z.object({name: z.string().nullable().optional(), fieldFormatType: z.string().nullable().optional(), canEdit: z.boolean().optional()}).optional()}).optional()}).optional(), createDate: z.string().optional(), createdBy: z.number().int().optional(), lastUpdateDate: z.string().optional(), lastUpdateBy: z.number().int().optional()})).optional(), status: z.number().int().optional()}).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 a Claim Type * * POST /update/CLT * * Also accepts auth: X-PriceFx-jwt */ export const createApiUpdateCLT = async (args: CreateApiUpdateCLTArgs, options?: CreateApiUpdateCLTOptions) => { 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(/\/+$/, '')}/update/CLT${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(createApiUpdateCLTResponse, await res.json()) };