@merkaly/nest 0.1.7 → 0.2.0
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 +43 -2
- package/dist/filters/abstract.filter.d.ts +12 -0
- package/dist/filters/abstract.filter.d.ts.map +1 -0
- package/dist/filters/abstract.filter.js +55 -0
- package/dist/filters/abstract.filter.js.map +1 -0
- package/dist/filters/exceptions.filter.d.ts +0 -3
- package/dist/filters/exceptions.filter.d.ts.map +1 -1
- package/dist/filters/exceptions.filter.js +7 -24
- package/dist/filters/exceptions.filter.js.map +1 -1
- package/dist/filters/index.d.ts +1 -0
- package/dist/filters/index.d.ts.map +1 -1
- package/dist/filters/index.js +1 -0
- package/dist/filters/index.js.map +1 -1
- package/dist/http/body-parsers.d.ts +9 -0
- package/dist/http/body-parsers.d.ts.map +1 -0
- package/dist/http/body-parsers.js +26 -0
- package/dist/http/body-parsers.js.map +1 -0
- package/dist/http/index.d.ts +3 -0
- package/dist/http/index.d.ts.map +1 -0
- package/dist/http/index.js +19 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request-logger.d.ts +11 -0
- package/dist/http/request-logger.d.ts.map +1 -0
- package/dist/http/request-logger.js +30 -0
- package/dist/http/request-logger.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/logger/index.d.ts +1 -0
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +1 -0
- package/dist/logger/index.js.map +1 -1
- package/dist/logger/logger.module.d.ts +6 -4
- package/dist/logger/logger.module.d.ts.map +1 -1
- package/dist/logger/logger.module.js +17 -2
- package/dist/logger/logger.module.js.map +1 -1
- package/dist/logger/logger.options.d.ts +13 -2
- package/dist/logger/logger.options.d.ts.map +1 -1
- package/dist/logger/logger.options.js.map +1 -1
- package/dist/logger/logger.service.d.ts +6 -3
- package/dist/logger/logger.service.d.ts.map +1 -1
- package/dist/logger/logger.service.js +27 -19
- package/dist/logger/logger.service.js.map +1 -1
- package/dist/logger/request-context.d.ts +14 -0
- package/dist/logger/request-context.d.ts.map +1 -0
- package/dist/logger/request-context.js +38 -0
- package/dist/logger/request-context.js.map +1 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -39,7 +39,8 @@ strict-peer-dependencies=false
|
|
|
39
39
|
import {
|
|
40
40
|
env, loadEnv, // config
|
|
41
41
|
LoggerModule, LoggerService, // logger
|
|
42
|
-
|
|
42
|
+
requestLogger, requestOrigin, requestContext, bodyParsers, bodyParserReason, // uma linha por request
|
|
43
|
+
AbstractExceptionFilter, ExceptionsFilter, rememberError, // filtros (motivo do erro para o log)
|
|
43
44
|
RabbitModule, EventsPublisher, rabbitConsumerOptions, OnRabbitEvent, ackMessage, nackMessage,
|
|
44
45
|
WebhookEvent, WebhookPatterns, WebhookPattern, AliexpressOrderStatus, createWebhookEvent, toPattern,
|
|
45
46
|
SignatureGuard, safeCompare, hmacDigest, verifyHmac,
|
|
@@ -51,7 +52,7 @@ import {
|
|
|
51
52
|
```ts
|
|
52
53
|
@Module({
|
|
53
54
|
imports: [
|
|
54
|
-
LoggerModule.forRoot(
|
|
55
|
+
LoggerModule.forRoot({ service: 'webhooks.merkaly.io' }), // global
|
|
55
56
|
RabbitModule.forRoot({
|
|
56
57
|
url: env.string('RABBITMQ_URL'), // sem url: publish rejeita com 503
|
|
57
58
|
queue: env.string('RABBITMQ_QUEUE', 'api.merkaly.io-queue'), // <repo>-queue
|
|
@@ -71,6 +72,46 @@ export class AppModule {}
|
|
|
71
72
|
|
|
72
73
|
**Health check**: o pacote não registra controllers. O Swagger servido em `/` responde 200 e funciona como health check.
|
|
73
74
|
|
|
75
|
+
### Logger e requests
|
|
76
|
+
|
|
77
|
+
Todas as linhas têm as mesmas colunas, com largura fixa (`-` quando falta o valor, `…` quando passa da largura), e a mensagem começa sempre na mesma posição:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
LEVEL ORIGIN SCOPE KIND MESSAGE
|
|
81
|
+
08:02:05 657 DEBUG api.merkaly.io - MongoService connected
|
|
82
|
+
08:02:08 410 LOG admin.merkaly.io org_6sflVH0jykwUzj8z OrderService order created
|
|
83
|
+
08:02:08 410 LOG admin.merkaly.io org_6sflVH0jykwUzj8z POST /admin/orders 201 15ms
|
|
84
|
+
08:02:08 423 WARN 203.0.113.7 - GET /wp-login.php 404 2ms "Cannot GET /wp-login.php"
|
|
85
|
+
08:02:08 439 ERROR admin.merkaly.io org_6sflVH0jykwUzj8z GET /admin/boom 500 0ms "boom"
|
|
86
|
+
Error: boom
|
|
87
|
+
at ...
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- **ORIGIN** (24): quem fez o request, nas linhas de request e nos logs internos emitidos durante ele: `x-merkaly-host`, senão o host do `Origin`, `cf-connecting-ip`, o primeiro `x-forwarded-for` ou o socket (`requestOrigin`). Fora de um request, o `service`.
|
|
91
|
+
- **SCOPE** (22): contexto de negócio definido pelo app (`requestLogger({ scope })` ou `LoggerModule({ scope })`, ex.: id da organização). Nas linhas de request é resolvido ao terminar a resposta, então pega o que os guards definiram.
|
|
92
|
+
- **KIND** (16): método HTTP nas linhas de request; nos logs internos, o `group` de quem loga.
|
|
93
|
+
- **MESSAGE**: `/path STATUS Nms "motivo"` (motivo cortado em 200, sem query, body nem headers) ou o texto. Nível: `ERROR` em 5xx com o stack embaixo, `WARN` em 4xx, `LOG` no resto.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
// main.ts
|
|
97
|
+
app.use(requestLogger(app)); // antes do Swagger; opcional { scope: (req) => 'stripe' }
|
|
98
|
+
bodyParsers(app, { raw: ['/webhook/stripe'] }); // json + urlencoded com motivo seguro ("Invalid JSON body")
|
|
99
|
+
SwaggerModule.setup('/', app, document);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- O ORIGIN do request fica num `AsyncLocalStorage` do pacote (`requestContext`). O `requestLogger` cria o contexto e o `LoggerModule` registra um middleware que volta a entrar nele depois dos body parsers (que podem perder o contexto assíncrono), igual ao `nestjs-cls`.
|
|
103
|
+
- `bodyParsers(app, { raw?, rawBody? })`: `raw` mantém o body como Buffer nesses paths; `rawBody: true` preenche `req.rawBody` (igual ao `NestFactory.create(..., { rawBody: true })`, que deixa de registrar os próprios parsers).
|
|
104
|
+
- SCOPE com CLS, sem o pacote depender de `nestjs-cls`:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
LoggerModule.forRootAsync({
|
|
108
|
+
inject: [ClsService],
|
|
109
|
+
useFactory: (cls: ClsService) => ({ service: 'api.merkaly.io', scope: () => cls.get('identity') }),
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Filtros**: o filtro não loga; guarda o motivo (e o stack de 5xx) em `response.locals` e o `requestLogger` imprime. `ExceptionsFilter` responde `{ statusCode, message, path, timestamp }`. Para filtros próprios, estender `AbstractExceptionFilter` (resposta padrão do Nest, sem o log duplicado do `ExceptionsHandler`) e usar `this.remember(response, motivo, trace?)` quando responder por conta própria.
|
|
114
|
+
|
|
74
115
|
### Eventos
|
|
75
116
|
|
|
76
117
|
Envelope `WebhookEvent<T>`: `{ id, source, type, receivedAt, organization?, data }`. Padrão do nome: `<source>.<evento>` em minúsculas (`toPattern('asaas', 'PAYMENT_CONFIRMED')` = `asaas.payment_confirmed`). Os padrões conhecidos ficam em `WebhookPatterns` e são a fonte única para produtores e consumidores, iguais ao que o webhooks publica:
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseExceptionFilter } from '@nestjs/core';
|
|
2
|
+
import type { ArgumentsHost, HttpServer } from '@nestjs/common';
|
|
3
|
+
import type { Response } from 'express';
|
|
4
|
+
export declare const exceptionStatus: (exception: unknown) => number;
|
|
5
|
+
export declare const exceptionReason: (exception: unknown) => string;
|
|
6
|
+
export declare function rememberError(response: Response, error: string, trace?: unknown): void;
|
|
7
|
+
export declare abstract class AbstractExceptionFilter<T = unknown> extends BaseExceptionFilter<T> {
|
|
8
|
+
protected remember(response: Response, error: string, trace?: unknown): void;
|
|
9
|
+
catch(exception: T, host: ArgumentsHost): void;
|
|
10
|
+
handleUnknownError(exception: T, host: ArgumentsHost, applicationRef: HttpServer): any;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=abstract.filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract.filter.d.ts","sourceRoot":"","sources":["../../src/filters/abstract.filter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAOxC,eAAO,MAAM,eAAe,GAAI,WAAW,OAAO,KAAG,MAKpD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,WAAW,OAAO,KAAG,MAWpD,CAAC;AAGF,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,QAI/E;AAGD,8BAAsB,uBAAuB,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,mBAAmB,CAAC,CAAC,CAAC;IACvF,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;IAI9D,KAAK,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,EAAE,aAAa;IAavC,kBAAkB,CAAC,SAAS,EAAE,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU;CAWxF"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractExceptionFilter = exports.exceptionReason = exports.exceptionStatus = void 0;
|
|
4
|
+
exports.rememberError = rememberError;
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const core_1 = require("@nestjs/core");
|
|
7
|
+
const isHttpError = (error) => !!error?.statusCode && !!error?.message;
|
|
8
|
+
const exceptionStatus = (exception) => {
|
|
9
|
+
if (exception instanceof common_1.HttpException)
|
|
10
|
+
return exception.getStatus();
|
|
11
|
+
if (isHttpError(exception))
|
|
12
|
+
return exception.statusCode;
|
|
13
|
+
return common_1.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
14
|
+
};
|
|
15
|
+
exports.exceptionStatus = exceptionStatus;
|
|
16
|
+
const exceptionReason = (exception) => {
|
|
17
|
+
const body = exception instanceof common_1.HttpException ? exception.getResponse() : undefined;
|
|
18
|
+
const message = typeof body === 'object' ? body?.message : body;
|
|
19
|
+
const text = Array.isArray(message)
|
|
20
|
+
? message.join('; ')
|
|
21
|
+
: typeof message === 'string'
|
|
22
|
+
? message
|
|
23
|
+
: '';
|
|
24
|
+
const reason = text || exception?.message || 'Unknown error';
|
|
25
|
+
return reason.replace(/\?\S*/g, '');
|
|
26
|
+
};
|
|
27
|
+
exports.exceptionReason = exceptionReason;
|
|
28
|
+
function rememberError(response, error, trace) {
|
|
29
|
+
const locals = response.locals;
|
|
30
|
+
Object.assign(locals, { error: locals.error ?? error, trace });
|
|
31
|
+
}
|
|
32
|
+
class AbstractExceptionFilter extends core_1.BaseExceptionFilter {
|
|
33
|
+
remember(response, error, trace) {
|
|
34
|
+
rememberError(response, error, trace);
|
|
35
|
+
}
|
|
36
|
+
catch(exception, host) {
|
|
37
|
+
if (host.getType() === 'http') {
|
|
38
|
+
const status = (0, exports.exceptionStatus)(exception);
|
|
39
|
+
const trace = status >= 500 && !(exception instanceof common_1.IntrinsicException) ? exception : undefined;
|
|
40
|
+
this.remember(host.switchToHttp().getResponse(), (0, exports.exceptionReason)(exception), trace);
|
|
41
|
+
}
|
|
42
|
+
return super.catch(exception, host);
|
|
43
|
+
}
|
|
44
|
+
handleUnknownError(exception, host, applicationRef) {
|
|
45
|
+
const body = isHttpError(exception)
|
|
46
|
+
? { statusCode: exception.statusCode, message: exception.message }
|
|
47
|
+
: { statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR, message: 'Internal server error' };
|
|
48
|
+
const response = host.getArgByIndex(1);
|
|
49
|
+
if (applicationRef.isHeadersSent(response))
|
|
50
|
+
return applicationRef.end(response);
|
|
51
|
+
return applicationRef.reply(response, body, body.statusCode);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.AbstractExceptionFilter = AbstractExceptionFilter;
|
|
55
|
+
//# sourceMappingURL=abstract.filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract.filter.js","sourceRoot":"","sources":["../../src/filters/abstract.filter.ts"],"names":[],"mappings":";;;AAgCA,sCAIC;AApCD,2CAA+E;AAC/E,uCAAmD;AAOnD,MAAM,WAAW,GAAG,CAAC,KAAc,EAAsB,EAAE,CACzD,CAAC,CAAE,KAAmB,EAAE,UAAU,IAAI,CAAC,CAAE,KAAmB,EAAE,OAAO,CAAC;AAEjE,MAAM,eAAe,GAAG,CAAC,SAAkB,EAAU,EAAE;IAC5D,IAAI,SAAS,YAAY,sBAAa;QAAE,OAAO,SAAS,CAAC,SAAS,EAAE,CAAC;IACrE,IAAI,WAAW,CAAC,SAAS,CAAC;QAAE,OAAO,SAAS,CAAC,UAAU,CAAC;IAExD,OAAO,mBAAU,CAAC,qBAAqB,CAAC;AAC1C,CAAC,CAAC;AALW,QAAA,eAAe,mBAK1B;AAEK,MAAM,eAAe,GAAG,CAAC,SAAkB,EAAU,EAAE;IAC5D,MAAM,IAAI,GAAG,SAAS,YAAY,sBAAa,CAAC,CAAC,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;IACtF,MAAM,OAAO,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAE,IAA8B,EAAE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AAe,EAAC,SAAS,CAAC,EAAE,KAAK,CAAC,CAAC;QAChG,CAAC;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;IAGM,kBAAkB,CAAC,SAAY,EAAE,IAAmB,EAAE,cAA0B;QACrF,MAAM,IAAI,GAAG,WAAW,CAAC,SAAS,CAAC;YACjC,CAAC,CAAC,EAAE,UAAU,EAAE,SAAS,CAAC,UAAU,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE;YAClE,CAAC,CAAC,EAAE,UAAU,EAAE,mBAAU,CAAC,qBAAqB,EAAE,OAAO,EAAE,uBAAuB,EAAE,CAAC;QAEvF,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QAEvC,IAAI,cAAc,CAAC,aAAa,CAAC,QAAQ,CAAC;YAAE,OAAO,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAEhF,OAAO,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAC/D,CAAC;CACF;AA7BD,0DA6BC"}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
-
import { LoggerService } from '../logger';
|
|
3
2
|
export declare class ExceptionsFilter implements ExceptionFilter {
|
|
4
|
-
private readonly logger;
|
|
5
|
-
constructor(logger?: LoggerService);
|
|
6
3
|
catch(exception: unknown, host: ArgumentsHost): void;
|
|
7
4
|
}
|
|
8
5
|
//# sourceMappingURL=exceptions.filter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exceptions.filter.d.ts","sourceRoot":"","sources":["../../src/filters/exceptions.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,eAAe,
|
|
1
|
+
{"version":3,"file":"exceptions.filter.d.ts","sourceRoot":"","sources":["../../src/filters/exceptions.filter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,eAAe,EAGhB,MAAM,gBAAgB,CAAC;AAKxB,qBACa,gBAAiB,YAAW,eAAe;IACtD,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa;CAuB9C"}
|
|
@@ -5,38 +5,23 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
-
};
|
|
14
|
-
var ExceptionsFilter_1;
|
|
15
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
9
|
exports.ExceptionsFilter = void 0;
|
|
17
10
|
const common_1 = require("@nestjs/common");
|
|
18
|
-
const
|
|
19
|
-
let ExceptionsFilter =
|
|
20
|
-
logger;
|
|
21
|
-
constructor(logger) {
|
|
22
|
-
this.logger = logger ?? new logger_1.LoggerService();
|
|
23
|
-
}
|
|
11
|
+
const abstract_filter_1 = require("./abstract.filter");
|
|
12
|
+
let ExceptionsFilter = class ExceptionsFilter {
|
|
24
13
|
catch(exception, host) {
|
|
25
14
|
if (host.getType() !== 'http')
|
|
26
15
|
throw exception;
|
|
27
16
|
const http = host.switchToHttp();
|
|
28
17
|
const request = http.getRequest();
|
|
29
18
|
const response = http.getResponse();
|
|
19
|
+
const statusCode = (0, abstract_filter_1.exceptionStatus)(exception);
|
|
30
20
|
const isHttp = exception instanceof common_1.HttpException;
|
|
31
|
-
const statusCode = isHttp ? exception.getStatus() : common_1.HttpStatus.INTERNAL_SERVER_ERROR;
|
|
32
21
|
const body = isHttp ? exception.getResponse() : 'Internal server error';
|
|
33
22
|
const message = typeof body === 'string' ? body : body.message;
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
if (statusCode >= 500)
|
|
37
|
-
this.logger.error({ ...log, message: summary, trace: exception });
|
|
38
|
-
else
|
|
39
|
-
this.logger.warn({ ...log, message: `${summary} ${JSON.stringify(message)}` });
|
|
23
|
+
const trace = statusCode >= 500 && !(exception instanceof common_1.IntrinsicException) ? exception : undefined;
|
|
24
|
+
(0, abstract_filter_1.rememberError)(response, (0, abstract_filter_1.exceptionReason)(exception), trace);
|
|
40
25
|
response.status(statusCode).json({
|
|
41
26
|
statusCode,
|
|
42
27
|
message,
|
|
@@ -46,9 +31,7 @@ let ExceptionsFilter = ExceptionsFilter_1 = class ExceptionsFilter {
|
|
|
46
31
|
}
|
|
47
32
|
};
|
|
48
33
|
exports.ExceptionsFilter = ExceptionsFilter;
|
|
49
|
-
exports.ExceptionsFilter = ExceptionsFilter =
|
|
50
|
-
(0, common_1.Catch)()
|
|
51
|
-
__param(0, (0, common_1.Optional)()),
|
|
52
|
-
__metadata("design:paramtypes", [logger_1.LoggerService])
|
|
34
|
+
exports.ExceptionsFilter = ExceptionsFilter = __decorate([
|
|
35
|
+
(0, common_1.Catch)()
|
|
53
36
|
], ExceptionsFilter);
|
|
54
37
|
//# sourceMappingURL=exceptions.filter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exceptions.filter.js","sourceRoot":"","sources":["../../src/filters/exceptions.filter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"exceptions.filter.js","sourceRoot":"","sources":["../../src/filters/exceptions.filter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAMwB;AACxB,uDAAoF;AAK7E,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAC3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sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
|
package/dist/filters/index.js
CHANGED
|
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract.filter"), exports);
|
|
17
18
|
__exportStar(require("./exceptions.filter"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,sDAAoC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
|
2
|
+
import type { ErrorRequestHandler } from 'express';
|
|
3
|
+
export interface BodyParsersOptions {
|
|
4
|
+
raw?: string[];
|
|
5
|
+
rawBody?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function bodyParserReason(): ErrorRequestHandler;
|
|
8
|
+
export declare function bodyParsers(app: INestApplication, options?: BodyParsersOptions): INestApplication<any>;
|
|
9
|
+
//# sourceMappingURL=body-parsers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-parsers.d.ts","sourceRoot":"","sources":["../../src/http/body-parsers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAmC,MAAM,SAAS,CAAC;AAGpF,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC;IACf,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAaD,wBAAgB,gBAAgB,IAAI,mBAAmB,CAUtD;AAGD,wBAAgB,WAAW,CAAC,GAAG,EAAE,gBAAgB,EAAE,OAAO,GAAE,kBAAuB,yBAQlF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bodyParserReason = bodyParserReason;
|
|
4
|
+
exports.bodyParsers = bodyParsers;
|
|
5
|
+
const express_1 = require("express");
|
|
6
|
+
const keepRawBody = (request, _, buffer) => {
|
|
7
|
+
if (Buffer.isBuffer(buffer))
|
|
8
|
+
request.rawBody = buffer;
|
|
9
|
+
};
|
|
10
|
+
function bodyParserReason() {
|
|
11
|
+
return (error, _, response, next) => {
|
|
12
|
+
if (error?.type && error.statusCode) {
|
|
13
|
+
const reason = error.type === 'entity.parse.failed' ? 'Invalid JSON body' : error.message;
|
|
14
|
+
Object.assign(response.locals, { error: reason });
|
|
15
|
+
}
|
|
16
|
+
next(error);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function bodyParsers(app, options = {}) {
|
|
20
|
+
const verify = options.rawBody ? keepRawBody : undefined;
|
|
21
|
+
for (const path of options.raw ?? [])
|
|
22
|
+
app.use(path, (0, express_1.raw)({ type: 'application/json' }));
|
|
23
|
+
app.use((0, express_1.json)({ verify }), (0, express_1.urlencoded)({ extended: true, verify }), bodyParserReason());
|
|
24
|
+
return app;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=body-parsers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-parsers.js","sourceRoot":"","sources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F,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./body-parsers"), exports);
|
|
18
|
+
__exportStar(require("./request-logger"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/http/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,mDAAiC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { INestApplicationContext } from '@nestjs/common';
|
|
2
|
+
import type { Request, RequestHandler } from 'express';
|
|
3
|
+
export interface RequestLogLocals {
|
|
4
|
+
error?: string;
|
|
5
|
+
trace?: unknown;
|
|
6
|
+
}
|
|
7
|
+
export interface RequestLoggerOptions {
|
|
8
|
+
scope?: (request: Request) => string | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare function requestLogger(app: INestApplicationContext, options?: RequestLoggerOptions): RequestHandler;
|
|
11
|
+
//# sourceMappingURL=request-logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-logger.d.ts","sourceRoot":"","sources":["../../src/http/request-logger.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,KAAK,EAAgB,OAAO,EAAE,cAAc,EAAY,MAAM,SAAS,CAAC;AAE/E,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;CAClD;AAQD,wBAAgB,aAAa,CAC3B,GAAG,EAAE,uBAAuB,EAC5B,OAAO,GAAE,oBAAyB,GACjC,cAAc,CA8BhB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requestLogger = requestLogger;
|
|
4
|
+
const logger_1 = require("../logger");
|
|
5
|
+
const MAX_REASON = 200;
|
|
6
|
+
const capped = (reason) => reason.length > MAX_REASON ? `${reason.slice(0, MAX_REASON)}…` : reason;
|
|
7
|
+
function requestLogger(app, options = {}) {
|
|
8
|
+
const logger = app.get(logger_1.LoggerService, { strict: false });
|
|
9
|
+
return (request, response, next) => {
|
|
10
|
+
const started = process.hrtime.bigint();
|
|
11
|
+
const context = (0, logger_1.contextOf)(request);
|
|
12
|
+
if (options.scope)
|
|
13
|
+
context.scope = () => options.scope?.(request);
|
|
14
|
+
response.on('finish', () => {
|
|
15
|
+
const ms = Number(process.hrtime.bigint() - started) / 1e6;
|
|
16
|
+
const path = request.originalUrl.split('?')[0];
|
|
17
|
+
const { error, trace } = response.locals;
|
|
18
|
+
const line = `${path} ${response.statusCode} ${ms.toFixed(0)}ms`;
|
|
19
|
+
const message = error ? `${line} ${JSON.stringify(capped(error))}` : line;
|
|
20
|
+
const tier = response.statusCode >= 500 ? 'error' : response.statusCode >= 400 ? 'warn' : 'log';
|
|
21
|
+
logger_1.requestContext.run(context, () => logger.request(tier, {
|
|
22
|
+
method: request.method,
|
|
23
|
+
message,
|
|
24
|
+
trace: tier === 'error' ? trace : undefined,
|
|
25
|
+
}));
|
|
26
|
+
});
|
|
27
|
+
logger_1.requestContext.run(context, next);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=request-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-logger.js","sourceRoot":"","sources":["../../src/http/request-logger.ts"],"names":[],"mappings":";;AAmBA,sCAiCC;AApDD,sCAAqE;AAarE,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB,MAAM,MAAM,GAAG,CAAC,MAAc,EAAE,EAAE,CAChC,MAAM,CAAC,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AAG1E,SAAgB,aAAa,CAC3B,GAA4B,EAC5B,UAAgC,EAAE;IAElC,MAAM,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,sBAAa,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IAEzD,OAAO,CAAC,OAAgB,EAAE,QAAkB,EAAE,IAAkB,EAAE,EAAE;QAClE,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACxC,MAAM,OAAO,GAAG,IAAA,kBAAS,EAAC,OAAO,CAAC,CA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,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./config"), exports);
|
|
18
18
|
__exportStar(require("./events"), exports);
|
|
19
19
|
__exportStar(require("./filters"), exports);
|
|
20
|
+
__exportStar(require("./http"), exports);
|
|
20
21
|
__exportStar(require("./logger"), exports);
|
|
21
22
|
__exportStar(require("./rabbit"), exports);
|
|
22
23
|
__exportStar(require("./signature"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,yCAAuB;AACvB,2CAAyB;AACzB,2CAAyB;AACzB,8CAA4B"}
|
package/dist/logger/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC"}
|
package/dist/logger/index.js
CHANGED
|
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./logger.module"), exports);
|
|
18
18
|
__exportStar(require("./logger.options"), exports);
|
|
19
19
|
__exportStar(require("./logger.service"), exports);
|
|
20
|
+
__exportStar(require("./request-context"), exports);
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
package/dist/logger/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,mDAAiC;AACjC,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logger/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,mDAAiC;AACjC,mDAAiC;AACjC,oDAAkC"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import { LoggerModuleOptions } from './logger.options';
|
|
3
|
-
export declare class LoggerModule {
|
|
4
|
-
static forRoot(options
|
|
1
|
+
import { DynamicModule, MiddlewareConsumer, NestModule } from '@nestjs/common';
|
|
2
|
+
import { LoggerModuleAsyncOptions, LoggerModuleOptions } from './logger.options';
|
|
3
|
+
export declare class LoggerModule implements NestModule {
|
|
4
|
+
static forRoot(options: LoggerModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync({ imports, inject, useFactory, }: LoggerModuleAsyncOptions): DynamicModule;
|
|
6
|
+
configure(consumer: MiddlewareConsumer): void;
|
|
5
7
|
}
|
|
6
8
|
//# sourceMappingURL=logger.module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.module.d.ts","sourceRoot":"","sources":["../../src/logger/logger.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"logger.module.d.ts","sourceRoot":"","sources":["../../src/logger/logger.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAU,kBAAkB,EAAU,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE/F,OAAO,EAAkB,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAIjG,qBAEa,YAAa,YAAW,UAAU;IAC7C,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;IAQ3D,MAAM,CAAC,YAAY,CAAC,EAClB,OAAY,EACZ,MAAW,EACX,UAAU,GACX,EAAE,wBAAwB,GAAG,aAAa;IAS3C,SAAS,CAAC,QAAQ,EAAE,kBAAkB;CAKvC"}
|
|
@@ -9,20 +9,35 @@ var LoggerModule_1;
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.LoggerModule = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
|
+
const common_2 = require("@nestjs/common");
|
|
12
13
|
const logger_options_1 = require("./logger.options");
|
|
13
14
|
const logger_service_1 = require("./logger.service");
|
|
15
|
+
const request_context_1 = require("./request-context");
|
|
14
16
|
let LoggerModule = LoggerModule_1 = class LoggerModule {
|
|
15
|
-
static forRoot(options
|
|
17
|
+
static forRoot(options) {
|
|
16
18
|
return {
|
|
17
19
|
module: LoggerModule_1,
|
|
18
20
|
providers: [{ provide: logger_options_1.LOGGER_OPTIONS, useValue: options }, logger_service_1.LoggerService],
|
|
19
21
|
exports: [logger_service_1.LoggerService],
|
|
20
22
|
};
|
|
21
23
|
}
|
|
24
|
+
static forRootAsync({ imports = [], inject = [], useFactory, }) {
|
|
25
|
+
return {
|
|
26
|
+
module: LoggerModule_1,
|
|
27
|
+
imports,
|
|
28
|
+
providers: [{ provide: logger_options_1.LOGGER_OPTIONS, inject, useFactory }, logger_service_1.LoggerService],
|
|
29
|
+
exports: [logger_service_1.LoggerService],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
configure(consumer) {
|
|
33
|
+
consumer
|
|
34
|
+
.apply(request_context_1.RequestContextMiddleware)
|
|
35
|
+
.forRoutes({ path: '*path', method: common_2.RequestMethod.ALL });
|
|
36
|
+
}
|
|
22
37
|
};
|
|
23
38
|
exports.LoggerModule = LoggerModule;
|
|
24
39
|
exports.LoggerModule = LoggerModule = LoggerModule_1 = __decorate([
|
|
25
40
|
(0, common_1.Global)(),
|
|
26
|
-
(0, common_1.Module)({
|
|
41
|
+
(0, common_1.Module)({})
|
|
27
42
|
], LoggerModule);
|
|
28
43
|
//# sourceMappingURL=logger.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.module.js","sourceRoot":"","sources":["../../src/logger/logger.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+
|
|
1
|
+
{"version":3,"file":"logger.module.js","sourceRoot":"","sources":["../../src/logger/logger.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+F;AAC/F,2CAA+C;AAC/C,qDAAiG;AACjG,qDAAiD;AACjD,uDAA6D;AAItD,IAAM,YAAY,oBAAlB,MAAM,YAAY;IACvB,MAAM,CAAC,OAAO,CAAC,OAA4B;QACzC,OAAO;YACL,MAAM,EAAE,cAAY;YACpB,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,+BAAc,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,8BAAa,CAAC;YAC1E,OAAO,EAAE,CAAC,8BAAa,CAAC;SACzB,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,EAClB,OAAO,GAAG,EAAE,EACZ,MAAM,GAAG,EAAE,EACX,UAAU,GACe;QACzB,OAAO;YACL,MAAM,EAAE,cAAY;YACpB,OAAO;YACP,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,+BAAc,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,8BAAa,CAAC;YAC3E,OAAO,EAAE,CAAC,8BAAa,CAAC;SACzB,CAAC;IACJ,CAAC;IAED,SAAS,CAAC,QAA4B;QACpC,QAAQ;aACL,KAAK,CAAC,0CAAwB,CAAC;aAC/B,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,sBAAa,CAAC,GAAG,EAAE,CAAC,CAAC;IAC7D,CAAC;CACF,CAAA;AA3BY,oCAAY;uBAAZ,YAAY;IAFxB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CA2BxB"}
|
|
@@ -1,11 +1,22 @@
|
|
|
1
|
+
import type { InjectionToken, ModuleMetadata, OptionalFactoryDependency } from '@nestjs/common';
|
|
1
2
|
export declare const LOGGER_OPTIONS: unique symbol;
|
|
2
3
|
export interface LoggerModuleOptions {
|
|
3
|
-
|
|
4
|
+
service: string;
|
|
5
|
+
scope?: () => string | undefined;
|
|
4
6
|
timestamps?: boolean;
|
|
5
7
|
}
|
|
8
|
+
export interface LoggerModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
9
|
+
inject?: Array<InjectionToken | OptionalFactoryDependency>;
|
|
10
|
+
useFactory: (...args: any[]) => LoggerModuleOptions | Promise<LoggerModuleOptions>;
|
|
11
|
+
}
|
|
6
12
|
export interface LoggingOptions {
|
|
7
13
|
group: string;
|
|
8
14
|
message: string;
|
|
9
|
-
type
|
|
15
|
+
type?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RequestLine {
|
|
18
|
+
method: string;
|
|
19
|
+
message: string;
|
|
20
|
+
trace?: unknown;
|
|
10
21
|
}
|
|
11
22
|
//# sourceMappingURL=logger.options.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.options.d.ts","sourceRoot":"","sources":["../../src/logger/logger.options.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,eAA2B,CAAC;AAEvD,MAAM,WAAW,mBAAmB;IAClC,
|
|
1
|
+
{"version":3,"file":"logger.options.d.ts","sourceRoot":"","sources":["../../src/logger/logger.options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAEhG,eAAO,MAAM,cAAc,eAA2B,CAAC;AAEvD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;IACjC,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,wBAAyB,SAAQ,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC;IAC/E,MAAM,CAAC,EAAE,KAAK,CAAC,cAAc,GAAG,yBAAyB,CAAC,CAAC;IAC3D,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,mBAAmB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACpF;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.options.js","sourceRoot":"","sources":["../../src/logger/logger.options.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"logger.options.js","sourceRoot":"","sources":["../../src/logger/logger.options.ts"],"names":[],"mappings":";;;AAEa,QAAA,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LogLevel } from '@nestjs/common';
|
|
2
|
+
import { LoggerModuleOptions, LoggingOptions, RequestLine } from './logger.options';
|
|
2
3
|
export declare class LoggerService {
|
|
3
|
-
private readonly options
|
|
4
|
-
constructor(options?: LoggerModuleOptions);
|
|
4
|
+
private readonly options?;
|
|
5
|
+
constructor(options?: LoggerModuleOptions | undefined);
|
|
5
6
|
log(options: LoggingOptions): void;
|
|
6
7
|
error(options: LoggingOptions & {
|
|
7
8
|
trace?: unknown;
|
|
@@ -9,6 +10,8 @@ export declare class LoggerService {
|
|
|
9
10
|
warn(options: LoggingOptions): void;
|
|
10
11
|
debug(options: LoggingOptions): void;
|
|
11
12
|
verbose(options: LoggingOptions): void;
|
|
13
|
+
request(tier: LogLevel, { method, message, trace }: RequestLine): void;
|
|
14
|
+
private trace;
|
|
12
15
|
private print;
|
|
13
16
|
private currentTime;
|
|
14
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.service.d.ts","sourceRoot":"","sources":["../../src/logger/logger.service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.service.d.ts","sourceRoot":"","sources":["../../src/logger/logger.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,QAAQ,EAAY,MAAM,gBAAgB,CAAC;AAExE,OAAO,EAAkB,mBAAmB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAsBpG,qBACa,aAAa;IACwB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAAR,OAAO,CAAC,EAAE,mBAAmB,YAAA;IAEvF,GAAG,CAAC,OAAO,EAAE,cAAc;IAI3B,KAAK,CAAC,OAAO,EAAE,cAAc,GAAG;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;IAKnD,IAAI,CAAC,OAAO,EAAE,cAAc;IAI5B,KAAK,CAAC,OAAO,EAAE,cAAc;IAI7B,OAAO,CAAC,OAAO,EAAE,cAAc;IAK/B,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,WAAW;IAKtE,OAAO,CAAC,KAAK;IAIb,OAAO,CAAC,KAAK;IAqBb,OAAO,CAAC,WAAW;CAMpB"}
|
|
@@ -19,6 +19,7 @@ exports.LoggerService = void 0;
|
|
|
19
19
|
const common_1 = require("@nestjs/common");
|
|
20
20
|
const chalk_1 = __importDefault(require("chalk"));
|
|
21
21
|
const logger_options_1 = require("./logger.options");
|
|
22
|
+
const request_context_1 = require("./request-context");
|
|
22
23
|
const chalk = new chalk_1.default.Instance({ level: 3 });
|
|
23
24
|
const levelColors = {
|
|
24
25
|
log: chalk.blueBright,
|
|
@@ -27,44 +28,51 @@ const levelColors = {
|
|
|
27
28
|
debug: chalk.magentaBright,
|
|
28
29
|
verbose: chalk.cyanBright,
|
|
29
30
|
};
|
|
31
|
+
const WIDTH = { level: 7, origin: 24, scope: 22, kind: 16 };
|
|
32
|
+
const fit = (value, width) => {
|
|
33
|
+
const text = value || '-';
|
|
34
|
+
return text.length > width ? `${text.slice(0, width - 1)}…` : text.padEnd(width);
|
|
35
|
+
};
|
|
30
36
|
let LoggerService = class LoggerService {
|
|
31
37
|
options;
|
|
32
|
-
constructor(options
|
|
38
|
+
constructor(options) {
|
|
33
39
|
this.options = options;
|
|
34
40
|
}
|
|
35
41
|
log(options) {
|
|
36
|
-
this.print('log', options);
|
|
42
|
+
this.print('log', options.group, options.message);
|
|
37
43
|
}
|
|
38
44
|
error(options) {
|
|
39
|
-
this.print('error', options);
|
|
40
|
-
|
|
41
|
-
console.error(chalk.gray(options.trace));
|
|
45
|
+
this.print('error', options.group, options.message);
|
|
46
|
+
this.trace(options.trace);
|
|
42
47
|
}
|
|
43
48
|
warn(options) {
|
|
44
|
-
this.print('warn', options);
|
|
49
|
+
this.print('warn', options.group, options.message);
|
|
45
50
|
}
|
|
46
51
|
debug(options) {
|
|
47
|
-
this.print('debug', options);
|
|
52
|
+
this.print('debug', options.group, options.message);
|
|
48
53
|
}
|
|
49
54
|
verbose(options) {
|
|
50
|
-
this.print('verbose', options);
|
|
55
|
+
this.print('verbose', options.group, options.message);
|
|
56
|
+
}
|
|
57
|
+
request(tier, { method, message, trace }) {
|
|
58
|
+
this.print(tier, method, message);
|
|
59
|
+
this.trace(trace);
|
|
60
|
+
}
|
|
61
|
+
trace(trace) {
|
|
62
|
+
if (trace)
|
|
63
|
+
console.error(chalk.gray(trace instanceof Error ? trace.stack : trace));
|
|
51
64
|
}
|
|
52
|
-
print(tier,
|
|
65
|
+
print(tier, kind, message) {
|
|
53
66
|
const color = levelColors[tier] ?? ((txt) => txt);
|
|
54
67
|
const timestamps = this.options?.timestamps ?? process.env.NODE_ENV !== 'production';
|
|
55
|
-
const
|
|
68
|
+
const context = request_context_1.requestContext.getStore();
|
|
69
|
+
const origin = context?.origin ?? this.options?.service;
|
|
70
|
+
const scope = context?.scope?.() ?? this.options?.scope?.();
|
|
56
71
|
const output = [];
|
|
57
72
|
if (timestamps)
|
|
58
73
|
output.push(this.currentTime());
|
|
59
|
-
output.push(color(tier.toUpperCase().
|
|
60
|
-
|
|
61
|
-
output.push(chalk.hex('#00FF77')(host));
|
|
62
|
-
output.push(chalk.hex('#FFA500')(group ? `[${group}]` : ''), message);
|
|
63
|
-
return console.log(output
|
|
64
|
-
.join(' ')
|
|
65
|
-
.padEnd(200)
|
|
66
|
-
.concat(chalk.hex('#FFA500')(`[${type}]`))
|
|
67
|
-
.trim());
|
|
74
|
+
output.push(color(fit(tier.toUpperCase(), WIDTH.level)), chalk.hex('#00FF77')(fit(origin, WIDTH.origin)), chalk.cyan(fit(scope, WIDTH.scope)), chalk.hex('#FFA500')(fit(kind, WIDTH.kind)), message);
|
|
75
|
+
return console.log(output.join(' '));
|
|
68
76
|
}
|
|
69
77
|
currentTime() {
|
|
70
78
|
const date = new Date();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.service.js","sourceRoot":"","sources":["../../src/logger/logger.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,2CAAwE;AACxE,kDAA0B;AAC1B,
|
|
1
|
+
{"version":3,"file":"logger.service.js","sourceRoot":"","sources":["../../src/logger/logger.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,2CAAwE;AACxE,kDAA0B;AAC1B,qDAAoG;AACpG,uDAAmD;AAEnD,MAAM,KAAK,GAAG,IAAI,eAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;AAE/C,MAAM,WAAW,GAA2C;IAC1D,GAAG,EAAE,KAAK,CAAC,UAAU;IACrB,KAAK,EAAE,KAAK,CAAC,SAAS;IACtB,IAAI,EAAE,KAAK,CAAC,YAAY;IACxB,KAAK,EAAE,KAAK,CAAC,aAAa;IAC1B,OAAO,EAAE,KAAK,CAAC,UAAU;CAC1B,CAAC;AAEF,MAAM,KAAK,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AAE5D,MAAM,GAAG,GAAG,CAAC,KAAyB,EAAE,KAAa,EAAE,EAAE;IACvD,MAAM,IAAI,GAAG,KAAK,IAAI,GAAG,CAAC;IAE1B,OAAO,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,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CAAC;QAEF,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACvC,CAAC;IAEO,WAAW;QACjB,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;QACxB,MAAM,GAAG,GAAG,CAAC,CAAS,EAAE,KAAK,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAExE,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC;IACzH,CAAC;CACF,CAAA;AA7DY,sCAAa;wBAAb,aAAa;IADzB,IAAA,mBAAU,GAAE;IAEE,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,+BAAc,CAAC,CAAA;;GADpC,aAAa,CA6DzB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
+
import { NestMiddleware } from '@nestjs/common';
|
|
3
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
4
|
+
export interface RequestContext {
|
|
5
|
+
origin: string;
|
|
6
|
+
scope?: () => string | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare const requestContext: AsyncLocalStorage<RequestContext>;
|
|
9
|
+
export declare function requestOrigin(request: Request): string;
|
|
10
|
+
export declare function contextOf(request: Request): RequestContext;
|
|
11
|
+
export declare class RequestContextMiddleware implements NestMiddleware {
|
|
12
|
+
use(request: Request, _: Response, next: NextFunction): void;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=request-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-context.d.ts","sourceRoot":"","sources":["../../src/logger/request-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAc,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE/D,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,MAAM,GAAG,SAAS,CAAC;CAClC;AAED,eAAO,MAAM,cAAc,mCAA0C,CAAC;AAStE,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,UAS7C;AAGD,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,cAAc,CAI1D;AAGD,qBACa,wBAAyB,YAAW,cAAc;IAC7D,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY;CAGtD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.RequestContextMiddleware = exports.requestContext = void 0;
|
|
10
|
+
exports.requestOrigin = requestOrigin;
|
|
11
|
+
exports.contextOf = contextOf;
|
|
12
|
+
const async_hooks_1 = require("async_hooks");
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
exports.requestContext = new async_hooks_1.AsyncLocalStorage();
|
|
15
|
+
const CONTEXT = Symbol('merkaly.request.context');
|
|
16
|
+
const hostOf = (origin) => origin?.split('/').pop() || undefined;
|
|
17
|
+
function requestOrigin(request) {
|
|
18
|
+
return (request.header('x-merkaly-host') ||
|
|
19
|
+
hostOf(request.header('origin')) ||
|
|
20
|
+
request.header('cf-connecting-ip') ||
|
|
21
|
+
request.header('x-forwarded-for')?.split(',')[0].trim() ||
|
|
22
|
+
request.socket?.remoteAddress?.replace(/^::ffff:/, '') ||
|
|
23
|
+
'unknown');
|
|
24
|
+
}
|
|
25
|
+
function contextOf(request) {
|
|
26
|
+
const req = request;
|
|
27
|
+
return (req[CONTEXT] ??= { origin: requestOrigin(request) });
|
|
28
|
+
}
|
|
29
|
+
let RequestContextMiddleware = class RequestContextMiddleware {
|
|
30
|
+
use(request, _, next) {
|
|
31
|
+
exports.requestContext.run(contextOf(request), next);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.RequestContextMiddleware = RequestContextMiddleware;
|
|
35
|
+
exports.RequestContextMiddleware = RequestContextMiddleware = __decorate([
|
|
36
|
+
(0, common_1.Injectable)()
|
|
37
|
+
], RequestContextMiddleware);
|
|
38
|
+
//# sourceMappingURL=request-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-context.js","sourceRoot":"","sources":["../../src/logger/request-context.ts"],"names":[],"mappings":";;;;;;;;;AAkBA,sCASC;AAGD,8BAIC;AAlCD,6CAAgD;AAChD,2CAA4D;AAQ/C,QAAA,cAAc,GAAG,IAAI,+BAAiB,EAAkB,CAAC;AAEtE,MAAM,OAAO,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAIlD,MAAM,MAAM,GAAG,CAAC,MAAe,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,SAAS,CAAC;AAG1E,SAAgB,aAAa,CAAC,OAAgB;IAC5C,OAAO,CACL,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC;QAChC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAChC,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAAC;QAClC,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;QACvD,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC;QACtD,SAAS,CACV,CAAC;AACJ,CAAC;AAGD,SAAgB,SAAS,CAAC,OAAgB;IACxC,MAAM,GAAG,GAAG,OAAyB,CAAC;IAEtC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AAC/D,CAAC;AAIM,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IACnC,GAAG,CAAC,OAAgB,EAAE,CAAW,EAAE,IAAkB;QACnD,sBAAc,CAAC,GAAG,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;CACF,CAAA;AAJY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;GACA,wBAAwB,CAIpC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkaly/nest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Shared NestJS runtime and tooling for Merkaly backend apps: pinned versions plus RabbitMQ events, logger, filters, config and signatures",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"@nestjs/platform-express": "^11.2.8",
|
|
34
34
|
"@nestjs/schematics": "^12.0.0",
|
|
35
35
|
"@nestjs/swagger": "^11.4.7",
|
|
36
|
+
"@types/express": "^5.0.6",
|
|
36
37
|
"@types/node": "^26.6.1",
|
|
37
38
|
"amqp-connection-manager": "^5.0.0",
|
|
38
39
|
"amqplib": "^2.2.0",
|
|
@@ -44,6 +45,7 @@
|
|
|
44
45
|
"eslint-config-prettier": "^10.1.8",
|
|
45
46
|
"eslint-plugin-import": "^2.32.0",
|
|
46
47
|
"eslint-plugin-prettier": "^5.5.4",
|
|
48
|
+
"express": "^5.3.0",
|
|
47
49
|
"globals": "^17.13.0",
|
|
48
50
|
"nestjs-console": "^10.0.0",
|
|
49
51
|
"prettier": "^3.9.3",
|