swaggie 2.1.2 → 2.1.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.
@@ -67,8 +67,13 @@ const MOCK_FILE_HEADER = `\
67
67
  .map((name) => {
68
68
  const fullName = servicePrefix + name;
69
69
  const camelName = _case.camel.call(void 0, fullName);
70
+ // `default` is a JS reserved word and cannot be used as a standalone
71
+ // `export const` identifier. The hooks templates rename it to `main`
72
+ // (see genOperations.ts — hooksCamelCaseName). Mock code that references
73
+ // the hooks namespace must use the same safe name.
74
+ const hooksCamelName = camelName === 'default' ? 'main' : camelName;
70
75
  const ops = _genOperations.prepareOperations.call(void 0, groups[name], options, spec.components);
71
- return { name, camelName, ops };
76
+ return { name, camelName, hooksCamelName, ops };
72
77
  })
73
78
  .filter((g) => g.ops.length > 0);
74
79
 
@@ -399,16 +404,16 @@ function buildCreateSwrHookMocks(
399
404
  const spy = spyFn(fw);
400
405
  const lines = ['export function createApiHookMocks() {', ' return {'];
401
406
 
402
- for (const { camelName, ops } of groups) {
407
+ for (const { hooksCamelName, ops } of groups) {
403
408
  const getOps = ops.filter((o) => o.method === 'GET');
404
409
  const mutOps = ops.filter((o) => o.method !== 'GET');
405
410
 
406
- lines.push(` ${camelName}: {`);
411
+ lines.push(` ${hooksCamelName}: {`);
407
412
  lines.push(' queries: {');
408
413
  for (const op of getOps) {
409
414
  const hookName = toHookName(op.name, 'use');
410
415
  lines.push(
411
- ` ${hookName}: withMockSWR(${spy}(${hooksAlias}.${camelName}.queries, '${hookName}').mockReturnValue(defaultSWRReturn)),`
416
+ ` ${hookName}: withMockSWR(${spy}(${hooksAlias}.${hooksCamelName}.queries, '${hookName}').mockReturnValue(defaultSWRReturn)),`
412
417
  );
413
418
  }
414
419
  lines.push(' },');
@@ -416,7 +421,7 @@ function buildCreateSwrHookMocks(
416
421
  for (const op of mutOps) {
417
422
  const hookName = 'use' + _case.pascal.call(void 0, op.name);
418
423
  lines.push(
419
- ` ${hookName}: withMockSWRMutation(${spy}(${hooksAlias}.${camelName}.mutations, '${hookName}').mockReturnValue(defaultSWRMutationReturn as any)),`
424
+ ` ${hookName}: withMockSWRMutation(${spy}(${hooksAlias}.${hooksCamelName}.mutations, '${hookName}').mockReturnValue(defaultSWRMutationReturn as any)),`
420
425
  );
421
426
  }
422
427
  lines.push(' },');
@@ -436,16 +441,16 @@ function buildCreateTsqHookMocks(
436
441
  const spy = spyFn(fw);
437
442
  const lines = ['export function createApiHookMocks() {', ' return {'];
438
443
 
439
- for (const { camelName, ops } of groups) {
444
+ for (const { hooksCamelName, ops } of groups) {
440
445
  const getOps = ops.filter((o) => o.method === 'GET');
441
446
  const mutOps = ops.filter((o) => o.method !== 'GET');
442
447
 
443
- lines.push(` ${camelName}: {`);
448
+ lines.push(` ${hooksCamelName}: {`);
444
449
  lines.push(' queries: {');
445
450
  for (const op of getOps) {
446
451
  const hookName = toHookName(op.name, 'use');
447
452
  lines.push(
448
- ` ${hookName}: withMockQuery(${spy}(${hooksAlias}.${camelName}.queries, '${hookName}').mockReturnValue(defaultQueryReturn as any)),`
453
+ ` ${hookName}: withMockQuery(${spy}(${hooksAlias}.${hooksCamelName}.queries, '${hookName}').mockReturnValue(defaultQueryReturn as any)),`
449
454
  );
450
455
  }
451
456
  lines.push(' },');
@@ -453,7 +458,7 @@ function buildCreateTsqHookMocks(
453
458
  for (const op of mutOps) {
454
459
  const hookName = 'use' + _case.pascal.call(void 0, op.name);
455
460
  lines.push(
456
- ` ${hookName}: withMockMutation(${spy}(${hooksAlias}.${camelName}.mutations, '${hookName}').mockReturnValue(defaultMutationReturn as any)),`
461
+ ` ${hookName}: withMockMutation(${spy}(${hooksAlias}.${hooksCamelName}.mutations, '${hookName}').mockReturnValue(defaultMutationReturn as any)),`
457
462
  );
458
463
  }
459
464
  lines.push(' },');
@@ -623,23 +623,36 @@ const PRIMITIVES =
623
623
  * and inline object types (including PascalCase names used as property value types
624
624
  * inside `{ key: SomeType }` shapes).
625
625
  *
626
- * @example prefixApiType('Pet') 'API.Pet'
627
- * @example prefixApiType('Pet[]') → 'API.Pet[]'
628
- * @example prefixApiType('Pet | null') → 'API.Pet | null'
629
- * @example prefixApiType('unknown') → 'unknown'
630
- * @example prefixApiType('{ id: number }') → '{ id: number }'
631
- * @example prefixApiType('{ profile: MyEnum; }') → '{ profile: API.MyEnum; }'
632
- * @example prefixApiType('API.Pet') → 'API.Pet' (API itself is in the exclude list)
626
+ * Quoted string literals (single or double) are left entirely untouched so that
627
+ * enum-derived types like `"AZURE" | "AWS"` are never incorrectly rewritten to
628
+ * `"API.AZURE" | "API.AWS"`.
629
+ *
630
+ * @example prefixApiType('Pet') → 'API.Pet'
631
+ * @example prefixApiType('Pet[]') → 'API.Pet[]'
632
+ * @example prefixApiType('Pet | null') → 'API.Pet | null'
633
+ * @example prefixApiType('unknown') → 'unknown'
634
+ * @example prefixApiType('{ id: number }') → '{ id: number }'
635
+ * @example prefixApiType('{ profile: MyEnum; }') → '{ profile: API.MyEnum; }'
636
+ * @example prefixApiType('API.Pet') → 'API.Pet'
637
+ * @example prefixApiType('"AZURE" | "AWS"') → '"AZURE" | "AWS"'
638
+ * @example prefixApiType('"available" | Pet') → '"available" | API.Pet'
633
639
  */
634
640
  function prefixApiType(typeStr) {
635
641
  if (!typeStr) {
636
642
  return typeStr;
637
643
  }
638
- // The negative lookbehind `(?<!\.)` skips identifiers that are already part
639
- // of a namespace reference (e.g. `API.Pet` when the regex reaches `Pet` it
640
- // is preceded by `.`, so we leave it alone).
641
- return typeStr.replace(/(?<!\.)\b([A-Z][A-Za-z0-9_]*)\b/g, (match) =>
642
- PRIMITIVES.test(match) ? match : `API.${match}`
644
+ // The alternation has two branches:
645
+ // Group 1: a full quoted string literal ("..." or '...') returned as-is so
646
+ // that enum-derived values like "AZURE" are never prefixed.
647
+ // Group 2: an uppercase-starting identifier that is NOT already preceded by a
648
+ // dot (negative lookbehind) prefixed with API. unless it is a known
649
+ // primitive / built-in.
650
+ return typeStr.replace(
651
+ /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')|(?<!\.)\b([A-Z][A-Za-z0-9_]*)\b/g,
652
+ (match, quotedLiteral, ident) => {
653
+ if (quotedLiteral !== undefined) return quotedLiteral;
654
+ return PRIMITIVES.test(ident) ? ident : `API.${ident}`;
655
+ },
643
656
  );
644
657
  } exports.prefixApiType = prefixApiType;
645
658
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggie",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "Generate a fully typed TypeScript API client from your OpenAPI 3 spec",
5
5
  "author": {
6
6
  "name": "Piotr Dabrowski",
@@ -72,16 +72,16 @@
72
72
  "dependencies": {
73
73
  "case": "^1.6.3",
74
74
  "commander": "^14.0.3",
75
- "eta": "^4.5.1",
76
- "yaml": "^2.8.3",
75
+ "eta": "^4.6.0",
76
+ "yaml": "^2.9.0",
77
77
  "picocolors": "^1.1.1"
78
78
  },
79
79
  "devDependencies": {
80
- "bun-types": "1.3.11",
80
+ "bun-types": "1.3.14",
81
81
  "openapi-types": "^12.1.3",
82
82
  "sucrase": "3.35.1",
83
- "typescript": "6.0.2",
83
+ "typescript": "6.0.3",
84
84
  "vitepress": "^2.0.0-alpha.17",
85
- "vitepress-plugin-tabs": "0.8.0"
85
+ "vitepress-plugin-tabs": "0.9.0"
86
86
  }
87
87
  }