Skip to content

Commit

Permalink
Merge pull request #56 from PrefectHQ/fix-types
Browse files Browse the repository at this point in the history
Fix types
  • Loading branch information
pleek91 authored May 19, 2022
2 parents bb1a0cc + 2e0e341 commit a3dc9cb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prefecthq/orion-design",
"version": "1.0.0-1",
"version": "1.0.0-2",
"private": false,
"keywords": [
"prefect 2.0",
Expand All @@ -26,7 +26,7 @@
"files": [
"dist"
],
"types": "./dist/types/index.d.ts",
"types": "./dist/types/src/index.d.ts",
"devDependencies": {
"@prefecthq/eslint-config": "1.0.15",
"@types/node": "^17.0.35",
Expand Down
11 changes: 7 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export * from './classes'
export * from './css'
export * from './dates'
export * from './filters'
export * from './FlowRunnerType'
export * from './LogsRequestFilter'
export * from './RouteGuard'
export * from './UnionFilters'
export * from './classes'
export * from './css'
export * from './dates'
// export * from './filters'
export * from './permissions'
export * from './tabs'
export * from './tailwind'
export * from './utilities'
116 changes: 116 additions & 0 deletions src/types/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { InjectionKey } from 'vue'

const permissionActions = [
'create',
'read',
'update',
'delete',
] as const
export type PermissionAction = typeof permissionActions[number]

const workspacePermissionKeys = [
'block',
'concurrency_limit',
'deployment',
'flow',
'flow_run',
'log',
'saved_search',
'task_run',
'work_queue',
] as const
export type WorkspacePermissionKey = typeof workspacePermissionKeys[number]
type WorkspacePermissions = Record<WorkspacePermissionKey, boolean>

export function isWorkspacePermissionKey(key: string): key is WorkspacePermissionKey {
return workspacePermissionKeys.includes(key as WorkspacePermissionKey)
}

function getWorkspacePermissions(check: (key: WorkspacePermissionKey) => boolean): WorkspacePermissions {
return workspacePermissionKeys.reduce<WorkspacePermissions>((reduced, key) => ({
...reduced,
[key]: check(key),
}), {} as WorkspacePermissions)
}

const accountPermissionKeys = [
'account',
'account_membership',
'account_role',
'bot',
'invitation',
'team',
'team',
'workspace',
'workspace_bot_access',
'workspace_role',
'workspace_user_access',
] as const
export type AccountPermissionKey = typeof accountPermissionKeys[number]
type AccountPermissions = Record<AccountPermissionKey, boolean>

export function isAccountPermissionKey(key: string): key is AccountPermissionKey {
return accountPermissionKeys.includes(key as AccountPermissionKey)
}

function getAccountPermissions(check: (key: AccountPermissionKey) => boolean): AccountPermissions {
return accountPermissionKeys.reduce<AccountPermissions>((reduced, key) => ({
...reduced,
[key]: check(key),
}), {} as AccountPermissions)
}

const featureFlags = [
'billing',
'collaboration',
] as const
export type FeatureFlag = typeof featureFlags[number]
type FeatureFlagPermissions = Record<FeatureFlag, boolean>

export function isFeatureFlag(key: string): key is FeatureFlag {
return featureFlags.includes(key as FeatureFlag)
}

function getFeatureFlagPermissions(check: (key: FeatureFlag) => boolean): FeatureFlagPermissions {
return featureFlags.reduce<FeatureFlagPermissions>((reduced, key) => ({
...reduced,
[key]: check(key),
}), {} as FeatureFlagPermissions)
}

export type AccountPermissionString = `${PermissionAction}:${AccountPermissionKey}`
export type WorkspacePermissionString = `${PermissionAction}:${WorkspacePermissionKey}`

export type AppPermissions = Record<PermissionAction, Record<AccountPermissionKey, boolean> & Record<WorkspacePermissionKey, boolean>>
export type AppFeatureFlags = Record<'access', FeatureFlagPermissions>

export function getAppPermissions(
checkAccountPermission: (action: PermissionAction, key: AccountPermissionKey) => boolean,
checkWorkspacePermission: (action: PermissionAction, key: WorkspacePermissionKey) => boolean,
checkFeatureFlag: (key: FeatureFlag) => boolean,
): Can {
return {
...permissionActions.reduce<Can>((result, action) => ({
...result,
[action]: {
...getAccountPermissions((key) => checkAccountPermission(action, key)),
...getWorkspacePermissions((key) => checkWorkspacePermission(action, key)),
},
}), {} as Can),
access: {
...getFeatureFlagPermissions((key) => checkFeatureFlag(key)),
},
}
}

export function byPassPermissions(value: boolean): Can {
return getAppPermissions(
() => value,
() => value,
() => value,
)
}

export type Can = AppPermissions & AppFeatureFlags

export const canKey: InjectionKey<Can> = Symbol()

0 comments on commit a3dc9cb

Please sign in to comment.