import {z} from 'zod' export type PermissionSet = {entity: 'table' | 'field', entity_id: string, permission: 'TABLE_VISIBILITY' | 'TABLE_RECORD_ADD' | 'TABLE_RECORD_DELETE' | 'RECORD_FIELD_EDIT', granted_type: 'role' | 'user' | 'nobody', granted_role?: ('creator' | 'editor') | undefined, subjects?: (Array<{type: 'user' | 'team', id?: string | undefined, email?: string | undefined, hierarchy_scope?: ('self_only' | 'self_and_descendants') | undefined}>) | undefined, enforce_for_automation?: boolean | undefined, enforce_for_form?: boolean | undefined}; export type UpdateApiApiV3MetaBasesBaseIdPermissionsArgs = {baseId: string, body: PermissionSet}; export type UpdateApiApiV3MetaBasesBaseIdPermissionsOptions = {baseUrl?: string | undefined, xcToken?: string | undefined, fetch?: typeof fetch | undefined}; export const permission = z.object({id: z.string().optional(), entity: z.enum(["table", "field"]).optional(), entity_id: z.string().optional(), permission: z.enum(["TABLE_VISIBILITY", "TABLE_RECORD_ADD", "TABLE_RECORD_DELETE", "RECORD_FIELD_EDIT"]).optional(), granted_type: z.enum(["role", "user", "nobody"]).optional(), granted_role: z.string().nullable().optional(), subjects: z.array(z.object({type: z.enum(["user", "team", "appTeam", "agent"]).optional(), id: z.string().optional(), hierarchy_scope: z.enum(["self_only", "self_and_descendants"]).optional()})).optional(), enforce_for_automation: z.boolean().optional(), enforce_for_form: z.boolean().optional(), created_by: 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 }; /** * Set permission * * PUT /api/v3/meta/bases/{baseId}/permissions * * Also accepts auth: bearerAuth */ export const updateApiApiV3MetaBasesBaseIdPermissions = async (args: UpdateApiApiV3MetaBasesBaseIdPermissionsArgs, options?: UpdateApiApiV3MetaBasesBaseIdPermissionsOptions) => { 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/bases/${encodeURIComponent(String(args.baseId))}/permissions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(permission, await res.json()) };