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;