@zedtech/zedkit-nest 0.0.0 → 0.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/dist/all-exceptions.filter.d.ts +17 -0
- package/dist/all-exceptions.filter.d.ts.map +1 -0
- package/dist/all-exceptions.filter.js +97 -0
- package/dist/all-exceptions.filter.js.map +1 -0
- package/dist/factory.d.ts +121 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +176 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/request-id.d.ts +3 -0
- package/dist/request-id.d.ts.map +1 -0
- package/dist/request-id.interceptor.d.ts +11 -0
- package/dist/request-id.interceptor.d.ts.map +1 -0
- package/dist/request-id.interceptor.js +31 -0
- package/dist/request-id.interceptor.js.map +1 -0
- package/dist/request-id.js +4 -0
- package/dist/request-id.js.map +1 -0
- package/dist/validate-env.d.ts +11 -0
- package/dist/validate-env.d.ts.map +1 -0
- package/dist/validate-env.js +31 -0
- package/dist/validate-env.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ArgumentsHost, ExceptionFilter } from "@nestjs/common";
|
|
2
|
+
/**
|
|
3
|
+
* Global error shape for every Nest app from this bootstrap:
|
|
4
|
+
* `{ code, message, requestId }` with the matching HTTP status.
|
|
5
|
+
* 4xx responses carry the safe client-facing message; 5xx responses never
|
|
6
|
+
* leak internals (the full error goes to the logger only).
|
|
7
|
+
*/
|
|
8
|
+
export interface ErrorBody {
|
|
9
|
+
code: string;
|
|
10
|
+
message: string;
|
|
11
|
+
requestId: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class AllExceptionsFilter implements ExceptionFilter {
|
|
14
|
+
private readonly logger;
|
|
15
|
+
catch(exception: unknown, host: ArgumentsHost): void;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=all-exceptions.filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-exceptions.filter.d.ts","sourceRoot":"","sources":["../src/all-exceptions.filter.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EAEb,eAAe,EAGhB,MAAM,gBAAgB,CAAC;AAIxB;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAwCD,qBACa,mBAAoB,YAAW,eAAe;IACzD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;IAE/D,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI;CAuCrD"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var AllExceptionsFilter_1;
|
|
8
|
+
// Original work by the same author.
|
|
9
|
+
import { Catch, HttpException, Logger, } from "@nestjs/common";
|
|
10
|
+
import { REQUEST_ID_HEADER } from "./request-id.js";
|
|
11
|
+
const STATUS_CODES = {
|
|
12
|
+
400: "invalid_input",
|
|
13
|
+
401: "unauthorized",
|
|
14
|
+
403: "forbidden",
|
|
15
|
+
404: "not_found",
|
|
16
|
+
409: "conflict",
|
|
17
|
+
413: "payload_too_large",
|
|
18
|
+
415: "unsupported_media_type",
|
|
19
|
+
429: "rate_limited",
|
|
20
|
+
500: "internal",
|
|
21
|
+
503: "unavailable",
|
|
22
|
+
};
|
|
23
|
+
/** Safe public messages for mapped 5xx; everything else stays "internal error". */
|
|
24
|
+
const STATUS_5XX_MESSAGES = {
|
|
25
|
+
503: "service unavailable",
|
|
26
|
+
};
|
|
27
|
+
function fastifyBodyErrorStatus(exception) {
|
|
28
|
+
if (typeof exception !== "object" || exception === null)
|
|
29
|
+
return null;
|
|
30
|
+
if (!("code" in exception) || typeof exception.code !== "string")
|
|
31
|
+
return null;
|
|
32
|
+
if (exception.code === "FST_ERR_CTP_BODY_TOO_LARGE")
|
|
33
|
+
return 413;
|
|
34
|
+
if (exception.code === "FST_ERR_CTP_INVALID_MEDIA_TYPE")
|
|
35
|
+
return 415;
|
|
36
|
+
if (exception.code.startsWith("FST_ERR_CTP_"))
|
|
37
|
+
return 400;
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function httpMessage(exception) {
|
|
41
|
+
const response = exception.getResponse();
|
|
42
|
+
if (typeof response === "string")
|
|
43
|
+
return response;
|
|
44
|
+
if (typeof response === "object" && response !== null && "message" in response) {
|
|
45
|
+
const message = response.message;
|
|
46
|
+
if (typeof message === "string")
|
|
47
|
+
return message;
|
|
48
|
+
if (Array.isArray(message))
|
|
49
|
+
return message.map(String).join("; ");
|
|
50
|
+
}
|
|
51
|
+
return exception.message;
|
|
52
|
+
}
|
|
53
|
+
let AllExceptionsFilter = AllExceptionsFilter_1 = class AllExceptionsFilter {
|
|
54
|
+
logger = new Logger(AllExceptionsFilter_1.name);
|
|
55
|
+
catch(exception, host) {
|
|
56
|
+
const ctx = host.switchToHttp();
|
|
57
|
+
const req = ctx.getRequest();
|
|
58
|
+
const reply = ctx.getResponse();
|
|
59
|
+
const requestId = typeof req.id === "string" ? req.id : "unknown";
|
|
60
|
+
let status;
|
|
61
|
+
let message;
|
|
62
|
+
if (exception instanceof HttpException) {
|
|
63
|
+
status = exception.getStatus();
|
|
64
|
+
message =
|
|
65
|
+
status >= 500
|
|
66
|
+
? (STATUS_5XX_MESSAGES[status] ?? "internal error")
|
|
67
|
+
: httpMessage(exception);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const bodyStatus = fastifyBodyErrorStatus(exception);
|
|
71
|
+
status = bodyStatus ?? 500;
|
|
72
|
+
message =
|
|
73
|
+
status === 413
|
|
74
|
+
? "body exceeds the configured limit"
|
|
75
|
+
: status === 415
|
|
76
|
+
? "unsupported media type"
|
|
77
|
+
: status === 400
|
|
78
|
+
? "invalid request body"
|
|
79
|
+
: "internal error";
|
|
80
|
+
}
|
|
81
|
+
if (status >= 500) {
|
|
82
|
+
this.logger.error(`Unhandled error on ${req.method} ${req.url}: ${String(exception)}`, exception instanceof Error ? exception.stack : undefined);
|
|
83
|
+
}
|
|
84
|
+
// A streaming handler that throws after the first flush already sent
|
|
85
|
+
// the reply: sending again would raise inside the filter itself.
|
|
86
|
+
if (reply.sent)
|
|
87
|
+
return;
|
|
88
|
+
const code = STATUS_CODES[status] ?? `http_${status}`;
|
|
89
|
+
reply.header(REQUEST_ID_HEADER, requestId);
|
|
90
|
+
reply.status(status).send({ code, message, requestId });
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
AllExceptionsFilter = AllExceptionsFilter_1 = __decorate([
|
|
94
|
+
Catch()
|
|
95
|
+
], AllExceptionsFilter);
|
|
96
|
+
export { AllExceptionsFilter };
|
|
97
|
+
//# sourceMappingURL=all-exceptions.filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-exceptions.filter.js","sourceRoot":"","sources":["../src/all-exceptions.filter.ts"],"names":[],"mappings":";;;;;;;AAAA,oCAAoC;AACpC,OAAO,EAEL,KAAK,EAEL,aAAa,EACb,MAAM,GACP,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAcpD,MAAM,YAAY,GAA2B;IAC3C,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AgB,CAAC;QAC7B,CAAC;QACD,IAAI,MAAM,IAAI,GAAG,EAAE,CAAC;YAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CACf,sBAAsB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,KAAK,MAAM,CAAC,SAAS,CAAC,EAAE,EACnE,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CACzD,CAAC;QACJ,CAAC;QACD,qEAAqE;QACrE,iEAAiE;QACjE,IAAI,KAAK,CAAC,IAAI;YAAE,OAAO;QACvB,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,IAAI,QAAQ,MAAM,EAAE,CAAC;QACtD,KAAK,CAAC,MAAM,CAAC,iBAAiB,EAAE,SAAS,CAAC,CAAC;QAC3C,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAsB,CAAC,CAAC;IAC9E,CAAC;CACF,CAAA;AA1CY,mBAAmB;IAD/B,KAAK,EAAE;GACK,mBAAmB,CA0C/B"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { type INestApplication, type Type } from "@nestjs/common";
|
|
3
|
+
import { type NestFastifyApplication } from "@nestjs/platform-fastify";
|
|
4
|
+
import { type ThrottlerStorage } from "@nestjs/throttler";
|
|
5
|
+
import type { FastifyBodyParser, FastifyServerOptions } from "fastify";
|
|
6
|
+
import type { DestinationStream } from "pino";
|
|
7
|
+
/** Whatever `app.enableCors()` accepts (`true` allows any origin). */
|
|
8
|
+
type CorsSetting = Parameters<INestApplication["enableCors"]>[0];
|
|
9
|
+
export interface ThrottleOptions {
|
|
10
|
+
ttlMs: number;
|
|
11
|
+
limit: number;
|
|
12
|
+
/**
|
|
13
|
+
* Custom hit counter (e.g. shared across replicas). Defaults to the
|
|
14
|
+
* in-memory store: correct for a single process only.
|
|
15
|
+
*/
|
|
16
|
+
storage?: ThrottlerStorage | undefined;
|
|
17
|
+
}
|
|
18
|
+
export interface DocsOptions {
|
|
19
|
+
title: string;
|
|
20
|
+
version: string;
|
|
21
|
+
description?: string | undefined;
|
|
22
|
+
/** Serve path for the UI and `<path>-json`. Defaults to `docs`. */
|
|
23
|
+
path?: string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Extra absolute paths serving the same JSON document (stable
|
|
26
|
+
* contract URLs beyond the swagger-default `<path>-json`). Served
|
|
27
|
+
* raw (no guards), like the default JSON route.
|
|
28
|
+
*/
|
|
29
|
+
aliases?: string[] | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Request-logging options. The logger is process-wide: when several apps
|
|
33
|
+
* boot in one process (tests), the first app's logger options win.
|
|
34
|
+
*/
|
|
35
|
+
export interface LoggerOptions {
|
|
36
|
+
level?: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* pino redact paths. Defaults to the Authorization header and cookies:
|
|
39
|
+
* request logs must never carry credentials.
|
|
40
|
+
*/
|
|
41
|
+
redact?: string[] | undefined;
|
|
42
|
+
/** Log destination (pino passthrough). Defaults to stdout. */
|
|
43
|
+
stream?: DestinationStream | undefined;
|
|
44
|
+
}
|
|
45
|
+
export interface ValidationOptions {
|
|
46
|
+
/** Strip properties with no decorators. Defaults to true. */
|
|
47
|
+
whitelist?: boolean | undefined;
|
|
48
|
+
/** Reject (400) unknown properties instead of stripping. Defaults to true. */
|
|
49
|
+
forbidNonWhitelisted?: boolean | undefined;
|
|
50
|
+
}
|
|
51
|
+
export interface NestBootstrapOptions {
|
|
52
|
+
/** Root application module of the product app. */
|
|
53
|
+
module: Type<object>;
|
|
54
|
+
/**
|
|
55
|
+
* class-validator decorated env schema. Validated at boot; boot fails
|
|
56
|
+
* fast with a readable error when a declared variable is invalid.
|
|
57
|
+
* Numbers need an explicit `@Type(() => Number)`: implicit conversion
|
|
58
|
+
* relies on decorator metadata, which vitest/esbuild cannot emit.
|
|
59
|
+
*/
|
|
60
|
+
envSchema?: Type<object> | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* `.env` file lookup (ConfigModule passthrough). Defaults to `[".env"]`
|
|
63
|
+
* (plus `process.env`, which always wins). Missing files are ignored.
|
|
64
|
+
*/
|
|
65
|
+
envFilePath?: string[] | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Generated OpenAPI. Enabled by default except when
|
|
68
|
+
* `NODE_ENV=production` (a public endpoint inventory is a gift to
|
|
69
|
+
* attackers — opt back in explicitly if you serve docs in prod). Pass
|
|
70
|
+
* `false` for apps without HTTP (workers).
|
|
71
|
+
*/
|
|
72
|
+
docs?: DocsOptions | false | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Global IP-based coarse guard. Enabled by default at 100 req/min;
|
|
75
|
+
* product apps add their own credential-scoped limits on top.
|
|
76
|
+
*/
|
|
77
|
+
throttle?: ThrottleOptions | false | undefined;
|
|
78
|
+
/** Populate `request.rawBody` with the exact bytes. Defaults to true. */
|
|
79
|
+
rawBody?: boolean | undefined;
|
|
80
|
+
/** Fastify body cap in bytes. Defaults to 1 MiB. */
|
|
81
|
+
bodyLimit?: number | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Trust proxy headers (Fastify passthrough). Set this before running
|
|
84
|
+
* behind a proxy or every client shares one throttle bucket.
|
|
85
|
+
*/
|
|
86
|
+
trustProxy?: FastifyServerOptions["trustProxy"] | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* `true` allows any origin; pass an object to restrict it. Note: a
|
|
89
|
+
* plain string origin is echoed unconditionally — use an array or
|
|
90
|
+
* function to actually restrict.
|
|
91
|
+
*/
|
|
92
|
+
cors?: CorsSetting | undefined;
|
|
93
|
+
logger?: LoggerOptions | undefined;
|
|
94
|
+
/** Global ValidationPipe shape. Defaults to strict (strip + forbid). */
|
|
95
|
+
validation?: ValidationOptions | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Global error filter. Defaults to AllExceptionsFilter; apps with
|
|
98
|
+
* domain errors pass their own filter class here.
|
|
99
|
+
*/
|
|
100
|
+
exceptionFilter?: Type<object> | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Custom body parsers by content type, installed through the
|
|
103
|
+
* adapter's standard `useBodyParser` (rawBody capture included)
|
|
104
|
+
* before Nest registers its defaults — so a custom
|
|
105
|
+
* `application/json` parser replaces the default instead of
|
|
106
|
+
* conflicting with it. Receives the raw buffer; `done(null, value)`
|
|
107
|
+
* sets `@Body()`, `done(error)` with `statusCode` rejects.
|
|
108
|
+
*/
|
|
109
|
+
bodyParsers?: Record<string, FastifyBodyParser<Buffer>> | undefined;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Generic Nest bootstrap shared by every app. Wires only standard pieces:
|
|
113
|
+
* Fastify platform, validated env (`@nestjs/config`), request-id JSON logs
|
|
114
|
+
* (`nestjs-pino`), strict global `ValidationPipe`, a global error-shape
|
|
115
|
+
* filter, an optional global throttler, generated OpenAPI, and graceful
|
|
116
|
+
* shutdown hooks. Returns the app ready to `listen()`; tests use it with
|
|
117
|
+
* supertest without ever touching product code.
|
|
118
|
+
*/
|
|
119
|
+
export declare function createNestApp(options: NestBootstrapOptions): Promise<NestFastifyApplication>;
|
|
120
|
+
export {};
|
|
121
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAIA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAGL,KAAK,gBAAgB,EACrB,KAAK,IAAI,EACV,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAEL,KAAK,sBAAsB,EAC5B,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAEv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,IAAI,CAAC,EAAE,WAAW,GAAG,KAAK,GAAG,SAAS,CAAC;IACvC;;;OAGG;IACH,QAAQ,CAAC,EAAE,eAAe,GAAG,KAAK,GAAG,SAAS,CAAC;IAC/C,yEAAyE;IACzE,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC9B,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B;;;OAGG;IACH,UAAU,CAAC,EAAE,oBAAoB,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC;IAC5D;;;;OAIG;IACH,IAAI,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;IAC/B,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;IACnC,wEAAwE;IACxE,UAAU,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;IAC3C;;;OAGG;IACH,eAAe,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;IAC3C;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;CACrE;AAiCD;;;;;;;GAOG;AACH,wBAAsB,aAAa,CACjC,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,sBAAsB,CAAC,CAuHjC"}
|
package/dist/factory.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
// Original work by the same author. Design notes:
|
|
8
|
+
// - `envFilePath` option (default `[".env"]`) instead of a hardcoded
|
|
9
|
+
// monorepo-relative lookup.
|
|
10
|
+
// - `trustProxy` passthrough so the documented proxy guidance works.
|
|
11
|
+
import "reflect-metadata";
|
|
12
|
+
import { Module, ValidationPipe, } from "@nestjs/common";
|
|
13
|
+
import { ConfigModule } from "@nestjs/config";
|
|
14
|
+
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE, NestFactory } from "@nestjs/core";
|
|
15
|
+
import { FastifyAdapter, } from "@nestjs/platform-fastify";
|
|
16
|
+
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
|
17
|
+
import { ThrottlerGuard, ThrottlerModule, } from "@nestjs/throttler";
|
|
18
|
+
import { Logger as PinoLogger, LoggerModule } from "nestjs-pino";
|
|
19
|
+
import { randomUUID } from "node:crypto";
|
|
20
|
+
import { AllExceptionsFilter } from "./all-exceptions.filter.js";
|
|
21
|
+
import { REQUEST_ID_HEADER } from "./request-id.js";
|
|
22
|
+
import { RequestIdInterceptor } from "./request-id.interceptor.js";
|
|
23
|
+
import { validateEnv } from "./validate-env.js";
|
|
24
|
+
const DEFAULT_THROTTLE = { ttlMs: 60_000, limit: 100 };
|
|
25
|
+
const DEFAULT_LOG_REDACT = ["req.headers.authorization", "req.headers.cookie"];
|
|
26
|
+
const DEFAULT_BODY_LIMIT = 1024 * 1024;
|
|
27
|
+
const DEFAULT_DOCS = {
|
|
28
|
+
title: "API",
|
|
29
|
+
version: "0.0.0",
|
|
30
|
+
path: "docs",
|
|
31
|
+
};
|
|
32
|
+
function requestIdOf(req) {
|
|
33
|
+
const header = req.headers[REQUEST_ID_HEADER];
|
|
34
|
+
// Node joins repeated HTTP/1 headers with ", "; HTTP/2 may deliver an
|
|
35
|
+
// array. Either way the first value wins.
|
|
36
|
+
const first = Array.isArray(header)
|
|
37
|
+
? header[0]?.trim()
|
|
38
|
+
: header?.split(",")[0]?.trim();
|
|
39
|
+
if (first)
|
|
40
|
+
return first;
|
|
41
|
+
return randomUUID();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Fastify assigns `request.id` (via its own `genReqId`) before pino-http
|
|
45
|
+
* runs, so pino reuses it: logs, error bodies, and the wire id are one
|
|
46
|
+
* value. Generating a second UUID here would split them apart.
|
|
47
|
+
*/
|
|
48
|
+
function pinoReqId(req) {
|
|
49
|
+
const existing = req.id;
|
|
50
|
+
if (typeof existing === "string" && existing.length > 0)
|
|
51
|
+
return existing;
|
|
52
|
+
return requestIdOf(req);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Generic Nest bootstrap shared by every app. Wires only standard pieces:
|
|
56
|
+
* Fastify platform, validated env (`@nestjs/config`), request-id JSON logs
|
|
57
|
+
* (`nestjs-pino`), strict global `ValidationPipe`, a global error-shape
|
|
58
|
+
* filter, an optional global throttler, generated OpenAPI, and graceful
|
|
59
|
+
* shutdown hooks. Returns the app ready to `listen()`; tests use it with
|
|
60
|
+
* supertest without ever touching product code.
|
|
61
|
+
*/
|
|
62
|
+
export async function createNestApp(options) {
|
|
63
|
+
const throttle = options.throttle === undefined ? DEFAULT_THROTTLE : options.throttle;
|
|
64
|
+
const schema = options.envSchema;
|
|
65
|
+
let BootstrapRootModule = class BootstrapRootModule {
|
|
66
|
+
};
|
|
67
|
+
BootstrapRootModule = __decorate([
|
|
68
|
+
Module({
|
|
69
|
+
imports: [
|
|
70
|
+
ConfigModule.forRoot({
|
|
71
|
+
isGlobal: true,
|
|
72
|
+
cache: true,
|
|
73
|
+
envFilePath: options.envFilePath ?? [".env"],
|
|
74
|
+
...(schema
|
|
75
|
+
? { validate: (config) => validateEnv(schema, config) }
|
|
76
|
+
: {}),
|
|
77
|
+
}),
|
|
78
|
+
LoggerModule.forRoot({
|
|
79
|
+
// nestjs-pino takes the destination as a tuple, not an option key.
|
|
80
|
+
pinoHttp: options.logger?.stream === undefined
|
|
81
|
+
? {
|
|
82
|
+
level: options.logger?.level ?? "info",
|
|
83
|
+
genReqId: (req) => pinoReqId(req),
|
|
84
|
+
redact: options.logger?.redact ?? DEFAULT_LOG_REDACT,
|
|
85
|
+
}
|
|
86
|
+
: [
|
|
87
|
+
{
|
|
88
|
+
level: options.logger.level ?? "info",
|
|
89
|
+
genReqId: (req) => pinoReqId(req),
|
|
90
|
+
redact: options.logger.redact ?? DEFAULT_LOG_REDACT,
|
|
91
|
+
},
|
|
92
|
+
options.logger.stream,
|
|
93
|
+
],
|
|
94
|
+
}),
|
|
95
|
+
...(throttle
|
|
96
|
+
? [
|
|
97
|
+
ThrottlerModule.forRoot(throttle.storage
|
|
98
|
+
? {
|
|
99
|
+
storage: throttle.storage,
|
|
100
|
+
throttlers: [{ ttl: throttle.ttlMs, limit: throttle.limit }],
|
|
101
|
+
}
|
|
102
|
+
: [{ ttl: throttle.ttlMs, limit: throttle.limit }]),
|
|
103
|
+
]
|
|
104
|
+
: []),
|
|
105
|
+
options.module,
|
|
106
|
+
],
|
|
107
|
+
providers: [
|
|
108
|
+
{
|
|
109
|
+
provide: APP_PIPE,
|
|
110
|
+
useValue: new ValidationPipe({
|
|
111
|
+
whitelist: options.validation?.whitelist ?? true,
|
|
112
|
+
forbidNonWhitelisted: options.validation?.forbidNonWhitelisted ?? true,
|
|
113
|
+
transform: true,
|
|
114
|
+
}),
|
|
115
|
+
},
|
|
116
|
+
{ provide: APP_FILTER, useClass: options.exceptionFilter ?? AllExceptionsFilter },
|
|
117
|
+
{ provide: APP_INTERCEPTOR, useClass: RequestIdInterceptor },
|
|
118
|
+
...(throttle ? [{ provide: APP_GUARD, useClass: ThrottlerGuard }] : []),
|
|
119
|
+
],
|
|
120
|
+
})
|
|
121
|
+
], BootstrapRootModule);
|
|
122
|
+
const bodyLimit = options.bodyLimit ?? DEFAULT_BODY_LIMIT;
|
|
123
|
+
const adapter = new FastifyAdapter({
|
|
124
|
+
bodyLimit,
|
|
125
|
+
genReqId: (req) => requestIdOf(req),
|
|
126
|
+
// exactOptionalPropertyTypes: only pass the key when set.
|
|
127
|
+
...(options.trustProxy === undefined ? {} : { trustProxy: options.trustProxy }),
|
|
128
|
+
});
|
|
129
|
+
for (const [contentType, parser] of Object.entries(options.bodyParsers ?? {})) {
|
|
130
|
+
adapter.useBodyParser(contentType, options.rawBody ?? true, { bodyLimit }, parser);
|
|
131
|
+
}
|
|
132
|
+
const app = await NestFactory.create(BootstrapRootModule, adapter, {
|
|
133
|
+
rawBody: options.rawBody ?? true,
|
|
134
|
+
bufferLogs: true,
|
|
135
|
+
// Surface boot errors (e.g. invalid env) as rejections instead of
|
|
136
|
+
// `process.abort()`: callers and tests can handle them, and an
|
|
137
|
+
// unhandled rejection still crashes with the error printed.
|
|
138
|
+
abortOnError: false,
|
|
139
|
+
});
|
|
140
|
+
app.useLogger(app.get(PinoLogger));
|
|
141
|
+
app.enableShutdownHooks();
|
|
142
|
+
if (options.cors) {
|
|
143
|
+
app.enableCors(options.cors === true ? undefined : options.cors);
|
|
144
|
+
}
|
|
145
|
+
const docs = options.docs === undefined
|
|
146
|
+
? process.env.NODE_ENV === "production"
|
|
147
|
+
? false
|
|
148
|
+
: DEFAULT_DOCS
|
|
149
|
+
: options.docs;
|
|
150
|
+
if (docs) {
|
|
151
|
+
const builder = new DocumentBuilder()
|
|
152
|
+
.setTitle(docs.title)
|
|
153
|
+
.setVersion(docs.version);
|
|
154
|
+
if (docs.description) {
|
|
155
|
+
builder.setDescription(docs.description);
|
|
156
|
+
}
|
|
157
|
+
// Tolerate "docs/" or "/docs": the JSON route is derived by suffix.
|
|
158
|
+
// Manual trim (no regex): the path is deployer-controlled input.
|
|
159
|
+
const rawDocsPath = docs.path ?? "docs";
|
|
160
|
+
let docsStart = 0;
|
|
161
|
+
let docsEnd = rawDocsPath.length;
|
|
162
|
+
while (docsStart < docsEnd && rawDocsPath[docsStart] === "/")
|
|
163
|
+
docsStart += 1;
|
|
164
|
+
while (docsEnd > docsStart && rawDocsPath[docsEnd - 1] === "/")
|
|
165
|
+
docsEnd -= 1;
|
|
166
|
+
const docsPath = rawDocsPath.slice(docsStart, docsEnd) || "docs";
|
|
167
|
+
const document = SwaggerModule.createDocument(app, builder.build());
|
|
168
|
+
SwaggerModule.setup(docsPath, app, document);
|
|
169
|
+
const fastify = app.getHttpAdapter().getInstance();
|
|
170
|
+
for (const alias of docs.aliases ?? []) {
|
|
171
|
+
fastify.get(alias, (_req, reply) => reply.send(document));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return app;
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAkD;AAClD,qEAAqE;AACrE,8BAA8B;AAC9B,qEAAqE;AACrE,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EACL,MAAM,EACN,cAAc,GAGf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC7F,OAAO,EACL,cAAc,GAEf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EACL,cAAc,EACd,eAAe,GAEhB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BA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, type DocsOptions, type LoggerOptions, type NestBootstrapOptions, type ThrottleOptions, type ValidationOptions, } from "./factory.js";
|
|
3
|
+
export { REQUEST_ID_HEADER } from "./request-id.js";
|
|
4
|
+
export { RequestIdInterceptor } from "./request-id.interceptor.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EACL,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,iBAAiB,GACvB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AllExceptionsFilter } from "./all-exceptions.filter.js";
|
|
2
|
+
export { createNestApp, } from "./factory.js";
|
|
3
|
+
export { REQUEST_ID_HEADER } from "./request-id.js";
|
|
4
|
+
export { RequestIdInterceptor } from "./request-id.interceptor.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAkB,MAAM,4BAA4B,CAAC;AACjF,OAAO,EACL,aAAa,GAMd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-id.d.ts","sourceRoot":"","sources":["../src/request-id.ts"],"names":[],"mappings":"AACA,yEAAyE;AACzE,eAAO,MAAM,iBAAiB,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type CallHandler, type ExecutionContext, type NestInterceptor } from "@nestjs/common";
|
|
2
|
+
/**
|
|
3
|
+
* Echoes the request id as a response header on every handled route.
|
|
4
|
+
* Set before the handler runs, so it survives handler errors; errors
|
|
5
|
+
* thrown earlier (guards, parsers, unknown routes) get the same
|
|
6
|
+
* header from the error filter instead.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RequestIdInterceptor implements NestInterceptor {
|
|
9
|
+
intercept(context: ExecutionContext, next: CallHandler): ReturnType<CallHandler["handle"]>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=request-id.interceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-id.interceptor.d.ts","sourceRoot":"","sources":["../src/request-id.interceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,KAAK,WAAW,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAI3G;;;;;GAKG;AACH,qBACa,oBAAqB,YAAW,eAAe;IAC1D,SAAS,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,UAAU,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;CAS3F"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
// Original work by the same author.
|
|
8
|
+
import { Injectable } from "@nestjs/common";
|
|
9
|
+
import { REQUEST_ID_HEADER } from "./request-id.js";
|
|
10
|
+
/**
|
|
11
|
+
* Echoes the request id as a response header on every handled route.
|
|
12
|
+
* Set before the handler runs, so it survives handler errors; errors
|
|
13
|
+
* thrown earlier (guards, parsers, unknown routes) get the same
|
|
14
|
+
* header from the error filter instead.
|
|
15
|
+
*/
|
|
16
|
+
let RequestIdInterceptor = class RequestIdInterceptor {
|
|
17
|
+
intercept(context, next) {
|
|
18
|
+
const ctx = context.switchToHttp();
|
|
19
|
+
const req = ctx.getRequest();
|
|
20
|
+
const reply = ctx.getResponse();
|
|
21
|
+
if (typeof req.id === "string" && req.id.length > 0) {
|
|
22
|
+
reply.header(REQUEST_ID_HEADER, req.id);
|
|
23
|
+
}
|
|
24
|
+
return next.handle();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
RequestIdInterceptor = __decorate([
|
|
28
|
+
Injectable()
|
|
29
|
+
], RequestIdInterceptor);
|
|
30
|
+
export { RequestIdInterceptor };
|
|
31
|
+
//# sourceMappingURL=request-id.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-id.interceptor.js","sourceRoot":"","sources":["../src/request-id.interceptor.ts"],"names":[],"mappings":";;;;;;AAAA,oCAAoC;AACpC,OAAO,EAAE,UAAU,EAAiE,MAAM,gBAAgB,CAAC;AAE3G,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD;;;;;GAKG;AAEI,IAAM,oBAAoB,GAA1B,MAAM,oBAAoB;IAC/B,SAAS,CAAC,OAAyB,EAAE,IAAiB;QACpD,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;QACnC,MAAM,GAAG,GAAG,GAAG,CAAC,UAAU,EAAkB,CAAC;QAC7C,MAAM,KAAK,GAAG,GAAG,CAAC,WAAW,EAAgB,CAAC;QAC9C,IAAI,OAAO,GAAG,CAAC,EAAE,KAAK,QAAQ,IAAI,GAAG,CAAC,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpD,KAAK,CAAC,MAAM,CAAC,iBAAiB,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAVY,oBAAoB;IADhC,UAAU,EAAE;GACA,oBAAoB,CAUhC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-id.js","sourceRoot":"","sources":["../src/request-id.ts"],"names":[],"mappings":"AAAA,oCAAoC;AACpC,yEAAyE;AACzE,MAAM,CAAC,MAAM,iBAAiB,GAAG,cAAc,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Type } from "@nestjs/common";
|
|
2
|
+
/**
|
|
3
|
+
* `ConfigModule` validate hook (documented Nest recipe): instantiate the
|
|
4
|
+
* schema class so field defaults and `@Type()` coercions apply, then fail
|
|
5
|
+
* fast with one readable error when a declared variable is invalid.
|
|
6
|
+
* Undeclared variables pass through untouched, so unrelated env (PATH,
|
|
7
|
+
* CI flags, other tools) never breaks boot. Returns the instance, so
|
|
8
|
+
* `ConfigService` serves defaults and coerced numbers.
|
|
9
|
+
*/
|
|
10
|
+
export declare function validateEnv(schema: Type<object>, config: Record<string, unknown>): Record<string, unknown>;
|
|
11
|
+
//# sourceMappingURL=validate-env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-env.d.ts","sourceRoot":"","sources":["../src/validate-env.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAkB3C;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,EACpB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAOzB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { plainToInstance } from "class-transformer";
|
|
2
|
+
import { validateSync } from "class-validator";
|
|
3
|
+
function flattenErrors(errors, prefix = "") {
|
|
4
|
+
return errors.flatMap((error) => {
|
|
5
|
+
const path = prefix ? `${prefix}.${error.property}` : error.property;
|
|
6
|
+
const reasons = Object.values(error.constraints ?? {}).join(", ");
|
|
7
|
+
const self = reasons ? [`${path}: ${reasons}`] : [];
|
|
8
|
+
// Unreachable via flat process env today (nested objects never arrive
|
|
9
|
+
// from env files), but the walk keeps the message readable if a nested
|
|
10
|
+
// schema ever validates nested input.
|
|
11
|
+
const children = error.children?.length ? flattenErrors(error.children, path) : [];
|
|
12
|
+
return [...self, ...children];
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* `ConfigModule` validate hook (documented Nest recipe): instantiate the
|
|
17
|
+
* schema class so field defaults and `@Type()` coercions apply, then fail
|
|
18
|
+
* fast with one readable error when a declared variable is invalid.
|
|
19
|
+
* Undeclared variables pass through untouched, so unrelated env (PATH,
|
|
20
|
+
* CI flags, other tools) never breaks boot. Returns the instance, so
|
|
21
|
+
* `ConfigService` serves defaults and coerced numbers.
|
|
22
|
+
*/
|
|
23
|
+
export function validateEnv(schema, config) {
|
|
24
|
+
const instance = plainToInstance(schema, config);
|
|
25
|
+
const errors = validateSync(instance);
|
|
26
|
+
if (errors.length > 0) {
|
|
27
|
+
throw new Error(`Invalid environment: ${flattenErrors(errors).join("; ")}`);
|
|
28
|
+
}
|
|
29
|
+
return instance;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=validate-env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-env.js","sourceRoot":"","sources":["../src/validate-env.ts"],"names":[],"mappings":"AAEA,OAAO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