import { PermissionVerb, WorkspacePermission, workspacePermissions } from '@/services/can' type PermissionVerbs = PermissionVerb const permissionVerbs = workspacePermissions.map(permission => permission.split(':')[0]) as readonly PermissionVerbs[] export type ObjectTypesWithPermissions = WorkspacePermission extends `${string}:${infer TObject}` ? TObject : never type ActionsForObjectsHelper = T extends `${infer Action}:${infer TObject}` ? TObject extends O ? Action : never : never type PermissionsForObjectType = ActionsForObjectsHelper export type ObjectLevelCan = { [key in PermissionsForObjectType]: boolean } export function createObjectLevelCan(): ObjectLevelCan { const knownProperties = permissionVerbs return new Proxy({} as ObjectLevelCan, { get(_target, property) { // only proxy known properties so that vue doesn't think it's a ref in templates if (knownProperties.includes(property as PermissionVerbs)) { return true } }, }) }