import {z} from 'zod' export type CreateApiFetchCIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const customer = z.object({createDate: z.string().optional(), createdBy: z.number().int().optional(), customerId: z.string().max(255).optional(), isParent: z.boolean().nullable().optional(), lastUpdateBy: z.number().int().optional(), lastUpdateDate: z.string().optional(), name: z.string().max(255).optional(), nodeId: z.number().int().optional(), parentId: z.number().int().optional(), typedId: z.string().optional(), userGroupEdit: z.string().max(255).optional(), userGroupViewDetails: z.string().max(255).optional(), version: z.number().int().optional(), attribute1: z.string().max(255).optional(), attribute2: z.string().max(255).optional(), attribute3: z.string().max(255).optional(), attribute4: z.string().max(255).optional(), attribute5: z.string().max(255).optional(), attribute6: z.string().max(255).optional(), attribute7: z.string().max(255).optional(), attribute8: z.string().max(255).optional(), attribute9: z.string().max(255).optional(), attribute10: z.string().max(255).optional(), attribute11: z.string().max(255).optional(), attribute12: z.string().max(255).optional(), attribute13: z.string().max(255).optional(), attribute14: z.string().max(255).optional(), attribute15: z.string().max(255).optional(), attribute16: z.string().max(255).optional(), attribute17: z.string().max(255).optional(), attribute18: z.string().max(255).optional(), attribute19: z.string().max(255).optional(), attribute20: z.string().max(255).optional(), attribute21: z.string().max(255).optional(), attribute22: z.string().max(255).optional(), attribute23: z.string().max(255).optional(), attribute24: z.string().max(255).optional(), attribute25: z.string().max(255).optional(), attribute26: z.string().max(255).optional(), attribute27: z.string().max(255).optional(), attribute28: z.string().max(255).optional(), attribute29: z.string().max(255).optional(), attribute30: z.string().max(255).optional()}); export const createApiFetchCIdResponse = z.object({response: z.union([z.object({node: z.string().min(1).optional(), data: z.array(customer).optional(), status: z.number().optional()}), z.object({data: z.array(z.unknown()).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 }; /** * Get a Customer * * POST /fetch/C/{id} * * Also accepts auth: X-PriceFx-jwt */ export const createApiFetchCId = async (options?: CreateApiFetchCIdOptions) => { const {baseUrl = "https://companynode.pricefx.com/pricefx/companypartition", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/fetch/C/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiFetchCIdResponse, await res.json()) };