import {z} from 'zod' export type GetApiApiV3MetaBasesBaseIdArgs = {baseId: string}; export type GetApiApiV3MetaBasesBaseIdOptions = {baseUrl?: string | undefined, xcToken?: string | undefined, fetch?: typeof fetch | undefined}; export const baseMetaRes = z.object({icon_color: z.string().optional()}); export const base = z.object({id: z.string(), title: z.string(), meta: baseMetaRes, created_at: z.string(), updated_at: z.string(), workspace_id: z.string(), sources: z.array(z.object({id: z.string(), title: z.string(), type: z.string(), is_schema_readonly: z.boolean(), is_data_readonly: z.boolean(), integration_id: z.string()})).optional()}); 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 base meta * * GET /api/v3/meta/bases/{baseId} * * Also accepts auth: bearerAuth */ export const getApiApiV3MetaBasesBaseId = async (args: GetApiApiV3MetaBasesBaseIdArgs, options?: GetApiApiV3MetaBasesBaseIdOptions) => { const {baseUrl = "https://app.nocodb.com", fetch: fetchFn = fetch, xcToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (xcToken !== undefined) headers.set("xc-token", xcToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/meta/bases/${encodeURIComponent(String(args.baseId))}${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(base, await res.json()) };