diff --git a/types/easy-rbac/easy-rbac-tests.ts b/types/easy-rbac/easy-rbac-tests.ts new file mode 100644 index 0000000000..2d9f3dc3b3 --- /dev/null +++ b/types/easy-rbac/easy-rbac-tests.ts @@ -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 }); diff --git a/types/easy-rbac/index.d.ts b/types/easy-rbac/index.d.ts new file mode 100644 index 0000000000..a0d3431c90 --- /dev/null +++ b/types/easy-rbac/index.d.ts @@ -0,0 +1,26 @@ +// Type definitions for easy-rbac 3.1 +// Project: https://github.com/DeadAlready/easy-rbac +// Definitions by: Adam Zerella +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface RoleObject { + name: string; + when: (params: object) => Promise; +} + +interface Roles { + [key: string]: { + can: Array; + inherits?: string[]; + }; +} + +type Options = Roles | (() => Promise) | Promise; + +declare class RBAC { + constructor(opts: Options); + can(role: string|string[]|Roles[], operation: string, params?: object): Promise; + static create(opts: Options): RBAC; +} + +export = RBAC; diff --git a/types/easy-rbac/tsconfig.json b/types/easy-rbac/tsconfig.json new file mode 100644 index 0000000000..dc9991555b --- /dev/null +++ b/types/easy-rbac/tsconfig.json @@ -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" + ] +} diff --git a/types/easy-rbac/tslint.json b/types/easy-rbac/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/easy-rbac/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file