@application-kit/bootstrap 0.1.0 → 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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @application-kit/bootstrap
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- da305d9: `bootstrap/web` экспортирует `createFastifyAdapter` и `configureFastifyApplication` — адаптер с request-id и шаг настройки (cookie, хук `x-request-id`, `configure`, интерцептор ошибок, `ValidationPipe`); `createFastifyApplication` собирается из них. `buildWebApplication` из `testing` вызывает оба: в e2e теперь 422 на невалидный DTO, заголовок `x-request-id` и плагин cookie, как в проде.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- @application-kit/observability@0.6.1
|
|
12
|
+
|
|
3
13
|
## 0.1.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { Type } from '@nestjs/common';
|
|
2
2
|
import type { NestFastifyApplication } from '@nestjs/platform-fastify';
|
|
3
|
+
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
|
3
4
|
import type { FastifyApplicationOptionsInterface } from './fastify-application-options.interface.js';
|
|
5
|
+
export declare const createFastifyAdapter: (trustProxyHops?: number) => FastifyAdapter;
|
|
6
|
+
export declare const configureFastifyApplication: (app: NestFastifyApplication, options?: FastifyApplicationOptionsInterface) => Promise<void>;
|
|
4
7
|
export declare const createFastifyApplication: (rootModule: Type, options?: FastifyApplicationOptionsInterface) => Promise<NestFastifyApplication>;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createFastifyApplication = void 0;
|
|
6
|
+
exports.createFastifyApplication = exports.configureFastifyApplication = exports.createFastifyAdapter = void 0;
|
|
7
7
|
const logging_1 = require("@application-kit/observability/logging");
|
|
8
8
|
const cookie_1 = __importDefault(require("@fastify/cookie"));
|
|
9
9
|
const common_1 = require("@nestjs/common");
|
|
@@ -19,10 +19,11 @@ const flattenValidationErrors = (errors, parentPath = '') => errors.flatMap((val
|
|
|
19
19
|
: [{ constraints: Object.values(validationError.constraints), property }];
|
|
20
20
|
return [...own, ...flattenValidationErrors(validationError.children ?? NO_CHILDREN, property)];
|
|
21
21
|
});
|
|
22
|
-
const
|
|
22
|
+
const createFastifyAdapter = (trustProxyHops = 0) => new platform_fastify_1.FastifyAdapter({
|
|
23
23
|
genReqId: logging_1.fastifyRequestId.genReqId,
|
|
24
24
|
trustProxy: (_address, hop) => hop < trustProxyHops,
|
|
25
25
|
});
|
|
26
|
+
exports.createFastifyAdapter = createFastifyAdapter;
|
|
26
27
|
const registerCookie = async (app, cookie = {}) => {
|
|
27
28
|
if (cookie !== false) {
|
|
28
29
|
await app.register(cookie_1.default, cookie);
|
|
@@ -42,18 +43,21 @@ const createValidationPipe = (status = common_1.HttpStatus.UNPROCESSABLE_ENTITY)
|
|
|
42
43
|
}, status),
|
|
43
44
|
transform: true,
|
|
44
45
|
});
|
|
45
|
-
const
|
|
46
|
-
const app = await core_1.NestFactory.create(rootModule, createAdapter(options.trustProxyHops), {
|
|
47
|
-
bufferLogs: true,
|
|
48
|
-
});
|
|
46
|
+
const configureFastifyApplication = async (app, options = {}) => {
|
|
49
47
|
await registerCookie(app, options.cookie);
|
|
50
48
|
app.getHttpAdapter().getInstance()
|
|
51
49
|
.addHook('onRequest', logging_1.fastifyRequestId.onRequest);
|
|
52
50
|
options.configure?.(app);
|
|
53
51
|
app.useGlobalInterceptors(new nestjs_pino_1.LoggerErrorInterceptor());
|
|
52
|
+
app.useGlobalPipes(createValidationPipe(options.validationErrorStatus));
|
|
53
|
+
};
|
|
54
|
+
exports.configureFastifyApplication = configureFastifyApplication;
|
|
55
|
+
const createFastifyApplication = async (rootModule, options = {}) => {
|
|
56
|
+
const adapter = (0, exports.createFastifyAdapter)(options.trustProxyHops);
|
|
57
|
+
const app = await core_1.NestFactory.create(rootModule, adapter, { bufferLogs: true });
|
|
58
|
+
await (0, exports.configureFastifyApplication)(app, options);
|
|
54
59
|
useProcessLogger(app);
|
|
55
60
|
app.enableShutdownHooks();
|
|
56
|
-
app.useGlobalPipes(createValidationPipe(options.validationErrorStatus));
|
|
57
61
|
return app;
|
|
58
62
|
};
|
|
59
63
|
exports.createFastifyApplication = createFastifyApplication;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type { Type } from '@nestjs/common';
|
|
2
2
|
import type { NestFastifyApplication } from '@nestjs/platform-fastify';
|
|
3
|
+
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
|
3
4
|
import type { FastifyApplicationOptionsInterface } from './fastify-application-options.interface.js';
|
|
5
|
+
export declare const createFastifyAdapter: (trustProxyHops?: number) => FastifyAdapter;
|
|
6
|
+
export declare const configureFastifyApplication: (app: NestFastifyApplication, options?: FastifyApplicationOptionsInterface) => Promise<void>;
|
|
4
7
|
export declare const createFastifyApplication: (rootModule: Type, options?: FastifyApplicationOptionsInterface) => Promise<NestFastifyApplication>;
|
|
@@ -13,7 +13,7 @@ const flattenValidationErrors = (errors, parentPath = '') => errors.flatMap((val
|
|
|
13
13
|
: [{ constraints: Object.values(validationError.constraints), property }];
|
|
14
14
|
return [...own, ...flattenValidationErrors(validationError.children ?? NO_CHILDREN, property)];
|
|
15
15
|
});
|
|
16
|
-
const
|
|
16
|
+
export const createFastifyAdapter = (trustProxyHops = 0) => new FastifyAdapter({
|
|
17
17
|
genReqId: fastifyRequestId.genReqId,
|
|
18
18
|
trustProxy: (_address, hop) => hop < trustProxyHops,
|
|
19
19
|
});
|
|
@@ -36,17 +36,19 @@ const createValidationPipe = (status = HttpStatus.UNPROCESSABLE_ENTITY) => new V
|
|
|
36
36
|
}, status),
|
|
37
37
|
transform: true,
|
|
38
38
|
});
|
|
39
|
-
export const
|
|
40
|
-
const app = await NestFactory.create(rootModule, createAdapter(options.trustProxyHops), {
|
|
41
|
-
bufferLogs: true,
|
|
42
|
-
});
|
|
39
|
+
export const configureFastifyApplication = async (app, options = {}) => {
|
|
43
40
|
await registerCookie(app, options.cookie);
|
|
44
41
|
app.getHttpAdapter().getInstance()
|
|
45
42
|
.addHook('onRequest', fastifyRequestId.onRequest);
|
|
46
43
|
options.configure?.(app);
|
|
47
44
|
app.useGlobalInterceptors(new LoggerErrorInterceptor());
|
|
45
|
+
app.useGlobalPipes(createValidationPipe(options.validationErrorStatus));
|
|
46
|
+
};
|
|
47
|
+
export const createFastifyApplication = async (rootModule, options = {}) => {
|
|
48
|
+
const adapter = createFastifyAdapter(options.trustProxyHops);
|
|
49
|
+
const app = await NestFactory.create(rootModule, adapter, { bufferLogs: true });
|
|
50
|
+
await configureFastifyApplication(app, options);
|
|
48
51
|
useProcessLogger(app);
|
|
49
52
|
app.enableShutdownHooks();
|
|
50
|
-
app.useGlobalPipes(createValidationPipe(options.validationErrorStatus));
|
|
51
53
|
return app;
|
|
52
54
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@application-kit/bootstrap",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Бутстрап процессов NestJS: web на Fastify с периметром (доверенные прокси по хопам, request-id в каждом ответе, cookie, ValidationPipe с плоским списком ошибок, graceful shutdown) и CLI на nestjs-console с логгером pino",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bootstrap",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"test:unit": "vitest run"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@application-kit/observability": "^0.6.
|
|
60
|
+
"@application-kit/observability": "^0.6.1"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@fastify/cookie": "^11.1.3",
|