diff --git a/types/acl/index.d.ts b/types/acl/index.d.ts index 3f4df31aa2..931452dc52 100644 --- a/types/acl/index.d.ts +++ b/types/acl/index.d.ts @@ -1,150 +1,179 @@ -// Type definitions for node_acl 0.4.8 +// Type definitions for acl 0.4 // Project: https://github.com/optimalbits/node_acl // Definitions by: Qubo // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 /// -/// import http = require('http'); -import Promise = require("bluebird"); -import express = require("express"); +import Promise = require('bluebird'); +import express = require('express'); +import redis = require('redis'); +import mongo = require('mongodb'); -type strings = string|string[]; -type Value = string|number; -type Values = Value|Value[]; +export = AclStatic; + +declare const AclStatic: AclStatic; + +type strings = string | string[]; +type Value = string | number; +type Values = Value | Value[]; type Action = () => any; -type Callback = (err: Error) => any; +type Callback = (err?: Error) => any; type AnyCallback = (err: Error, obj: any) => any; type AllowedCallback = (err: Error, allowed: boolean) => any; type GetUserId = (req: http.IncomingMessage, res: http.ServerResponse) => Value; interface AclStatic { - new (backend: Backend, logger: Logger, options: Option): Acl; - new (backend: Backend, logger: Logger): Acl; - new (backend: Backend): Acl; - memoryBackend: MemoryBackendStatic; + new ( + backend: AclStatic.Backend, + logger?: AclStatic.Logger, + options?: AclStatic.Option + ): AclStatic.Acl; + readonly memoryBackend: AclStatic.MemoryBackendStatic; + readonly mongodbBackend: AclStatic.MongodbBackendStatic; + readonly redisBackend: AclStatic.RedisBackendStatic; } -interface Logger { - debug: (msg: string) => any; -} - -interface Acl { - addUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise; - removeUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise; - userRoles: (userId: Value, cb?: (err: Error, roles: string[]) => any) => Promise; - roleUsers: (role: Value, cb?: (err: Error, users: Values) => any) => Promise; - hasRole: (userId: Value, role: string, cb?: (err: Error, isInRole: boolean) => any) => Promise; - addRoleParents: (role: string, parents: Values, cb?: Callback) => Promise; - removeRole: (role: string, cb?: Callback) => Promise; - removeResource: (resource: string, cb?: Callback) => Promise; - allow: { - (roles: Values, resources: strings, permissions: strings, cb?: Callback): Promise; - (aclSets: AclSet | AclSet[]): Promise; +declare namespace AclStatic { + interface Logger { + debug: (msg: string) => any; } - removeAllow: (role: string, resources: strings, permissions: strings, cb?: Callback) => Promise; - removePermissions: (role: string, resources: strings, permissions: strings, cb?: Function) => Promise; - allowedPermissions: (userId: Value, resources: strings, cb?: AnyCallback) => Promise; - isAllowed: (userId: Value, resources: strings, permissions: strings, cb?: AllowedCallback) => Promise; - areAnyRolesAllowed: (roles: strings, resource: strings, permissions: strings, cb?: AllowedCallback) => Promise; - whatResources: { - (roles: strings, cb?: AnyCallback): Promise; - (roles: strings, permissions: strings, cb?: AnyCallback): Promise; + + interface Acl { + addUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise; + removeUserRoles: (userId: Value, roles: strings, cb?: Callback) => Promise; + userRoles: (userId: Value, cb?: (err: Error, roles: string[]) => any) => Promise; + roleUsers: (role: Value, cb?: (err: Error, users: Values) => any) => Promise; + hasRole: ( + userId: Value, + role: string, + cb?: (err: Error, isInRole: boolean) => any + ) => Promise; + addRoleParents: (role: string, parents: Values, cb?: Callback) => Promise; + removeRole: (role: string, cb?: Callback) => Promise; + removeResource: (resource: string, cb?: Callback) => Promise; + allow: { + (roles: Values, resources: strings, permissions: strings, cb?: Callback): Promise; + (aclSets: AclSet | AclSet[]): Promise; + }; + removeAllow: ( + role: string, + resources: strings, + permissions: strings, + cb?: Callback + ) => Promise; + removePermissions: ( + role: string, + resources: strings, + permissions: strings, + cb?: Callback + ) => Promise; + allowedPermissions: (userId: Value, resources: strings, cb?: AnyCallback) => Promise; + isAllowed: ( + userId: Value, + resources: strings, + permissions: strings, + cb?: AllowedCallback + ) => Promise; + areAnyRolesAllowed: ( + roles: strings, + resource: strings, + permissions: strings, + cb?: AllowedCallback + ) => Promise; + whatResources: { + (roles: strings, cb?: AnyCallback): Promise; + (roles: strings, permissions: strings, cb?: AnyCallback): Promise; + }; + permittedResources: (roles: strings, permissions: strings, cb?: Callback) => Promise; + middleware: ( + numPathComponents?: number, + userId?: Value | GetUserId, + actions?: strings + ) => express.RequestHandler; + } + + interface Option { + buckets?: BucketsOption; + } + + interface BucketsOption { + meta?: string; + parents?: string; + permissions?: string; + resources?: string; + roles?: string; + users?: string; + } + + interface AclSet { + roles: strings; + allows: AclAllow[]; + } + + interface AclAllow { + resources: strings; + permissions: strings; + } + + interface MemoryBackend extends Backend {} + interface MemoryBackendStatic { + new (): MemoryBackend; + } + + // + // For internal use + // + interface Backend { + begin: () => T; + end: (transaction: T, cb?: Action) => void; + clean: (cb?: Action) => void; + get: (bucket: string, key: Value, cb?: Action) => void; + union: (bucket: string, keys: Value[], cb?: Action) => void; + add: (transaction: T, bucket: string, key: Value, values: Values) => void; + del: (transaction: T, bucket: string, keys: Value[]) => void; + remove: (transaction: T, bucket: string, key: Value, values: Values) => void; + + endAsync: (transaction: T, cb?: (err: Error | null) => void) => Promise; + getAsync: ( + bucket: string, + key: Value, + cb?: (err: Error | null, value: any) => void + ) => Promise; + cleanAsync: (cb?: (error?: Error) => void) => Promise; + unionAsync: ( + bucket: string, + keys: Value[], + cb?: (error: Error | undefined, results: any[]) => void + ) => Promise; + } + + interface Contract { + (args: IArguments): Contract | NoOp; + debug: boolean; + fulfilled: boolean; + args: any[]; + checkedParams: string[]; + params: (...types: string[]) => Contract | NoOp; + end: () => void; + } + + interface NoOp { + params: (...types: string[]) => NoOp; + end: () => void; + } + + // for redis backend + interface RedisBackend extends Backend {} + interface RedisBackendStatic { + new (redis: redis.RedisClient, prefix?: string): RedisBackend; + } + + // for mongodb backend + interface MongodbBackend extends Backend {} + interface MongodbBackendStatic { + new (db: mongo.Db, prefix?: string, useSingle?: boolean): MongodbBackend; } - permittedResources: (roles: strings, permissions: strings, cb?: Function) => Promise; - middleware: (numPathComponents?: number, userId?: Value | GetUserId, actions?: strings) => express.RequestHandler; } - -interface Option { - buckets?: BucketsOption; -} - -interface BucketsOption { - meta?: string; - parents?: string; - permissions?: string; - resources?: string; - roles?: string; - users?: string; -} - -interface AclSet { - roles: strings; - allows: AclAllow[]; -} - -interface AclAllow { - resources: strings; - permissions: strings; -} - -interface MemoryBackend extends Backend { } -interface MemoryBackendStatic { - new (): MemoryBackend; -} - -// -// For internal use -// -interface Backend { - begin: () => T; - end: (transaction: T, cb?: Action) => void; - clean: (cb?: Action) => void; - get: (bucket: string, key: Value, cb?: Action) => void; - union: (bucket: string, keys: Value[], cb?: Action) => void; - add: (transaction: T, bucket: string, key: Value, values: Values) => void; - del: (transaction: T, bucket: string, keys: Value[]) => void; - remove: (transaction: T, bucket: string, key: Value, values: Values) => void; - - endAsync: Function; //TODO: Give more specific function signature - getAsync: Function; - cleanAsync: Function; - unionAsync: Function; -} - -interface Contract { - (args: IArguments): Contract | NoOp; - debug: boolean; - fulfilled: boolean; - args: any[]; - checkedParams: string[]; - params: (...types: string[]) => Contract | NoOp; - end: () => void; -} - -interface NoOp { - params: (...types: string[]) => NoOp; - end: () => void; -} - -// for redis backend -import redis = require('redis'); - -interface AclStatic { - redisBackend: RedisBackendStatic; -} - -interface RedisBackend extends Backend { } -interface RedisBackendStatic { - new (redis: redis.RedisClient, prefix: string): RedisBackend; - new (redis: redis.RedisClient): RedisBackend; -} - -// for mongodb backend -import mongo = require('mongodb'); - -interface AclStatic { - mongodbBackend: MongodbBackendStatic; -} - -interface MongodbBackend extends Backend { } -interface MongodbBackendStatic { - new (db: mongo.Db, prefix: string, useSingle: boolean): MongodbBackend; - new (db: mongo.Db, prefix: string): MongodbBackend; - new (db: mongo.Db): MongodbBackend; -} - -declare var _: AclStatic; -export = _; diff --git a/types/acl/test/index.ts b/types/acl/test/index.ts index bb3e6fa54e..3485ab3662 100644 --- a/types/acl/test/index.ts +++ b/types/acl/test/index.ts @@ -2,15 +2,15 @@ // https://github.com/OptimalBits/node_acl/blob/master/Readme.md import Acl = require('acl'); -var report = (err: Error, value: T) => { - if (err) { - console.error(err); - } - console.info(value); +const report = (err: Error, value: any) => { + if (err) { + console.error(err); + } + console.info(value); }; // Using the memory backend -var acl = new Acl(new Acl.memoryBackend()); +const acl: Acl.Acl = new Acl(new Acl.memoryBackend()); // middleware with no optional parameters acl.middleware(); @@ -18,11 +18,11 @@ acl.middleware(); acl.middleware(1); acl.middleware(1, () => { - return "joed"; + return 'joed'; }); acl.middleware(1, () => { - return 2; + return 2; }); acl.middleware(1, 'joed'); @@ -33,36 +33,36 @@ acl.middleware(3, 'joed', 'post'); acl.allow('guest', 'blogs', 'view'); // allow function accepts arrays as any parameter -acl.allow('member', 'blogs', ['edit','view', 'delete']); +acl.allow('member', 'blogs', ['edit', 'view', 'delete']); acl.addUserRoles('joed', 'guest'); -acl.addRoleParents('baz', ['foo','bar']); +acl.addRoleParents('baz', ['foo', 'bar']); -acl.allow('foo', ['blogs','forums','news'], ['view', 'delete']); +acl.allow('foo', ['blogs', 'forums', 'news'], ['view', 'delete']); -acl.allow('admin', ['blogs','forums'], '*'); +acl.allow('admin', ['blogs', 'forums'], '*'); acl.allow([ { - roles:['guest','special-member'], - allows:[ - {resources:'blogs', permissions:'get'}, - {resources:['forums','news'], permissions:['get','put','delete']} - ] + roles: ['guest', 'special-member'], + allows: [ + { resources: 'blogs', permissions: 'get' }, + { resources: ['forums', 'news'], permissions: ['get', 'put', 'delete'] }, + ], }, { - roles:['gold','silver'], - allows:[ - {resources:'cash', permissions:['sell','exchange']}, - {resources:['account','deposit'], permissions:['put','delete']} - ] - } + roles: ['gold', 'silver'], + allows: [ + { resources: 'cash', permissions: ['sell', 'exchange'] }, + { resources: ['account', 'deposit'], permissions: ['put', 'delete'] }, + ], + }, ]); acl.isAllowed('joed', 'blogs', 'view', (err, res) => { if (res) { - console.log("User joed is allowed to view blogs"); + console.log('User joed is allowed to view blogs'); } }); @@ -78,15 +78,14 @@ acl.whatResources('foo', 'view', (err, res) => { } }); -acl.isAllowed('jsmith', 'blogs', ['edit','view','delete']) -.then((result) => { - console.dir('jsmith is allowed blogs ' + result); - acl.addUserRoles('jsmith', 'member'); -}).then(() => - acl.isAllowed('jsmith', 'blogs', ['edit','view','delete']) -).then((result) => - console.dir('jsmith is allowed blogs ' + result) -).then(() => { - acl.allowedPermissions('james', ['blogs','forums'], report); - acl.allowedPermissions('jsmith', ['blogs','forums'], report); -}); +acl.isAllowed('jsmith', 'blogs', ['edit', 'view', 'delete']) + .then(result => { + console.dir('jsmith is allowed blogs ' + result); + acl.addUserRoles('jsmith', 'member'); + }) + .then(() => acl.isAllowed('jsmith', 'blogs', ['edit', 'view', 'delete'])) + .then(result => console.dir('jsmith is allowed blogs ' + result)) + .then(() => { + acl.allowedPermissions('james', ['blogs', 'forums'], report); + acl.allowedPermissions('jsmith', ['blogs', 'forums'], report); + }); diff --git a/types/acl/test/mongodbBackend.ts b/types/acl/test/mongodbBackend.ts index ae3b1d03c9..449cefa030 100644 --- a/types/acl/test/mongodbBackend.ts +++ b/types/acl/test/mongodbBackend.ts @@ -5,10 +5,10 @@ import mongodb = require('mongodb'); declare var db: mongodb.Db; // Using the mongo db backend -var acl = new Acl(new Acl.mongodbBackend(db, 'acl_', true)); +const acl = new Acl(new Acl.mongodbBackend(db, 'acl_', true)); // guest is allowed to view blogs acl.allow('guest', 'blogs', 'view'); // allow function accepts arrays as any parameter -acl.allow('member', 'blogs', ['edit','view', 'delete']); +acl.allow('member', 'blogs', ['edit', 'view', 'delete']); diff --git a/types/acl/test/redisBackend.ts b/types/acl/test/redisBackend.ts index ec1924cce6..669d28ed6d 100644 --- a/types/acl/test/redisBackend.ts +++ b/types/acl/test/redisBackend.ts @@ -5,10 +5,10 @@ import redis = require('redis'); declare var client: redis.RedisClient; // Using the redis backend -var acl = new Acl(new Acl.redisBackend(client, 'acl_')); +const acl = new Acl(new Acl.redisBackend(client, 'acl_')); // guest is allowed to view blogs acl.allow('guest', 'blogs', 'view'); // allow function accepts arrays as any parameter -acl.allow('member', 'blogs', ['edit','view', 'delete']); +acl.allow('member', 'blogs', ['edit', 'view', 'delete']); diff --git a/types/acl/tslint.json b/types/acl/tslint.json index a41bf5d19a..f93cf8562a 100644 --- a/types/acl/tslint.json +++ b/types/acl/tslint.json @@ -1,79 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "extends": "dtslint/dt.json" }