@cley_faye/boilerplate 3.21.2 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -220
- package/bin/serve_spa.js +0 -1
- package/lib/bin/serve_spa.js +67 -39
- package/lib/express/middlewares/singlepageapp.d.ts +2 -2
- package/lib/express/middlewares/singlepageapp.js +20 -13
- package/lib/grunt/args.d.ts +14 -42
- package/lib/grunt/args.js +27 -41
- package/lib/grunt/reactapp/copy.d.ts +8 -34
- package/lib/grunt/reactapp/copy.js +47 -49
- package/lib/grunt/reactapp/image.d.ts +8 -23
- package/lib/grunt/reactapp/image.js +45 -42
- package/lib/grunt/reactapp/pug.d.ts +8 -30
- package/lib/grunt/reactapp/pug.js +49 -50
- package/lib/grunt/reactapp/sass.d.ts +8 -29
- package/lib/grunt/reactapp/sass.js +48 -43
- package/lib/grunt/reactapp/util.d.ts +5 -9
- package/lib/grunt/reactapp/util.js +12 -15
- package/lib/grunt/reactapp/webpack.d.ts +10 -81
- package/lib/grunt/reactapp/webpack.js +72 -132
- package/lib/grunt/reactapp.d.ts +22 -65
- package/lib/grunt/reactapp.js +70 -93
- package/lib/grunt/util.d.ts +4 -7
- package/lib/grunt/util.js +5 -8
- package/lib/grunt.d.ts +2 -3
- package/lib/grunt.js +5 -5
- package/lib/util.js +5 -4
- package/package.json +24 -36
- package/grunt.js +0 -3
- package/lib/express/autoclose.d.ts +0 -17
- package/lib/express/autoclose.js +0 -76
- package/lib/express/logger.d.ts +0 -25
- package/lib/express/logger.js +0 -132
- package/lib/express/middlewares/bodylogger.d.ts +0 -9
- package/lib/express/middlewares/bodylogger.js +0 -90
- package/lib/express/pipelinebuilder.d.ts +0 -115
- package/lib/express/pipelinebuilder.js +0 -288
- package/lib/express/unprocessableentityerror.d.ts +0 -8
- package/lib/express/unprocessableentityerror.js +0 -57
- package/lib/express.d.ts +0 -158
- package/lib/express.js +0 -218
- package/lib/repl.d.ts +0 -27
- package/lib/repl.js +0 -68
- package/lib/winston.d.ts +0 -38
- package/lib/winston.js +0 -224
package/lib/express/logger.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.registerErrorLogger = exports.registerRouteLogger = exports.defaultRouteLoggerConfig = void 0;
|
|
33
|
-
const winston_1 = __importDefault(require("winston"));
|
|
34
|
-
const express_winston_1 = __importDefault(require("express-winston"));
|
|
35
|
-
const winston_js_1 = require("../winston.js");
|
|
36
|
-
const customRouteFormat = (outputTimestamp) => winston_1.default.format.printf((info) => {
|
|
37
|
-
const meta = info.meta;
|
|
38
|
-
// Clean the meta object from stuff displayed in the regular message
|
|
39
|
-
if (meta.res?.statusCode) {
|
|
40
|
-
delete meta.res.statusCode;
|
|
41
|
-
}
|
|
42
|
-
if (meta.res) {
|
|
43
|
-
if (Object.keys(meta.res).length === 0) {
|
|
44
|
-
delete meta.res;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if ("responseTime" in meta) {
|
|
48
|
-
delete meta.responseTime;
|
|
49
|
-
}
|
|
50
|
-
if (meta.req) {
|
|
51
|
-
if (Object.keys(meta.req).length === 0) {
|
|
52
|
-
delete meta.req;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const metaString = JSON.stringify(info.meta);
|
|
56
|
-
const finalMessage = `${info.message.toString()} ${metaString === "{}" ? "" : metaString}`;
|
|
57
|
-
return (0, winston_js_1.prefixOutput)(info.level, finalMessage, outputTimestamp);
|
|
58
|
-
});
|
|
59
|
-
/** Return the default value for route logger is it is set as `true` */
|
|
60
|
-
const defaultRouteLoggerConfig = (authFromReq) => ({
|
|
61
|
-
meta: true,
|
|
62
|
-
requestWhitelist: ["body"],
|
|
63
|
-
responseWhitelist: [],
|
|
64
|
-
dynamicMeta: (req) => {
|
|
65
|
-
const castReq = req;
|
|
66
|
-
const data = { ...castReq._cleyfayeLogging?.extraLoggingData };
|
|
67
|
-
if (authFromReq) {
|
|
68
|
-
const value = authFromReq(req);
|
|
69
|
-
if (!value) {
|
|
70
|
-
data.auth = null;
|
|
71
|
-
}
|
|
72
|
-
else if (typeof value === "string") {
|
|
73
|
-
data.auth = value;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
Object.assign(data, value);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return data;
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
exports.defaultRouteLoggerConfig = defaultRouteLoggerConfig;
|
|
83
|
-
const registerRouteLogger = (app, logOptions) => {
|
|
84
|
-
const { route, timestamp, userFromReq, authFromReq, ignoredRoutes } = logOptions ?? {};
|
|
85
|
-
let extraConfig;
|
|
86
|
-
if (typeof route === "boolean") {
|
|
87
|
-
extraConfig = (0, exports.defaultRouteLoggerConfig)(authFromReq ?? userFromReq);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
extraConfig = { ...route };
|
|
91
|
-
const rec = extraConfig;
|
|
92
|
-
if (rec.winstonInstance) {
|
|
93
|
-
delete rec.winstonInstance;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
const baseConfig = {
|
|
97
|
-
meta: true,
|
|
98
|
-
requestWhitelist: ["body"],
|
|
99
|
-
bodyWhitelist: [],
|
|
100
|
-
expressFormat: false,
|
|
101
|
-
format: customRouteFormat(timestamp),
|
|
102
|
-
msg: "{{req.method}} {{req.url}} {{res.statusCode}} {{res.responseTime}}ms",
|
|
103
|
-
ignoredRoutes,
|
|
104
|
-
};
|
|
105
|
-
app.use(express_winston_1.default.logger({
|
|
106
|
-
transports: winston_js_1.transports,
|
|
107
|
-
...baseConfig,
|
|
108
|
-
...extraConfig,
|
|
109
|
-
}));
|
|
110
|
-
};
|
|
111
|
-
exports.registerRouteLogger = registerRouteLogger;
|
|
112
|
-
const getExtraFilter = (userExtraFilter) => {
|
|
113
|
-
const expressRegEx = /node_modules\/express/u;
|
|
114
|
-
if (!userExtraFilter)
|
|
115
|
-
return expressRegEx;
|
|
116
|
-
if (Array.isArray(userExtraFilter))
|
|
117
|
-
return [expressRegEx, ...userExtraFilter];
|
|
118
|
-
return [expressRegEx, userExtraFilter];
|
|
119
|
-
};
|
|
120
|
-
const registerErrorLogger = (app, logOptions) => {
|
|
121
|
-
const { error, logger, timestamp } = logOptions ?? {};
|
|
122
|
-
const config = typeof error === "boolean"
|
|
123
|
-
? {}
|
|
124
|
-
: error ?? {};
|
|
125
|
-
const activeTransports = logger ? logger.transports : winston_js_1.transports;
|
|
126
|
-
app.use(express_winston_1.default.errorLogger({
|
|
127
|
-
transports: activeTransports,
|
|
128
|
-
format: (0, winston_js_1.customFormat)(timestamp, config.collapseStacktrace ?? config.collapseNodeModules, getExtraFilter(config.extraFilter)),
|
|
129
|
-
meta: false,
|
|
130
|
-
}));
|
|
131
|
-
};
|
|
132
|
-
exports.registerErrorLogger = registerErrorLogger;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from "express";
|
|
2
|
-
export type ExtraLoggingDataType = Record<string, unknown> | undefined;
|
|
3
|
-
export type ExtraLoggingDataFunc = (req: Request) => Promise<ExtraLoggingDataType> | ExtraLoggingDataType;
|
|
4
|
-
export type ExtraLoggingData = ExtraLoggingDataFunc | Record<string, unknown>;
|
|
5
|
-
export interface ExtraLoggingOptions {
|
|
6
|
-
/** Do not expose the "hidden" body props */
|
|
7
|
-
noHiddenBodyProps?: boolean;
|
|
8
|
-
}
|
|
9
|
-
export declare const bodyLogger: (bodyProps?: Array<string>, extraLoggingData?: ExtraLoggingData, options?: ExtraLoggingOptions) => (req: Request, res: Response, next: NextFunction) => void;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.bodyLogger = void 0;
|
|
30
|
-
const setLogInfoInReq = (req, body, extraData) => {
|
|
31
|
-
const castReq = req;
|
|
32
|
-
if (body) {
|
|
33
|
-
if (!castReq._routeWhitelists)
|
|
34
|
-
castReq._routeWhitelists = {};
|
|
35
|
-
castReq._routeWhitelists.body = body;
|
|
36
|
-
}
|
|
37
|
-
if (extraData) {
|
|
38
|
-
castReq._cleyfayeLogging = { extraLoggingData: extraData };
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const getPropType = (value) => {
|
|
42
|
-
if (typeof value === "string")
|
|
43
|
-
return `string(${value.length})`;
|
|
44
|
-
if (typeof value === "number")
|
|
45
|
-
return "number";
|
|
46
|
-
if (typeof value === "boolean")
|
|
47
|
-
return "boolean";
|
|
48
|
-
if (typeof value === "undefined")
|
|
49
|
-
return "undefined";
|
|
50
|
-
if (Array.isArray(value))
|
|
51
|
-
return `Array(${value.length})`;
|
|
52
|
-
return "object";
|
|
53
|
-
};
|
|
54
|
-
const getHiddenBodyProps = (req, ignoreKeys = []) => {
|
|
55
|
-
if (typeof req.body !== "object")
|
|
56
|
-
return;
|
|
57
|
-
const body = req.body;
|
|
58
|
-
const res = {};
|
|
59
|
-
for (const key of Object.keys(body)) {
|
|
60
|
-
if (ignoreKeys.includes(key))
|
|
61
|
-
continue;
|
|
62
|
-
res[key] = getPropType(body[key]);
|
|
63
|
-
}
|
|
64
|
-
if (Object.keys(res).length === 0)
|
|
65
|
-
return;
|
|
66
|
-
return res;
|
|
67
|
-
};
|
|
68
|
-
const bodyLogger = (bodyProps, extraLoggingData, options = {}) => (req, res, next) => {
|
|
69
|
-
const asyncFunc = async () => {
|
|
70
|
-
const allExtraData = {};
|
|
71
|
-
if (!options.noHiddenBodyProps) {
|
|
72
|
-
const redactedBody = getHiddenBodyProps(req, bodyProps);
|
|
73
|
-
if (redactedBody)
|
|
74
|
-
Object.assign(allExtraData, { redactedBody });
|
|
75
|
-
}
|
|
76
|
-
const extraLogData = typeof extraLoggingData === "function"
|
|
77
|
-
? await extraLoggingData(req)
|
|
78
|
-
: extraLoggingData;
|
|
79
|
-
Object.assign(allExtraData, extraLogData);
|
|
80
|
-
setLogInfoInReq(req, bodyProps, allExtraData);
|
|
81
|
-
};
|
|
82
|
-
asyncFunc()
|
|
83
|
-
.then(() => {
|
|
84
|
-
next();
|
|
85
|
-
})
|
|
86
|
-
.catch((error) => {
|
|
87
|
-
next(error);
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
exports.bodyLogger = bodyLogger;
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import express, { Request } from "express";
|
|
2
|
-
import { OptionsJson, OptionsUrlencoded, OptionsText, Options as OptionsRaw } from "body-parser";
|
|
3
|
-
import winston from "winston";
|
|
4
|
-
import { LoggerOptions, ErrorLoggerOptions } from "./logger.js";
|
|
5
|
-
export type LogFunction = (...args: Array<string | number | Record<string, unknown>>) => void;
|
|
6
|
-
export interface MiddlewareOptions {
|
|
7
|
-
urlencoded?: boolean | OptionsUrlencoded;
|
|
8
|
-
text?: boolean | OptionsText;
|
|
9
|
-
raw?: boolean | OptionsRaw;
|
|
10
|
-
json?: boolean | OptionsJson;
|
|
11
|
-
}
|
|
12
|
-
export type ComplexRouteHandler = express.RequestHandler | express.Router | Array<express.RequestHandler>;
|
|
13
|
-
export interface ComplexRouteDefinition {
|
|
14
|
-
route: string;
|
|
15
|
-
handler: ComplexRouteHandler;
|
|
16
|
-
method?: "get" | "post" | "put" | "options";
|
|
17
|
-
}
|
|
18
|
-
export type RouteDefinition = ComplexRouteDefinition | express.RequestHandler;
|
|
19
|
-
export type RouterDefinition = express.Router | RouteDefinition;
|
|
20
|
-
export type RouterWithNullDef = RouterDefinition | null | undefined;
|
|
21
|
-
export interface ComplexStaticDefinition {
|
|
22
|
-
root: string;
|
|
23
|
-
options?: Record<string, unknown>;
|
|
24
|
-
route?: string;
|
|
25
|
-
}
|
|
26
|
-
export type StaticDefinition = ComplexStaticDefinition | string;
|
|
27
|
-
export type UserFromReqFunc = (req: Request) => string | Record<string, unknown> | undefined;
|
|
28
|
-
export interface LogOptions {
|
|
29
|
-
route?: LoggerOptions;
|
|
30
|
-
error?: ErrorLoggerOptions;
|
|
31
|
-
logger?: winston.Logger;
|
|
32
|
-
timestamp?: boolean;
|
|
33
|
-
extraStacktraceFilter?: Array<RegExp> | RegExp;
|
|
34
|
-
ignoredRoutes?: Array<string>;
|
|
35
|
-
/** @deprecated Use `authFromReq` instead */
|
|
36
|
-
userFromReq?: UserFromReqFunc;
|
|
37
|
-
authFromReq?: UserFromReqFunc;
|
|
38
|
-
}
|
|
39
|
-
export interface DefaultErrorHandlerConfig {
|
|
40
|
-
/** Set to true to allow server errors without a code to be sent as-is in the response */
|
|
41
|
-
noErrorFilter?: boolean;
|
|
42
|
-
}
|
|
43
|
-
export interface PipelineSettings {
|
|
44
|
-
topLevels?: Array<RouterWithNullDef>;
|
|
45
|
-
routes?: Array<RouterWithNullDef>;
|
|
46
|
-
statics?: Array<StaticDefinition>;
|
|
47
|
-
postStatics?: Array<RouterWithNullDef>;
|
|
48
|
-
errorHandlers?: Array<express.ErrorRequestHandler>;
|
|
49
|
-
options?: {
|
|
50
|
-
log?: LogOptions;
|
|
51
|
-
middleware?: MiddlewareOptions;
|
|
52
|
-
defaultErrorHandler?: boolean | DefaultErrorHandlerConfig;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export default class PipelineBuilder {
|
|
56
|
-
private _debugLog?;
|
|
57
|
-
constructor(debugLog?: LogFunction);
|
|
58
|
-
private static _addRouterToRouter;
|
|
59
|
-
private static _addMiddlewareToRouter;
|
|
60
|
-
/** Register error handlers
|
|
61
|
-
*
|
|
62
|
-
* @param router
|
|
63
|
-
* @param errorHandlers
|
|
64
|
-
*/
|
|
65
|
-
private static _setErrorHandlers;
|
|
66
|
-
/** Appropriate call to express.static() */
|
|
67
|
-
private static _buildStaticHandler;
|
|
68
|
-
/** See createPipeline() for more info */
|
|
69
|
-
createPipeline({ topLevels, routes, statics, postStatics, errorHandlers, options, }: PipelineSettings): express.Router;
|
|
70
|
-
protected internalLog(...args: Array<string | number | Record<string, string>>): void;
|
|
71
|
-
/** Add generic middlewares to a router
|
|
72
|
-
*
|
|
73
|
-
* @param router
|
|
74
|
-
* @param [middlewareOptions]
|
|
75
|
-
* @param [middlewareOptions.json]
|
|
76
|
-
* Process JSON body
|
|
77
|
-
*/
|
|
78
|
-
private _setGenericMiddlewares;
|
|
79
|
-
private _addRouteToRouter;
|
|
80
|
-
/** Register routes
|
|
81
|
-
*
|
|
82
|
-
* @param router
|
|
83
|
-
* @param routes
|
|
84
|
-
* See routes in createPipeline()
|
|
85
|
-
*/
|
|
86
|
-
private _setRoutes;
|
|
87
|
-
/** Use a static definition */
|
|
88
|
-
private _useComplexStatic;
|
|
89
|
-
/** Define static resources for the express server
|
|
90
|
-
*
|
|
91
|
-
* @param router
|
|
92
|
-
* @param statics
|
|
93
|
-
* See createPipeline()
|
|
94
|
-
*/
|
|
95
|
-
private _setStatics;
|
|
96
|
-
/** Register pre-route loggers
|
|
97
|
-
*
|
|
98
|
-
* @param router
|
|
99
|
-
* @param [logOptions]
|
|
100
|
-
* @param [logOptions.route]
|
|
101
|
-
* Log each route call
|
|
102
|
-
* @param [logOptions.logger]
|
|
103
|
-
*/
|
|
104
|
-
private _setRouteLogger;
|
|
105
|
-
/** Register post-route loggers
|
|
106
|
-
*
|
|
107
|
-
* @param {Router} router
|
|
108
|
-
* @param {object} [logOptions]
|
|
109
|
-
* @param {bool} [logOptions.error]
|
|
110
|
-
* Log each error
|
|
111
|
-
* @param {winstonLogger} [logOptions.logger]
|
|
112
|
-
*/
|
|
113
|
-
private _setErrorLogger;
|
|
114
|
-
private _setDefaultErrorHandler;
|
|
115
|
-
}
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
const express_1 = __importDefault(require("express"));
|
|
33
|
-
const winston_js_1 = require("../winston.js");
|
|
34
|
-
const logger_js_1 = require("./logger.js");
|
|
35
|
-
const unprocessableentityerror_js_1 = require("./unprocessableentityerror.js");
|
|
36
|
-
const getErrorData = (error) => {
|
|
37
|
-
const unprocessable = (0, unprocessableentityerror_js_1.get422Fields)(error);
|
|
38
|
-
if (unprocessable) {
|
|
39
|
-
return {
|
|
40
|
-
message: unprocessable.message,
|
|
41
|
-
extraFields: { fields: unprocessable.fields, errors: unprocessable.errors },
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
return error.expose ? { message: error.message } : {};
|
|
45
|
-
};
|
|
46
|
-
const defaultErrorHandler = (errorHandlerConfig) => (err, req, res, next) => {
|
|
47
|
-
const noErrorFilter = typeof errorHandlerConfig === "boolean"
|
|
48
|
-
? false
|
|
49
|
-
: Boolean(errorHandlerConfig.noErrorFilter);
|
|
50
|
-
if (!res.headersSent) {
|
|
51
|
-
if (err.statusCode) {
|
|
52
|
-
const errorData = getErrorData(err);
|
|
53
|
-
const accepted = req.accepts([
|
|
54
|
-
"text",
|
|
55
|
-
"html",
|
|
56
|
-
"json",
|
|
57
|
-
]);
|
|
58
|
-
if (accepted === "json") {
|
|
59
|
-
res.status(err.statusCode).send({
|
|
60
|
-
statusCode: err.statusCode,
|
|
61
|
-
message: errorData.message,
|
|
62
|
-
...errorData.extraFields,
|
|
63
|
-
});
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
if (errorData.message) {
|
|
67
|
-
res.status(err.statusCode).send(errorData.message);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
res.sendStatus(err.statusCode);
|
|
71
|
-
}
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (!noErrorFilter) {
|
|
75
|
-
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
76
|
-
res.status(500).send("An unexpected error occured");
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
next(err);
|
|
81
|
-
};
|
|
82
|
-
class PipelineBuilder {
|
|
83
|
-
constructor(debugLog) {
|
|
84
|
-
this._debugLog = debugLog;
|
|
85
|
-
}
|
|
86
|
-
static _addRouterToRouter(baseRouter, newRouter) {
|
|
87
|
-
baseRouter.use(newRouter);
|
|
88
|
-
}
|
|
89
|
-
static _addMiddlewareToRouter(router, middleware) {
|
|
90
|
-
router.use(middleware);
|
|
91
|
-
}
|
|
92
|
-
/** Register error handlers
|
|
93
|
-
*
|
|
94
|
-
* @param router
|
|
95
|
-
* @param errorHandlers
|
|
96
|
-
*/
|
|
97
|
-
static _setErrorHandlers(router, errorHandlers) {
|
|
98
|
-
(errorHandlers ?? []).forEach(errorHandler => {
|
|
99
|
-
router.use(errorHandler);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
/** Appropriate call to express.static() */
|
|
103
|
-
static _buildStaticHandler(staticDef) {
|
|
104
|
-
return typeof staticDef === "string"
|
|
105
|
-
? express_1.default.static(staticDef)
|
|
106
|
-
: express_1.default.static(staticDef.root, staticDef.options);
|
|
107
|
-
}
|
|
108
|
-
/** See createPipeline() for more info */
|
|
109
|
-
createPipeline({ topLevels, routes, statics, postStatics, errorHandlers, options, }) {
|
|
110
|
-
const { middleware, log, defaultErrorHandler: useDefaultErrorHandler } = options ?? {};
|
|
111
|
-
this.internalLog("createPipeline()");
|
|
112
|
-
if (log) {
|
|
113
|
-
const errorConfig = typeof log.error === "boolean"
|
|
114
|
-
? {}
|
|
115
|
-
: log.error ?? {};
|
|
116
|
-
(0, winston_js_1.setConfig)({
|
|
117
|
-
timestamp: log.timestamp ?? false,
|
|
118
|
-
collapseNodeModules: errorConfig.collapseNodeModules ?? true,
|
|
119
|
-
collapseStacktrace: errorConfig.collapseStacktrace ?? true,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
const router = express_1.default.Router();
|
|
123
|
-
this.internalLog("setGenericMiddlewares()");
|
|
124
|
-
this._setGenericMiddlewares(router, middleware);
|
|
125
|
-
this.internalLog("add top level routes/middleware");
|
|
126
|
-
this._setRoutes(router, topLevels);
|
|
127
|
-
this.internalLog("add logger (route call)");
|
|
128
|
-
this._setRouteLogger(router, log);
|
|
129
|
-
this.internalLog("add routes");
|
|
130
|
-
this._setRoutes(router, routes);
|
|
131
|
-
this.internalLog("add statics");
|
|
132
|
-
this._setStatics(router, statics);
|
|
133
|
-
this.internalLog("add post statics routes");
|
|
134
|
-
this._setRoutes(router, postStatics);
|
|
135
|
-
this.internalLog("add logger (errors)");
|
|
136
|
-
this._setErrorLogger(router, log);
|
|
137
|
-
this.internalLog("add error handlers");
|
|
138
|
-
PipelineBuilder._setErrorHandlers(router, errorHandlers);
|
|
139
|
-
this.internalLog("add default error handlers");
|
|
140
|
-
this._setDefaultErrorHandler(router, useDefaultErrorHandler);
|
|
141
|
-
return router;
|
|
142
|
-
}
|
|
143
|
-
internalLog(...args) {
|
|
144
|
-
if (this._debugLog) {
|
|
145
|
-
this._debugLog(...args);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
/** Add generic middlewares to a router
|
|
149
|
-
*
|
|
150
|
-
* @param router
|
|
151
|
-
* @param [middlewareOptions]
|
|
152
|
-
* @param [middlewareOptions.json]
|
|
153
|
-
* Process JSON body
|
|
154
|
-
*/
|
|
155
|
-
_setGenericMiddlewares(router, middlewareOptions) {
|
|
156
|
-
const { urlencoded, text, raw, json, } = middlewareOptions ?? {};
|
|
157
|
-
if (urlencoded) {
|
|
158
|
-
this.internalLog("express.urlencoded()");
|
|
159
|
-
const options = urlencoded === true
|
|
160
|
-
? { extended: true }
|
|
161
|
-
: urlencoded;
|
|
162
|
-
router.use(express_1.default.urlencoded(options));
|
|
163
|
-
}
|
|
164
|
-
if (text) {
|
|
165
|
-
this.internalLog("express.text()");
|
|
166
|
-
const options = text === true
|
|
167
|
-
? undefined
|
|
168
|
-
: text;
|
|
169
|
-
router.use(express_1.default.text(options));
|
|
170
|
-
}
|
|
171
|
-
if (raw) {
|
|
172
|
-
this.internalLog("express.raw()");
|
|
173
|
-
const options = raw === true
|
|
174
|
-
? undefined
|
|
175
|
-
: raw;
|
|
176
|
-
router.use(express_1.default.raw(options));
|
|
177
|
-
}
|
|
178
|
-
if (json) {
|
|
179
|
-
this.internalLog("express.json()");
|
|
180
|
-
const options = json === true
|
|
181
|
-
? undefined
|
|
182
|
-
: json;
|
|
183
|
-
router.use(express_1.default.json(options));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
_addRouteToRouter(router, route) {
|
|
187
|
-
const method = route.method ?? "get";
|
|
188
|
-
this.internalLog(`Adding route "${route.route}" [${method}]`);
|
|
189
|
-
// Simple route definition
|
|
190
|
-
if (Array.isArray(route.handler)) {
|
|
191
|
-
// Multiple handlers for same route
|
|
192
|
-
route.handler.forEach(handler => router[method](route.route, handler));
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
// Simple handler
|
|
196
|
-
router[method](route.route, route.handler);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/** Register routes
|
|
200
|
-
*
|
|
201
|
-
* @param router
|
|
202
|
-
* @param routes
|
|
203
|
-
* See routes in createPipeline()
|
|
204
|
-
*/
|
|
205
|
-
_setRoutes(router, routes) {
|
|
206
|
-
(routes ?? []).filter((e) => Boolean(e)).forEach(routeDef => {
|
|
207
|
-
const asRouter = routeDef;
|
|
208
|
-
const asRouteDef = routeDef;
|
|
209
|
-
const asRequestHandler = routeDef;
|
|
210
|
-
if (asRouter.name === "router") {
|
|
211
|
-
// Raw router
|
|
212
|
-
this.internalLog("Adding raw router");
|
|
213
|
-
PipelineBuilder._addRouterToRouter(router, asRouter);
|
|
214
|
-
}
|
|
215
|
-
else if (asRouteDef.route) {
|
|
216
|
-
// Specific routes
|
|
217
|
-
this._addRouteToRouter(router, asRouteDef);
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
// Middleware
|
|
221
|
-
this.internalLog("Adding middleware");
|
|
222
|
-
PipelineBuilder._addMiddlewareToRouter(router, asRequestHandler);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
/** Use a static definition */
|
|
227
|
-
_useComplexStatic(router, staticDef) {
|
|
228
|
-
const staticHandler = PipelineBuilder._buildStaticHandler(staticDef);
|
|
229
|
-
if (typeof staticDef === "string") {
|
|
230
|
-
this.internalLog("Adding statics");
|
|
231
|
-
router.use(staticHandler);
|
|
232
|
-
}
|
|
233
|
-
else if (staticDef.route) {
|
|
234
|
-
this.internalLog(`Adding statics for route "${staticDef.route}"`);
|
|
235
|
-
router.use(staticDef.route, staticHandler);
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
this.internalLog("Adding statics");
|
|
239
|
-
router.use(staticHandler);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/** Define static resources for the express server
|
|
243
|
-
*
|
|
244
|
-
* @param router
|
|
245
|
-
* @param statics
|
|
246
|
-
* See createPipeline()
|
|
247
|
-
*/
|
|
248
|
-
_setStatics(router, statics) {
|
|
249
|
-
(statics ?? []).forEach(staticDef => this._useComplexStatic(router, staticDef));
|
|
250
|
-
}
|
|
251
|
-
/** Register pre-route loggers
|
|
252
|
-
*
|
|
253
|
-
* @param router
|
|
254
|
-
* @param [logOptions]
|
|
255
|
-
* @param [logOptions.route]
|
|
256
|
-
* Log each route call
|
|
257
|
-
* @param [logOptions.logger]
|
|
258
|
-
*/
|
|
259
|
-
_setRouteLogger(router, logOptions) {
|
|
260
|
-
const { route } = logOptions ?? {};
|
|
261
|
-
if (route) {
|
|
262
|
-
this.internalLog("Adding route logger");
|
|
263
|
-
(0, logger_js_1.registerRouteLogger)(router, logOptions);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
/** Register post-route loggers
|
|
267
|
-
*
|
|
268
|
-
* @param {Router} router
|
|
269
|
-
* @param {object} [logOptions]
|
|
270
|
-
* @param {bool} [logOptions.error]
|
|
271
|
-
* Log each error
|
|
272
|
-
* @param {winstonLogger} [logOptions.logger]
|
|
273
|
-
*/
|
|
274
|
-
_setErrorLogger(router, logOptions) {
|
|
275
|
-
const { error } = logOptions ?? {};
|
|
276
|
-
if (error) {
|
|
277
|
-
this.internalLog("Adding error logger");
|
|
278
|
-
(0, logger_js_1.registerErrorLogger)(router, logOptions);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
_setDefaultErrorHandler(router, defaultErrorHandlerConfig) {
|
|
282
|
-
if (defaultErrorHandlerConfig) {
|
|
283
|
-
this.internalLog("Adding default error handler");
|
|
284
|
-
router.use(defaultErrorHandler(defaultErrorHandlerConfig));
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
exports.default = PipelineBuilder;
|