@jnpt/nest-logging 1.0.0 → 1.1.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 +107 -42
- package/dist/httpclient/http-client.d.ts +23 -0
- package/dist/httpclient/http-client.js +62 -0
- package/dist/httpclient/http-client.module.d.ts +10 -0
- package/dist/httpclient/http-client.module.js +40 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Library สำหรับทำ **structured JSON logging** ใน NestJS (ท
|
|
|
9
9
|
- 📝 **Structured JSON Logging** — ทุก log ออกเป็น JSON 1 บรรทัด ไปที่ `stdout` (field เหมือน Go version ทุกตัว)
|
|
10
10
|
- 🔗 **Correlation ID** — รับ/สร้าง `X-Correlation-ID` อัตโนมัติ และส่งต่อไปทุก log ใน request เดียวกันโดยไม่ต้องส่ง context เอง
|
|
11
11
|
- 📥 **Incoming Request Logging** — log request + response ของทุก route เป็น entry เดียว (Express middleware / Fastify hooks — เลือกให้อัตโนมัติ)
|
|
12
|
-
- 📤 **Outgoing Request Logging** —
|
|
12
|
+
- 📤 **Outgoing Request Logging** — inject `HttpClient` ผ่าน `HttpClientModule` (Dynamic Module แบบ `@nestjs/axios`) หรือใช้ `fetch` ที่มี log ก็ได้ พร้อมส่ง correlation ID ต่อ
|
|
13
13
|
- 🔒 **Security** — mask header และ field ที่เป็นความลับอัตโนมัติ (case-insensitive, nested object/array)
|
|
14
14
|
- ⚙️ **Configurable** — config ได้ทุกอย่างเหมือน Go version และทุกค่าเป็น optional
|
|
15
15
|
- ⚡ **Express และ Fastify 100%** — `@nestjs/platform-express` และ `@nestjs/platform-fastify` ได้ข้อมูลใน log ครบเท่ากัน รวมถึง Express / Fastify แบบไม่ใช้ Nest
|
|
@@ -37,27 +37,43 @@ Library สำหรับทำ **structured JSON logging** ใน NestJS (ท
|
|
|
37
37
|
|
|
38
38
|
## Installation
|
|
39
39
|
|
|
40
|
-
### 1. ติดตั้ง package
|
|
40
|
+
### 1. ติดตั้ง package จาก npm (แนะนำ)
|
|
41
41
|
|
|
42
|
-
package
|
|
42
|
+
package อยู่บน npm: [`@jnpt/nest-logging`](https://www.npmjs.com/package/@jnpt/nest-logging)
|
|
43
43
|
|
|
44
44
|
```bash
|
|
45
|
-
# ก) ติดตั้งจาก registry (หลัง publish ด้วย `npm publish --access public`)
|
|
46
45
|
npm install @jnpt/nest-logging
|
|
46
|
+
# หรือ
|
|
47
|
+
yarn add @jnpt/nest-logging
|
|
48
|
+
# หรือ
|
|
49
|
+
pnpm add @jnpt/nest-logging
|
|
50
|
+
```
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
npm install git+ssh://git@github.com/jnpt-nestjs-templates/logging.git
|
|
50
|
-
# หรือ (ระบุ tag/branch ได้): npm install github:jnpt-nestjs-templates/logging#main
|
|
52
|
+
ถ้าใช้ NestJS บน Fastify ต้องติดตั้ง adapter ของ Nest ด้วย (ถ้ามีอยู่แล้วก็ข้ามได้):
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
npm install /
|
|
54
|
+
```bash
|
|
55
|
+
npm install @nestjs/platform-fastify
|
|
56
|
+
```
|
|
54
57
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
ระบุ version ได้ เช่น `npm install @jnpt/nest-logging@1.0.0`, ดู version ทั้งหมดด้วย `npm view @jnpt/nest-logging versions`
|
|
59
|
+
|
|
60
|
+
#### วิธีอื่น (ไม่ผ่าน npm registry)
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
# ก) จาก GitHub (ระบุ tag/branch ได้หลัง #)
|
|
64
|
+
npm install github:jnpt-nestjs-templates/logging#main
|
|
65
|
+
|
|
66
|
+
# ข) จากโฟลเดอร์ในเครื่อง (ระหว่างพัฒนา library)
|
|
67
|
+
npm install /path/to/nest_logging
|
|
68
|
+
|
|
69
|
+
# ค) จากไฟล์ tarball
|
|
70
|
+
cd /path/to/nest_logging && npm pack # ได้ไฟล์ jnpt-nest-logging-<version>.tgz
|
|
71
|
+
cd /path/to/your-nest-app && npm install /path/to/jnpt-nest-logging-<version>.tgz
|
|
58
72
|
```
|
|
59
73
|
|
|
60
|
-
> วิธี
|
|
74
|
+
> วิธี ก/ข/ค จะ build `dist/` ผ่าน script `prepare` ตอนติดตั้ง
|
|
75
|
+
> **npm 12 ขึ้นไป:** ติดตั้งจาก git ต้องใส่ `--allow-git=all` และต้องอนุญาต install script ก่อนด้วย
|
|
76
|
+
> `npm install-scripts approve @jnpt/nest-logging` ไม่งั้น `prepare` จะถูกบล็อกและไม่มี `dist/` — ติดตั้งจาก npm registry ไม่เจอปัญหานี้
|
|
61
77
|
|
|
62
78
|
### 2. Requirements
|
|
63
79
|
|
|
@@ -391,53 +407,100 @@ app.use(createLoggingMiddleware(new AppLogger('main')));
|
|
|
391
407
|
|
|
392
408
|
## Outgoing Request Logging (HTTP Client)
|
|
393
409
|
|
|
394
|
-
|
|
410
|
+
### แบบที่ 1 (แนะนำสำหรับ NestJS) — `HttpClientModule` + inject `HttpClient`
|
|
395
411
|
|
|
396
|
-
|
|
412
|
+
ใช้เหมือน `HttpModule` / `HttpService` ของ NestJS: register module ครั้งเดียว แล้ว inject `HttpClient` ผ่าน constructor
|
|
413
|
+
(`HttpClientModule` สร้างด้วย [`ConfigurableModuleBuilder`](https://docs.nestjs.com/fundamentals/dynamic-modules#configurable-module-builder))
|
|
397
414
|
|
|
398
415
|
```ts
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
const fetch = createLoggingFetch(log);
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
### แบบกำหนด config เอง
|
|
416
|
+
// app.module.ts
|
|
417
|
+
import { Module } from '@nestjs/common';
|
|
418
|
+
import { HttpClientModule, LoggingModule } from '@jnpt/nest-logging';
|
|
406
419
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
420
|
+
@Module({
|
|
421
|
+
imports: [
|
|
422
|
+
LoggingModule.forRoot({ applicationName: 'my-service' }),
|
|
423
|
+
HttpClientModule.register({
|
|
424
|
+
baseUrl: 'https://jsonplaceholder.typicode.com', // optional — ใช้กับ URL แบบ relative
|
|
425
|
+
headers: { 'X-Api-Key': process.env.API_KEY! }, // optional — ส่งทุก request
|
|
426
|
+
}),
|
|
427
|
+
],
|
|
428
|
+
providers: [TodoClient],
|
|
429
|
+
})
|
|
430
|
+
export class AppModule {}
|
|
412
431
|
```
|
|
413
432
|
|
|
414
|
-
### ใช้ใน NestJS service
|
|
415
|
-
|
|
416
433
|
```ts
|
|
434
|
+
// todo.client.ts
|
|
417
435
|
import { Injectable } from '@nestjs/common';
|
|
418
|
-
import {
|
|
436
|
+
import { HttpClient } from '@jnpt/nest-logging';
|
|
419
437
|
|
|
420
438
|
@Injectable()
|
|
421
439
|
export class TodoClient {
|
|
422
|
-
private readonly
|
|
423
|
-
private readonly fetch = createLoggingFetch(this.log);
|
|
440
|
+
constructor(private readonly http: HttpClient) {}
|
|
424
441
|
|
|
425
442
|
async getTodo(id: number) {
|
|
426
|
-
const res = await this.
|
|
443
|
+
const res = await this.http.get(`/todos/${id}`);
|
|
427
444
|
return res.json(); // อ่าน body ได้ตามปกติ (logger อ่านจาก clone)
|
|
428
445
|
}
|
|
429
446
|
|
|
430
|
-
async createTodo(todo:
|
|
431
|
-
const res = await this.
|
|
432
|
-
method: 'POST',
|
|
433
|
-
headers: { 'Content-Type': 'application/json' },
|
|
434
|
-
body: JSON.stringify(todo),
|
|
435
|
-
});
|
|
447
|
+
async createTodo(todo: { title: string }) {
|
|
448
|
+
const res = await this.http.post('/todos', todo); // object → JSON + Content-Type: application/json ให้อัตโนมัติ
|
|
436
449
|
return res.json();
|
|
437
450
|
}
|
|
438
451
|
}
|
|
439
452
|
```
|
|
440
453
|
|
|
454
|
+
**Options ของ `register()` / ที่ `useFactory` คืนค่า** (ทุกตัว optional)
|
|
455
|
+
|
|
456
|
+
| Option | ความหมาย |
|
|
457
|
+
|---|---|
|
|
458
|
+
| `baseUrl` | prefix ของ URL แบบ relative (`'/todos/1'` → `baseUrl + '/todos/1'`); URL เต็ม (`https://...`) ใช้ตามเดิม |
|
|
459
|
+
| `headers` | header ที่ส่งทุก request (header ที่ส่งตอนเรียกจะทับ) |
|
|
460
|
+
| `loggerName` | `logger_name` ของ log ขาออก (default `"HttpClient"`) |
|
|
461
|
+
| `config` | logging config เฉพาะ client นี้ (default: config จาก `LoggingModule.forRoot`) |
|
|
462
|
+
| `fetch` | fetch implementation อื่น (default: global `fetch`) |
|
|
463
|
+
| `isGlobal` | `true` = ใช้ได้ทุก module โดยไม่ต้อง import ซ้ำ (เฉพาะ `register` / `registerAsync`) |
|
|
464
|
+
|
|
465
|
+
**อ่าน options จาก `ConfigService`** — `registerAsync` รองรับ `useFactory` / `useClass` / `useExisting` + `imports` / `inject` ตามมาตรฐาน Nest
|
|
466
|
+
|
|
467
|
+
```ts
|
|
468
|
+
HttpClientModule.registerAsync({
|
|
469
|
+
imports: [ConfigModule],
|
|
470
|
+
inject: [ConfigService],
|
|
471
|
+
useFactory: (config: ConfigService) => ({ baseUrl: config.get('TODO_API_URL') }),
|
|
472
|
+
})
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
**Methods ของ `HttpClient`** — ทุกตัวคืน `Promise<Response>` (fetch Response มาตรฐาน) และเหมือน `fetch` คือ **ไม่ throw เมื่อได้ 4xx/5xx** (เช็ค `res.ok` เอง) แต่ throw เมื่อ network error
|
|
476
|
+
|
|
477
|
+
| Method | หมายเหตุ |
|
|
478
|
+
|---|---|
|
|
479
|
+
| `get(url, init?)`, `head(url, init?)`, `delete(url, init?)` | `init` = `RequestInit` ของ fetch (headers, signal, ...) |
|
|
480
|
+
| `post(url, body?, init?)`, `put(...)`, `patch(...)` | `body` เป็น object/array/number/boolean → ส่งเป็น JSON; string / `Buffer` / `Blob` / `URLSearchParams` / `FormData` / stream → ส่งตามเดิม |
|
|
481
|
+
| `request(url, init)` | เหมือน `fetch(url, init)` |
|
|
482
|
+
|
|
483
|
+
ต้องการหลาย client (คนละ `baseUrl`) → สร้าง provider เอง เช่น
|
|
484
|
+
`{ provide: 'PAYMENT_HTTP', useFactory: () => new HttpClient({ baseUrl: 'https://pay.example.com' }) }`
|
|
485
|
+
และใช้นอก NestJS ได้ด้วย `new HttpClient(options)`
|
|
486
|
+
|
|
487
|
+
### แบบที่ 2 — `createLoggingFetch` (fetch function)
|
|
488
|
+
|
|
489
|
+
`createLoggingFetch(logger, config?, baseFetch?)` คืนค่า function ที่ใช้แทน `fetch` ได้ทันที (เทียบเท่า `httpclient.NewClient` ของ Go) — `HttpClient` ด้านบนก็ใช้ตัวนี้ข้างใน
|
|
490
|
+
|
|
491
|
+
```ts
|
|
492
|
+
import { AppLogger, createLoggingFetch } from '@jnpt/nest-logging';
|
|
493
|
+
|
|
494
|
+
const log = new AppLogger('MyService');
|
|
495
|
+
const fetch = createLoggingFetch(log); // ใช้ default config
|
|
496
|
+
|
|
497
|
+
// หรือกำหนด config เอง
|
|
498
|
+
const fetchWithConfig = createLoggingFetch(log, {
|
|
499
|
+
applicationName: 'my-service',
|
|
500
|
+
requestLogging: { logBody: true, logResponseBody: true },
|
|
501
|
+
});
|
|
502
|
+
```
|
|
503
|
+
|
|
441
504
|
### ทำอะไรบ้าง
|
|
442
505
|
|
|
443
506
|
1. ใช้ correlation ID ของ request ปัจจุบัน (ถ้าไม่มี เช่นเรียกจาก cron job จะสร้างใหม่) แล้วใส่ header `X-Correlation-ID`
|
|
@@ -708,6 +771,8 @@ Outgoing log เหมือนกันแต่ `message` = `"Outgoing request
|
|
|
708
771
|
| `createLoggingMiddleware(log, config?)` | function | `middleware.LoggingMiddleware(log, config)` (Express) |
|
|
709
772
|
| `createFastifyLoggingPlugin(log, config?)` | function | `middleware.LoggingMiddleware(log, config)` (Fastify plugin) |
|
|
710
773
|
| `registerFastifyLogging(fastify, log, config?)` | function | เหมือนด้านบน แต่เรียก `addHook` ตรงๆ |
|
|
774
|
+
| `HttpClientModule.register(options)` / `.registerAsync(...)` | Nest dynamic module | — (provide `HttpClient`) |
|
|
775
|
+
| `HttpClient` | injectable class | `httpclient.NewClient(log, config)` |
|
|
711
776
|
| `createLoggingFetch(log, config?, baseFetch?)` | function | `httpclient.NewClient(log, config)` |
|
|
712
777
|
| `LogEntry`, `ErrorInfo`, `RequestInfo`, `ResponseInfo`, `Config`, `ConfigInput`, ... | types | struct เดียวกัน |
|
|
713
778
|
|
|
@@ -721,7 +786,7 @@ Outgoing log เหมือนกันแต่ `message` = `"Outgoing request
|
|
|
721
786
|
| ชื่อ method | `Info`, `ErrorWithErr` | `info`, `errorWithErr` (camelCase) |
|
|
722
787
|
| ชื่อ config | `RequestLogging.MaxBodySize` | `requestLogging.maxBodySize` |
|
|
723
788
|
| Middleware | Gin `r.Use(...)` | `LoggingModule.forRoot()` (Express หรือ Fastify อัตโนมัติ) / `createLoggingMiddleware()` / `createFastifyLoggingPlugin()` |
|
|
724
|
-
| HTTP client | `*http.Client` | `fetch` function |
|
|
789
|
+
| HTTP client | `*http.Client` | `HttpClient` (inject ผ่าน `HttpClientModule`) หรือ `fetch` function |
|
|
725
790
|
| `thread_name` | `goroutine-N` | `main` / `worker-N` |
|
|
726
791
|
| `error.class` | `%T` เช่น `*errors.errorString` | ชื่อ class เช่น `Error`, `TypeError` |
|
|
727
792
|
| `stack_trace` | stack ณ จุดที่เรียก log | stack ของ error ที่ส่งเข้ามา (มีประโยชน์กว่า) |
|
|
@@ -740,7 +805,7 @@ Outgoing log เหมือนกันแต่ `message` = `"Outgoing request
|
|
|
740
805
|
```bash
|
|
741
806
|
npm install
|
|
742
807
|
npm run build # tsc → dist/
|
|
743
|
-
npm test # compile + node --test (unit + NestJS e2e บน Express และ Fastify + Fastify ล้วน + fetch)
|
|
808
|
+
npm test # compile + node --test (unit + NestJS e2e บน Express และ Fastify + Fastify ล้วน + fetch + HttpClientModule)
|
|
744
809
|
npm run example # รัน examples/nest-example บน Express ที่ port 8080
|
|
745
810
|
npm run example:fastify # รัน example เดียวกันบน Fastify
|
|
746
811
|
```
|
|
@@ -763,7 +828,7 @@ src/
|
|
|
763
828
|
logger/ # AppLogger, Config, LogEntry, MaskingUtil, correlation ID (AsyncLocalStorage)
|
|
764
829
|
middleware/ # LoggingMiddleware, createLoggingMiddleware (incoming, Express)
|
|
765
830
|
fastify/ # createFastifyLoggingPlugin, registerFastifyLogging (incoming, Fastify)
|
|
766
|
-
httpclient/ # createLoggingFetch (outgoing)
|
|
831
|
+
httpclient/ # HttpClient, HttpClientModule, createLoggingFetch (outgoing)
|
|
767
832
|
internal/ # incoming request tracker (ใช้ร่วม Express/Fastify), pattern matching, header/body helpers
|
|
768
833
|
logging.module.ts
|
|
769
834
|
examples/nest-example/ # เทียบเท่า examples/gin-example ของ Go (main.ts = Express, main-fastify.ts = Fastify)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ConfigInput } from '../logger/types';
|
|
2
|
+
import { type FetchFn } from './logging-fetch';
|
|
3
|
+
export interface HttpClientOptions {
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
headers?: RequestInit['headers'];
|
|
6
|
+
loggerName?: string;
|
|
7
|
+
config?: ConfigInput | null;
|
|
8
|
+
fetch?: FetchFn;
|
|
9
|
+
}
|
|
10
|
+
export type HttpBody = RequestInit['body'] | Record<string, unknown> | unknown[] | number | boolean;
|
|
11
|
+
export declare class HttpClient {
|
|
12
|
+
private readonly options;
|
|
13
|
+
private readonly fetchFn;
|
|
14
|
+
constructor(options?: HttpClientOptions);
|
|
15
|
+
request(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
16
|
+
get(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
17
|
+
head(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
18
|
+
delete(url: string | URL, init?: RequestInit): Promise<Response>;
|
|
19
|
+
post(url: string | URL, body?: HttpBody, init?: RequestInit): Promise<Response>;
|
|
20
|
+
put(url: string | URL, body?: HttpBody, init?: RequestInit): Promise<Response>;
|
|
21
|
+
patch(url: string | URL, body?: HttpBody, init?: RequestInit): Promise<Response>;
|
|
22
|
+
private resolveUrl;
|
|
23
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClient = void 0;
|
|
4
|
+
const logger_1 = require("../logger/logger");
|
|
5
|
+
const logging_fetch_1 = require("./logging-fetch");
|
|
6
|
+
// HttpClient is an injectable HTTP client (via HttpClientModule) built on createLoggingFetch:
|
|
7
|
+
// every call propagates X-Correlation-ID and is logged as an outgoing request.
|
|
8
|
+
// Methods resolve to a standard fetch Response and, like fetch, do not throw on 4xx/5xx.
|
|
9
|
+
class HttpClient {
|
|
10
|
+
options;
|
|
11
|
+
fetchFn;
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
const log = new logger_1.AppLogger(options.loggerName ?? 'HttpClient');
|
|
15
|
+
this.fetchFn = (0, logging_fetch_1.createLoggingFetch)(log, options.config, options.fetch);
|
|
16
|
+
}
|
|
17
|
+
request(url, init = {}) {
|
|
18
|
+
const headers = new Headers(this.options.headers);
|
|
19
|
+
new Headers(init.headers).forEach((value, key) => headers.set(key, value));
|
|
20
|
+
return this.fetchFn(this.resolveUrl(url), { ...init, headers });
|
|
21
|
+
}
|
|
22
|
+
get(url, init) {
|
|
23
|
+
return this.request(url, { ...init, method: 'GET' });
|
|
24
|
+
}
|
|
25
|
+
head(url, init) {
|
|
26
|
+
return this.request(url, { ...init, method: 'HEAD' });
|
|
27
|
+
}
|
|
28
|
+
delete(url, init) {
|
|
29
|
+
return this.request(url, { ...init, method: 'DELETE' });
|
|
30
|
+
}
|
|
31
|
+
post(url, body, init) {
|
|
32
|
+
return this.request(url, withBody('POST', body, init));
|
|
33
|
+
}
|
|
34
|
+
put(url, body, init) {
|
|
35
|
+
return this.request(url, withBody('PUT', body, init));
|
|
36
|
+
}
|
|
37
|
+
patch(url, body, init) {
|
|
38
|
+
return this.request(url, withBody('PATCH', body, init));
|
|
39
|
+
}
|
|
40
|
+
resolveUrl(url) {
|
|
41
|
+
const { baseUrl } = this.options;
|
|
42
|
+
if (!baseUrl || typeof url !== 'string' || /^[a-z][a-z\d+.-]*:/i.test(url))
|
|
43
|
+
return url;
|
|
44
|
+
return `${baseUrl.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.HttpClient = HttpClient;
|
|
48
|
+
function withBody(method, body, init = {}) {
|
|
49
|
+
if (body === undefined)
|
|
50
|
+
return { ...init, method };
|
|
51
|
+
if (isFetchBody(body))
|
|
52
|
+
return { ...init, method, body };
|
|
53
|
+
const headers = new Headers(init.headers);
|
|
54
|
+
if (!headers.has('content-type'))
|
|
55
|
+
headers.set('Content-Type', 'application/json');
|
|
56
|
+
return { ...init, method, headers, body: JSON.stringify(body) };
|
|
57
|
+
}
|
|
58
|
+
function isFetchBody(body) {
|
|
59
|
+
return body === null || typeof body === 'string' || body instanceof ArrayBuffer || ArrayBuffer.isView(body)
|
|
60
|
+
|| body instanceof Blob || body instanceof URLSearchParams || body instanceof FormData
|
|
61
|
+
|| body instanceof ReadableStream;
|
|
62
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ConfigurableModuleCls } from '@nestjs/common';
|
|
2
|
+
import { type HttpClientOptions } from './http-client';
|
|
3
|
+
export interface HttpClientModuleExtras {
|
|
4
|
+
isGlobal?: boolean;
|
|
5
|
+
}
|
|
6
|
+
type HttpClientModuleBase = ConfigurableModuleCls<HttpClientOptions, 'register', 'create', HttpClientModuleExtras>;
|
|
7
|
+
declare const HttpClientModule_base: HttpClientModuleBase;
|
|
8
|
+
export declare class HttpClientModule extends HttpClientModule_base {
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClientModule = void 0;
|
|
4
|
+
const http_client_1 = require("./http-client");
|
|
5
|
+
// HttpClientModule.register(options) / registerAsync({ useFactory, inject, ... }) provides HttpClient,
|
|
6
|
+
// built with NestJS ConfigurableModuleBuilder. @nestjs/common is required at load time only when it is
|
|
7
|
+
// installed, so the rest of the package keeps working in plain Express / Fastify apps.
|
|
8
|
+
function buildBase() {
|
|
9
|
+
let common;
|
|
10
|
+
try {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
12
|
+
common = require('@nestjs/common');
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
const fail = () => {
|
|
16
|
+
throw new Error('HttpClientModule requires @nestjs/common; use new HttpClient(options) without NestJS');
|
|
17
|
+
};
|
|
18
|
+
return class {
|
|
19
|
+
static register = fail;
|
|
20
|
+
static registerAsync = fail;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const built = new common.ConfigurableModuleBuilder({ moduleName: 'HttpClient' })
|
|
24
|
+
.setClassMethodName('register')
|
|
25
|
+
.setExtras({ isGlobal: false }, (definition, extras) => ({
|
|
26
|
+
...definition,
|
|
27
|
+
global: extras.isGlobal,
|
|
28
|
+
providers: [
|
|
29
|
+
...(definition.providers ?? []),
|
|
30
|
+
{ provide: http_client_1.HttpClient, useFactory: (options) => new http_client_1.HttpClient(options), inject: [optionsToken] },
|
|
31
|
+
],
|
|
32
|
+
exports: [http_client_1.HttpClient],
|
|
33
|
+
}))
|
|
34
|
+
.build();
|
|
35
|
+
const optionsToken = built.MODULE_OPTIONS_TOKEN;
|
|
36
|
+
return built.ConfigurableModuleClass;
|
|
37
|
+
}
|
|
38
|
+
class HttpClientModule extends buildBase() {
|
|
39
|
+
}
|
|
40
|
+
exports.HttpClientModule = HttpClientModule;
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,6 @@ export { matchPattern, shouldSkip } from './internal/http';
|
|
|
6
6
|
export { createLoggingMiddleware, LoggingMiddleware, type HttpRequest, type HttpResponse, type NextFunction, } from './middleware/logging.middleware';
|
|
7
7
|
export { createFastifyLoggingPlugin, registerFastifyLogging, type FastifyInstanceLike, type FastifyReplyLike, type FastifyRequestLike, } from './fastify/fastify-logging';
|
|
8
8
|
export { createLoggingFetch, type FetchFn } from './httpclient/logging-fetch';
|
|
9
|
+
export { HttpClient, type HttpBody, type HttpClientOptions } from './httpclient/http-client';
|
|
10
|
+
export { HttpClientModule, type HttpClientModuleExtras } from './httpclient/http-client.module';
|
|
9
11
|
export { LoggingModule } from './logging.module';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ 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
|
-
exports.LoggingModule = exports.createLoggingFetch = exports.registerFastifyLogging = exports.createFastifyLoggingPlugin = exports.LoggingMiddleware = exports.createLoggingMiddleware = exports.shouldSkip = exports.matchPattern = exports.mergeExtras = exports.getDefaultConfig = exports.configureDefaults = exports.AppLogger = exports.MaskingUtil = void 0;
|
|
17
|
+
exports.LoggingModule = exports.HttpClientModule = exports.HttpClient = exports.createLoggingFetch = exports.registerFastifyLogging = exports.createFastifyLoggingPlugin = exports.LoggingMiddleware = exports.createLoggingMiddleware = exports.shouldSkip = exports.matchPattern = exports.mergeExtras = exports.getDefaultConfig = exports.configureDefaults = exports.AppLogger = exports.MaskingUtil = void 0;
|
|
18
18
|
__exportStar(require("./logger/types"), exports);
|
|
19
19
|
__exportStar(require("./logger/context"), exports);
|
|
20
20
|
var masking_1 = require("./logger/masking");
|
|
@@ -35,5 +35,9 @@ Object.defineProperty(exports, "createFastifyLoggingPlugin", { enumerable: true,
|
|
|
35
35
|
Object.defineProperty(exports, "registerFastifyLogging", { enumerable: true, get: function () { return fastify_logging_1.registerFastifyLogging; } });
|
|
36
36
|
var logging_fetch_1 = require("./httpclient/logging-fetch");
|
|
37
37
|
Object.defineProperty(exports, "createLoggingFetch", { enumerable: true, get: function () { return logging_fetch_1.createLoggingFetch; } });
|
|
38
|
+
var http_client_1 = require("./httpclient/http-client");
|
|
39
|
+
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return http_client_1.HttpClient; } });
|
|
40
|
+
var http_client_module_1 = require("./httpclient/http-client.module");
|
|
41
|
+
Object.defineProperty(exports, "HttpClientModule", { enumerable: true, get: function () { return http_client_module_1.HttpClientModule; } });
|
|
38
42
|
var logging_module_1 = require("./logging.module");
|
|
39
43
|
Object.defineProperty(exports, "LoggingModule", { enumerable: true, get: function () { return logging_module_1.LoggingModule; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jnpt/nest-logging",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Structured JSON logging for NestJS on Express or Fastify (port of github.com/jnpt-golangs/logging): application + request logs, correlation ID, masking, outgoing HTTP logging.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|