beer-swagger 1.0.2 → 1.0.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.
Files changed (2) hide show
  1. package/generate-apis.js +40 -20
  2. package/package.json +1 -1
package/generate-apis.js CHANGED
@@ -105,8 +105,19 @@ function toParamSuffix(params, nameMap) {
105
105
  .join('');
106
106
  }
107
107
 
108
- // Add a verb for single-segment paths when needed.
109
- function addVerbForSingleSegment(name, method) {
108
+ // Avoid duplicate "By" when the base method name already ends with it.
109
+ function appendParamSuffix(baseName, paramSuffix) {
110
+ if (!paramSuffix) {
111
+ return baseName;
112
+ }
113
+ if (baseName.endsWith('By') && paramSuffix.startsWith('By')) {
114
+ return baseName + paramSuffix.slice(2);
115
+ }
116
+ return `${baseName}${paramSuffix}`;
117
+ }
118
+
119
+ // Add a verb when the HTTP method should be reflected in the name.
120
+ function addVerbForSingleSegment(name, method, hasPathParams = false) {
110
121
  const lower = name.toLowerCase();
111
122
  if (KEEP_METHOD_NAMES.has(lower)) {
112
123
  return name;
@@ -115,6 +126,9 @@ function addVerbForSingleSegment(name, method) {
115
126
  const suffix = name[0].toUpperCase() + name.slice(1);
116
127
  return `removeBy${suffix}`;
117
128
  }
129
+ if ((method === 'post' || method === 'put') && !hasPathParams) {
130
+ return name;
131
+ }
118
132
  const verbMap = {
119
133
  get: 'get',
120
134
  post: 'save',
@@ -227,9 +241,9 @@ function mapSchemaToType(schema, schemas, useTypes) {
227
241
  return `${itemType}[]`;
228
242
  }
229
243
  if (type === 'object') {
230
- return '{}';
244
+ return 'object';
231
245
  }
232
- return '{}';
246
+ return 'object';
233
247
  }
234
248
 
235
249
  // Map parameter schema to a type for method signatures.
@@ -270,9 +284,9 @@ function mapParamSchemaToType(schema, schemas, useTypes) {
270
284
  return '[]';
271
285
  }
272
286
  if (type === 'object') {
273
- return '{}';
287
+ return 'object';
274
288
  }
275
- return '{}';
289
+ return 'object';
276
290
  }
277
291
 
278
292
  // Resolve default response type from Swagger responses.
@@ -433,12 +447,17 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
433
447
  if (name === 'unnamed') {
434
448
  name = toCamel(opId);
435
449
  }
450
+ const pathParams = getParams(op).filter((p) => p.in === 'path');
451
+ if (method === 'delete' && pathParams.length === 1) {
452
+ name = 'removeBy';
453
+ }
436
454
  if (!KEEP_METHOD_NAMES.has(name.toLowerCase()) && isAllLowerWord(name)) {
437
455
  const inferred = pathToName(p);
438
456
  const pathSegments = p.split('/')
439
457
  .filter(Boolean)
440
458
  .filter((part) => !part.startsWith('{'));
441
- name = pathSegments.length === 1 ? addVerbForSingleSegment(inferred, method) : inferred;
459
+ const shouldAddVerb = pathSegments.length === 1 || ((method === 'post' || method === 'put') && pathParams.length > 0);
460
+ name = shouldAddVerb ? addVerbForSingleSegment(inferred, method, pathParams.length > 0) : inferred;
442
461
  }
443
462
  if (!KEEP_METHOD_NAMES.has(name.toLowerCase())) {
444
463
  name = fixVerbCase(name);
@@ -509,7 +528,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
509
528
  if (seen.has(name)) {
510
529
  const paramSuffix = toParamSuffix(entryParams, paramNameMap);
511
530
  if (paramSuffix) {
512
- name = `${entryName}${paramSuffix}`;
531
+ name = appendParamSuffix(entryName, paramSuffix);
513
532
  }
514
533
  }
515
534
  if (seen.has(name)) {
@@ -600,7 +619,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
600
619
 
601
620
  if (entryMethod === 'get') {
602
621
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
603
- methods.push(` return this.get<T>(${pathLiteral}, ${paramsObject});`);
622
+ methods.push(` return await this.get<T>(${pathLiteral}, ${paramsObject});`);
604
623
  methods.push(' }');
605
624
  methods.push('');
606
625
  continue;
@@ -609,12 +628,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
609
628
  if (entryMethod === 'delete') {
610
629
  if (entryHasBody) {
611
630
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
612
- methods.push(` return this.deleteBody<T>(${pathLiteral}, ${paramsObject}, body);`);
631
+ methods.push(` return await this.deleteBody<T>(${pathLiteral}, ${paramsObject}, body);`);
613
632
  methods.push(' }');
614
633
  methods.push('');
615
634
  } else {
616
635
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
617
- methods.push(` return this.delete<T>(${pathLiteral}, ${paramsObject});`);
636
+ methods.push(` return await this.delete<T>(${pathLiteral}, ${paramsObject});`);
618
637
  methods.push(' }');
619
638
  methods.push('');
620
639
  }
@@ -624,12 +643,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
624
643
  if (entryMethod === 'post') {
625
644
  if (entryHasBody) {
626
645
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
627
- methods.push(` return this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
646
+ methods.push(` return await this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
628
647
  methods.push(' }');
629
648
  methods.push('');
630
649
  } else {
631
650
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
632
- methods.push(` return this.post<T>(${pathLiteral}, ${paramsObject});`);
651
+ methods.push(` return await this.post<T>(${pathLiteral}, ${paramsObject});`);
633
652
  methods.push(' }');
634
653
  methods.push('');
635
654
  }
@@ -639,12 +658,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
639
658
  if (entryMethod === 'put') {
640
659
  if (entryHasBody) {
641
660
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
642
- methods.push(` return this.putBody<T>(${pathLiteral}, ${paramsObject}, body);`);
661
+ methods.push(` return await this.putBody<T>(${pathLiteral}, ${paramsObject}, body);`);
643
662
  methods.push(' }');
644
663
  methods.push('');
645
664
  } else {
646
665
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
647
- methods.push(` return this.put<T>(${pathLiteral}, ${paramsObject});`);
666
+ methods.push(` return await this.put<T>(${pathLiteral}, ${paramsObject});`);
648
667
  methods.push(' }');
649
668
  methods.push('');
650
669
  }
@@ -652,7 +671,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
652
671
  }
653
672
 
654
673
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
655
- methods.push(` return this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
674
+ methods.push(` return await this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
656
675
  methods.push(' }');
657
676
  methods.push('');
658
677
  }
@@ -676,7 +695,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
676
695
 
677
696
  const baseName = toServiceBaseName(serviceName);
678
697
  const apiClassName = `${toPascal(baseName)}Api`;
679
- const factoryName = `get${apiClassName}`;
698
+ const factoryName = apiClassName === 'ApiApi' ? 'getApi' : `get${apiClassName}`;
680
699
  const cacheName = `${baseName}ApiCache`;
681
700
  const lines = [];
682
701
  lines.push('/*');
@@ -684,7 +703,8 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
684
703
  lines.push(' * To update, run the generate-apis.js script.');
685
704
  lines.push(' */');
686
705
  const fetchImport = clientType === 'WeChatMiniProgram' ? './fetch' : 'beer-network/api';
687
- lines.push(`import Fetch, { JsonResponse } from '${fetchImport}';`);
706
+ lines.push(`import type { JsonResponse } from '${fetchImport}';`);
707
+ lines.push(`import Fetch from '${fetchImport}';`);
688
708
  if (fileBase) {
689
709
  lines.push(`import type * as Types from './types/${fileBase}-type';`);
690
710
  }
@@ -694,7 +714,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
694
714
  lines.push(' public readonly pathPrefix: string;');
695
715
  lines.push(...apiProps);
696
716
  lines.push(' constructor(pathPrefix = \'\') {');
697
- lines.push(` this.pathPrefix = (pathPrefix ?? '') + '${defaultPathPrefix}';`);
717
+ lines.push(` this.pathPrefix = \`\${pathPrefix ?? ''}${defaultPathPrefix}\`;`);
698
718
  lines.push(...apiInit);
699
719
  lines.push(' }');
700
720
  lines.push('}');
@@ -704,7 +724,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
704
724
  lines.push(` if (!${cacheName}.has(pathPrefix)) {`);
705
725
  lines.push(` ${cacheName}.set(pathPrefix, new ${apiClassName}(pathPrefix));`);
706
726
  lines.push(' }');
707
- lines.push(` return ${cacheName}.get(pathPrefix)!;`);
727
+ lines.push(` return ${cacheName}.get(pathPrefix) ?? (() => { throw new Error('API instance not found'); })();`);
708
728
  lines.push('}');
709
729
  lines.push('');
710
730
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beer-swagger",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "bin": {
5
5
  "beer-swagger": "./generate-apis.js"
6
6
  },