openapi-ts-request 1.12.4 → 1.12.6

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.
@@ -752,6 +752,7 @@ class ServiceGenerator {
752
752
  autoescape: false,
753
753
  });
754
754
  env.addFilter('capitalizeFirst', util_2.capitalizeFirstLetter);
755
+ env.addFilter('escapeJs', util_2.escapeStringForJs);
755
756
  const destPath = (0, path_1.join)(this.config.serversPath, fileName);
756
757
  const destCode = nunjucks_1.default.renderString(template, Object.assign({ disableTypeCheck: false }, processedParams));
757
758
  let mergerProps = {};
@@ -29,6 +29,11 @@ export declare function resolveRefs(obj: OpenAPIObject, fields: string[]): unkno
29
29
  export declare function isAllNumeric(arr: any): boolean;
30
30
  export declare function isAllNumber(arr: any): boolean;
31
31
  export declare function capitalizeFirstLetter(str: string): string;
32
+ /**
33
+ * 转义字符串,用于 JavaScript 字符串字面量
34
+ * 将换行符、单引号等特殊字符转义为转义序列
35
+ */
36
+ export declare function escapeStringForJs(str: string): string;
32
37
  export declare const parseDescriptionEnum: (description: string) => Map<number, string>;
33
38
  /**
34
39
  * 通过自定义正则表达式解析 description 中的枚举翻译
@@ -24,6 +24,7 @@ exports.resolveRefs = resolveRefs;
24
24
  exports.isAllNumeric = isAllNumeric;
25
25
  exports.isAllNumber = isAllNumber;
26
26
  exports.capitalizeFirstLetter = capitalizeFirstLetter;
27
+ exports.escapeStringForJs = escapeStringForJs;
27
28
  const tslib_1 = require("tslib");
28
29
  const lodash_1 = require("lodash");
29
30
  const reserved_words_1 = tslib_1.__importDefault(require("reserved-words"));
@@ -400,6 +401,20 @@ function isAllNumber(arr) {
400
401
  function capitalizeFirstLetter(str) {
401
402
  return str.replace(/^[a-z]/, (match) => match.toUpperCase());
402
403
  }
404
+ /**
405
+ * 转义字符串,用于 JavaScript 字符串字面量
406
+ * 将换行符、单引号等特殊字符转义为转义序列
407
+ */
408
+ function escapeStringForJs(str) {
409
+ if (!str)
410
+ return str;
411
+ return str
412
+ .replace(/\\/g, '\\\\') // 先转义反斜杠
413
+ .replace(/'/g, "\\'") // 转义单引号
414
+ .replace(/\n/g, '\\n') // 转义换行符
415
+ .replace(/\r/g, '\\r') // 转义回车符
416
+ .replace(/\t/g, '\\t'); // 转义制表符
417
+ }
403
418
  // 解析 description 中的枚举翻译
404
419
  const parseDescriptionEnum = (description) => {
405
420
  const enumMap = new Map();
package/dist/util.js CHANGED
@@ -254,7 +254,15 @@ function translateChineseModuleNodeToEnglish(openAPI) {
254
254
  });
255
255
  });
256
256
  resolve(translateMap);
257
- void writeFileAsync(process.cwd() + '/openapi-ts-request.cache.json', JSON.stringify(translateMap, null, 2));
257
+ // 在写入前再次读取缓存,合并多个任务的翻译结果
258
+ const existingContent = readFileSafelySync(process.cwd() + '/openapi-ts-request.cache.json');
259
+ let existingCache = {};
260
+ if (existingContent !== null && isJSONString(existingContent)) {
261
+ existingCache = JSON.parse(existingContent);
262
+ }
263
+ // 合并现有缓存和新的翻译结果(新结果优先)
264
+ const mergedCache = Object.assign(Object.assign({}, existingCache), translateMap);
265
+ void writeFileAsync(process.cwd() + '/openapi-ts-request.cache.json', JSON.stringify(mergedCache, null, 2));
258
266
  })
259
267
  .catch(() => {
260
268
  reject(false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.12.4",
3
+ "version": "1.12.6",
4
4
  "description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",
@@ -23,7 +23,7 @@ import * as {{ namespace }} from './{{ interfaceFileName }}';
23
23
  {%- if p["$ref"] and not p.name %}
24
24
  display{{ p.type | safe }}(field as keyof {{ namespace }}.{{ p.type }})
25
25
  {%- else %}
26
- {{ p.name }}: '{{ p.desc if p.desc else p.name }}',
26
+ {{ p.name }}: '{{ (p.desc if p.desc else p.name) | escapeJs }}',
27
27
  {%- endif %}
28
28
  {%- endfor %}
29
29
  {%- if prop.length > 1 %}