From b875a296ffb56a74061b1dcda4cdfafc2aec781e Mon Sep 17 00:00:00 2001 From: Gaetan SENN Date: Mon, 28 May 2018 00:35:26 +0200 Subject: [PATCH] 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" + ] +}