Skip to content

Commit 71a8914

Browse files
committed
fix: linter
1 parent 05bbc25 commit 71a8914

File tree

11 files changed

+110
-94
lines changed

11 files changed

+110
-94
lines changed

src/RoutingControllers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Action } from './Action';
2-
import { ActionMetadata } from './metadata/ActionMetadata';
32
import { ActionParameterHandler } from './ActionParameterHandler';
3+
import { getFromContainer } from './container';
44
import { BaseDriver } from './driver/BaseDriver';
5-
import { InterceptorInterface } from './InterceptorInterface';
6-
import { InterceptorMetadata } from './metadata/InterceptorMetadata';
75
import { MetadataBuilder } from './metadata-builder/MetadataBuilder';
6+
import { ActionMetadata } from './metadata/ActionMetadata';
7+
import { InterceptorMetadata } from './metadata/InterceptorMetadata';
88
import { RoutingControllersOptions } from './RoutingControllersOptions';
9-
import { getFromContainer } from './container';
109
import { isPromiseLike } from './util/isPromiseLike';
1110
import { runInSequence } from './util/runInSequence';
1211

@@ -176,7 +175,7 @@ export class RoutingControllers<T extends BaseDriver> {
176175
if (use.interceptor.prototype && use.interceptor.prototype.intercept) {
177176
// if this is function instance of InterceptorInterface
178177
return function (action: Action, result: any) {
179-
return (getFromContainer(use.interceptor, action) as InterceptorInterface).intercept(action, result);
178+
return (getFromContainer(use.interceptor, action)).intercept(action, result);
180179
};
181180
}
182181
return use.interceptor;

src/RoutingControllersOptions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { AuthorizationChecker } from './AuthorizationChecker';
21
import { ClassTransformOptions } from 'class-transformer';
3-
import { CurrentUserChecker } from './CurrentUserChecker';
4-
import { ParamOptions } from './decorator-options/ParamOptions';
52
import { ValidatorOptions } from 'class-validator';
3+
import { AuthorizationChecker } from './AuthorizationChecker';
4+
import { CurrentUserChecker } from './CurrentUserChecker';
65

76
/**
87
* Routing controller initialization options.

src/driver/BaseDriver.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,6 @@ export abstract class BaseDriver {
8787
*/
8888
currentUserChecker?: CurrentUserChecker;
8989

90-
/**
91-
* Initializes the things driver needs before routes and middleware registration.
92-
*/
93-
abstract initialize(): void;
94-
95-
/**
96-
* Registers given middleware.
97-
*/
98-
abstract registerMiddleware(middleware: MiddlewareMetadata): void;
99-
100-
/**
101-
* Registers action in the driver.
102-
*/
103-
abstract registerAction(action: ActionMetadata, executeCallback: (options: Action) => any): void;
104-
105-
/**
106-
* Registers all routes in the framework.
107-
*/
108-
abstract registerRoutes(): void;
109-
110-
/**
111-
* Gets param from the request.
112-
*/
113-
abstract getParamFromRequest(actionOptions: Action, param: ParamMetadata): any;
114-
115-
/**
116-
* Defines an algorithm of how to handle error during executing controller action.
117-
*/
118-
abstract handleError(error: any, action: ActionMetadata, options: Action): any;
119-
120-
/**
121-
* Defines an algorithm of how to handle success result of executing controller action.
122-
*/
123-
abstract handleSuccess(result: any, action: ActionMetadata, options: Action): void;
12490

12591
// -------------------------------------------------------------------------
12692
// Protected Methods
@@ -194,16 +160,51 @@ export abstract class BaseDriver {
194160

195161
protected merge(obj1: any, obj2: any): any {
196162
const result: any = {};
197-
for (let i in obj1) {
163+
for (const i in obj1) {
198164
if (i in obj2 && typeof obj1[i] === 'object' && i !== null) {
199165
result[i] = this.merge(obj1[i], obj2[i]);
200166
} else {
201167
result[i] = obj1[i];
202168
}
203169
}
204-
for (let i in obj2) {
170+
for (const i in obj2) {
205171
result[i] = obj2[i];
206172
}
207173
return result;
208174
}
175+
176+
/**
177+
* Initializes the things driver needs before routes and middleware registration.
178+
*/
179+
abstract initialize(): void;
180+
181+
/**
182+
* Registers given middleware.
183+
*/
184+
abstract registerMiddleware(middleware: MiddlewareMetadata): void;
185+
186+
/**
187+
* Registers action in the driver.
188+
*/
189+
abstract registerAction(action: ActionMetadata, executeCallback: (options: Action) => any): void;
190+
191+
/**
192+
* Registers all routes in the framework.
193+
*/
194+
abstract registerRoutes(): void;
195+
196+
/**
197+
* Gets param from the request.
198+
*/
199+
abstract getParamFromRequest(actionOptions: Action, param: ParamMetadata): any;
200+
201+
/**
202+
* Defines an algorithm of how to handle error during executing controller action.
203+
*/
204+
abstract handleError(error: any, action: ActionMetadata, options: Action): any;
205+
206+
/**
207+
* Defines an algorithm of how to handle success result of executing controller action.
208+
*/
209+
abstract handleSuccess(result: any, action: ActionMetadata, options: Action): void;
209210
}

src/driver/express/ExpressDriver.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { getFromContainer } from '../../container';
1313
import { AuthorizationRequiredError } from '../../error/AuthorizationRequiredError';
1414
import { NotFoundError } from '../../index';
1515

16+
// eslint-disable-next-line @typescript-eslint/no-var-requires
1617
const cookie = require('cookie');
18+
// eslint-disable-next-line @typescript-eslint/no-var-requires
1719
const templateUrl = require('template-url');
1820

1921
/**
@@ -39,6 +41,7 @@ export class ExpressDriver extends BaseDriver {
3941
*/
4042
initialize() {
4143
if (this.cors) {
44+
// eslint-disable-next-line @typescript-eslint/no-var-requires
4245
const cors = require('cors');
4346
if (this.cors === true) {
4447
this.express.use(cors());
@@ -114,7 +117,7 @@ export class ExpressDriver extends BaseDriver {
114117

115118
const handleError = (result: any) => {
116119
if (!result) {
117-
let error =
120+
const error =
118121
actionMetadata.authorizedRoles.length === 0
119122
? new AuthorizationRequiredError(action)
120123
: new AccessDeniedError(action);
@@ -186,6 +189,7 @@ export class ExpressDriver extends BaseDriver {
186189
/**
187190
* Registers all routes in the framework.
188191
*/
192+
// eslint-disable-next-line @typescript-eslint/no-empty-function
189193
registerRoutes() {}
190194

191195
/**
@@ -392,7 +396,7 @@ export class ExpressDriver extends BaseDriver {
392396
// if this is function instance of MiddlewareInterface
393397
middlewareFunctions.push((request: any, response: any, next: (err: any) => any) => {
394398
try {
395-
const useResult = (getFromContainer(use.middleware) as ExpressMiddlewareInterface).use(
399+
const useResult = (getFromContainer(use.middleware)).use(
396400
request,
397401
response,
398402
next
@@ -412,7 +416,7 @@ export class ExpressDriver extends BaseDriver {
412416
} else if (use.middleware.prototype && use.middleware.prototype.error) {
413417
// if this is function instance of ErrorMiddlewareInterface
414418
middlewareFunctions.push(function (error: any, request: any, response: any, next: (err: any) => any) {
415-
return (getFromContainer(use.middleware) as ExpressErrorMiddlewareInterface).error(
419+
return (getFromContainer(use.middleware)).error(
416420
error,
417421
request,
418422
response,
@@ -433,6 +437,7 @@ export class ExpressDriver extends BaseDriver {
433437
if (require) {
434438
if (!this.express) {
435439
try {
440+
// eslint-disable-next-line @typescript-eslint/no-var-requires
436441
this.express = require('express')();
437442
} catch (e) {
438443
throw new Error('express package was not found installed. Try to install it: npm install express --save');

src/driver/koa/KoaDriver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import { RoleChecker } from '../../RoleChecker';
1313
import { AuthorizationRequiredError } from '../../error/AuthorizationRequiredError';
1414
import { HttpError, NotFoundError } from '../../index';
1515

16+
// eslint-disable-next-line @typescript-eslint/no-var-requires
1617
const cookie = require('cookie');
18+
// eslint-disable-next-line @typescript-eslint/no-var-requires
1719
const templateUrl = require('template-url');
1820

1921
/**
@@ -39,9 +41,11 @@ export class KoaDriver extends BaseDriver {
3941
* Initializes the things driver needs before routes and middleware registration.
4042
*/
4143
initialize() {
44+
// eslint-disable-next-line @typescript-eslint/no-var-requires
4245
const bodyParser = require('koa-bodyparser');
4346
this.koa.use(bodyParser());
4447
if (this.cors) {
48+
// eslint-disable-next-line @typescript-eslint/no-var-requires
4549
const cors = require('kcors');
4650
if (this.cors === true) {
4751
this.koa.use(cors());
@@ -82,7 +86,7 @@ export class KoaDriver extends BaseDriver {
8286

8387
const handleError = (result: any) => {
8488
if (!result) {
85-
let error =
89+
const error =
8690
actionMetadata.authorizedRoles.length === 0
8791
? new AuthorizationRequiredError(action)
8892
: new AccessDeniedError(action);
@@ -118,6 +122,7 @@ export class KoaDriver extends BaseDriver {
118122
defaultMiddlewares.push(multer(param.extraOptions).array(param.name));
119123
});
120124

125+
// eslint-disable-next-line @typescript-eslint/unbound-method
121126
defaultMiddlewares.push(this.fixMulterRequestAssignment);
122127
}
123128

@@ -330,7 +335,7 @@ export class KoaDriver extends BaseDriver {
330335
// if this is function instance of MiddlewareInterface
331336
middlewareFunctions.push(async (context: any, next: (err?: any) => Promise<any>) => {
332337
try {
333-
return await (getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
338+
return await (getFromContainer(use.middleware)).use(context, next);
334339
} catch (error) {
335340
return await this.handleError(error, undefined, {
336341
request: context.request,

src/error/AccessDeniedError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export class AccessDeniedError extends ForbiddenError {
1010
constructor(action: Action) {
1111
super();
1212
Object.setPrototypeOf(this, AccessDeniedError.prototype);
13-
const uri = action.request.method + ' ' + action.request.url; // todo: check it it works in koa
13+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
14+
const uri = `${action.request.method} ${action.request.url}`; // todo: check it it works in koa
1415
this.message = `Access is denied for request on ${uri}`;
1516
}
1617
}

src/error/AuthorizationRequiredError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export class AuthorizationRequiredError extends UnauthorizedError {
1010
constructor(action: Action) {
1111
super();
1212
Object.setPrototypeOf(this, AuthorizationRequiredError.prototype);
13-
const uri = action.request.method + ' ' + action.request.url; // todo: check it it works in koa
13+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
14+
const uri = `${action.request.method} ${action.request.url}`; // todo: check it it works in koa
1415
this.message = `Authorization is required for request on ${uri}`;
1516
}
1617
}

src/error/ParamRequiredError.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export class ParamRequiredError extends BadRequestError {
5454
paramName = 'Parameter is';
5555
}
5656

57-
const uri = action.request.method + ' ' + action.request.url; // todo: check it it works in koa
57+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
58+
const uri = `${action.request.method} ${action.request.url}`; // todo: check it it works in koa
5859
this.message = `${paramName} required for request on ${uri}`;
5960
}
6061
}

src/metadata-builder/MetadataBuilder.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class MetadataBuilder {
9494
*/
9595
protected createActions(controller: ControllerMetadata): ActionMetadata[] {
9696
let target = controller.target;
97-
let actionsWithTarget: ActionMetadataArgs[] = [];
97+
const actionsWithTarget: ActionMetadataArgs[] = [];
9898
while (target) {
9999
actionsWithTarget.push(
100100
...getMetadataArgsStorage()
@@ -125,17 +125,6 @@ export class MetadataBuilder {
125125
.map(paramArgs => new ParamMetadata(action, this.decorateDefaultParamOptions(paramArgs)));
126126
}
127127

128-
/**
129-
* Decorate paramArgs with default settings
130-
*/
131-
private decorateDefaultParamOptions(paramArgs: ParamMetadataArgs) {
132-
let options = this.options.defaults && this.options.defaults.paramOptions;
133-
if (!options) return paramArgs;
134-
135-
if (paramArgs.required === undefined) paramArgs.required = options.required || false;
136-
137-
return paramArgs;
138-
}
139128

140129
/**
141130
* Creates response handler metadatas for action.
@@ -190,4 +179,16 @@ export class MetadataBuilder {
190179
.filterInterceptorUsesWithTargetAndMethod(controller.target, undefined)
191180
.map(useArgs => new InterceptorMetadata(useArgs));
192181
}
182+
183+
/**
184+
* Decorate paramArgs with default settings
185+
*/
186+
private decorateDefaultParamOptions(paramArgs: ParamMetadataArgs) {
187+
const options = this.options.defaults && this.options.defaults.paramOptions;
188+
if (!options) return paramArgs;
189+
190+
if (paramArgs.required === undefined) paramArgs.required = options.required || false;
191+
192+
return paramArgs;
193+
}
193194
}

src/metadata/ActionMetadata.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ export class ActionMetadata {
168168
this.methodOverride = args.methodOverride;
169169
}
170170

171+
// -------------------------------------------------------------------------
172+
// Static Methods
173+
// -------------------------------------------------------------------------
174+
175+
/**
176+
* Appends base route to a given regexp route.
177+
*/
178+
static appendBaseRoute(baseRoute: string, route: RegExp | string) {
179+
const prefix = `${baseRoute.length > 0 && baseRoute.indexOf('/') < 0 ? '/' : ''}${baseRoute}`;
180+
if (typeof route === 'string') return `${prefix}${route}`;
181+
182+
if (!baseRoute || baseRoute === '') return route;
183+
184+
const fullPath = `^${prefix}${route.toString().substr(1)}?$`;
185+
186+
return new RegExp(fullPath, route.flags);
187+
}
188+
171189
// -------------------------------------------------------------------------
172190
// Public Methods
173191
// -------------------------------------------------------------------------
@@ -220,6 +238,20 @@ export class ActionMetadata {
220238
);
221239
}
222240

241+
// -------------------------------------------------------------------------
242+
// Public Methods
243+
// -------------------------------------------------------------------------
244+
245+
/**
246+
* Calls action method.
247+
* Action method is an action defined in a user controller.
248+
*/
249+
callMethod(params: any[], action: Action) {
250+
const controllerInstance = this.controllerMetadata.getInstance(action);
251+
// eslint-disable-next-line prefer-spread
252+
return controllerInstance[this.method].apply(controllerInstance, params);
253+
}
254+
223255
// -------------------------------------------------------------------------
224256
// Private Methods
225257
// -------------------------------------------------------------------------
@@ -259,34 +291,4 @@ export class ActionMetadata {
259291
return headers;
260292
}
261293

262-
// -------------------------------------------------------------------------
263-
// Public Methods
264-
// -------------------------------------------------------------------------
265-
266-
/**
267-
* Calls action method.
268-
* Action method is an action defined in a user controller.
269-
*/
270-
callMethod(params: any[], action: Action) {
271-
const controllerInstance = this.controllerMetadata.getInstance(action);
272-
return controllerInstance[this.method].apply(controllerInstance, params);
273-
}
274-
275-
// -------------------------------------------------------------------------
276-
// Static Methods
277-
// -------------------------------------------------------------------------
278-
279-
/**
280-
* Appends base route to a given regexp route.
281-
*/
282-
static appendBaseRoute(baseRoute: string, route: RegExp | string) {
283-
const prefix = `${baseRoute.length > 0 && baseRoute.indexOf('/') < 0 ? '/' : ''}${baseRoute}`;
284-
if (typeof route === 'string') return `${prefix}${route}`;
285-
286-
if (!baseRoute || baseRoute === '') return route;
287-
288-
const fullPath = `^${prefix}${route.toString().substr(1)}?$`;
289-
290-
return new RegExp(fullPath, route.flags);
291-
}
292294
}

0 commit comments

Comments
 (0)