import {z} from 'zod' export type GetApiV1DataSourcesDataSourceIdArgs = {'data-view'?: ('none' | 'preview') | undefined}; export type GetApiV1DataSourcesDataSourceIdOptions = {baseUrl?: string | undefined, tokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dataSourceColumnMetadata = z.object({name: z.string(), type: z.object({name: z.string().optional()})}); export const getDataSourceResponse = z.union([z.object({id: z.string(), name: z.string(), aclObject: z.string().optional(), lastUpdatedTime: z.string(), version: z.number(), type: z.literal("CSV"), state: z.enum(["ENABLED", "DISABLED"]), links: z.object({self: z.string(), acl: z.string()}), columnsMetadata: z.array(dataSourceColumnMetadata), rowsPreview: z.array(z.array(z.string())).optional()}), z.object({id: z.string(), name: z.string(), aclObject: z.string().optional(), lastUpdatedTime: z.string(), version: z.number(), type: z.literal("DATABASE"), state: z.enum(["ENABLED", "DISABLED"]), links: z.object({self: z.string(), acl: z.string()}), databaseConnection: z.string(), tableName: z.string(), columnsMetadata: z.array(z.object({name: z.string().optional()})).optional(), rowsPreview: z.array(z.array(z.string())).optional()})]); export const getApiV1DataSourcesDataSourceIdResponse = z.object({dataSource: getDataSourceResponse}); 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 }; /** * Get data source by ID * * GET /v1/data-sources/{dataSourceId} */ export const getApiV1DataSourcesDataSourceId = async (args: GetApiV1DataSourcesDataSourceIdArgs, options?: GetApiV1DataSourcesDataSourceIdOptions) => { const {baseUrl = "https://wmc.wiremockapi.cloud", fetch: fetchFn = fetch, tokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['data-view'] != null) query.append("data-view", String(args['data-view'])) if (tokenAuth !== undefined) headers.set("Authorization", tokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/data-sources/${encodeURIComponent(String(args.dataSourceId))}${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(getApiV1DataSourcesDataSourceIdResponse, await res.json()) };