import {z} from 'zod' export type GetApiCatalogV1MetadataModelEntitiesArgs = {after?: string | undefined, size?: number | undefined, search?: string | undefined, name?: string | undefined}; export type GetApiCatalogV1MetadataModelEntitiesOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationMetadata = z.object({next: z.string().optional(), total: z.number().int().optional()}); export const metadataModelEntityProperty = z.object({name: z.string(), description: z.string().optional(), type: z.string(), editable: z.boolean()}); export const metadataModelEntity = z.object({name: z.string(), description: z.string().optional(), nodePaths: z.array(z.string()), superEntities: z.array(z.string()), subEntities: z.array(z.string()), properties: z.array(metadataModelEntityProperty), editable: z.boolean()}); export const getApiCatalogV1MetadataModelEntitiesResponse = z.object({meta: paginationMetadata, data: z.array(metadataModelEntity)}); 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 metadata model entities * * GET /catalog/v1/metadata-model/entities */ export const getApiCatalogV1MetadataModelEntities = async (args: GetApiCatalogV1MetadataModelEntitiesArgs, options?: GetApiCatalogV1MetadataModelEntitiesOptions) => { const {baseUrl = "https://your-environment.ataccama.one/api", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.size != null) query.append("size", String(args.size)) if (args.search != null) query.append("search", String(args.search)) if (args.name != null) query.append("name", String(args.name)) if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/catalog/v1/metadata-model/entities${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(getApiCatalogV1MetadataModelEntitiesResponse, await res.json()) };