import {z} from 'zod' export type GetApiV1UsersUserIdDataSourcesArgs = {limit?: number | undefined, page?: number | undefined, q?: string | undefined}; export type GetApiV1UsersUserIdDataSourcesOptions = {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 dataSourceResponse = 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)}), 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()})]); export const dataSources = z.object({dataSources: z.array(dataSourceResponse), links: z.object({self: z.string(), next: z.string().optional(), previous: z.string().optional()}), meta: z.object({start: z.number(), end: z.number(), total: z.number(), page: z.number(), totalPages: z.number()})}); 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 sources accessible by a user * * GET /v1/users/{userId}/data-sources */ export const getApiV1UsersUserIdDataSources = async (args: GetApiV1UsersUserIdDataSourcesArgs, options?: GetApiV1UsersUserIdDataSourcesOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.q != null) query.append("q", String(args.q)) if (tokenAuth !== undefined) headers.set("Authorization", tokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.userId))}/data-sources${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(dataSources, await res.json()) };