@saihu/common 1.2.2 → 1.2.4

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
+ }
@@ -1,2 +1,12 @@
1
+ /**
2
+ * 获取中文字符串的拼音首字母。
3
+ *
4
+ * - 保留 `-` 分隔符(每段独立取首字母后用 `-` 重新拼接)
5
+ * - 非中文字符被忽略
6
+ * - 特殊处理:`调-` → `条-`
7
+ *
8
+ * @example chnInitials('爱国') // 'ag'
9
+ * @example chnInitials('调-味品') // 'tw-wp'
10
+ */
1
11
  export declare function chnInitials(text: string): string;
2
12
  //# sourceMappingURL=chn-initials.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"chn-initials.d.ts","sourceRoot":"","sources":["../../src/util/chn-initials.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAUhD"}
1
+ {"version":3,"file":"chn-initials.d.ts","sourceRoot":"","sources":["../../src/util/chn-initials.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAgBhD"}
@@ -2,14 +2,27 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.chnInitials = chnInitials;
4
4
  const pinyin_pro_1 = require("pinyin-pro");
5
+ /**
6
+ * 获取中文字符串的拼音首字母。
7
+ *
8
+ * - 保留 `-` 分隔符(每段独立取首字母后用 `-` 重新拼接)
9
+ * - 非中文字符被忽略
10
+ * - 特殊处理:`调-` → `条-`
11
+ *
12
+ * @example chnInitials('爱国') // 'ag'
13
+ * @example chnInitials('调-味品') // 'tw-wp'
14
+ */
5
15
  function chnInitials(text) {
16
+ if (!text || typeof text !== 'string')
17
+ return '';
18
+ // 特殊处理"调-"到"条-"
19
+ text = text.replace(/调-/g, '条-');
6
20
  return text
7
- .split('')
8
- .map((char) => {
9
- if (/[\u4e00-\u9fa5]/.test(char)) {
10
- return (0, pinyin_pro_1.pinyin)(char, { pattern: 'initial' });
11
- }
12
- return char; // preserve symbols like '-' or spaces
13
- })
14
- .join('');
21
+ .split('-')
22
+ .map((part) => [...part]
23
+ .filter((c) => /[\u4e00-\u9fa5]/.test(c))
24
+ .map((c) => (0, pinyin_pro_1.pinyin)(c, { pattern: 'first' }))
25
+ .join(''))
26
+ .filter((s) => s.length > 0)
27
+ .join('-');
15
28
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saihu/common",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
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": {