@biorate/nestjs-tools 1.24.6 → 1.24.7
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/CHANGELOG.md +11 -0
- package/dist/interfaces.js +9 -0
- package/dist/interfaces.js.map +1 -0
- package/dist/src/enums/user.js +2 -5
- package/dist/src/enums/user.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/index.d.ts +3 -3
- package/{src/enums/user.ts → interfaces.ts} +0 -0
- package/package.json +2 -2
- package/dist/src/middlewares/proxy.js +0 -70
- package/dist/src/middlewares/proxy.js.map +0 -1
- package/dist/src/middlewares/request-count.js +0 -45
- package/dist/src/middlewares/request-count.js.map +0 -1
- package/dist/src/middlewares/response-time.js +0 -61
- package/dist/src/middlewares/response-time.js.map +0 -1
- package/src/decorators/auth.ts +0 -9
- package/src/decorators/index.ts +0 -2
- package/src/decorators/roles.ts +0 -13
- package/src/enums/index.ts +0 -1
- package/src/filters/all-exception-filter.ts +0 -47
- package/src/filters/index.ts +0 -1
- package/src/index.ts +0 -6
- package/src/interceptors/index.ts +0 -1
- package/src/interceptors/routes-interceptor.ts +0 -30
- package/src/middlewares/index.ts +0 -3
- package/src/middlewares/proxy-middleware.ts +0 -69
- package/src/middlewares/request-count-middleware.ts +0 -30
- package/src/middlewares/response-time-middleware.ts +0 -54
- package/src/providers/auth.ts +0 -36
- package/src/providers/index.ts +0 -2
- package/src/providers/roles.ts +0 -34
package/index.d.ts
CHANGED
|
@@ -10,7 +10,9 @@ import {
|
|
|
10
10
|
import { Request, Response, NextFunction } from 'express';
|
|
11
11
|
import { Observable } from 'rxjs';
|
|
12
12
|
import { Options, RequestHandler } from 'http-proxy-middleware/dist/types';
|
|
13
|
-
import { UserRoles } from './
|
|
13
|
+
import { UserRoles } from './interfaces';
|
|
14
|
+
|
|
15
|
+
export * from './interfaces';
|
|
14
16
|
|
|
15
17
|
declare module '@biorate/nestjs-tools' {
|
|
16
18
|
export function AuthBasic(): (
|
|
@@ -28,8 +30,6 @@ declare module '@biorate/nestjs-tools' {
|
|
|
28
30
|
KEY: symbol;
|
|
29
31
|
};
|
|
30
32
|
|
|
31
|
-
export * from './src/enums';
|
|
32
|
-
|
|
33
33
|
export class AllExceptionsFilter implements ExceptionFilter {
|
|
34
34
|
public catch(exception: unknown, host: ArgumentsHost): void;
|
|
35
35
|
}
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/nestjs-tools",
|
|
3
|
-
"version": "1.24.
|
|
3
|
+
"version": "1.24.7",
|
|
4
4
|
"description": "NestJS tools",
|
|
5
5
|
"main": "dist",
|
|
6
6
|
"scripts": {
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/express": "^4.17.16"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "368a743233088401c96ca5a1b15e3dc1ba8cd6a7"
|
|
43
43
|
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ProxyPrometheusMiddleware = void 0;
|
|
13
|
-
const lodash_1 = require("lodash");
|
|
14
|
-
const http_proxy_middleware_1 = require("http-proxy-middleware");
|
|
15
|
-
const common_1 = require("@nestjs/common");
|
|
16
|
-
const prometheus_1 = require("@biorate/prometheus");
|
|
17
|
-
const tools_1 = require("@biorate/tools");
|
|
18
|
-
let ProxyPrometheusMiddleware = class ProxyPrometheusMiddleware {
|
|
19
|
-
constructor(options) {
|
|
20
|
-
this.time = new WeakMap();
|
|
21
|
-
const { onProxyReq, onProxyRes } = (0, lodash_1.pick)(options, 'onProxyReq', 'onProxyRes');
|
|
22
|
-
this.handler = (0, http_proxy_middleware_1.createProxyMiddleware)(Object.assign(Object.assign({}, (0, lodash_1.omit)(options, 'onProxyReq', 'onProxyRes')), { onProxyReq: (proxyReq, req, res, options) => {
|
|
23
|
-
this.time.set(req, { time: tools_1.time.diff() });
|
|
24
|
-
return onProxyReq === null || onProxyReq === void 0 ? void 0 : onProxyReq(proxyReq, req, res, options);
|
|
25
|
-
}, onProxyRes: (proxyRes, req, res) => {
|
|
26
|
-
const diff = this.time.get(req);
|
|
27
|
-
this.counter
|
|
28
|
-
.labels({
|
|
29
|
-
method: req.method,
|
|
30
|
-
url: req.baseUrl,
|
|
31
|
-
status: res.statusCode,
|
|
32
|
-
})
|
|
33
|
-
.inc();
|
|
34
|
-
this.histogram
|
|
35
|
-
.labels({
|
|
36
|
-
method: req.method,
|
|
37
|
-
url: req.baseUrl,
|
|
38
|
-
status: res.statusCode,
|
|
39
|
-
})
|
|
40
|
-
.observe(diff.time());
|
|
41
|
-
return onProxyRes === null || onProxyRes === void 0 ? void 0 : onProxyRes(proxyRes, req, res);
|
|
42
|
-
} }));
|
|
43
|
-
}
|
|
44
|
-
static create(options) {
|
|
45
|
-
return new this(options).handler;
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
__decorate([
|
|
49
|
-
(0, prometheus_1.counter)({
|
|
50
|
-
name: 'http_proxy_server_requests_seconds_count',
|
|
51
|
-
help: 'Http proxy server requests count',
|
|
52
|
-
labelNames: ['method', 'url', 'status'],
|
|
53
|
-
}),
|
|
54
|
-
__metadata("design:type", Object)
|
|
55
|
-
], ProxyPrometheusMiddleware.prototype, "counter", void 0);
|
|
56
|
-
__decorate([
|
|
57
|
-
(0, prometheus_1.histogram)({
|
|
58
|
-
name: 'http_proxy_server_requests_seconds',
|
|
59
|
-
help: 'Http proxy server requests seconds bucket',
|
|
60
|
-
labelNames: ['method', 'url', 'status'],
|
|
61
|
-
buckets: [5, 10, 20, 50, 100, 300, 500, 1000, 2000, 3000, 5000, 10000],
|
|
62
|
-
}),
|
|
63
|
-
__metadata("design:type", Object)
|
|
64
|
-
], ProxyPrometheusMiddleware.prototype, "histogram", void 0);
|
|
65
|
-
ProxyPrometheusMiddleware = __decorate([
|
|
66
|
-
(0, common_1.Injectable)(),
|
|
67
|
-
__metadata("design:paramtypes", [Object])
|
|
68
|
-
], ProxyPrometheusMiddleware);
|
|
69
|
-
exports.ProxyPrometheusMiddleware = ProxyPrometheusMiddleware;
|
|
70
|
-
//# sourceMappingURL=proxy.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/middlewares/proxy.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,mCAAoC;AAEpC,iEAA8D;AAE9D,2CAA4C;AAC5C,oDAA6E;AAC7E,0CAAsC;AAGtC,IAAa,yBAAyB,GAAtC,MAAa,yBAAyB;IAwBpC,YAAoB,OAAiB;QAN7B,SAAI,GAAG,IAAI,OAAO,EAAmC,CAAC;QAO5D,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,IAAA,aAAI,EAAC,OAAO,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QAC7E,IAAI,CAAC,OAAO,GAAG,IAAA,6CAAqB,kCAC/B,IAAA,aAAI,EAAC,OAAO,EAAE,YAAY,EAAE,YAAY,CAAC,KAC5C,UAAU,EAAE,CACV,QAA4B,EAC5B,GAAY,EACZ,GAAa,EACb,OAAgC,EAChC,EAAE;gBACF,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,YAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBAC1C,OAAO,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAG,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;YACnD,CAAC,EACD,UAAU,EAAE,CAAC,QAA8B,EAAE,GAAY,EAAE,GAAa,EAAE,EAAE;gBAC1E,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAChC,IAAI,CAAC,OAAO;qBACT,MAAM,CAAC;oBACN,MAAM,EAAE,GAAG,CAAC,MAAM;oBAClB,GAAG,EAAE,GAAG,CAAC,OAAO;oBAChB,MAAM,EAAE,GAAG,CAAC,UAAU;iBACvB,CAAC;qBACD,GAAG,EAAE,CAAC;gBACT,IAAI,CAAC,SAAS;qBACX,MAAM,CAAC;oBACN,MAAM,EAAE,GAAG,CAAC,MAAM;oBAClB,GAAG,EAAE,GAAG,CAAC,OAAO;oBAChB,MAAM,EAAE,GAAG,CAAC,UAAU;iBACvB,CAAC;qBACD,OAAO,CAAC,IAAK,CAAC,IAAI,EAAE,CAAC,CAAC;gBACzB,OAAO,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAG,QAAQ,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;YAC1C,CAAC,IACD,CAAC;IACL,CAAC;IApCM,MAAM,CAAC,MAAM,CAAC,OAAiB;QACpC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;IACnC,CAAC;CAmCF,CAAA;AAnDC;IALC,IAAA,oBAAO,EAAC;QACP,IAAI,EAAE,0CAA0C;QAChD,IAAI,EAAE,kCAAkC;QACxC,UAAU,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC;KACxC,CAAC;;0DACuB;AAQzB;IANC,IAAA,sBAAS,EAAC;QACT,IAAI,EAAE,oCAAoC;QAC1C,IAAI,EAAE,2CAA2C;QACjD,UAAU,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC;QACvC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC;KACvE,CAAC;;4DAC2B;AAdlB,yBAAyB;IADrC,IAAA,mBAAU,GAAE;;GACA,yBAAyB,CAyDrC;AAzDY,8DAAyB"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RequestCountMiddleware = void 0;
|
|
13
|
-
const common_1 = require("@nestjs/common");
|
|
14
|
-
const prometheus_1 = require("@biorate/prometheus");
|
|
15
|
-
const on_headers_1 = require("on-headers");
|
|
16
|
-
const interceptors_1 = require("../interceptors");
|
|
17
|
-
let RequestCountMiddleware = class RequestCountMiddleware {
|
|
18
|
-
use(req, res, next) {
|
|
19
|
-
(0, on_headers_1.default)(res, () => {
|
|
20
|
-
var _a;
|
|
21
|
-
const route = interceptors_1.RoutesInterceptor.map.get(req);
|
|
22
|
-
this.counter
|
|
23
|
-
.labels({
|
|
24
|
-
method: req.method,
|
|
25
|
-
url: (_a = route === null || route === void 0 ? void 0 : route.path) !== null && _a !== void 0 ? _a : (req.baseUrl || req.originalUrl),
|
|
26
|
-
status: res.statusCode,
|
|
27
|
-
})
|
|
28
|
-
.inc();
|
|
29
|
-
});
|
|
30
|
-
next();
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
__decorate([
|
|
34
|
-
(0, prometheus_1.counter)({
|
|
35
|
-
name: 'http_server_requests_seconds_count',
|
|
36
|
-
help: 'Http server requests count',
|
|
37
|
-
labelNames: ['method', 'url', 'status'],
|
|
38
|
-
}),
|
|
39
|
-
__metadata("design:type", Object)
|
|
40
|
-
], RequestCountMiddleware.prototype, "counter", void 0);
|
|
41
|
-
RequestCountMiddleware = __decorate([
|
|
42
|
-
(0, common_1.Injectable)()
|
|
43
|
-
], RequestCountMiddleware);
|
|
44
|
-
exports.RequestCountMiddleware = RequestCountMiddleware;
|
|
45
|
-
//# sourceMappingURL=request-count.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request-count.js","sourceRoot":"","sources":["../../../src/middlewares/request-count.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4D;AAC5D,oDAAuD;AAEvD,2CAAmC;AACnC,kDAAoD;AAGpD,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;IAQ1B,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACxD,IAAA,oBAAS,EAAC,GAAG,EAAE,GAAG,EAAE;;YAClB,MAAM,KAAK,GAAG,gCAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC7C,IAAI,CAAC,OAAO;iBACT,MAAM,CAAC;gBACN,MAAM,EAAE,GAAG,CAAC,MAAM;gBAClB,GAAG,EAAE,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,IAAI,mCAAI,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,WAAW,CAAC;gBACpD,MAAM,EAAE,GAAG,CAAC,UAAU;aACvB,CAAC;iBACD,GAAG,EAAE,CAAC;QACX,CAAC,CAAC,CAAC;QACH,IAAI,EAAE,CAAC;IACT,CAAC;CACF,CAAA;AAfC;IALC,IAAA,oBAAO,EAAC;QACP,IAAI,EAAE,oCAAoC;QAC1C,IAAI,EAAE,4BAA4B;QAClC,UAAU,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC;KACxC,CAAC;;uDACuB;AANd,sBAAsB;IADlC,IAAA,mBAAU,GAAE;GACA,sBAAsB,CAqBlC;AArBY,wDAAsB"}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ResponseTimeMiddleware = void 0;
|
|
13
|
-
const common_1 = require("@nestjs/common");
|
|
14
|
-
const inversion_1 = require("@biorate/inversion");
|
|
15
|
-
const prometheus_1 = require("@biorate/prometheus");
|
|
16
|
-
const on_headers_1 = require("on-headers");
|
|
17
|
-
const interceptors_1 = require("../interceptors");
|
|
18
|
-
const tools_1 = require("@biorate/tools");
|
|
19
|
-
let ResponseTimeMiddleware = class ResponseTimeMiddleware {
|
|
20
|
-
constructor() {
|
|
21
|
-
this.suffix = this.config.get('app.middleware.ResponseTime.suffix', true);
|
|
22
|
-
this.header = this.config.get('app.middleware.ResponseTime.header', 'x-response-time');
|
|
23
|
-
this.digits = this.config.get('app.middleware.ResponseTime.digits', 3);
|
|
24
|
-
}
|
|
25
|
-
use(req, res, next) {
|
|
26
|
-
const diff = tools_1.time.diff();
|
|
27
|
-
(0, on_headers_1.default)(res, () => {
|
|
28
|
-
var _a;
|
|
29
|
-
const time = diff();
|
|
30
|
-
const route = interceptors_1.RoutesInterceptor.map.get(req);
|
|
31
|
-
this.histogram
|
|
32
|
-
.labels({
|
|
33
|
-
method: req.method,
|
|
34
|
-
url: (_a = route === null || route === void 0 ? void 0 : route.path) !== null && _a !== void 0 ? _a : (req.baseUrl || req.originalUrl),
|
|
35
|
-
status: res.statusCode,
|
|
36
|
-
})
|
|
37
|
-
.observe(time);
|
|
38
|
-
res.setHeader(this.header, time.toFixed(this.digits) + (this.suffix ? 'ms' : ''));
|
|
39
|
-
});
|
|
40
|
-
next();
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
__decorate([
|
|
44
|
-
(0, inversion_1.inject)(inversion_1.Types.Config),
|
|
45
|
-
__metadata("design:type", Object)
|
|
46
|
-
], ResponseTimeMiddleware.prototype, "config", void 0);
|
|
47
|
-
__decorate([
|
|
48
|
-
(0, prometheus_1.histogram)({
|
|
49
|
-
name: 'http_server_requests_seconds',
|
|
50
|
-
help: 'Http server requests seconds bucket',
|
|
51
|
-
labelNames: ['method', 'url', 'status'],
|
|
52
|
-
buckets: [5, 10, 20, 50, 100, 300, 500, 1000, 2000, 3000, 5000, 10000],
|
|
53
|
-
}),
|
|
54
|
-
__metadata("design:type", Object)
|
|
55
|
-
], ResponseTimeMiddleware.prototype, "histogram", void 0);
|
|
56
|
-
ResponseTimeMiddleware = __decorate([
|
|
57
|
-
(0, common_1.Injectable)(),
|
|
58
|
-
__metadata("design:paramtypes", [])
|
|
59
|
-
], ResponseTimeMiddleware);
|
|
60
|
-
exports.ResponseTimeMiddleware = ResponseTimeMiddleware;
|
|
61
|
-
//# sourceMappingURL=response-time.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"response-time.js","sourceRoot":"","sources":["../../../src/middlewares/response-time.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA4D;AAC5D,kDAAmD;AAEnD,oDAA2D;AAE3D,2CAAmC;AACnC,kDAAoD;AACpD,0CAAsC;AAGtC,IAAa,sBAAsB,GAAnC,MAAa,sBAAsB;IAiBjC;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAU,oCAAoC,EAAE,IAAI,CAAC,CAAC;QACnF,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAC3B,oCAAoC,EACpC,iBAAiB,CAClB,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAS,oCAAoC,EAAE,CAAC,CAAC,CAAC;IACjF,CAAC;IAEM,GAAG,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB;QACxD,MAAM,IAAI,GAAG,YAAI,CAAC,IAAI,EAAE,CAAC;QACzB,IAAA,oBAAS,EAAC,GAAG,EAAE,GAAG,EAAE;;YAClB,MAAM,IAAI,GAAG,IAAI,EAAE,CAAC;YACpB,MAAM,KAAK,GAAG,gCAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC7C,IAAI,CAAC,SAAS;iBACX,MAAM,CAAC;gBACN,MAAM,EAAE,GAAG,CAAC,MAAM;gBAClB,GAAG,EAAE,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,IAAI,mCAAI,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,WAAW,CAAC;gBACpD,MAAM,EAAE,GAAG,CAAC,UAAU;aACvB,CAAC;iBACD,OAAO,CAAC,IAAI,CAAC,CAAC;YACjB,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpF,CAAC,CAAC,CAAC;QACH,IAAI,EAAE,CAAC;IACT,CAAC;CACF,CAAA;AAzCuB;IAArB,IAAA,kBAAM,EAAC,iBAAK,CAAC,MAAM,CAAC;;sDAAyB;AAc9C;IANC,IAAA,sBAAS,EAAC;QACT,IAAI,EAAE,8BAA8B;QACpC,IAAI,EAAE,qCAAqC;QAC3C,UAAU,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC;QACvC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC;KACvE,CAAC;;yDAC2B;AAflB,sBAAsB;IADlC,IAAA,mBAAU,GAAE;;GACA,sBAAsB,CA0ClC;AA1CY,wDAAsB"}
|
package/src/decorators/auth.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { UseGuards } from '@nestjs/common';
|
|
2
|
-
import { ApiBasicAuth } from '@nestjs/swagger';
|
|
3
|
-
import { AuthGuardProvider } from '../providers';
|
|
4
|
-
|
|
5
|
-
export const AuthBasic =
|
|
6
|
-
() => (target: any, propertyKey?: any, descriptor?: TypedPropertyDescriptor<any>) => {
|
|
7
|
-
ApiBasicAuth()(target, propertyKey!, descriptor!);
|
|
8
|
-
UseGuards(AuthGuardProvider)(target, propertyKey!, descriptor!);
|
|
9
|
-
};
|
package/src/decorators/index.ts
DELETED
package/src/decorators/roles.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { SetMetadata } from '@nestjs/common';
|
|
2
|
-
import { UserRoles } from '../enums';
|
|
3
|
-
|
|
4
|
-
export const ROLES_KEY = Symbol('RolesKey');
|
|
5
|
-
|
|
6
|
-
export const Roles = (...roles: UserRoles[]) =>
|
|
7
|
-
SetMetadata(
|
|
8
|
-
ROLES_KEY,
|
|
9
|
-
roles.reduce((memo, item) => {
|
|
10
|
-
memo |= item;
|
|
11
|
-
return memo;
|
|
12
|
-
}, 0),
|
|
13
|
-
);
|
package/src/enums/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './user';
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { BaseError } from '@biorate/errors';
|
|
2
|
-
import {
|
|
3
|
-
ExceptionFilter,
|
|
4
|
-
Catch,
|
|
5
|
-
ArgumentsHost,
|
|
6
|
-
HttpException,
|
|
7
|
-
HttpStatus,
|
|
8
|
-
BadRequestException,
|
|
9
|
-
} from '@nestjs/common';
|
|
10
|
-
|
|
11
|
-
@Catch()
|
|
12
|
-
export class AllExceptionsFilter implements ExceptionFilter {
|
|
13
|
-
public catch(exception: unknown, host: ArgumentsHost) {
|
|
14
|
-
const ctx = host.switchToHttp();
|
|
15
|
-
const response = ctx.getResponse();
|
|
16
|
-
const request = ctx.getRequest();
|
|
17
|
-
const status =
|
|
18
|
-
exception instanceof HttpException
|
|
19
|
-
? exception.getStatus()
|
|
20
|
-
: (exception as BaseError)?.meta?.status ?? HttpStatus.INTERNAL_SERVER_ERROR;
|
|
21
|
-
this.log(exception);
|
|
22
|
-
response.status(status).json({
|
|
23
|
-
status,
|
|
24
|
-
code: this.code(exception),
|
|
25
|
-
hint: this.hint(exception),
|
|
26
|
-
path: request.url,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private code(exception: unknown) {
|
|
31
|
-
return (<BaseError>exception).code ?? (<Error>exception).constructor.name;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private hint(exception: unknown) {
|
|
35
|
-
return (
|
|
36
|
-
(
|
|
37
|
-
(<BadRequestException>exception)?.getResponse?.() as unknown as {
|
|
38
|
-
message?: [string];
|
|
39
|
-
}
|
|
40
|
-
)?.message ?? (<BaseError>exception).message
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private log(exception: unknown) {
|
|
45
|
-
console.error(exception);
|
|
46
|
-
}
|
|
47
|
-
}
|
package/src/filters/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './all-exception-filter';
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './routes-interceptor';
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { join, normalize } from 'path/posix';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
import { PATH_METADATA } from '@nestjs/common/constants';
|
|
4
|
-
import { Request } from 'express';
|
|
5
|
-
import {
|
|
6
|
-
Injectable,
|
|
7
|
-
ExecutionContext,
|
|
8
|
-
CallHandler,
|
|
9
|
-
NestInterceptor,
|
|
10
|
-
} from '@nestjs/common';
|
|
11
|
-
|
|
12
|
-
@Injectable()
|
|
13
|
-
export class RoutesInterceptor implements NestInterceptor {
|
|
14
|
-
public static readonly map = new WeakMap<Request, { path: string }>();
|
|
15
|
-
|
|
16
|
-
private readonly reflector = new Reflector();
|
|
17
|
-
|
|
18
|
-
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
19
|
-
RoutesInterceptor.map.set(context.switchToHttp().getRequest(), {
|
|
20
|
-
path: normalize(
|
|
21
|
-
join(
|
|
22
|
-
'/',
|
|
23
|
-
this.reflector.get<string>(PATH_METADATA, context.getClass()),
|
|
24
|
-
this.reflector.get<string>(PATH_METADATA, context.getHandler()),
|
|
25
|
-
),
|
|
26
|
-
),
|
|
27
|
-
});
|
|
28
|
-
return next.handle();
|
|
29
|
-
}
|
|
30
|
-
}
|
package/src/middlewares/index.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import * as httpProxy from 'http-proxy';
|
|
3
|
-
import { pick, omit } from 'lodash';
|
|
4
|
-
import { Request, Response } from 'express';
|
|
5
|
-
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
6
|
-
import { Options, RequestHandler } from 'http-proxy-middleware/dist/types';
|
|
7
|
-
import { Injectable } from '@nestjs/common';
|
|
8
|
-
import { counter, histogram, Counter, Histogram } from '@biorate/prometheus';
|
|
9
|
-
import { time } from '@biorate/tools';
|
|
10
|
-
|
|
11
|
-
@Injectable()
|
|
12
|
-
export class ProxyPrometheusMiddleware {
|
|
13
|
-
@counter({
|
|
14
|
-
name: 'http_proxy_server_requests_seconds_count',
|
|
15
|
-
help: 'Http proxy server requests count',
|
|
16
|
-
labelNames: ['method', 'url', 'status'],
|
|
17
|
-
})
|
|
18
|
-
private counter: Counter;
|
|
19
|
-
|
|
20
|
-
@histogram({
|
|
21
|
-
name: 'http_proxy_server_requests_seconds',
|
|
22
|
-
help: 'Http proxy server requests seconds bucket',
|
|
23
|
-
labelNames: ['method', 'url', 'status'],
|
|
24
|
-
buckets: [5, 10, 20, 50, 100, 300, 500, 1000, 2000, 3000, 5000, 10000],
|
|
25
|
-
})
|
|
26
|
-
private histogram: Histogram;
|
|
27
|
-
|
|
28
|
-
private handler: RequestHandler;
|
|
29
|
-
|
|
30
|
-
private time = new WeakMap<Request, { time: () => number }>();
|
|
31
|
-
|
|
32
|
-
public static create(options?: Options) {
|
|
33
|
-
return new this(options).handler;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private constructor(options?: Options) {
|
|
37
|
-
const { onProxyReq, onProxyRes } = pick(options, 'onProxyReq', 'onProxyRes');
|
|
38
|
-
this.handler = createProxyMiddleware({
|
|
39
|
-
...omit(options, 'onProxyReq', 'onProxyRes'),
|
|
40
|
-
onProxyReq: (
|
|
41
|
-
proxyReq: http.ClientRequest,
|
|
42
|
-
req: Request,
|
|
43
|
-
res: Response,
|
|
44
|
-
options: httpProxy.ServerOptions,
|
|
45
|
-
) => {
|
|
46
|
-
this.time.set(req, { time: time.diff() });
|
|
47
|
-
return onProxyReq?.(proxyReq, req, res, options);
|
|
48
|
-
},
|
|
49
|
-
onProxyRes: (proxyRes: http.IncomingMessage, req: Request, res: Response) => {
|
|
50
|
-
const diff = this.time.get(req);
|
|
51
|
-
this.counter
|
|
52
|
-
.labels({
|
|
53
|
-
method: req.method,
|
|
54
|
-
url: req.baseUrl,
|
|
55
|
-
status: res.statusCode,
|
|
56
|
-
})
|
|
57
|
-
.inc();
|
|
58
|
-
this.histogram
|
|
59
|
-
.labels({
|
|
60
|
-
method: req.method,
|
|
61
|
-
url: req.baseUrl,
|
|
62
|
-
status: res.statusCode,
|
|
63
|
-
})
|
|
64
|
-
.observe(diff!.time());
|
|
65
|
-
return onProxyRes?.(proxyRes, req, res);
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import * as onHeaders from 'on-headers';
|
|
3
|
-
import { Injectable, NestMiddleware } from '@nestjs/common';
|
|
4
|
-
import { counter, Counter } from '@biorate/prometheus';
|
|
5
|
-
import { Request, Response, NextFunction } from 'express';
|
|
6
|
-
import { RoutesInterceptor } from '../interceptors';
|
|
7
|
-
|
|
8
|
-
@Injectable()
|
|
9
|
-
export class RequestCountMiddleware implements NestMiddleware {
|
|
10
|
-
@counter({
|
|
11
|
-
name: 'http_server_requests_seconds_count',
|
|
12
|
-
help: 'Http server requests count',
|
|
13
|
-
labelNames: ['method', 'url', 'status'],
|
|
14
|
-
})
|
|
15
|
-
private counter: Counter;
|
|
16
|
-
|
|
17
|
-
public use(req: Request, res: Response, next: NextFunction) {
|
|
18
|
-
onHeaders(res, () => {
|
|
19
|
-
const route = RoutesInterceptor.map.get(req);
|
|
20
|
-
this.counter
|
|
21
|
-
.labels({
|
|
22
|
-
method: req.method,
|
|
23
|
-
url: route?.path ?? (req.baseUrl || req.originalUrl),
|
|
24
|
-
status: res.statusCode,
|
|
25
|
-
})
|
|
26
|
-
.inc();
|
|
27
|
-
});
|
|
28
|
-
next();
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
import * as onHeaders from 'on-headers';
|
|
3
|
-
import { Injectable, NestMiddleware } from '@nestjs/common';
|
|
4
|
-
import { inject, Types } from '@biorate/inversion';
|
|
5
|
-
import { IConfig } from '@biorate/config';
|
|
6
|
-
import { histogram, Histogram } from '@biorate/prometheus';
|
|
7
|
-
import { Request, Response, NextFunction } from 'express';
|
|
8
|
-
import { RoutesInterceptor } from '../interceptors';
|
|
9
|
-
import { time } from '@biorate/tools';
|
|
10
|
-
|
|
11
|
-
@Injectable()
|
|
12
|
-
export class ResponseTimeMiddleware implements NestMiddleware {
|
|
13
|
-
@inject(Types.Config) private config: IConfig;
|
|
14
|
-
|
|
15
|
-
private readonly suffix: boolean;
|
|
16
|
-
|
|
17
|
-
private readonly header: string;
|
|
18
|
-
|
|
19
|
-
private readonly digits: number;
|
|
20
|
-
|
|
21
|
-
@histogram({
|
|
22
|
-
name: 'http_server_requests_seconds',
|
|
23
|
-
help: 'Http server requests seconds bucket',
|
|
24
|
-
labelNames: ['method', 'url', 'status'],
|
|
25
|
-
buckets: [5, 10, 20, 50, 100, 300, 500, 1000, 2000, 3000, 5000, 10000],
|
|
26
|
-
})
|
|
27
|
-
private histogram: Histogram;
|
|
28
|
-
|
|
29
|
-
public constructor() {
|
|
30
|
-
this.suffix = this.config.get<boolean>('app.middleware.ResponseTime.suffix', true);
|
|
31
|
-
this.header = this.config.get<string>(
|
|
32
|
-
'app.middleware.ResponseTime.header',
|
|
33
|
-
'x-response-time',
|
|
34
|
-
);
|
|
35
|
-
this.digits = this.config.get<number>('app.middleware.ResponseTime.digits', 3);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public use(req: Request, res: Response, next: NextFunction) {
|
|
39
|
-
const diff = time.diff();
|
|
40
|
-
onHeaders(res, () => {
|
|
41
|
-
const time = diff();
|
|
42
|
-
const route = RoutesInterceptor.map.get(req);
|
|
43
|
-
this.histogram
|
|
44
|
-
.labels({
|
|
45
|
-
method: req.method,
|
|
46
|
-
url: route?.path ?? (req.baseUrl || req.originalUrl),
|
|
47
|
-
status: res.statusCode,
|
|
48
|
-
})
|
|
49
|
-
.observe(time);
|
|
50
|
-
res.setHeader(this.header, time.toFixed(this.digits) + (this.suffix ? 'ms' : ''));
|
|
51
|
-
});
|
|
52
|
-
next();
|
|
53
|
-
}
|
|
54
|
-
}
|
package/src/providers/auth.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { inject, Types } from '@biorate/inversion';
|
|
2
|
-
import { IConfig } from '@biorate/config';
|
|
3
|
-
import {
|
|
4
|
-
Injectable,
|
|
5
|
-
CanActivate,
|
|
6
|
-
ExecutionContext,
|
|
7
|
-
UnauthorizedException,
|
|
8
|
-
} from '@nestjs/common';
|
|
9
|
-
|
|
10
|
-
@Injectable()
|
|
11
|
-
export class AuthGuardProvider implements CanActivate {
|
|
12
|
-
@inject(Types.Config) private config: IConfig;
|
|
13
|
-
|
|
14
|
-
public canActivate(context: ExecutionContext): boolean {
|
|
15
|
-
const request = context.switchToHttp().getRequest();
|
|
16
|
-
const config = this.config.get<{ [key: string]: string } | null>(
|
|
17
|
-
'app.auth.basic',
|
|
18
|
-
null,
|
|
19
|
-
);
|
|
20
|
-
if (!config) return true;
|
|
21
|
-
let username: string, password: string;
|
|
22
|
-
try {
|
|
23
|
-
[username, password] = Buffer.from(
|
|
24
|
-
request.headers.authorization.replace('Basic ', ''),
|
|
25
|
-
'base64',
|
|
26
|
-
)
|
|
27
|
-
.toString('utf-8')
|
|
28
|
-
.split(':');
|
|
29
|
-
} catch {
|
|
30
|
-
throw new UnauthorizedException();
|
|
31
|
-
}
|
|
32
|
-
if (!username || !password) throw new UnauthorizedException();
|
|
33
|
-
if (config[username] === password) return true;
|
|
34
|
-
throw new UnauthorizedException();
|
|
35
|
-
}
|
|
36
|
-
}
|
package/src/providers/index.ts
DELETED
package/src/providers/roles.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|
2
|
-
import { Reflector } from '@nestjs/core';
|
|
3
|
-
import { ROLES_KEY } from '../decorators';
|
|
4
|
-
import { UserRoles } from '../enums';
|
|
5
|
-
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class RolesGuardProvider implements CanActivate {
|
|
8
|
-
constructor(private reflector: Reflector) {}
|
|
9
|
-
/**
|
|
10
|
-
* @description - This method for override
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const request = context.switchToHttp().getRequest();
|
|
14
|
-
* const { login, sessionId } = request.cookies;
|
|
15
|
-
* if (!login || !sessionId) throw new AuthorizationNotAuthorizedError();
|
|
16
|
-
* // get user from db, or somewhere else
|
|
17
|
-
* if (!user) throw new AuthorizationNotAuthorizedError();
|
|
18
|
-
* return user.role;
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
protected async getUser(context: ExecutionContext) {
|
|
22
|
-
return { role: UserRoles.Basic | UserRoles.Admin };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async canActivate(context: ExecutionContext) {
|
|
26
|
-
const roles = this.reflector.getAllAndOverride<number>(ROLES_KEY, [
|
|
27
|
-
context.getHandler(),
|
|
28
|
-
context.getClass(),
|
|
29
|
-
]);
|
|
30
|
-
if (roles === undefined) return true;
|
|
31
|
-
const user = await this.getUser(context);
|
|
32
|
-
return (user.role & roles) === roles;
|
|
33
|
-
}
|
|
34
|
-
}
|