@rsdk/http.server 6.0.0-next.7 → 6.0.0-next.9
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/http.config.js +5 -5
- package/dist/http.config.js.map +1 -1
- package/dist/http.transport.d.ts +12 -13
- package/dist/http.transport.js +24 -23
- package/dist/http.transport.js.map +1 -1
- package/package.json +5 -2
- package/src/http.config.ts +4 -7
- package/src/http.transport.ts +36 -43
package/dist/http.config.js
CHANGED
|
@@ -20,14 +20,14 @@ let HttpConfig = class HttpConfig extends core_1.Config {
|
|
|
20
20
|
};
|
|
21
21
|
exports.HttpConfig = HttpConfig;
|
|
22
22
|
__decorate([
|
|
23
|
-
(0, core_1.Property)('HTTP_HOST', new
|
|
23
|
+
(0, core_1.Property)('HTTP_HOST', new common_1.StringParser(), {
|
|
24
24
|
defaultValue: '0.0.0.0',
|
|
25
25
|
description: 'HTTP host',
|
|
26
26
|
}),
|
|
27
27
|
__metadata("design:type", String)
|
|
28
28
|
], HttpConfig.prototype, "host", void 0);
|
|
29
29
|
__decorate([
|
|
30
|
-
(0, core_1.Property)('HTTP_PORT', new
|
|
30
|
+
(0, core_1.Property)('HTTP_PORT', new common_1.IntParser(), {
|
|
31
31
|
// eslint-disable-next-line unicorn/numeric-separators-style
|
|
32
32
|
defaultValue: 50050,
|
|
33
33
|
description: 'HTTP port',
|
|
@@ -35,16 +35,16 @@ __decorate([
|
|
|
35
35
|
__metadata("design:type", Number)
|
|
36
36
|
], HttpConfig.prototype, "port", void 0);
|
|
37
37
|
__decorate([
|
|
38
|
-
(0, core_1.Property)('HTTP_BODY_LIMIT', new
|
|
38
|
+
(0, core_1.Property)('HTTP_BODY_LIMIT', new common_1.SizeParser(), {
|
|
39
39
|
defaultValue: new common_1.Size(100, 'kb'),
|
|
40
40
|
description: 'Max HTTP body size',
|
|
41
41
|
}),
|
|
42
42
|
__metadata("design:type", common_1.Size)
|
|
43
43
|
], HttpConfig.prototype, "bodyLimit", void 0);
|
|
44
44
|
__decorate([
|
|
45
|
-
(0, core_1.Property)('HTTP_SWAGGER_UI_ENABLED', new
|
|
45
|
+
(0, core_1.Property)('HTTP_SWAGGER_UI_ENABLED', new common_1.BoolParser(), {
|
|
46
46
|
defaultValue: true,
|
|
47
|
-
description: 'Enable swagger UI on /swagger endpoint',
|
|
47
|
+
description: 'Enable swagger UI on /swagger (can be configured) endpoint',
|
|
48
48
|
}),
|
|
49
49
|
__metadata("design:type", Boolean)
|
|
50
50
|
], HttpConfig.prototype, "swaggerUI", void 0);
|
package/dist/http.config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.config.js","sourceRoot":"","sources":["../src/http.config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"http.config.js","sourceRoot":"","sources":["../src/http.config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAMsB;AACtB,qCAAwE;AAKjE,IAAM,UAAU,GAAhB,MAAM,UAAW,SAAQ,aAAM;IAK3B,IAAI,CAAU;IAOd,IAAI,CAAU;IAMd,SAAS,CAAQ;IAMjB,SAAS,CAAW;CAC9B,CAAA;AAzBY,gCAAU;AAKZ;IAJR,IAAA,eAAQ,EAAC,WAAW,EAAE,IAAI,qBAAY,EAAE,EAAE;QACzC,YAAY,EAAE,SAAS;QACvB,WAAW,EAAE,WAAW;KACzB,CAAC;;wCACqB;AAOd;IALR,IAAA,eAAQ,EAAC,WAAW,EAAE,IAAI,kBAAS,EAAE,EAAE;QACtC,4DAA4D;QAC5D,YAAY,EAAE,KAAK;QACnB,WAAW,EAAE,WAAW;KACzB,CAAC;;wCACqB;AAMd;IAJR,IAAA,eAAQ,EAAC,iBAAiB,EAAE,IAAI,mBAAU,EAAE,EAAE;QAC7C,YAAY,EAAE,IAAI,aAAI,CAAC,GAAG,EAAE,IAAI,CAAC;QACjC,WAAW,EAAE,oBAAoB;KAClC,CAAC;8BACmB,aAAI;6CAAC;AAMjB;IAJR,IAAA,eAAQ,EAAC,yBAAyB,EAAE,IAAI,mBAAU,EAAE,EAAE;QACrD,YAAY,EAAE,IAAI;QAClB,WAAW,EAAE,4DAA4D;KAC1E,CAAC;;6CAC2B;qBAxBlB,UAAU;IAHtB,IAAA,oBAAa,EAAC;QACb,IAAI,EAAE,CAAC,gBAAS,CAAC,cAAc,EAAE,gBAAS,CAAC,SAAS,EAAE,gBAAS,CAAC,IAAI,CAAC;KACtE,CAAC;GACW,UAAU,CAyBtB"}
|
package/dist/http.transport.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { type ExecutionContext } from '@nestjs/common';
|
|
|
2
2
|
import type { Controller, INestApplication } from '@nestjs/common/interfaces';
|
|
3
3
|
import type { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
|
|
4
4
|
import type { AbstractHttpAdapter } from '@nestjs/core';
|
|
5
|
-
import { DocumentBuilder as OriginalDocumentBuilder } from '@nestjs/swagger';
|
|
6
5
|
import type { Constructor, DeepPartial } from '@rsdk/common';
|
|
7
|
-
import type { ConfigContext, GenericHeaders, HttpOptions, IErrorsFormatter, IErrorsSender, IErrorsTransformer, IHttpTransport, LogFormatter } from '@rsdk/core';
|
|
6
|
+
import type { ConfigContext, GenericHeaders, HttpOptions, IErrorsFormatter, IErrorsSender, IErrorsTransformer, IHttpTransport, LogFormatter, PlatformContext } from '@rsdk/core';
|
|
7
|
+
import type { OpenApiOptions } from '@rsdk/http.openapi';
|
|
8
8
|
import { HttpConfig } from './http.config';
|
|
9
9
|
export interface ParsersConfig {
|
|
10
10
|
body: {
|
|
@@ -13,12 +13,12 @@ export interface ParsersConfig {
|
|
|
13
13
|
};
|
|
14
14
|
cookie: boolean;
|
|
15
15
|
}
|
|
16
|
-
export type
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
16
|
+
export type SwaggerOptions = Partial<OpenApiOptions> | {
|
|
17
|
+
fromFile: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function isFromFile(swagger: SwaggerOptions): swagger is {
|
|
20
|
+
fromFile: string;
|
|
21
|
+
};
|
|
22
22
|
export interface HttpTransportOptions {
|
|
23
23
|
/**
|
|
24
24
|
* CORS middleware options
|
|
@@ -30,7 +30,6 @@ export interface HttpTransportOptions {
|
|
|
30
30
|
globalPrefix?: string;
|
|
31
31
|
/**
|
|
32
32
|
* Configuration of body and cookie parsers
|
|
33
|
-
// NOTE: This fields is actually is handled in transport specific way
|
|
34
33
|
*/
|
|
35
34
|
parsers?: DeepPartial<ParsersConfig>;
|
|
36
35
|
/**
|
|
@@ -40,11 +39,11 @@ export interface HttpTransportOptions {
|
|
|
40
39
|
* NOTE: SwaggerUI is enabled or disabled by HTTP_SWAGGER_UI_ENABLED
|
|
41
40
|
* (true by default).
|
|
42
41
|
*/
|
|
43
|
-
swagger?:
|
|
42
|
+
swagger?: Record<string, SwaggerOptions>;
|
|
44
43
|
}
|
|
45
44
|
export declare abstract class AbstractHttpTransport implements IHttpTransport {
|
|
46
|
-
protected readonly options
|
|
47
|
-
constructor(options?: HttpTransportOptions
|
|
45
|
+
protected readonly options: HttpTransportOptions;
|
|
46
|
+
constructor(options?: HttpTransportOptions);
|
|
48
47
|
extractHeaders(ctx: ExecutionContext): GenericHeaders;
|
|
49
48
|
matchByContext(ctx: ExecutionContext): boolean;
|
|
50
49
|
errorFormatter(): IErrorsFormatter;
|
|
@@ -55,7 +54,7 @@ export declare abstract class AbstractHttpTransport implements IHttpTransport {
|
|
|
55
54
|
getMetricsController(): Constructor<Controller>;
|
|
56
55
|
createHttpOptions(configContext: ConfigContext): HttpOptions;
|
|
57
56
|
createAdapter(configContext: ConfigContext): AbstractHttpAdapter;
|
|
58
|
-
configureApp(app: INestApplication,
|
|
57
|
+
configureApp(app: INestApplication, context: PlatformContext): Promise<void>;
|
|
59
58
|
abstract createHttpAdapter(config: HttpConfig): AbstractHttpAdapter;
|
|
60
59
|
abstract logFormatter(): LogFormatter;
|
|
61
60
|
}
|
package/dist/http.transport.js
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AbstractHttpTransport = void 0;
|
|
4
|
+
exports.isFromFile = isFromFile;
|
|
4
5
|
const swagger_1 = require("@nestjs/swagger");
|
|
6
|
+
const common_node_1 = require("@rsdk/common.node");
|
|
5
7
|
const core_1 = require("@rsdk/core");
|
|
6
|
-
const
|
|
8
|
+
const http_openapi_1 = require("@rsdk/http.openapi");
|
|
7
9
|
const controllers_1 = require("./controllers");
|
|
8
10
|
const error_handling_1 = require("./error-handling");
|
|
9
11
|
const http_config_1 = require("./http.config");
|
|
10
12
|
const http_headers_1 = require("./http.headers");
|
|
13
|
+
function isFromFile(swagger) {
|
|
14
|
+
return typeof swagger === 'object' && 'fromFile' in swagger;
|
|
15
|
+
}
|
|
11
16
|
class AbstractHttpTransport {
|
|
12
17
|
options;
|
|
13
|
-
constructor(options) {
|
|
18
|
+
constructor(options = {}) {
|
|
14
19
|
this.options = options;
|
|
15
20
|
}
|
|
16
21
|
extractHeaders(ctx) {
|
|
@@ -39,38 +44,34 @@ class AbstractHttpTransport {
|
|
|
39
44
|
}
|
|
40
45
|
createHttpOptions(configContext) {
|
|
41
46
|
const { host, port } = configContext.resolve(http_config_1.HttpConfig);
|
|
42
|
-
return {
|
|
43
|
-
host,
|
|
44
|
-
port,
|
|
45
|
-
};
|
|
47
|
+
return { host, port };
|
|
46
48
|
}
|
|
47
49
|
createAdapter(configContext) {
|
|
48
50
|
return this.createHttpAdapter(configContext.resolve(http_config_1.HttpConfig));
|
|
49
51
|
}
|
|
50
|
-
configureApp(app,
|
|
52
|
+
async configureApp(app, context) {
|
|
53
|
+
const configContext = await context.getConfigContext();
|
|
51
54
|
const { swaggerUI } = configContext.resolve(http_config_1.HttpConfig);
|
|
52
|
-
const { globalPrefix, swagger, cors } = this.options
|
|
55
|
+
const { globalPrefix, swagger, cors } = this.options;
|
|
53
56
|
if (globalPrefix) {
|
|
54
57
|
app.setGlobalPrefix(globalPrefix);
|
|
55
58
|
}
|
|
56
59
|
if (cors) {
|
|
57
60
|
app.enableCors(cors);
|
|
58
61
|
}
|
|
59
|
-
if (swaggerUI || swagger
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
swagger_1.SwaggerModule.setup(swagger?.url ?? '/swagger', app, spec);
|
|
73
|
-
}
|
|
62
|
+
if (!swaggerUI || !swagger) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
for (const [url, config] of Object.entries(swagger)) {
|
|
66
|
+
const document = isFromFile(config)
|
|
67
|
+
? await (0, common_node_1.readObj)(config.fromFile)
|
|
68
|
+
: await new http_openapi_1.OpenApiGenerator(context, {
|
|
69
|
+
title: app.get(core_1.APP_NAME),
|
|
70
|
+
version: app.get(core_1.APP_VERSION),
|
|
71
|
+
description: app.get(core_1.APP_DESCRIPTION),
|
|
72
|
+
...config,
|
|
73
|
+
}).generate();
|
|
74
|
+
swagger_1.SwaggerModule.setup(url, app, document);
|
|
74
75
|
}
|
|
75
76
|
}
|
|
76
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.transport.js","sourceRoot":"","sources":["../src/http.transport.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"http.transport.js","sourceRoot":"","sources":["../src/http.transport.ts"],"names":[],"mappings":";;;AA2CA,gCAIC;AA1CD,6CAAgD;AAEhD,mDAA4C;AAY5C,qCAAoE;AAEpE,qDAAsD;AAGtD,+CAA4E;AAC5E,qDAI0B;AAC1B,+CAA2C;AAC3C,iDAA6C;AAY7C,SAAgB,UAAU,CACxB,OAAuB;IAEvB,OAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,UAAU,IAAI,OAAO,CAAC;AAC9D,CAAC;AA4BD,MAAsB,qBAAqB;IACV;IAA/B,YAA+B,UAAgC,EAAE;QAAlC,YAAO,GAAP,OAAO,CAA2B;IAAG,CAAC;IAErE,cAAc,CAAC,GAAqB;QAClC,OAAO,IAAI,0BAAW,CAAC,GAAG,CAAC,CAAC;IAC9B,CAAC;IAED,cAAc,CAAC,GAAqB;QAClC,OAAO,GAAG,CAAC,OAAO,EAAE,KAAK,MAAM,CAAC;IAClC,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,oCAAmB,EAAE,CAAC;IACnC,CAAC;IAED,WAAW;QACT,OAAO,IAAI,iCAAgB,EAAE,CAAC;IAChC,CAAC;IAED,iBAAiB;QACf,OAAO,CAAC,IAAI,sCAAqB,EAAE,CAAC,CAAC;IACvC,CAAC;IAED,WAAW;QACT,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,mBAAmB;QACjB,OAAO,kCAAoB,CAAC;IAC9B,CAAC;IAED,oBAAoB;QAClB,OAAO,mCAAqB,CAAC;IAC/B,CAAC;IAED,iBAAiB,CAAC,aAA4B;QAC5C,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,aAAa,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QAEzD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;IAED,aAAa,CAAC,aAA4B;QACxC,OAAO,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC,CAAC;IACnE,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,GAAqB,EACrB,OAAwB;QAExB,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,gBAAgB,EAAE,CAAC;QACvD,MAAM,EAAE,SAAS,EAAE,GAAG,aAAa,CAAC,OAAO,CAAC,wBAAU,CAAC,CAAC;QACxD,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAErD,IAAI,YAAY,EAAE,CAAC;YACjB,GAAG,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC;QACpC,CAAC;QAED,IAAI,IAAI,EAAE,CAAC;YACT,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC;QAED,IAAI,CAAC,SAAS,IAAI,CAAC,OAAO,EAAE,CAAC;YAC3B,OAAO;QACT,CAAC;QAED,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACpD,MAAM,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC;gBACjC,CAAC,CAAC,MAAM,IAAA,qBAAO,EAAgB,MAAM,CAAC,QAAQ,CAAC;gBAC/C,CAAC,CAAC,MAAM,IAAI,+BAAgB,CAAC,OAAO,EAAE;oBAClC,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,eAAQ,CAAC;oBACxB,OAAO,EAAE,GAAG,CAAC,GAAG,CAAC,kBAAW,CAAC;oBAC7B,WAAW,EAAE,GAAG,CAAC,GAAG,CAAC,sBAAe,CAAC;oBACrC,GAAG,MAAM;iBACV,CAAC,CAAC,QAAQ,EAAE,CAAC;YAElB,uBAAa,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;QAC1C,CAAC;IACH,CAAC;CAKF;AAlFD,sDAkFC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/http.server",
|
|
3
|
-
"version": "6.0.0-next.
|
|
3
|
+
"version": "6.0.0-next.9",
|
|
4
4
|
"description": "HTTP transport for rsdk apps (needs some of HTTP adapters)",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,13 +15,16 @@
|
|
|
15
15
|
"@nestjs/core": "^10.0.0",
|
|
16
16
|
"@nestjs/swagger": "^7.0.0 || ^8.0.0",
|
|
17
17
|
"@rsdk/common": "*",
|
|
18
|
+
"@rsdk/common.node": "*",
|
|
18
19
|
"@rsdk/core": "*",
|
|
20
|
+
"@rsdk/http.openapi": "*",
|
|
19
21
|
"@rsdk/logging": "*",
|
|
20
22
|
"@rsdk/zones": "*",
|
|
21
23
|
"body-parser": "^1.20.1",
|
|
22
24
|
"cookie-parser": "^1.4.6",
|
|
25
|
+
"lodash": "^4.17.21",
|
|
23
26
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
24
27
|
"rxjs": "^7.8.1"
|
|
25
28
|
},
|
|
26
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "1489dda3b612de37a2b927e8c74058a910a4ddc9"
|
|
27
30
|
}
|
package/src/http.config.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { Size } from '@rsdk/common';
|
|
2
1
|
import {
|
|
3
2
|
BoolParser,
|
|
4
|
-
Config,
|
|
5
|
-
ConfigSection,
|
|
6
|
-
ConfigTag,
|
|
7
3
|
IntParser,
|
|
8
|
-
|
|
4
|
+
Size,
|
|
9
5
|
SizeParser,
|
|
10
6
|
StringParser,
|
|
11
|
-
} from '@rsdk/
|
|
7
|
+
} from '@rsdk/common';
|
|
8
|
+
import { Config, ConfigSection, ConfigTag, Property } from '@rsdk/core';
|
|
12
9
|
|
|
13
10
|
@ConfigSection({
|
|
14
11
|
tags: [ConfigTag.infrastructure, ConfigTag.transport, ConfigTag.http],
|
|
@@ -35,7 +32,7 @@ export class HttpConfig extends Config {
|
|
|
35
32
|
|
|
36
33
|
@Property('HTTP_SWAGGER_UI_ENABLED', new BoolParser(), {
|
|
37
34
|
defaultValue: true,
|
|
38
|
-
description: 'Enable swagger UI on /swagger endpoint',
|
|
35
|
+
description: 'Enable swagger UI on /swagger (can be configured) endpoint',
|
|
39
36
|
})
|
|
40
37
|
readonly swaggerUI!: boolean;
|
|
41
38
|
}
|
package/src/http.transport.ts
CHANGED
|
@@ -2,11 +2,10 @@ import { type ExecutionContext } from '@nestjs/common';
|
|
|
2
2
|
import type { Controller, INestApplication } from '@nestjs/common/interfaces';
|
|
3
3
|
import type { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
|
|
4
4
|
import type { AbstractHttpAdapter } from '@nestjs/core';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
SwaggerModule,
|
|
8
|
-
} from '@nestjs/swagger';
|
|
5
|
+
import type { OpenAPIObject } from '@nestjs/swagger';
|
|
6
|
+
import { SwaggerModule } from '@nestjs/swagger';
|
|
9
7
|
import type { Constructor, DeepPartial } from '@rsdk/common';
|
|
8
|
+
import { readObj } from '@rsdk/common.node';
|
|
10
9
|
import type {
|
|
11
10
|
ConfigContext,
|
|
12
11
|
GenericHeaders,
|
|
@@ -16,9 +15,12 @@ import type {
|
|
|
16
15
|
IErrorsTransformer,
|
|
17
16
|
IHttpTransport,
|
|
18
17
|
LogFormatter,
|
|
18
|
+
PlatformContext,
|
|
19
19
|
} from '@rsdk/core';
|
|
20
20
|
import { APP_DESCRIPTION, APP_NAME, APP_VERSION } from '@rsdk/core';
|
|
21
|
-
import {
|
|
21
|
+
import type { OpenApiOptions } from '@rsdk/http.openapi';
|
|
22
|
+
import { OpenApiGenerator } from '@rsdk/http.openapi';
|
|
23
|
+
import _ from 'lodash';
|
|
22
24
|
|
|
23
25
|
import { HealthHttpController, MetricsHttpController } from './controllers';
|
|
24
26
|
import {
|
|
@@ -37,16 +39,12 @@ export interface ParsersConfig {
|
|
|
37
39
|
cookie: boolean;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
export type
|
|
41
|
-
OriginalDocumentBuilder,
|
|
42
|
-
'build' | 'setTitle' | 'setDescription' | 'setVersion'
|
|
43
|
-
>;
|
|
44
|
-
|
|
45
|
-
export interface SwaggerOptions {
|
|
46
|
-
url: string;
|
|
47
|
-
file: string;
|
|
42
|
+
export type SwaggerOptions = Partial<OpenApiOptions> | { fromFile: string };
|
|
48
43
|
|
|
49
|
-
|
|
44
|
+
export function isFromFile(
|
|
45
|
+
swagger: SwaggerOptions,
|
|
46
|
+
): swagger is { fromFile: string } {
|
|
47
|
+
return typeof swagger === 'object' && 'fromFile' in swagger;
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
export interface HttpTransportOptions {
|
|
@@ -62,7 +60,6 @@ export interface HttpTransportOptions {
|
|
|
62
60
|
|
|
63
61
|
/**
|
|
64
62
|
* Configuration of body and cookie parsers
|
|
65
|
-
// NOTE: This fields is actually is handled in transport specific way
|
|
66
63
|
*/
|
|
67
64
|
parsers?: DeepPartial<ParsersConfig>;
|
|
68
65
|
|
|
@@ -73,11 +70,11 @@ export interface HttpTransportOptions {
|
|
|
73
70
|
* NOTE: SwaggerUI is enabled or disabled by HTTP_SWAGGER_UI_ENABLED
|
|
74
71
|
* (true by default).
|
|
75
72
|
*/
|
|
76
|
-
swagger?:
|
|
73
|
+
swagger?: Record<string, SwaggerOptions>;
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
export abstract class AbstractHttpTransport implements IHttpTransport {
|
|
80
|
-
constructor(protected readonly options
|
|
77
|
+
constructor(protected readonly options: HttpTransportOptions = {}) {}
|
|
81
78
|
|
|
82
79
|
extractHeaders(ctx: ExecutionContext): GenericHeaders {
|
|
83
80
|
return new HttpHeaders(ctx);
|
|
@@ -114,48 +111,44 @@ export abstract class AbstractHttpTransport implements IHttpTransport {
|
|
|
114
111
|
createHttpOptions(configContext: ConfigContext): HttpOptions {
|
|
115
112
|
const { host, port } = configContext.resolve(HttpConfig);
|
|
116
113
|
|
|
117
|
-
return {
|
|
118
|
-
host,
|
|
119
|
-
port,
|
|
120
|
-
};
|
|
114
|
+
return { host, port };
|
|
121
115
|
}
|
|
122
116
|
|
|
123
117
|
createAdapter(configContext: ConfigContext): AbstractHttpAdapter {
|
|
124
118
|
return this.createHttpAdapter(configContext.resolve(HttpConfig));
|
|
125
119
|
}
|
|
126
120
|
|
|
127
|
-
configureApp(
|
|
121
|
+
async configureApp(
|
|
128
122
|
app: INestApplication,
|
|
129
|
-
|
|
130
|
-
): Promise<void>
|
|
123
|
+
context: PlatformContext,
|
|
124
|
+
): Promise<void> {
|
|
125
|
+
const configContext = await context.getConfigContext();
|
|
131
126
|
const { swaggerUI } = configContext.resolve(HttpConfig);
|
|
132
|
-
const { globalPrefix, swagger, cors } = this.options
|
|
127
|
+
const { globalPrefix, swagger, cors } = this.options;
|
|
128
|
+
|
|
133
129
|
if (globalPrefix) {
|
|
134
130
|
app.setGlobalPrefix(globalPrefix);
|
|
135
131
|
}
|
|
132
|
+
|
|
136
133
|
if (cors) {
|
|
137
134
|
app.enableCors(cors);
|
|
138
135
|
}
|
|
139
136
|
|
|
140
|
-
if (swaggerUI || swagger
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
.setVersion(app.get(APP_VERSION))
|
|
144
|
-
.setDescription(app.get(APP_DESCRIPTION));
|
|
145
|
-
|
|
146
|
-
// Applies additional configuration from user
|
|
147
|
-
swagger?.configure?.(builder);
|
|
148
|
-
|
|
149
|
-
const openApiConfig = builder.build();
|
|
150
|
-
const spec = SwaggerModule.createDocument(app, openApiConfig);
|
|
151
|
-
|
|
152
|
-
if (swagger?.file) {
|
|
153
|
-
writeFileSync(swagger.file, JSON.stringify(spec));
|
|
154
|
-
}
|
|
137
|
+
if (!swaggerUI || !swagger) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
155
140
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
141
|
+
for (const [url, config] of Object.entries(swagger)) {
|
|
142
|
+
const document = isFromFile(config)
|
|
143
|
+
? await readObj<OpenAPIObject>(config.fromFile)
|
|
144
|
+
: await new OpenApiGenerator(context, {
|
|
145
|
+
title: app.get(APP_NAME),
|
|
146
|
+
version: app.get(APP_VERSION),
|
|
147
|
+
description: app.get(APP_DESCRIPTION),
|
|
148
|
+
...config,
|
|
149
|
+
}).generate();
|
|
150
|
+
|
|
151
|
+
SwaggerModule.setup(url, app, document);
|
|
159
152
|
}
|
|
160
153
|
}
|
|
161
154
|
|