import {z} from 'zod' export type Ord = 'asc' | 'desc'; export type GetApiPublicSelfServeV1CatalogArgs = {page?: number | undefined, itemsPerPage?: number | undefined, sort?: Ord | undefined}; export type GetApiPublicSelfServeV1CatalogOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mapLabelKeyString = z.record(z.string(), z.string()); export const catalogVisibility = z.enum(["PRIVATE", "PUBLIC"]); export const topicSqlStorage = z.object({retentionTimeInSecond: z.number().int(), enabled: z.boolean().optional()}); export const topicMetadata = z.object({name: z.string(), cluster: z.string(), labels: mapLabelKeyString.optional(), catalogVisibility: catalogVisibility.optional(), descriptionIsEditable: z.boolean().optional(), description: z.string().optional(), sqlStorage: topicSqlStorage.optional()}); export const topicConfigMap = z.record(z.string(), z.string()); export const topicSpec = z.object({partitions: z.number().int(), replicationFactor: z.number().int().optional(), configs: topicConfigMap.optional()}); export const resourceTopicMetadataTopicSpecStringV2 = z.object({apiVersion: z.string(), kind: z.string(), metadata: topicMetadata, spec: topicSpec}); export const pageResource = z.object({data: z.array(resourceTopicMetadataTopicSpecStringV2).optional(), page: z.number().int(), totalPages: z.number().int(), perPage: z.number().int(), totalElements: z.number().int()}); 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 }; /** * Data catalog * * GET /public/self-serve/v1/catalog */ export const getApiPublicSelfServeV1Catalog = async (args: GetApiPublicSelfServeV1CatalogArgs, options: GetApiPublicSelfServeV1CatalogOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.itemsPerPage != null) query.append("itemsPerPage", String(args.itemsPerPage)) if (args.sort != null) query.append("sort", String(args.sort)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/self-serve/v1/catalog${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(pageResource, await res.json()) };