From b875a296ffb56a74061b1dcda4cdfafc2aec781e Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Mon, 28 May 2018 00:35:26 +0200 Subject: [PATCH 1/7] Added type definitions for imperium --- types/imperium/imperium-tests.ts | 21 ++++++++ types/imperium/index.d.ts | 93 ++++++++++++++++++++++++++++++++ types/imperium/tsconfig.json | 23 ++++++++ 3 files changed, 137 insertions(+) create mode 100644 types/imperium/imperium-tests.ts create mode 100644 types/imperium/index.d.ts create mode 100644 types/imperium/tsconfig.json diff --git a/types/imperium/imperium-tests.ts b/types/imperium/imperium-tests.ts new file mode 100644 index 0000000000..2f6cbaa8b4 --- /dev/null +++ b/types/imperium/imperium-tests.ts @@ -0,0 +1,21 @@ +import imperium from 'imperium' + +import { Imperium, ImperiumUnauthorizedError } from 'imperium' + +imperium.role('admin', async (req) => { + return req.session.role === 'admin' +}) + +imperium.role('user') + .can('seeUser', { user: '@' }) + .can('manageUser', { user: '@' }) + +imperium.role('admin') + .is('user', { user: '*' }) + +imperium.can('seeUser') +imperium.can(['seeUser', 'manageUser']) +imperium.can({ action: 'seeUser', user: ':userId' }) +imperium.can([{ action: 'manageUser', user: ':userId' }]) +imperium.is('admin') +imperium.is(['admin', 'user']) diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts new file mode 100644 index 0000000000..2695e79574 --- /dev/null +++ b/types/imperium/index.d.ts @@ -0,0 +1,93 @@ +// Type definitions for imperium v2.0.2 +// Project: https://www.npmjs.org/package/mono-js/imperium +// Definitions by: Gaetan SENN +// Definitions: https://github.com/psnider/DefinitelyTyped/imperium +// TypeScript Version: 2.7 + +/// +/// + +declare module 'imperium' { + type ImperiumMiddleware = (req: Express.Request, res: Express.Response, next: Function) => Promise + type ImperiumGetAcl = (req: Express.Request) => Promise | Promise + type ImperiumActions = Array | String + type ImperiumContext = Array<'params' | 'query' | 'headers' | 'body'> + // Can contain when key that is evaluated during route action + type ImperiumRoleParams = { [key: string]: String } + + export class Imperium { + constructor() + + context: Array + roles: Roles + + // Add new role with specific ImperiumGetAcl + role(roleName: String, getAcl: ImperiumGetAcl): ImperiumRole + + // Return specific imperium role instance + role(roleName: String): ImperiumRole + + // Check if user has role + is(roleName: String): ImperiumMiddleware + + // Check if user has on of listed roles act like an OR + is(roleNames: Array): ImperiumMiddleware + + // Check if current user can do action + can(action: String): ImperiumMiddleware + + can(actions: Array): ImperiumMiddleware + + can(action: Action): ImperiumMiddleware + + can(actions: Array): ImperiumMiddleware + + private addRole(roleName: String, getAcl: ImperiumGetAcl): void + + private evaluateRouteActions(req: Express.Request, action: Array, context: ImperiumContext): ImperiumActions + + private evaluateRouteAction(req: Express.Request, expr: String, key: String, context: ImperiumContext): String + + private evaluateUserActions(req: Express.Request, roles: Array): Promise> + + private evaluateUserAction(action: ImperiumRoleParams, context: { [key: string]: Array }): { [key: string]: Array } + } + + interface Roles { + [key: string]: Role + } + + interface Role { + actions: Array + getAcl?: ImperiumGetAcl + } + + interface Action { + action: String + [key: string]: String + } + + export class ImperiumRole { + constructor(imperium: Imperium, roleName: String) + + // Imperium instance to retreive child role + imperium: Imperium + // Role name + roleName: String + // Contain all the actions for this specific role + role: Role + + /* Add action with specific params */ + can(action: String, params: ImperiumRoleParams): ImperiumRole + + /* Get actions of childRoleName and replace params */ + is(childRoleName: String, params: ImperiumRoleParams): ImperiumRole + } + + export class ImperiumUnauthorizedError extends Error { + constructor(message: String, status: number, context: any) + } + + const out: Imperium + export default out +} diff --git a/types/imperium/tsconfig.json b/types/imperium/tsconfig.json new file mode 100644 index 0000000000..597aae24fa --- /dev/null +++ b/types/imperium/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "imperium-tests.ts" + ] +} From a78b7f9d7e0b4be38aeb472c6100920c267e71bc Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Mon, 28 May 2018 16:15:06 +0200 Subject: [PATCH 2/7] imperium: Add tslint and fix compilation --- types/imperium/imperium-tests.ts | 4 +- types/imperium/index.d.ts | 54 +++++++++++----------- types/imperium/tsconfig.json | 43 ++++++++--------- types/imperium/tslint.json | 79 ++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 49 deletions(-) create mode 100644 types/imperium/tslint.json diff --git a/types/imperium/imperium-tests.ts b/types/imperium/imperium-tests.ts index 2f6cbaa8b4..72fda8f52a 100644 --- a/types/imperium/imperium-tests.ts +++ b/types/imperium/imperium-tests.ts @@ -1,9 +1,9 @@ import imperium from 'imperium' -import { Imperium, ImperiumUnauthorizedError } from 'imperium' +import { Imperium, UnauthorizedError } from 'imperium' imperium.role('admin', async (req) => { - return req.session.role === 'admin' + return true }) imperium.role('user') diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index 2695e79574..f7f2191a3f 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -8,12 +8,14 @@ /// declare module 'imperium' { - type ImperiumMiddleware = (req: Express.Request, res: Express.Response, next: Function) => Promise - type ImperiumGetAcl = (req: Express.Request) => Promise | Promise - type ImperiumActions = Array | String - type ImperiumContext = Array<'params' | 'query' | 'headers' | 'body'> + import express = require('express') + + type Middleware = (req: express.Request, res: express.Response, next: Function) => Promise + type GetAcl = (req: express.Request) => Promise | Promise + type Actions = Array | String + type Context = Array<'params' | 'query' | 'headers' | 'body'> // Can contain when key that is evaluated during route action - type ImperiumRoleParams = { [key: string]: String } + type RoleParams = { [key: string]: String } export class Imperium { constructor() @@ -22,44 +24,44 @@ declare module 'imperium' { roles: Roles // Add new role with specific ImperiumGetAcl - role(roleName: String, getAcl: ImperiumGetAcl): ImperiumRole + role(roleName: String, getAcl: GetAcl): Role // Return specific imperium role instance - role(roleName: String): ImperiumRole + role(roleName: String): Role // Check if user has role - is(roleName: String): ImperiumMiddleware + is(roleName: String): Middleware // Check if user has on of listed roles act like an OR - is(roleNames: Array): ImperiumMiddleware + is(roleNames: Array): Middleware // Check if current user can do action - can(action: String): ImperiumMiddleware + can(action: String): Middleware - can(actions: Array): ImperiumMiddleware + can(actions: Array): Middleware - can(action: Action): ImperiumMiddleware + can(action: Action): Middleware - can(actions: Array): ImperiumMiddleware + can(actions: Array): Middleware - private addRole(roleName: String, getAcl: ImperiumGetAcl): void + private addRole(roleName: String, getAcl: GetAcl): void - private evaluateRouteActions(req: Express.Request, action: Array, context: ImperiumContext): ImperiumActions + private evaluateRouteActions(req: express.Request, action: Array, context: Context): Actions - private evaluateRouteAction(req: Express.Request, expr: String, key: String, context: ImperiumContext): String + private evaluateRouteAction(req: express.Request, expr: String, key: String, context: Context): String - private evaluateUserActions(req: Express.Request, roles: Array): Promise> + private evaluateUserActions(req: express.Request, roles: Array): Promise> - private evaluateUserAction(action: ImperiumRoleParams, context: { [key: string]: Array }): { [key: string]: Array } + private evaluateUserAction(action: RoleParams, context: { [key: string]: Array }): { [key: string]: Array } } interface Roles { - [key: string]: Role + [key: string]: RoleActions } - interface Role { + interface RoleActions { actions: Array - getAcl?: ImperiumGetAcl + getAcl?: GetAcl } interface Action { @@ -67,7 +69,7 @@ declare module 'imperium' { [key: string]: String } - export class ImperiumRole { + export class Role { constructor(imperium: Imperium, roleName: String) // Imperium instance to retreive child role @@ -75,16 +77,16 @@ declare module 'imperium' { // Role name roleName: String // Contain all the actions for this specific role - role: Role + role: RoleActions /* Add action with specific params */ - can(action: String, params: ImperiumRoleParams): ImperiumRole + can(action: String, params: RoleParams): Role /* Get actions of childRoleName and replace params */ - is(childRoleName: String, params: ImperiumRoleParams): ImperiumRole + is(childRoleName: String, params: RoleParams): Role } - export class ImperiumUnauthorizedError extends Error { + export class UnauthorizedError extends Error { constructor(message: String, status: number, context: any) } diff --git a/types/imperium/tsconfig.json b/types/imperium/tsconfig.json index 597aae24fa..98e23e4de8 100644 --- a/types/imperium/tsconfig.json +++ b/types/imperium/tsconfig.json @@ -1,23 +1,24 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6", - "dom" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": false, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "imperium-tests.ts" - ] + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "imperium-tests.ts" + ] } diff --git a/types/imperium/tslint.json b/types/imperium/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/imperium/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} From 9cb77195a439c097b6778f113c0c24c17eac02ef Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Tue, 29 May 2018 11:33:45 +0200 Subject: [PATCH 3/7] imperium: Fix feedback from plantain-00 --- types/imperium/index.d.ts | 2 +- types/imperium/tsconfig.json | 2 +- types/imperium/tslint.json | 78 +----------------------------------- 3 files changed, 3 insertions(+), 79 deletions(-) diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index f7f2191a3f..cdfbf3da39 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -1,5 +1,5 @@ // Type definitions for imperium v2.0.2 -// Project: https://www.npmjs.org/package/mono-js/imperium +// Project: https://www.npmjs.org/package/imperium // Definitions by: Gaetan SENN // Definitions: https://github.com/psnider/DefinitelyTyped/imperium // TypeScript Version: 2.7 diff --git a/types/imperium/tsconfig.json b/types/imperium/tsconfig.json index 98e23e4de8..c8563caffe 100644 --- a/types/imperium/tsconfig.json +++ b/types/imperium/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ diff --git a/types/imperium/tslint.json b/types/imperium/tslint.json index a41bf5d19a..f93cf8562a 100644 --- a/types/imperium/tslint.json +++ b/types/imperium/tslint.json @@ -1,79 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "extends": "dtslint/dt.json" } From 228d215c8fed5102a8b7769eb8584b65c4e2e79e Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Tue, 29 May 2018 12:19:47 +0200 Subject: [PATCH 4/7] imperium: Fix tslint errors --- types/imperium/imperium-tests.ts | 24 +++-- types/imperium/index.d.ts | 158 ++++++++++++++----------------- 2 files changed, 83 insertions(+), 99 deletions(-) diff --git a/types/imperium/imperium-tests.ts b/types/imperium/imperium-tests.ts index 72fda8f52a..f7380c25cc 100644 --- a/types/imperium/imperium-tests.ts +++ b/types/imperium/imperium-tests.ts @@ -1,21 +1,19 @@ -import imperium from 'imperium' - -import { Imperium, UnauthorizedError } from 'imperium' +import imperium from 'imperium'; imperium.role('admin', async (req) => { - return true -}) + return true; +}); imperium.role('user') .can('seeUser', { user: '@' }) - .can('manageUser', { user: '@' }) + .can('manageUser', { user: '@' }); imperium.role('admin') - .is('user', { user: '*' }) + .is('user', { user: '*' }); -imperium.can('seeUser') -imperium.can(['seeUser', 'manageUser']) -imperium.can({ action: 'seeUser', user: ':userId' }) -imperium.can([{ action: 'manageUser', user: ':userId' }]) -imperium.is('admin') -imperium.is(['admin', 'user']) +imperium.can('seeUser'); +imperium.can(['seeUser', 'manageUser']); +imperium.can({ action: 'seeUser', user: ':userId' }); +imperium.can([{ action: 'manageUser', user: ':userId' }]); +imperium.is('admin'); +imperium.is(['admin', 'user']); diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index cdfbf3da39..9ec2ca9c09 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -1,95 +1,81 @@ -// Type definitions for imperium v2.0.2 +// Type definitions for imperium 2.0 // Project: https://www.npmjs.org/package/imperium // Definitions by: Gaetan SENN // Definitions: https://github.com/psnider/DefinitelyTyped/imperium // TypeScript Version: 2.7 /// -/// -declare module 'imperium' { - import express = require('express') +import express = require('express'); - type Middleware = (req: express.Request, res: express.Response, next: Function) => Promise - type GetAcl = (req: express.Request) => Promise | Promise - type Actions = Array | String - type Context = Array<'params' | 'query' | 'headers' | 'body'> - // Can contain when key that is evaluated during route action - type RoleParams = { [key: string]: String } - - export class Imperium { - constructor() - - context: Array - roles: Roles - - // Add new role with specific ImperiumGetAcl - role(roleName: String, getAcl: GetAcl): Role - - // Return specific imperium role instance - role(roleName: String): Role - - // Check if user has role - is(roleName: String): Middleware - - // Check if user has on of listed roles act like an OR - is(roleNames: Array): Middleware - - // Check if current user can do action - can(action: String): Middleware - - can(actions: Array): Middleware - - can(action: Action): Middleware - - can(actions: Array): Middleware - - private addRole(roleName: String, getAcl: GetAcl): void - - private evaluateRouteActions(req: express.Request, action: Array, context: Context): Actions - - private evaluateRouteAction(req: express.Request, expr: String, key: String, context: Context): String - - private evaluateUserActions(req: express.Request, roles: Array): Promise> - - private evaluateUserAction(action: RoleParams, context: { [key: string]: Array }): { [key: string]: Array } - } - - interface Roles { - [key: string]: RoleActions - } - - interface RoleActions { - actions: Array - getAcl?: GetAcl - } - - interface Action { - action: String - [key: string]: String - } - - export class Role { - constructor(imperium: Imperium, roleName: String) - - // Imperium instance to retreive child role - imperium: Imperium - // Role name - roleName: String - // Contain all the actions for this specific role - role: RoleActions - - /* Add action with specific params */ - can(action: String, params: RoleParams): Role - - /* Get actions of childRoleName and replace params */ - is(childRoleName: String, params: RoleParams): Role - } - - export class UnauthorizedError extends Error { - constructor(message: String, status: number, context: any) - } - - const out: Imperium - export default out +type GetAcl = (req: express.Request) => Promise | Promise; +type Actions = string[] | string; +type Context = Array<'params' | 'query' | 'headers' | 'body'>; +// Can contain when key that is evaluated during route action +interface RoleParams { + [key: string]: string; } + +export class Imperium { + constructor() + + context: string[]; + roles: Roles; + + // Add new role with specific ImperiumGetAcl + role(roleName: string, getAcl?: GetAcl): Role; + + // Check if user has role(s) act like as an OR + is(roleNames: string | string[]): Promise; + + // Check if current user can do action(s) + can(actionS: string | string[] | Action | Action[]): Promise; + + private addRole(roleName: string, getAcl: GetAcl): void; + + private evaluateRouteActions(req: express.Request, action: Action[], context: Context): Actions; + + private evaluateRouteAction(req: express.Request, expr: string, key: string, context: Context): string; + + private evaluateUserActions(req: express.Request, roles: Role[]): Promise; + + private evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] }; +} + +interface Roles { + [key: string]: RoleActions; +} + +interface RoleActions { + actions: Action[]; + getAcl?: GetAcl; +} + +interface Action { + action: string; + [key: string]: string; +} + +export class Role { + constructor(imperium: Imperium, roleName: string) + + // Imperium instance to retreive child role + imperium: Imperium; + // Role name + roleName: string; + // Contain all the actions for this specific role + role: RoleActions; + + /* Add action with specific params */ + can(action: string, params: RoleParams): Role; + + /* Get actions of childRoleName and replace params */ + is(childRoleName: string, params: RoleParams): Role; +} + +export class UnauthorizedError extends Error { + constructor(message: string, status: number, context: any) +} + +declare const init: Imperium; +export default init; From 54bca77d2224bda9eda3d54e4c6fa853eb5a0d0a Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Fri, 1 Jun 2018 19:00:04 +0200 Subject: [PATCH 5/7] imperium: Set new methods to public --- types/imperium/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index 9ec2ca9c09..f6765eea93 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -33,13 +33,13 @@ export class Imperium { private addRole(roleName: string, getAcl: GetAcl): void; - private evaluateRouteActions(req: express.Request, action: Action[], context: Context): Actions; + evaluateRouteActions(req: express.Request, action: Action[], context: Context): Actions; - private evaluateRouteAction(req: express.Request, expr: string, key: string, context: Context): string; + evaluateRouteAction(req: express.Request, expr: string, key: string, context: Context): string; - private evaluateUserActions(req: express.Request, roles: Role[]): Promise; + evaluateUserActions(req: express.Request, roles: Role[]): Promise; - private evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] }; + evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] }; } interface Roles { From b36e585c8ad0888e55f63c197e3f923c3bd38112 Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Mon, 4 Jun 2018 16:40:08 +0200 Subject: [PATCH 6/7] imperium: Update library to support commonJS module --- types/imperium/index.d.ts | 23 ++++++++++++++++++++--- types/imperium/tsconfig.json | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index f6765eea93..d3f4205c05 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -16,6 +16,26 @@ interface RoleParams { [key: string]: string; } +export const context: string[]; +export const roles: Roles; + +// Add new role with specific ImperiumGetAcl +export function role(roleName: string, getAcl?: GetAcl): Role; + +// Check if user has role(s) act like as an OR +export function is(roleNames: string | string[]): Promise; + +// Check if current user can do action(s) +export function can(actionS: string | string[] | Action | Action[]): Promise; + +export function evaluateRouteActions(req: express.Request, action: Action[], context: Context): Actions; + +export function evaluateRouteAction(req: express.Request, expr: string, key: string, context: Context): string; + +export function evaluateUserActions(req: express.Request, roles: Role[]): Promise; + +export function evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] }; + export class Imperium { constructor() @@ -76,6 +96,3 @@ export class Role { export class UnauthorizedError extends Error { constructor(message: string, status: number, context: any) } - -declare const init: Imperium; -export default init; diff --git a/types/imperium/tsconfig.json b/types/imperium/tsconfig.json index c8563caffe..caaa7b74ce 100644 --- a/types/imperium/tsconfig.json +++ b/types/imperium/tsconfig.json @@ -9,6 +9,7 @@ "noImplicitThis": true, "strictNullChecks": true, "strictFunctionTypes": true, + "esModuleInterop": true, "baseUrl": "../", "typeRoots": [ "../" From 7e1a49f0b06650995db4e7c6c3dbc466aa5c50c2 Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Mon, 4 Jun 2018 18:19:29 +0200 Subject: [PATCH 7/7] imperium: Fix typescript errors --- types/imperium/index.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts index d3f4205c05..67eace244e 100644 --- a/types/imperium/index.d.ts +++ b/types/imperium/index.d.ts @@ -8,11 +8,11 @@ import express = require('express'); -type GetAcl = (req: express.Request) => Promise | Promise; -type Actions = string[] | string; -type Context = Array<'params' | 'query' | 'headers' | 'body'>; +export type GetAcl = (req: express.Request) => Promise | Promise; +export type Actions = string[] | string; +export type Context = Array<'params' | 'query' | 'headers' | 'body'>; // Can contain when key that is evaluated during route action -interface RoleParams { +export interface RoleParams { [key: string]: string; } @@ -62,16 +62,16 @@ export class Imperium { evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] }; } -interface Roles { +export interface Roles { [key: string]: RoleActions; } -interface RoleActions { +export interface RoleActions { actions: Action[]; getAcl?: GetAcl; } -interface Action { +export interface Action { action: string; [key: string]: string; }