import {z} from 'zod' export type GetApiIndexUuidDataCollectionsArgs = {uuid: string}; export type GetApiIndexUuidDataCollectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dataCollectionsSpec = z.object({auto_persist: z.boolean().optional(), compression: z.enum(["NONE", "GZIP"]).optional(), date_col: z.string().optional(), format: z.literal("csv").optional(), index_dc_subtype: z.string(), layout_type: z.enum(["SINGLE_FILE", "YEARLY_DIR_DAILY_FILE"]).optional(), location: z.string(), name: z.string().min(3).max(128), seed_file_path: z.string().optional(), start_time: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const listDataCollections = z.object({data_collection: z.array(dataCollectionsSpec).optional()}).and(z.record(z.string(), z.unknown())); 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 the timeseries of data collections for an index * * GET /index/{uuid}/data_collections */ export const getApiIndexUuidDataCollections = async (args: GetApiIndexUuidDataCollectionsArgs, options?: GetApiIndexUuidDataCollectionsOptions) => { const {baseUrl = "https://api.merqube.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/index/${encodeURIComponent(String(args.uuid))}/data_collections${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(listDataCollections, await res.json()) };