swagger-typescript-api 13.6.11 → 13.7.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.
@@ -208,7 +208,7 @@ var ComponentTypeNameResolver = class extends NameResolver {
208
208
  //#endregion
209
209
  //#region package.json
210
210
  var name = "swagger-typescript-api";
211
- var version = "13.6.11";
211
+ var version = "13.7.0";
212
212
  var description = "Generate the API client for Fetch or Axios from an OpenAPI Specification";
213
213
  //#endregion
214
214
  //#region src/constants.ts
@@ -373,6 +373,7 @@ var CodeGenConfig = class {
373
373
  httpClientType = HTTP_CLIENT.FETCH;
374
374
  unwrapResponseData = false;
375
375
  disableThrowOnError = false;
376
+ disableFormatTypeNames = false;
376
377
  sortTypes = false;
377
378
  sortRoutes = false;
378
379
  templatePaths = {
@@ -405,6 +406,7 @@ var CodeGenConfig = class {
405
406
  silent = false;
406
407
  typePrefix = "";
407
408
  typeSuffix = "";
409
+ typeNameSeparator = "_";
408
410
  enumKeyPrefix = "";
409
411
  enumKeySuffix = "";
410
412
  patch = false;
@@ -3203,39 +3205,119 @@ var JavascriptTranslator = class extends Translator {
3203
3205
  //#region src/type-name-formatter.ts
3204
3206
  var TypeNameFormatter = class {
3205
3207
  formattedModelNamesMap = /* @__PURE__ */ new Map();
3208
+ usedFormattedTypeNames = /* @__PURE__ */ new Map();
3206
3209
  config;
3207
3210
  constructor(config) {
3208
3211
  this.config = config;
3209
3212
  }
3213
+ /**
3214
+ * Return the TypeScript identifier for a raw OpenAPI name. Fast path is a
3215
+ * cache hit on names resolved by `precommit`. The fallback (for names
3216
+ * discovered after precommit, e.g. enum keys inside schemas or types added
3217
+ * dynamically by `extractEnums`/`extractResponses`) computes the identifier
3218
+ * inline WITHOUT collision handling — collision resolution is the sole
3219
+ * responsibility of `precommit`. Callers expecting collision safety must
3220
+ * list every raw name in the precommit input.
3221
+ */
3210
3222
  format = (name, options = {}) => {
3211
3223
  const schemaType = options.type ?? "type-name";
3212
- const typePrefix = schemaType === "enum-key" ? this.config.enumKeyPrefix : this.config.typePrefix;
3213
- const typeSuffix = schemaType === "enum-key" ? this.config.enumKeySuffix : this.config.typeSuffix;
3224
+ const { typePrefix, typeSuffix } = this.getAffixes(schemaType);
3214
3225
  const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
3226
+ const cached = this.formattedModelNamesMap.get(hashKey);
3227
+ if (cached !== void 0) return cached;
3215
3228
  if (typeof name !== "string") {
3216
3229
  consola.consola.warn("wrong model name", name);
3217
3230
  return name;
3218
3231
  }
3219
- if (/^(?!\d)([A-Z0-9_]{1,})$/g.test(name)) return (0, es_toolkit.compact)([
3220
- typePrefix,
3221
- name,
3222
- typeSuffix
3223
- ]).join("_");
3224
- if (this.formattedModelNamesMap.has(hashKey)) return this.formattedModelNamesMap.get(hashKey);
3225
- const formattedName = (0, es_toolkit_compat.startCase)(`${typePrefix}_${this.fixModelName(name, { type: schemaType })}_${typeSuffix}`).replace(/\s/g, "");
3226
- const formattedResultName = this.config.hooks.onFormatTypeName?.(formattedName, name, schemaType) || formattedName;
3227
- this.formattedModelNamesMap.set(hashKey, formattedResultName);
3228
- return formattedResultName;
3232
+ const result = this.computeFormattedName(name, schemaType, typePrefix, typeSuffix);
3233
+ this.formattedModelNamesMap.set(hashKey, result);
3234
+ return result;
3235
+ };
3236
+ /**
3237
+ * Resolve the TypeScript identifier for every raw schema name passed in,
3238
+ * populating `formattedModelNamesMap` and `usedFormattedTypeNames`. Must be
3239
+ * called once, before any `format()` call from the schema parser, so that
3240
+ * every subsequent `format()` for these names is a cache hit and returns
3241
+ * the collision-resolved identifier.
3242
+ *
3243
+ * Two passes:
3244
+ * 1. Claim every raw name whose formatted output equals the raw name
3245
+ * itself ("canonical"). User-declared identifiers like `FooBar` or
3246
+ * `FooBar1` are preserved regardless of the source order in which
3247
+ * the generator later visits them.
3248
+ * 2. For non-canonical names (e.g. `Foo_Bar` → `FooBar`), suffix with
3249
+ * the smallest integer not already claimed, so collisions produce
3250
+ * `FooBar`, `FooBar1`, `FooBar2`, … deterministically.
3251
+ *
3252
+ * Only type-names go through collision resolution here. Enum keys use the
3253
+ * fallback path in `format()` and are handled per-enum by the template.
3254
+ */
3255
+ precommit = (rawNames) => {
3256
+ const schemaType = "type-name";
3257
+ const { typePrefix, typeSuffix } = this.getAffixes(schemaType);
3258
+ const seen = /* @__PURE__ */ new Set();
3259
+ const names = [];
3260
+ for (const name of rawNames) {
3261
+ if (typeof name !== "string") continue;
3262
+ if (seen.has(name)) continue;
3263
+ seen.add(name);
3264
+ names.push(name);
3265
+ }
3266
+ for (const name of names) {
3267
+ const formatted = this.computeFormattedName(name, schemaType, typePrefix, typeSuffix);
3268
+ if (name !== formatted) continue;
3269
+ if (this.usedFormattedTypeNames.has(formatted)) continue;
3270
+ this.usedFormattedTypeNames.set(formatted, name);
3271
+ const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
3272
+ this.formattedModelNamesMap.set(hashKey, formatted);
3273
+ }
3274
+ for (const name of names) {
3275
+ const hashKey = `${typePrefix}_${name}_${typeSuffix}`;
3276
+ if (this.formattedModelNamesMap.has(hashKey)) continue;
3277
+ const formatted = this.computeFormattedName(name, schemaType, typePrefix, typeSuffix);
3278
+ let final = formatted;
3279
+ if (this.usedFormattedTypeNames.has(final)) {
3280
+ let suffix = 1;
3281
+ while (this.usedFormattedTypeNames.has(`${formatted}${suffix}`)) suffix += 1;
3282
+ final = `${formatted}${suffix}`;
3283
+ }
3284
+ this.usedFormattedTypeNames.set(final, name);
3285
+ this.formattedModelNamesMap.set(hashKey, final);
3286
+ }
3229
3287
  };
3230
3288
  isValidName = (name) => /^([A-Za-z$_]{1,})$/g.test(name);
3231
3289
  fixModelName = (name, options) => {
3232
3290
  if (!this.isValidName(name)) {
3233
3291
  if (!/^[a-zA-Z_$]/g.test(name)) return `${options.type === "enum-key" ? this.config.fixInvalidEnumKeyPrefix : this.config.fixInvalidTypeNamePrefix} ${name}`;
3234
3292
  if (name.includes(".")) return name.replace(/Exclude_keyof[A-Za-z]+/g, () => "ExcludeKeys").replace(/%22~AND~%22/g, "And").replace(/%22~OR~%22/g, "Or").replace(/(\.?%22)|\./g, "_").replace(/__+$/, "");
3235
- if (name.includes("-")) return (0, es_toolkit_compat.startCase)(name).replace(/ /g, "");
3293
+ if (name.includes("-")) return this.config.disableFormatTypeNames ? name.replace(/-/g, "_") : (0, es_toolkit_compat.startCase)(name).replace(/ /g, "");
3236
3294
  }
3237
3295
  return name;
3238
3296
  };
3297
+ getAffixes = (schemaType) => ({
3298
+ typePrefix: schemaType === "enum-key" ? this.config.enumKeyPrefix : this.config.typePrefix,
3299
+ typeSuffix: schemaType === "enum-key" ? this.config.enumKeySuffix : this.config.typeSuffix
3300
+ });
3301
+ computeFormattedName = (name, schemaType, typePrefix, typeSuffix) => {
3302
+ const typeNameSeparator = this.config.typeNameSeparator;
3303
+ let resultName = name;
3304
+ if (this.config.disableFormatTypeNames) resultName = (0, es_toolkit.compact)([
3305
+ typePrefix,
3306
+ resultName,
3307
+ typeSuffix
3308
+ ]).join(typeNameSeparator);
3309
+ else if (/^(?!\d)([A-Z0-9_]{1,})$/g.test(resultName)) resultName = (0, es_toolkit.compact)([
3310
+ typePrefix,
3311
+ resultName,
3312
+ typeSuffix
3313
+ ]).join(typeNameSeparator);
3314
+ else resultName = (0, es_toolkit_compat.startCase)((0, es_toolkit.compact)([
3315
+ typePrefix,
3316
+ this.fixModelName(resultName, { type: schemaType }),
3317
+ typeSuffix
3318
+ ]).join(typeNameSeparator)).replace(/\s/g, "");
3319
+ return this.config.hooks.onFormatTypeName?.(resultName, name, schemaType) || resultName;
3320
+ };
3239
3321
  };
3240
3322
  //#endregion
3241
3323
  //#region src/util/file-system.ts
@@ -3354,7 +3436,9 @@ var CodeGenProcess = class {
3354
3436
  ]), rawTypeData);
3355
3437
  this.schemaComponentsMap.discriminatorsFirst();
3356
3438
  this.schemaComponentsMap.enumsFirst();
3357
- const parsedSchemas = this.schemaComponentsMap.filter((0, es_toolkit.compact)(["schemas", this.config.extractResponses && "responses"])).map((schemaComponent) => {
3439
+ const componentsToParse = this.schemaComponentsMap.filter((0, es_toolkit.compact)(["schemas", this.config.extractResponses && "responses"]));
3440
+ this.typeNameFormatter.precommit(componentsToParse.map((c) => c.typeName));
3441
+ const parsedSchemas = componentsToParse.map((schemaComponent) => {
3358
3442
  const parsed = this.schemaParserFabric.parseSchema(schemaComponent.rawTypeData, schemaComponent.typeName);
3359
3443
  schemaComponent.typeData = parsed;
3360
3444
  return parsed;
@@ -3817,4 +3901,4 @@ Object.defineProperty(exports, "version", {
3817
3901
  }
3818
3902
  });
3819
3903
 
3820
- //# sourceMappingURL=src-D7B_7nSd.cjs.map
3904
+ //# sourceMappingURL=src-DTdmNLxQ.cjs.map