diff --git a/types/imperium/imperium-tests.ts b/types/imperium/imperium-tests.ts new file mode 100644 index 0000000000..f7380c25cc --- /dev/null +++ b/types/imperium/imperium-tests.ts @@ -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']); diff --git a/types/imperium/index.d.ts b/types/imperium/index.d.ts new file mode 100644 index 0000000000..67eace244e --- /dev/null +++ b/types/imperium/index.d.ts @@ -0,0 +1,98 @@ +// 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 + +/// + +import express = require('express'); + +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 +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; + +// 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() + + 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; + + 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; + + 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) +} diff --git a/types/imperium/tsconfig.json b/types/imperium/tsconfig.json new file mode 100644 index 0000000000..caaa7b74ce --- /dev/null +++ b/types/imperium/tsconfig.json @@ -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" + ] +} diff --git a/types/imperium/tslint.json b/types/imperium/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/imperium/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}