2022-04-25 14:17:59 +02:00
|
|
|
import { IRouter, Router, Request, Response, RequestHandler } from 'express';
|
2021-08-13 10:36:19 +02:00
|
|
|
import { Logger } from 'lib/logger';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
2021-12-03 12:46:50 +01:00
|
|
|
import { NONE } from '../types/permissions';
|
2021-08-13 10:36:19 +02:00
|
|
|
import { handleErrors } from './util';
|
2021-12-03 12:46:50 +01:00
|
|
|
import NoAccessError from '../error/no-access-error';
|
|
|
|
import requireContentType from '../middleware/content_type_checker';
|
2021-04-12 20:25:03 +02:00
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
interface IRequestHandler<
|
|
|
|
P = any,
|
|
|
|
ResBody = any,
|
|
|
|
ReqBody = any,
|
|
|
|
ReqQuery = any,
|
|
|
|
> {
|
|
|
|
(
|
|
|
|
req: Request<P, ResBody, ReqBody, ReqQuery>,
|
|
|
|
res: Response<ResBody>,
|
|
|
|
): Promise<void> | void;
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:32:18 +02:00
|
|
|
interface IRouteOptionsBase {
|
2022-04-25 14:17:59 +02:00
|
|
|
path: string;
|
Complete open api schemas for project features controller (#1563)
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* bug fix
* bug fix
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* added emptyResponse, patch feature operation schemas and request
* added emptyResponse, patch feature operation schemas and request
* patch strategy
* patch strategy
* update strategy
* update strategy
* fix pr comment
* fix pr comments
* improvements
* added operationId to schema for better generation
* fix pr comment
* fix pr comment
* fix pr comment
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* Update response types to use inferred types
* Update addTag response status to 201
* refactor: move schema ref destructuring into createSchemaObject
* made serialize date handle deep objects
* made serialize date handle deep objects
* add `name` to IFeatureStrategy nad fix tests
* fix pr comments
* fix pr comments
* Add types to IAuthRequest
* Sync StrategySchema for FE and BE - into the rabbit hole
* Sync model with OAS spec
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* bug fix
* bug fix
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* added emptyResponse, patch feature operation schemas and request
* added emptyResponse, patch feature operation schemas and request
* patch strategy
* patch strategy
* update strategy
* update strategy
* fix pr comment
* fix pr comments
* improvements
* added operationId to schema for better generation
* fix pr comment
* fix pr comment
* fix pr comment
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* Update response types to use inferred types
* Update addTag response status to 201
* refactor: move schema ref destructuring into createSchemaObject
* made serialize date handle deep objects
* made serialize date handle deep objects
* add `name` to IFeatureStrategy nad fix tests
* fix pr comments
* fix pr comments
* Add types to IAuthRequest
* Sync StrategySchema for FE and BE - into the rabbit hole
* Sync model with OAS spec
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* bug fix
* bug fix
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* added emptyResponse, patch feature operation schemas and request
* added emptyResponse, patch feature operation schemas and request
* patch strategy
* patch strategy
* update strategy
* update strategy
* fix pr comment
* fix pr comments
* improvements
* added operationId to schema for better generation
* fix pr comment
* fix pr comment
* fix pr comment
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* Update response types to use inferred types
* Update addTag response status to 201
* refactor: move schema ref destructuring into createSchemaObject
* made serialize date handle deep objects
* made serialize date handle deep objects
* add `name` to IFeatureStrategy nad fix tests
* fix pr comments
* fix pr comments
* Add types to IAuthRequest
* Sync StrategySchema for FE and BE - into the rabbit hole
* Sync model with OAS spec
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* Completed OpenAPI Schemas for ProjectFeatures Controller
Completed OpenAPI Schemas for Feature Controller (tags)
* bug fix
* bug fix
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* fix merge conflicts, some refactoring
* added emptyResponse, patch feature operation schemas and request
* added emptyResponse, patch feature operation schemas and request
* patch strategy
* patch strategy
* update strategy
* update strategy
* fix pr comment
* fix pr comments
* improvements
* added operationId to schema for better generation
* fix pr comment
* fix pr comment
* fix pr comment
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* improvements to generated and dynamic types
* Update response types to use inferred types
* Update addTag response status to 201
* refactor: move schema ref destructuring into createSchemaObject
* made serialize date handle deep objects
* made serialize date handle deep objects
* add `name` to IFeatureStrategy nad fix tests
* fix pr comments
* fix pr comments
* Add types to IAuthRequest
* Sync StrategySchema for FE and BE - into the rabbit hole
* Sync model with OAS spec
* revert
* revert
* revert
* revert
* revert
* mapper
* revert
* revert
* revert
* remove serialize-dates.ts
* remove serialize-dates.ts
* remove serialize-dates.ts
* remove serialize-dates.ts
* remove serialize-dates.ts
* revert
* revert
* add mappers
* add mappers
* fix pr comments
* ignore report.json
* ignore report.json
* Route permission required
Co-authored-by: olav <mail@olav.io>
2022-05-18 15:17:09 +02:00
|
|
|
permission: string;
|
2022-04-25 14:17:59 +02:00
|
|
|
middleware?: RequestHandler[];
|
|
|
|
handler: IRequestHandler;
|
|
|
|
acceptedContentTypes?: string[];
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:32:18 +02:00
|
|
|
interface IRouteOptionsGet extends IRouteOptionsBase {
|
|
|
|
method: 'get';
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IRouteOptionsNonGet extends IRouteOptionsBase {
|
|
|
|
method: 'post' | 'put' | 'patch' | 'delete';
|
|
|
|
acceptAnyContentType?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
type IRouteOptions = IRouteOptionsNonGet | IRouteOptionsGet;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const checkPermission = (permission) => async (req, res, next) => {
|
2021-12-03 12:46:50 +01:00
|
|
|
if (!permission || permission === NONE) {
|
2021-04-16 15:29:23 +02:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (req.checkRbac && (await req.checkRbac(permission))) {
|
|
|
|
return next();
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
return res.status(403).json(new NoAccessError(permission)).end();
|
2021-04-12 20:25:03 +02:00
|
|
|
};
|
|
|
|
|
2018-11-30 10:11:36 +01:00
|
|
|
/**
|
|
|
|
* Base class for Controllers to standardize binding to express Router.
|
2021-08-13 10:36:19 +02:00
|
|
|
*
|
|
|
|
* This class will take care of the following:
|
|
|
|
* - try/catch inside RequestHandler
|
|
|
|
* - await if the RequestHandler returns a promise.
|
|
|
|
* - access control
|
2018-11-30 10:11:36 +01:00
|
|
|
*/
|
2021-04-22 10:07:10 +02:00
|
|
|
export default class Controller {
|
2021-08-13 10:36:19 +02:00
|
|
|
private ownLogger: Logger;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
app: IRouter;
|
|
|
|
|
|
|
|
config: IUnleashConfig;
|
|
|
|
|
|
|
|
constructor(config: IUnleashConfig) {
|
2021-08-13 10:36:19 +02:00
|
|
|
this.ownLogger = config.getLogger(
|
|
|
|
`controller/${this.constructor.name}`,
|
|
|
|
);
|
2021-04-22 10:07:10 +02:00
|
|
|
this.app = Router();
|
2018-12-19 14:50:01 +01:00
|
|
|
this.config = config;
|
2018-12-19 13:35:54 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 14:17:59 +02:00
|
|
|
private useRouteErrorHandler(handler: IRequestHandler): IRequestHandler {
|
2021-08-13 10:36:19 +02:00
|
|
|
return async (req: Request, res: Response) => {
|
|
|
|
try {
|
|
|
|
await handler(req, res);
|
|
|
|
} catch (error) {
|
|
|
|
handleErrors(res, this.ownLogger, error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-25 14:17:59 +02:00
|
|
|
private useContentTypeMiddleware(options: IRouteOptions): RequestHandler[] {
|
|
|
|
const { middleware = [], acceptedContentTypes = [] } = options;
|
|
|
|
|
2022-06-07 09:32:18 +02:00
|
|
|
return options.method === 'get' || options.acceptAnyContentType
|
2022-04-25 14:17:59 +02:00
|
|
|
? middleware
|
|
|
|
: [requireContentType(...acceptedContentTypes), ...middleware];
|
|
|
|
}
|
|
|
|
|
|
|
|
route(options: IRouteOptions): void {
|
|
|
|
this.app[options.method](
|
|
|
|
options.path,
|
|
|
|
checkPermission(options.permission),
|
|
|
|
this.useContentTypeMiddleware(options),
|
|
|
|
this.useRouteErrorHandler(options.handler.bind(this)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
get(path: string, handler: IRequestHandler, permission?: string): void {
|
2022-04-25 14:17:59 +02:00
|
|
|
this.route({
|
|
|
|
method: 'get',
|
2021-08-13 10:36:19 +02:00
|
|
|
path,
|
2022-04-25 14:17:59 +02:00
|
|
|
handler,
|
|
|
|
permission,
|
|
|
|
});
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
post(
|
|
|
|
path: string,
|
2021-08-13 10:36:19 +02:00
|
|
|
handler: IRequestHandler,
|
2021-12-03 12:46:50 +01:00
|
|
|
permission: string,
|
2021-04-22 10:07:10 +02:00
|
|
|
...acceptedContentTypes: string[]
|
|
|
|
): void {
|
2022-04-25 14:17:59 +02:00
|
|
|
this.route({
|
|
|
|
method: 'post',
|
2018-12-19 13:35:54 +01:00
|
|
|
path,
|
2022-04-25 14:17:59 +02:00
|
|
|
handler,
|
|
|
|
permission,
|
|
|
|
acceptedContentTypes,
|
|
|
|
});
|
2018-11-30 10:11:36 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
put(
|
|
|
|
path: string,
|
2021-08-13 10:36:19 +02:00
|
|
|
handler: IRequestHandler,
|
2021-12-03 12:46:50 +01:00
|
|
|
permission: string,
|
2021-04-22 10:07:10 +02:00
|
|
|
...acceptedContentTypes: string[]
|
|
|
|
): void {
|
2022-04-25 14:17:59 +02:00
|
|
|
this.route({
|
|
|
|
method: 'put',
|
2018-12-19 13:35:54 +01:00
|
|
|
path,
|
2022-04-25 14:17:59 +02:00
|
|
|
handler,
|
|
|
|
permission,
|
|
|
|
acceptedContentTypes,
|
|
|
|
});
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
patch(
|
|
|
|
path: string,
|
|
|
|
handler: IRequestHandler,
|
2021-12-03 12:46:50 +01:00
|
|
|
permission: string,
|
2021-09-13 10:23:57 +02:00
|
|
|
...acceptedContentTypes: string[]
|
|
|
|
): void {
|
2022-04-25 14:17:59 +02:00
|
|
|
this.route({
|
|
|
|
method: 'patch',
|
2021-09-13 10:23:57 +02:00
|
|
|
path,
|
2022-04-25 14:17:59 +02:00
|
|
|
handler,
|
|
|
|
permission,
|
|
|
|
acceptedContentTypes,
|
|
|
|
});
|
2021-09-13 10:23:57 +02:00
|
|
|
}
|
|
|
|
|
2021-12-03 12:46:50 +01:00
|
|
|
delete(path: string, handler: IRequestHandler, permission: string): void {
|
2022-04-25 14:17:59 +02:00
|
|
|
this.route({
|
|
|
|
method: 'delete',
|
2021-08-13 10:36:19 +02:00
|
|
|
path,
|
2022-04-25 14:17:59 +02:00
|
|
|
handler,
|
|
|
|
permission,
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
});
|
2018-11-30 11:11:12 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
fileupload(
|
|
|
|
path: string,
|
2021-08-13 10:36:19 +02:00
|
|
|
filehandler: IRequestHandler,
|
2021-04-22 10:07:10 +02:00
|
|
|
handler: Function,
|
2021-12-03 12:46:50 +01:00
|
|
|
permission: string,
|
2021-04-22 10:07:10 +02:00
|
|
|
): void {
|
2019-03-13 19:10:13 +01:00
|
|
|
this.app.post(
|
|
|
|
path,
|
2021-04-12 20:25:03 +02:00
|
|
|
checkPermission(permission),
|
2021-04-22 10:07:10 +02:00
|
|
|
filehandler.bind(this),
|
2022-04-25 14:17:59 +02:00
|
|
|
this.useRouteErrorHandler(handler.bind(this)),
|
2019-03-13 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
use(path: string, router: IRouter): void {
|
2018-12-03 08:59:13 +01:00
|
|
|
this.app.use(path, router);
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:58:58 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
useWithMiddleware(path: string, router: IRouter, middleware: any): void {
|
|
|
|
this.app.use(path, middleware, router);
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
get router(): any {
|
2018-11-30 10:11:36 +01:00
|
|
|
return this.app;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Controller;
|