import {z} from 'zod' export type CreateApiFetchCLArgs = {body: {endRow?: number | undefined, oldValues: unknown, operationType?: string | undefined, startRow?: number | undefined, textMatchStyle?: string | undefined}}; export type CreateApiFetchCLOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const claim = z.object({lastUpdateByName: z.string().max(255).optional(), createdByName: z.string().max(255).optional(), fileName: z.string().max(255).nullable().optional(), typedId: z.string().optional(), claimRecord: z.string().max(255).nullable().optional(), workflowStatus: z.enum(["DRAFT", "SUBMITTED", "DENIED", "APPROVED", "NO_APPROVAL_REQUIRED", "WITHDRAWN", "INVALIDATED"]).max(255).optional(), lastUpdateDate: z.string().optional(), submitDate: z.string().nullable().optional(), locale: z.string().max(255).nullable().optional(), calculationDate: z.string().nullable().optional(), externalRef: z.string().max(255).nullable().optional(), deniedByName: z.string().max(255).nullable().optional(), claimType: z.string().max(255).optional(), fileHandle: z.string().max(255).nullable().optional(), sourceQuoteUniqueName: z.string().max(255).nullable().optional(), customerId: z.string().max(255).nullable().optional(), submittedByName: z.string().max(255).nullable().optional(), approvedByName: z.string().max(255).nullable().optional(), id: z.number().int().optional(), createDate: z.string().optional(), sourceContractUniqueName: z.string().max(255).nullable().optional(), calculationMessages: z.string().nullable().optional(), approvalRequiredEmailAttachments: z.array(z.object({})).nullable().optional(), targetDate: z.string().nullable().optional(), label: z.string().max(255).optional(), version: z.number().int().optional(), customerName: z.string().max(255).nullable().optional(), uniqueName: z.string().max(255).optional(), createdBy: z.number().int().optional(), progress: z.string().max(255).nullable().optional(), claimStatus: z.enum(["DRAFT", "APPROVED", "REVOKED"]).max(255).optional(), calculationStartDate: z.string().nullable().optional(), lastUpdateBy: z.number().int().optional(), status: z.enum(["DRAFT", "PENDING", "SCHEDULED", "PROCESSING", "SCHEDULED_DIRTY", "ERROR", "READY", "CANCELLED"]).max(255).optional()}); export const createApiFetchCLResponse = z.object({response: z.object({node: z.string().min(1).optional(), startRow: z.number().optional(), data: z.array(claim).optional(), endRow: z.number().optional(), totalRows: z.number().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 }; /** * List Claims * * POST /fetch/CL * * Also accepts auth: X-PriceFx-jwt */ export const createApiFetchCL = async (args: CreateApiFetchCLArgs, options?: CreateApiFetchCLOptions) => { 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(/\/+$/, '')}/fetch/CL${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(createApiFetchCLResponse, await res.json()) };