-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic typings to internal implementation and create module leve…
…l types
- Loading branch information
1 parent
7edd4dc
commit 4aaef7e
Showing
8 changed files
with
158 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ComputedRef } from "@vue/reactivity"; | ||
|
||
// type User with properties and callable/call signature | ||
export type User = { | ||
[key: string]: any, | ||
(): any, // callable / call Signature | ||
} | ||
|
||
export type AsyncUser<U> = () => Promise<U>; | ||
|
||
export interface PluginOption<U = User> { | ||
user?: U | (() => Promise<U>); | ||
rules?: Function | null; | ||
router?: any; | ||
onDeniedRoute?: string; | ||
directiveName?: string; | ||
helperName?: string; | ||
enableSematicAlias?: boolean; | ||
} | ||
|
||
export interface PluginOptionWithDefaults<U = User> { | ||
user: U | (() => Promise<U>); | ||
rules: Function | null; | ||
router: any; | ||
onDeniedRoute: string; | ||
directiveName: string; | ||
helperName: string; | ||
enableSematicAlias: boolean; | ||
} | ||
|
||
export interface State<U = User> { | ||
registeredUser: { [key: string]: any }; | ||
registeredRules: { [key: string]: any }; | ||
options: PluginOptionWithDefaults<U>; | ||
} | ||
|
||
export type Ability = string | any[] | null; | ||
export type AbilityArgs = string | any[] | null; | ||
|
||
export type AbilitiesEvaluationProps = { abilities?: Ability, args?: AbilityArgs, any?: boolean }; | ||
export type AbilitiesEvaluation = (abilities: Ability, args?: AbilityArgs) => boolean; | ||
export type PolicyEvaluation<U = User> = (user: U, ...params: any[]) => boolean; | ||
export type RuleSetter<U = User> = (abilities: Ability, callback: PolicyEvaluation<U>) => void; | ||
|
||
export interface Acl<User> { | ||
user: ComputedRef<User>; | ||
getUser: () => User; | ||
can: { | ||
(abilities: Ability, args?: AbilityArgs): boolean; | ||
not: AbilitiesEvaluation; | ||
any: AbilitiesEvaluation; | ||
} | ||
notCan: AbilitiesEvaluation; | ||
canNot: AbilitiesEvaluation; | ||
cannot: AbilitiesEvaluation; | ||
anyCan: AbilitiesEvaluation; | ||
allPermission: AbilitiesEvaluation; | ||
notPermission: AbilitiesEvaluation; | ||
anyPermission: AbilitiesEvaluation; | ||
permission: { | ||
(abilities: Ability, args?: AbilityArgs): boolean; | ||
not: AbilitiesEvaluation; | ||
any: AbilitiesEvaluation; | ||
} | ||
role: { | ||
(abilities: Ability, args?: AbilityArgs): boolean; | ||
not: AbilitiesEvaluation; | ||
any: AbilitiesEvaluation; | ||
} | ||
allRole: AbilitiesEvaluation; | ||
notRole: AbilitiesEvaluation; | ||
anyRole: AbilitiesEvaluation; | ||
roleOrPermission: { | ||
(abilities: Ability, args?: AbilityArgs): boolean; | ||
not: AbilitiesEvaluation; | ||
any: AbilitiesEvaluation; | ||
} | ||
allRoleOrPermission: AbilitiesEvaluation; | ||
notRoleOrPermission: AbilitiesEvaluation; | ||
anyRoleOrPermission: AbilitiesEvaluation; | ||
} | ||
|
||
|
||
export declare const createAcl: <U = User>(userDefinedOptions: PluginOption<U>) => any; | ||
export declare const defineAclRules: <U = User>(aclRulesCallback: (setter: RuleSetter<U>) => void) => void; | ||
export declare const useAcl: <U = User>() => Acl<U>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import './vue'; | ||
|
||
export { | ||
User, | ||
PluginOption, | ||
State, | ||
Ability, | ||
AbilityArgs, | ||
createAcl, | ||
defineAclRules, | ||
useAcl, | ||
} from './acl'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Augment the typings of Vue.js | ||
*/ | ||
|
||
import { AbilitiesEvaluation } from './acl'; | ||
|
||
declare global { | ||
interface Window { | ||
Vue: any; | ||
} | ||
} | ||
|
||
declare module 'vue/types/vue' { | ||
interface Vue { | ||
$can: { | ||
(): AbilitiesEvaluation; | ||
all: AbilitiesEvaluation; | ||
not: AbilitiesEvaluation; | ||
any: AbilitiesEvaluation; | ||
} | ||
} | ||
} |