Merge pull request #26083 from mono-js/imperium

Add imperium library typing
This commit is contained in:
Benjamin Lichtman
2018-06-04 14:30:01 -07:00
committed by GitHub
4 changed files with 145 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
import imperium from 'imperium';
imperium.role('admin', async (req) => {
return true;
});
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']);
+98
View File
@@ -0,0 +1,98 @@
// 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" />
import express = require('express');
export type GetAcl = (req: express.Request) => Promise<boolean> | Promise<object>;
export type Actions = string[] | string;
export type Context = Array<'params' | 'query' | 'headers' | 'body'>;
// Can contain when key that is evaluated during route action
export 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<express.RequestHandler>;
// Check if current user can do action(s)
export function can(actionS: string | string[] | Action | Action[]): Promise<express.RequestHandler>;
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<Action[]>;
export function evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [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;
evaluateRouteActions(req: express.Request, action: Action[], context: Context): Actions;
evaluateRouteAction(req: express.Request, expr: string, key: string, context: Context): string;
evaluateUserActions(req: express.Request, roles: Role[]): Promise<Action[]>;
evaluateUserAction(action: RoleParams, context: { [key: string]: string[] }): { [key: string]: string[] };
}
export interface Roles {
[key: string]: RoleActions;
}
export interface RoleActions {
actions: Action[];
getAcl?: GetAcl;
}
export 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)
}
+25
View File
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"esModuleInterop": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"imperium-tests.ts"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}