import {z} from 'zod' export type CreateApiMetadataFetchTypeCodeArgs = {name?: string | undefined, lookupTableId?: string | undefined}; export type CreateApiMetadataFetchTypeCodeOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiMetadataFetchTypeCodeResponse = z.object({response: z.object({node: z.string().min(1).optional(), csrfToken: z.string().min(1).optional(), data: z.array(z.object({attributeMetas: z.array(z.object({version: z.number(), typedId: z.string().min(1), fieldName: z.string().min(1), label: z.string().min(1), fieldType: z.number(), formatType: z.string().min(1), fieldValueOptions: z.unknown(), cssProperties: z.unknown(), description: z.string(), requiredField: z.boolean(), readOnly: z.boolean(), entityRefTypeCode: z.string(), labelTranslations: z.string().min(1), name: z.string().min(1), createDate: z.string().min(1), createdBy: z.number(), lastUpdateDate: z.string().min(1), lastUpdateBy: z.number()})).optional(), jsonSchema: z.object({properties: z.object({version: z.object({type: z.string().min(1)}), typedId: z.object({type: z.string().min(1)}), name: z.object({type: z.string().min(1)}), sku: z.object({type: z.string().min(1)}), createDate: z.object({type: z.string().min(1)}), createdBy: z.object({type: z.string().min(1)}), lastUpdateDate: z.object({type: z.string().min(1)}), lastUpdateBy: z.object({type: z.string().min(1)}), attribute1: z.object({type: z.string().min(1)}), attribute2: z.object({type: z.string().min(1)}), attribute3: z.object({type: z.string().min(1)}), attribute4: z.object({type: z.string().min(1)}), attribute5: z.object({type: z.string().min(1)}), attribute6: z.object({type: z.string().min(1)}), attribute7: z.object({type: z.string().min(1)}), attribute8: z.object({type: z.string().min(1)}), attribute9: z.object({type: z.string().min(1)}), attribute10: z.object({type: z.string().min(1)}), attribute11: z.object({type: z.string().min(1)}), attribute12: z.object({type: z.string().min(1)}), attribute13: z.object({type: z.string().min(1)}), attribute14: z.object({type: z.string().min(1)}), attribute15: z.object({type: z.string().min(1)}), attribute16: z.object({type: z.string().min(1)}), attribute17: z.object({type: z.string().min(1)}), attribute18: z.object({type: z.string().min(1)}), attribute19: z.object({type: z.string().min(1)}), attribute20: z.object({type: z.string().min(1)}), attribute21: z.object({type: z.string().min(1)}), attribute22: z.object({type: z.string().min(1)}), attribute23: z.object({type: z.string().min(1)}), attribute24: z.object({type: z.string().min(1)}), attribute25: z.object({type: z.string().min(1)}), attribute26: z.object({type: z.string().min(1)}), attribute27: z.object({type: z.string().min(1)}), attribute28: z.object({type: z.string().min(1)}), attribute29: z.object({type: z.string().min(1)}), attribute30: z.object({type: z.string().min(1)})}).optional()}).optional(), attributeMetaSignificantFields: z.array(z.object({})).optional(), attributeMetaTypeCode: z.string().min(1).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 }; /** * List Attribute Fields' Metadata * * POST /metadata.fetch/{TypeCode} * * Also accepts auth: X-PriceFx-jwt */ export const createApiMetadataFetchTypeCode = async (args: CreateApiMetadataFetchTypeCodeArgs, options?: CreateApiMetadataFetchTypeCodeOptions) => { 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 (args.name != null) query.append("name", String(args.name)) if (args.lookupTableId != null) query.append("lookupTableId", String(args.lookupTableId)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/metadata.fetch/${encodeURIComponent(String(args.TypeCode))}${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(createApiMetadataFetchTypeCodeResponse, await res.json()) };