import {z} from 'zod' export type GetApiV1MetaFieldsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const billing = z.object({currency: z.literal("USD"), price_usd: z.string(), unit: z.string()}); export const jsonValue = z.unknown(); export const fetchCatalogField = z.object({billing: billing, derivation: z.record(z.string(), jsonValue).nullable(), description: z.string(), interpretation_hints: z.string().nullable(), layer: z.string(), name: z.string(), null_meaning: z.string().nullable(), nullable: z.boolean(), presets: z.array(z.string()), source: z.string().nullable(), source_url: z.string().nullable(), ttl_seconds: z.number().int().nullable(), type: z.string(), unit: z.string().nullable()}); export const fetchCatalogResponse = z.object({billing: billing, contract: z.string(), fields: z.array(fetchCatalogField), presets: z.record(z.string(), z.array(z.string())), us_envelope: z.record(z.string(), z.number()), version: z.string()}); 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 Fetch fields * * GET /v1/meta/fields */ export const getApiV1MetaFields = async (options?: GetApiV1MetaFieldsOptions) => { const {baseUrl = "https://api.mireye.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/meta/fields${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(fetchCatalogResponse, await res.json()) };