import {z} from 'zod' export type GetApiApiV1ProjectsProjectUuidCodeVirtualViewsArgs = {projectUuid: string, slugs?: Array | undefined}; export type GetApiApiV1ProjectsProjectUuidCodeVirtualViewsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const virtualViewAsCodeSkip = z.object({reason: z.string(), slug: z.string()}); export const parameterValue = z.union([z.string(), z.number(), z.array(z.string()), z.array(z.number())]); export const parametersValuesMap = z.record(z.string(), parameterValue); export const dimensionType = z.enum(["string", "number", "timestamp", "date", "boolean"]); export const resultColumn = z.object({type: dimensionType, reference: z.string()}); export const contentAsCodeTypeVIRTUALVIEW = z.literal("virtual_view"); export const virtualViewAsCode = z.object({parameters: parametersValuesMap.nullable(), columns: z.array(resultColumn), sql: z.string(), name: z.string(), slug: z.string(), version: z.number(), contentType: contentAsCodeTypeVIRTUALVIEW}); export const apiVirtualViewAsCodeListResponse = z.object({results: z.object({missingSlugs: z.array(z.string()), skipped: z.array(virtualViewAsCodeSkip), virtualViews: z.array(virtualViewAsCode)}), status: z.literal("ok")}); 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 virtual views as code * * GET /api/v1/projects/{projectUuid}/code/virtualViews */ export const getApiApiV1ProjectsProjectUuidCodeVirtualViews = async (args: GetApiApiV1ProjectsProjectUuidCodeVirtualViewsArgs, options: GetApiApiV1ProjectsProjectUuidCodeVirtualViewsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.slugs ?? []) query.append("slugs", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/code/virtualViews${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(apiVirtualViewAsCodeListResponse, await res.json()) };