Typing for "easy-rbac" (#43149)

Co-authored-by: Adam Zerella <adamzerella@users.noreply.github.com>
This commit is contained in:
Adam Zerella 2020-03-27 15:53:08 +10:30 committed by GitHub
parent 83570fa572
commit 76168b497f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import RBAC = require('easy-rbac');
import rbac = require('easy-rbac');
const roles = {
manager: {
can: ['post:save', 'post:delete', 'account:*'],
inherits: ['user']
},
user: {
can: [
'post:add',
{
name: 'post:save',
when: async () => true,
},
'user:create'
],
inherits: ['manager']
}
};
const RBACInstance = new RBAC(roles);
RBACInstance.can(['user', 'manager'], 'post:save', { userId: 1, ownerId: 2 });
const RBACFunction = rbac.create(roles);
RBACFunction.can('user', 'post:save', { userId: 1, ownerId: 2 });
RBACFunction.can(['user', 'manager'], 'post:save', { userId: 1 });

26
types/easy-rbac/index.d.ts vendored Normal file
View File

@ -0,0 +1,26 @@
// Type definitions for easy-rbac 3.1
// Project: https://github.com/DeadAlready/easy-rbac
// Definitions by: Adam Zerella <https://github.com/adamzerella>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface RoleObject {
name: string;
when: (params: object) => Promise<boolean>;
}
interface Roles {
[key: string]: {
can: Array<string|RoleObject>;
inherits?: string[];
};
}
type Options = Roles | (() => Promise<Roles>) | Promise<Roles>;
declare class RBAC {
constructor(opts: Options);
can(role: string|string[]|Roles[], operation: string, params?: object): Promise<boolean>;
static create(opts: Options): RBAC;
}
export = RBAC;

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"easy-rbac-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}