beer-swagger 1.0.3 → 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 +23 -18
  2. package/package.json +1 -1
package/generate-apis.js CHANGED
@@ -116,8 +116,8 @@ function appendParamSuffix(baseName, paramSuffix) {
116
116
  return `${baseName}${paramSuffix}`;
117
117
  }
118
118
 
119
- // Add a verb for single-segment paths when needed.
120
- function addVerbForSingleSegment(name, method) {
119
+ // Add a verb when the HTTP method should be reflected in the name.
120
+ function addVerbForSingleSegment(name, method, hasPathParams = false) {
121
121
  const lower = name.toLowerCase();
122
122
  if (KEEP_METHOD_NAMES.has(lower)) {
123
123
  return name;
@@ -126,6 +126,9 @@ function addVerbForSingleSegment(name, method) {
126
126
  const suffix = name[0].toUpperCase() + name.slice(1);
127
127
  return `removeBy${suffix}`;
128
128
  }
129
+ if ((method === 'post' || method === 'put') && !hasPathParams) {
130
+ return name;
131
+ }
129
132
  const verbMap = {
130
133
  get: 'get',
131
134
  post: 'save',
@@ -238,9 +241,9 @@ function mapSchemaToType(schema, schemas, useTypes) {
238
241
  return `${itemType}[]`;
239
242
  }
240
243
  if (type === 'object') {
241
- return '{}';
244
+ return 'object';
242
245
  }
243
- return '{}';
246
+ return 'object';
244
247
  }
245
248
 
246
249
  // Map parameter schema to a type for method signatures.
@@ -281,9 +284,9 @@ function mapParamSchemaToType(schema, schemas, useTypes) {
281
284
  return '[]';
282
285
  }
283
286
  if (type === 'object') {
284
- return '{}';
287
+ return 'object';
285
288
  }
286
- return '{}';
289
+ return 'object';
287
290
  }
288
291
 
289
292
  // Resolve default response type from Swagger responses.
@@ -453,7 +456,8 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
453
456
  const pathSegments = p.split('/')
454
457
  .filter(Boolean)
455
458
  .filter((part) => !part.startsWith('{'));
456
- 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;
457
461
  }
458
462
  if (!KEEP_METHOD_NAMES.has(name.toLowerCase())) {
459
463
  name = fixVerbCase(name);
@@ -615,7 +619,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
615
619
 
616
620
  if (entryMethod === 'get') {
617
621
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
618
- methods.push(` return this.get<T>(${pathLiteral}, ${paramsObject});`);
622
+ methods.push(` return await this.get<T>(${pathLiteral}, ${paramsObject});`);
619
623
  methods.push(' }');
620
624
  methods.push('');
621
625
  continue;
@@ -624,12 +628,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
624
628
  if (entryMethod === 'delete') {
625
629
  if (entryHasBody) {
626
630
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
627
- methods.push(` return this.deleteBody<T>(${pathLiteral}, ${paramsObject}, body);`);
631
+ methods.push(` return await this.deleteBody<T>(${pathLiteral}, ${paramsObject}, body);`);
628
632
  methods.push(' }');
629
633
  methods.push('');
630
634
  } else {
631
635
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
632
- methods.push(` return this.delete<T>(${pathLiteral}, ${paramsObject});`);
636
+ methods.push(` return await this.delete<T>(${pathLiteral}, ${paramsObject});`);
633
637
  methods.push(' }');
634
638
  methods.push('');
635
639
  }
@@ -639,12 +643,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
639
643
  if (entryMethod === 'post') {
640
644
  if (entryHasBody) {
641
645
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
642
- methods.push(` return this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
646
+ methods.push(` return await this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
643
647
  methods.push(' }');
644
648
  methods.push('');
645
649
  } else {
646
650
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
647
- methods.push(` return this.post<T>(${pathLiteral}, ${paramsObject});`);
651
+ methods.push(` return await this.post<T>(${pathLiteral}, ${paramsObject});`);
648
652
  methods.push(' }');
649
653
  methods.push('');
650
654
  }
@@ -654,12 +658,12 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
654
658
  if (entryMethod === 'put') {
655
659
  if (entryHasBody) {
656
660
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
657
- methods.push(` return this.putBody<T>(${pathLiteral}, ${paramsObject}, body);`);
661
+ methods.push(` return await this.putBody<T>(${pathLiteral}, ${paramsObject}, body);`);
658
662
  methods.push(' }');
659
663
  methods.push('');
660
664
  } else {
661
665
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
662
- methods.push(` return this.put<T>(${pathLiteral}, ${paramsObject});`);
666
+ methods.push(` return await this.put<T>(${pathLiteral}, ${paramsObject});`);
663
667
  methods.push(' }');
664
668
  methods.push('');
665
669
  }
@@ -667,7 +671,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
667
671
  }
668
672
 
669
673
  methods.push(` public async ${name}<T = ${defaultType}>(${sigParts}): Promise<JsonResponse<T>> {`);
670
- methods.push(` return this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
674
+ methods.push(` return await this.postBody<T>(${pathLiteral}, ${paramsObject}, body);`);
671
675
  methods.push(' }');
672
676
  methods.push('');
673
677
  }
@@ -699,7 +703,8 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
699
703
  lines.push(' * To update, run the generate-apis.js script.');
700
704
  lines.push(' */');
701
705
  const fetchImport = clientType === 'WeChatMiniProgram' ? './fetch' : 'beer-network/api';
702
- lines.push(`import Fetch, { JsonResponse } from '${fetchImport}';`);
706
+ lines.push(`import type { JsonResponse } from '${fetchImport}';`);
707
+ lines.push(`import Fetch from '${fetchImport}';`);
703
708
  if (fileBase) {
704
709
  lines.push(`import type * as Types from './types/${fileBase}-type';`);
705
710
  }
@@ -709,7 +714,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
709
714
  lines.push(' public readonly pathPrefix: string;');
710
715
  lines.push(...apiProps);
711
716
  lines.push(' constructor(pathPrefix = \'\') {');
712
- lines.push(` this.pathPrefix = (pathPrefix ?? '') + '${defaultPathPrefix}';`);
717
+ lines.push(` this.pathPrefix = \`\${pathPrefix ?? ''}${defaultPathPrefix}\`;`);
713
718
  lines.push(...apiInit);
714
719
  lines.push(' }');
715
720
  lines.push('}');
@@ -719,7 +724,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
719
724
  lines.push(` if (!${cacheName}.has(pathPrefix)) {`);
720
725
  lines.push(` ${cacheName}.set(pathPrefix, new ${apiClassName}(pathPrefix));`);
721
726
  lines.push(' }');
722
- lines.push(` return ${cacheName}.get(pathPrefix)!;`);
727
+ lines.push(` return ${cacheName}.get(pathPrefix) ?? (() => { throw new Error('API instance not found'); })();`);
723
728
  lines.push('}');
724
729
  lines.push('');
725
730
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beer-swagger",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "bin": {
5
5
  "beer-swagger": "./generate-apis.js"
6
6
  },