imperium: Fix tslint errors

This commit is contained in:
Gaetan SENN
2018-05-29 12:19:47 +02:00
parent 9cb77195a4
commit 228d215c8f
2 changed files with 83 additions and 99 deletions
+11 -13
View File
@@ -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']);
+72 -86
View File
@@ -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 <https://github.com/gaetansenn>
// Definitions: https://github.com/psnider/DefinitelyTyped/imperium
// TypeScript Version: 2.7
/// <reference types="node" />
/// <reference types="express" />
declare module 'imperium' {
import express = require('express')
import express = require('express');
type Middleware = (req: express.Request, res: express.Response, next: Function) => Promise<void>
type GetAcl = (req: express.Request) => Promise<Boolean> | Promise<Object>
type Actions = Array<String> | 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<String>
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<String>): Middleware
// Check if current user can do action
can(action: String): Middleware
can(actions: Array<String>): Middleware
can(action: Action): Middleware
can(actions: Array<Action>): Middleware
private addRole(roleName: String, getAcl: GetAcl): void
private evaluateRouteActions(req: express.Request, action: Array<Action>, context: Context): Actions
private evaluateRouteAction(req: express.Request, expr: String, key: String, context: Context): String
private evaluateUserActions(req: express.Request, roles: Array<Role>): Promise<Array<Action>>
private evaluateUserAction(action: RoleParams, context: { [key: string]: Array<String> }): { [key: string]: Array<String> }
}
interface Roles {
[key: string]: RoleActions
}
interface RoleActions {
actions: Array<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)
}
const out: Imperium
export default out
type GetAcl = (req: express.Request) => Promise<boolean> | Promise<object>;
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<express.RequestHandler>;
// Check if current user can do action(s)
can(actionS: string | string[] | Action | Action[]): Promise<express.RequestHandler>;
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<Action[]>;
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;