import {z} from 'zod' export type GetApiApiV1DiscoveredDatasetsArgs = {datasourceId?: string | undefined, page?: number | undefined, search?: string | undefined, size?: number | undefined}; export type GetApiApiV1DiscoveredDatasetsOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const discoveredDatasetContentDTO = z.object({createdAt: z.string().optional(), datasourceId: z.string(), id: z.string(), name: z.string(), onboarded: z.boolean(), qualifiedName: z.string()}); export const publicApiDiscoveredDatasetsResponse = z.object({content: z.array(discoveredDatasetContentDTO), first: z.boolean(), last: z.boolean(), number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: 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 }; /** * Discovered datasets * * GET /api/v1/discoveredDatasets * * Also accepts auth: cookieToken */ export const getApiApiV1DiscoveredDatasets = async (args: GetApiApiV1DiscoveredDatasetsArgs, options?: GetApiApiV1DiscoveredDatasetsOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.datasourceId != null) query.append("datasourceId", String(args.datasourceId)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.size != null) query.append("size", String(args.size)) if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/discoveredDatasets${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(publicApiDiscoveredDatasetsResponse, await res.json()) };