@saihu/common 1.2.2 → 1.2.3

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/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from './logger';
8
8
  export * from './messaging';
9
9
  export * from './decorators';
10
10
  export * from './errors';
11
+ export * from './swagger';
11
12
  export { SaihuCommonModule } from './saihu-common.module';
12
13
  export { BaseService } from './services/base.service';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC"}
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ __exportStar(require("./logger"), exports);
25
25
  __exportStar(require("./messaging"), exports);
26
26
  __exportStar(require("./decorators"), exports);
27
27
  __exportStar(require("./errors"), exports);
28
+ __exportStar(require("./swagger"), exports);
28
29
  var saihu_common_module_1 = require("./saihu-common.module");
29
30
  Object.defineProperty(exports, "SaihuCommonModule", { enumerable: true, get: function () { return saihu_common_module_1.SaihuCommonModule; } });
30
31
  var base_service_1 = require("./services/base.service");
@@ -0,0 +1,2 @@
1
+ export { setupSaihuSwagger } from './setup-swagger';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/swagger/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupSaihuSwagger = void 0;
4
+ var setup_swagger_1 = require("./setup-swagger");
5
+ Object.defineProperty(exports, "setupSaihuSwagger", { enumerable: true, get: function () { return setup_swagger_1.setupSaihuSwagger; } });
@@ -0,0 +1,4 @@
1
+ import { INestApplication } from '@nestjs/common';
2
+ import { DocumentBuilder } from '@nestjs/swagger';
3
+ export declare function setupSaihuSwagger(app: INestApplication, config: Omit<DocumentBuilder, 'build'>, path?: string): void;
4
+ //# sourceMappingURL=setup-swagger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup-swagger.d.ts","sourceRoot":"","sources":["../../src/swagger/setup-swagger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAgC,MAAM,iBAAiB,CAAC;AAEhF,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,gBAAgB,EACrB,MAAM,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,CAAC,EACtC,IAAI,SAAQ,QAQb"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setupSaihuSwagger = setupSaihuSwagger;
4
+ const swagger_1 = require("@nestjs/swagger");
5
+ function setupSaihuSwagger(app, config, path = 'api') {
6
+ const swaggerConfig = config.build();
7
+ const documentFactory = () => wrapResponsesInBaseDto(swagger_1.SwaggerModule.createDocument(app, swaggerConfig));
8
+ swagger_1.SwaggerModule.setup(path, app, documentFactory);
9
+ }
10
+ function wrapResponsesInBaseDto(document) {
11
+ var _a, _b, _c;
12
+ const ERROR_SCHEMA = {
13
+ type: 'object',
14
+ properties: {
15
+ code: { type: 'number', example: 400 },
16
+ msg: { type: 'string', example: 'Error message' },
17
+ data: { nullable: true, example: null },
18
+ },
19
+ };
20
+ for (const path of Object.values((_a = document.paths) !== null && _a !== void 0 ? _a : {})) {
21
+ for (const method of Object.values(path)) {
22
+ if (!(method === null || method === void 0 ? void 0 : method.responses))
23
+ continue;
24
+ for (const [statusCode, response] of Object.entries(method.responses)) {
25
+ const schema = (_c = (_b = response === null || response === void 0 ? void 0 : response.content) === null || _b === void 0 ? void 0 : _b['application/json']) === null || _c === void 0 ? void 0 : _c.schema;
26
+ const isSuccess = statusCode.startsWith('2') || statusCode === 'default';
27
+ if (schema) {
28
+ response.content['application/json'].schema = {
29
+ type: 'object',
30
+ properties: {
31
+ code: {
32
+ type: 'number',
33
+ example: isSuccess ? 0 : Number(statusCode),
34
+ },
35
+ msg: {
36
+ type: 'string',
37
+ example: isSuccess ? 'success' : 'Error message',
38
+ },
39
+ data: isSuccess ? schema : { nullable: true, example: null },
40
+ },
41
+ };
42
+ }
43
+ else if (!isSuccess) {
44
+ if (!response.content) {
45
+ response.content = {};
46
+ }
47
+ if (!response.content['application/json']) {
48
+ response.content['application/json'] = {};
49
+ }
50
+ response.content['application/json'].schema = ERROR_SCHEMA;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ return document;
56
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saihu/common",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Common utilities for NestJS applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,6 +20,7 @@
20
20
  "license": "MIT",
21
21
  "peerDependencies": {
22
22
  "@nestjs/common": "^10.0.0 || ^11.0.0",
23
+ "@nestjs/swagger": "^7.0.0 || ^11.0.0",
23
24
  "rxjs": "^7.0.0"
24
25
  },
25
26
  "devDependencies": {