import {z} from 'zod' export type GetApiDatamartFetchObjectIdArgs = {output?: string | undefined, stream?: boolean | undefined, dataLocale?: string | undefined, typedUniquename?: string | undefined}; export type GetApiDatamartFetchObjectIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiDatamartFetchObjectIdResponse = z.object({response: z.object({status: z.number().int().optional(), startRow: z.number().int().optional(), data: z.array(z.object({Country: z.string().optional(), Fertility: z.number().optional(), Life_Expectancy: z.number().optional(), Population: z.number().int().optional(), Region: z.string().optional(), Year: z.string().optional(), ID_Country_Year: z.string().optional(), lastUpdateDate: z.string().optional(), formulaResult: z.string().optional(), formulaDetailedResults: z.string().nullable().optional(), warnings: z.string().optional(), alerts: z.string().optional(), completeResultsAvailable: z.boolean().optional(), Percent: z.number().optional(), Test: z.number().int().optional()})).optional(), endRow: 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 }; /** * Get a DM Object * * GET /datamart.fetch/{objectId} * * Also accepts auth: X-PriceFx-jwt */ export const getApiDatamartFetchObjectId = async (args: GetApiDatamartFetchObjectIdArgs, options?: GetApiDatamartFetchObjectIdOptions) => { 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.output != null) query.append("output", String(args.output)) if (args.stream != null) query.append("stream", String(args.stream)) if (args.dataLocale != null) query.append("dataLocale", String(args.dataLocale)) if (args.typedUniquename != null) query.append("typedUniquename", String(args.typedUniquename)) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/datamart.fetch/${encodeURIComponent(String(args.objectId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiDatamartFetchObjectIdResponse, await res.json()) };