import {z} from 'zod' export type CreateApiFormulamanagerUpdateIdArgs = {body: {data?: ({version?: number | undefined, typedId?: string | undefined, uniqueName?: string | undefined, label?: string | undefined, validAfter?: string | undefined, status?: string | undefined, simulationSet: unknown, userGroupEdit: unknown, userGroupViewDetails: unknown, formulaNature: unknown, lastUpdateByName?: string | undefined, elements?: (Array<{version?: number | undefined, typedId?: string | undefined, elementName?: string | undefined, elementLabel?: string | undefined, elementDescription: unknown, elementGroups?: Array | undefined, conditionElementName: unknown, hideWarnings?: boolean | undefined, excludeFromExport?: boolean | undefined, protectedExpression?: boolean | undefined, elementTimeout?: number | undefined, displayOptions?: number | undefined, formatType?: (string | null) | undefined, elementSuffix: unknown, allowOverride?: boolean | undefined, summarize?: boolean | undefined, hideOnNull?: boolean | undefined, userGroup: unknown, cssProperties: unknown, resultGroup: unknown, combinationType?: string | undefined, storeInAttributeExtension?: boolean | undefined, criticalAlert: unknown, redAlert: unknown, yellowAlert: unknown, labelTranslations: unknown, createDate?: string | undefined, createdBy?: number | undefined, lastUpdateDate?: string | undefined, lastUpdateBy?: number | undefined, formulaExpression?: string | undefined}>) | undefined, inputDescriptors?: Array> | undefined, formulaType?: string | undefined, createdByName: unknown, createDate?: string | undefined, createdBy?: number | undefined, lastUpdateDate?: string | undefined, lastUpdateBy?: number | undefined}) | undefined}}; export type CreateApiFormulamanagerUpdateIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiFormulamanagerUpdateIdResponse = z.object({response: z.object({node: z.string().min(1).optional(), data: z.array(z.object({formula: z.object({version: z.number().optional(), typedId: z.string().min(1).optional(), uniqueName: z.string().min(1).optional(), label: z.string().min(1).optional(), validAfter: z.string().min(1).optional(), status: z.string().min(1).optional(), simulationSet: z.unknown(), userGroupEdit: z.unknown(), userGroupViewDetails: z.unknown(), formulaNature: z.unknown(), lastUpdateByName: z.string().min(1).optional(), formulaType: z.string().min(1).optional(), createdByName: z.unknown(), createDate: z.string().min(1).optional(), createdBy: z.number().optional(), lastUpdateDate: z.string().min(1).optional(), lastUpdateBy: z.number().optional()}).optional()})).optional(), status: z.number().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 Logic * * POST /formulamanager.update/{id} * * Also accepts auth: X-PriceFx-jwt */ export const createApiFormulamanagerUpdateId = async (args: CreateApiFormulamanagerUpdateIdArgs, options?: CreateApiFormulamanagerUpdateIdOptions) => { 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(/\/+$/, '')}/formulamanager.update/${encodeURIComponent(String(args.id))}${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(createApiFormulamanagerUpdateIdResponse, await res.json()) };