import {z} from 'zod' export type GetApiSchemasArgs = {catalogName?: string | undefined, maxResults?: number | undefined, pageToken?: string | undefined, includeBrowse?: boolean | undefined}; export type GetApiSchemasOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const schemaInfo = z.object({name: z.string(), catalogName: z.string(), comment: z.string().optional(), properties: z.object({}).optional(), fullName: z.string().optional(), owner: z.string().optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional(), schemaId: z.string().optional()}); export const listSchemasResponse = z.object({schemas: z.array(schemaInfo).optional(), nextPageToken: z.string().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 }; /** * Gets an array of schemas for a catalog in the metastore. If the caller is the metastore * * GET /schemas */ export const getApiSchemas = async (args: GetApiSchemasArgs, options: GetApiSchemasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.catalogName != null) query.append("catalogName", String(args.catalogName)) if (args.maxResults != null) query.append("maxResults", String(args.maxResults)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.includeBrowse != null) query.append("includeBrowse", String(args.includeBrowse)) const url = `${baseUrl.replace(/\/+$/, '')}/schemas${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(listSchemasResponse, await res.json()) };