import {z} from 'zod' export type ApiTokenPermissionLevel = 'none' | 'read' | 'write' | 'delete'; export type ApiTokenPermissions = {records?: ApiTokenPermissionLevel | undefined, tables?: ApiTokenPermissionLevel | undefined, fields?: ApiTokenPermissionLevel | undefined, views?: ApiTokenPermissionLevel | undefined, base?: ApiTokenPermissionLevel | undefined, comments?: ApiTokenPermissionLevel | undefined, webhooks?: ApiTokenPermissionLevel | undefined, users?: ApiTokenPermissionLevel | undefined, workflows?: ApiTokenPermissionLevel | undefined, agents?: ApiTokenPermissionLevel | undefined, scripts?: ApiTokenPermissionLevel | undefined, integrations?: ApiTokenPermissionLevel | undefined, environments?: ApiTokenPermissionLevel | undefined, apps?: ApiTokenPermissionLevel | undefined, workspaces?: ApiTokenPermissionLevel | undefined}; export type ApiTokenScope = {id?: string | undefined, resource_type: 'base' | 'workspace' | 'all', resource_id: string, permissions?: ApiTokenPermissions | undefined}; export type PatchApiApiV3MetaTokensTokenIdArgs = {tokenId: string, body: {title?: string | undefined, scopes?: Array | undefined, expiry?: (string | null) | undefined, enabled?: boolean | undefined, ip_allowlist?: (Array | null) | undefined}}; export type PatchApiApiV3MetaTokensTokenIdOptions = {baseUrl?: string | undefined, xcToken?: string | undefined, fetch?: typeof fetch | undefined}; export const apiTokenPermissionLevel = z.enum(["none", "read", "write", "delete"]); export const apiTokenPermissions = z.object({records: apiTokenPermissionLevel.optional(), tables: apiTokenPermissionLevel.optional(), fields: apiTokenPermissionLevel.optional(), views: apiTokenPermissionLevel.optional(), base: apiTokenPermissionLevel.optional(), comments: apiTokenPermissionLevel.optional(), webhooks: apiTokenPermissionLevel.optional(), users: apiTokenPermissionLevel.optional(), workflows: apiTokenPermissionLevel.optional(), agents: apiTokenPermissionLevel.optional(), scripts: apiTokenPermissionLevel.optional(), integrations: apiTokenPermissionLevel.optional(), environments: apiTokenPermissionLevel.optional(), apps: apiTokenPermissionLevel.optional(), workspaces: apiTokenPermissionLevel.optional()}); export const apiTokenScope = z.object({id: z.string().optional(), resource_type: z.enum(["base", "workspace", "all"]), resource_id: z.string(), permissions: apiTokenPermissions.optional()}); export const apiTokenV3 = z.object({id: z.number().int(), title: z.string(), created_at: z.string(), updated_at: z.string(), scopes: z.array(apiTokenScope).optional(), token_prefix: z.string().optional(), expiry: z.string().nullable().optional(), enabled: z.boolean().optional(), ip_allowlist: z.array(z.string()).optional(), self_lockout_warning: z.boolean().optional(), caller_ip: z.string().optional(), caller_ip_internal: z.boolean().optional(), last_used_at: 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 }; /** * Update API token * * PATCH /api/v3/meta/tokens/{tokenId} * * Also accepts auth: bearerAuth */ export const patchApiApiV3MetaTokensTokenId = async (args: PatchApiApiV3MetaTokensTokenIdArgs, options?: PatchApiApiV3MetaTokensTokenIdOptions) => { const {baseUrl = "https://app.nocodb.com", fetch: fetchFn = fetch, xcToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xcToken !== undefined) headers.set("xc-token", xcToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/meta/tokens/${encodeURIComponent(String(args.tokenId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(apiTokenV3, await res.json()) };