import {z} from 'zod' export type GetApiApiV1DatasourcesArgs = {page?: number | undefined, search?: string | undefined, size?: number | undefined}; export type GetApiApiV1DatasourcesOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const publicApiDatasourceTypeDTO = z.enum(["bigquery", "mysql", "postgres", "snowflake", "dask", "spark", "sparkdf", "redshift", "duckdb", "presto", "oracle", "athena", "trino", "dremio", "vertica", "denodo", "db2", "db2z", "sqlserver", "databricks", "synapse", "fabric"]); export const publicApiDatasourceDTO = z.object({createdAt: z.string().optional(), id: z.string().optional(), label: z.string().optional(), name: z.string().optional(), type: publicApiDatasourceTypeDTO.optional(), updatedAt: z.string().optional()}); export const publicApiListDatasourcesResponse = z.object({content: z.array(publicApiDatasourceDTO), 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 }; /** * List datasources * * GET /api/v1/datasources * * Also accepts auth: cookieToken */ export const getApiApiV1Datasources = async (args: GetApiApiV1DatasourcesArgs, options?: GetApiApiV1DatasourcesOptions) => { 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.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/datasources${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(publicApiListDatasourcesResponse, await res.json()) };