swagger-typescript-api 11.0.0--beta-1 → 11.0.0--beta-2

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.
package/index.js CHANGED
@@ -202,55 +202,60 @@ const program = cli({
202
202
  const main = async () => {
203
203
  const { command, options } = await program.execute({ args: process.argv });
204
204
 
205
- switch (command) {
206
- case null: {
207
- await generateApi({
208
- name: options.name,
209
- url: options.path,
210
- generateRouteTypes: options.routeTypes,
211
- generateClient: !!(options.axios || options.client),
212
- httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
213
- defaultResponseAsSuccess: options.defaultAsSuccess,
214
- defaultResponseType: options.defaultResponse,
215
- unwrapResponseData: options.unwrapResponseData,
216
- disableThrowOnError: options.disableThrowOnError,
217
- sortTypes: options.sortTypes,
218
- generateUnionEnums: options.unionEnums,
219
- addReadonly: options.addReadonly,
220
- generateResponses: options.responses,
221
- extractRequestParams: !!options.extractRequestParams,
222
- extractRequestBody: !!options.extractRequestBody,
223
- extractResponseBody: !!options.extractResponseBody,
224
- extractResponseError: !!options.extractResponseError,
225
- input: resolve(process.cwd(), options.path),
226
- output: resolve(process.cwd(), options.output || "."),
227
- templates: options.templates,
228
- modular: !!options.modular,
229
- toJS: !!options.js,
230
- enumNamesAsValues: options.enumNamesAsValues,
231
- moduleNameIndex: +(options.moduleNameIndex || 0),
232
- moduleNameFirstTag: options.moduleNameFirstTag,
233
- disableStrictSSL: !!options.disableStrictSSL,
234
- disableProxy: !!options.disableProxy,
235
- singleHttpClient: !!options.singleHttpClient,
236
- cleanOutput: !!options.cleanOutput,
237
- silent: !!options.silent,
238
- typePrefix: options.typePrefix,
239
- typeSuffix: options.typeSuffix,
240
- patch: !!options.patch,
241
- apiClassName: options.apiClassName,
242
- debug: options.debug,
243
- anotherArrayType: options.anotherArrayType,
244
- });
245
- break;
246
- }
247
- case "generate-templates": {
248
- console.info("todo");
249
- break;
250
- }
251
- default: {
252
- break;
205
+ try {
206
+ switch (command) {
207
+ case null: {
208
+ await generateApi({
209
+ name: options.name,
210
+ url: options.path,
211
+ generateRouteTypes: options.routeTypes,
212
+ generateClient: !!(options.axios || options.client),
213
+ httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
214
+ defaultResponseAsSuccess: options.defaultAsSuccess,
215
+ defaultResponseType: options.defaultResponse,
216
+ unwrapResponseData: options.unwrapResponseData,
217
+ disableThrowOnError: options.disableThrowOnError,
218
+ sortTypes: options.sortTypes,
219
+ generateUnionEnums: options.unionEnums,
220
+ addReadonly: options.addReadonly,
221
+ generateResponses: options.responses,
222
+ extractRequestParams: !!options.extractRequestParams,
223
+ extractRequestBody: !!options.extractRequestBody,
224
+ extractResponseBody: !!options.extractResponseBody,
225
+ extractResponseError: !!options.extractResponseError,
226
+ input: resolve(process.cwd(), options.path),
227
+ output: resolve(process.cwd(), options.output || "."),
228
+ templates: options.templates,
229
+ modular: !!options.modular,
230
+ toJS: !!options.js,
231
+ enumNamesAsValues: options.enumNamesAsValues,
232
+ moduleNameIndex: +(options.moduleNameIndex || 0),
233
+ moduleNameFirstTag: options.moduleNameFirstTag,
234
+ disableStrictSSL: !!options.disableStrictSSL,
235
+ disableProxy: !!options.disableProxy,
236
+ singleHttpClient: !!options.singleHttpClient,
237
+ cleanOutput: !!options.cleanOutput,
238
+ silent: !!options.silent,
239
+ typePrefix: options.typePrefix,
240
+ typeSuffix: options.typeSuffix,
241
+ patch: !!options.patch,
242
+ apiClassName: options.apiClassName,
243
+ debug: options.debug,
244
+ anotherArrayType: options.anotherArrayType,
245
+ });
246
+ break;
247
+ }
248
+ case "generate-templates": {
249
+ console.info("todo");
250
+ break;
251
+ }
252
+ default: {
253
+ break;
254
+ }
253
255
  }
256
+ } catch (e) {
257
+ console.error(e);
258
+ process.exit(1);
254
259
  }
255
260
  };
256
261
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "11.0.0--beta-1",
3
+ "version": "11.0.0--beta-2",
4
4
  "description": "Generate typescript/javascript api from swagger schema",
5
5
  "scripts": {
6
6
  "cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
File without changes
@@ -529,13 +529,8 @@ class SchemaRoutes {
529
529
  return schema;
530
530
  };
531
531
 
532
- extractResponseBodyIfItNeeded = (routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName) => {
533
- if (
534
- this.config.extractResponseBody &&
535
- responseBodyInfo.responses.length &&
536
- responseBodyInfo.success &&
537
- responseBodyInfo.success.schema
538
- ) {
532
+ extractResponseBodyIfItNeeded = (routeInfo, responseBodyInfo, routeName) => {
533
+ if (responseBodyInfo.responses.length && responseBodyInfo.success && responseBodyInfo.success.schema) {
539
534
  const typeName = this.config.componentTypeNameResolver.resolve([
540
535
  pascalCase(`${routeName.usage} Data`),
541
536
  pascalCase(`${routeName.usage} Result`),
@@ -552,19 +547,14 @@ class SchemaRoutes {
552
547
  successResponse.type = this.schemaParser.getInlineParseContent(successResponse.schema);
553
548
 
554
549
  if (idx > -1) {
555
- responseBodyInfo.responses[idx] = successResponse.schema;
550
+ _.assign(responseBodyInfo.responses[idx], successResponse.schema);
556
551
  }
557
552
  }
558
553
  }
559
554
  };
560
555
 
561
- extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName) => {
562
- if (
563
- this.config.extractResponseError &&
564
- responseBodyInfo.responses.length &&
565
- responseBodyInfo.error.schemas &&
566
- responseBodyInfo.error.schemas.length
567
- ) {
556
+ extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo, routeName) => {
557
+ if (responseBodyInfo.responses.length && responseBodyInfo.error.schemas && responseBodyInfo.error.schemas.length) {
568
558
  const typeName = this.config.componentTypeNameResolver.resolve([
569
559
  pascalCase(`${routeName.usage} Error`),
570
560
  pascalCase(`${routeName.usage} Fail`),
@@ -595,16 +585,16 @@ class SchemaRoutes {
595
585
  }
596
586
  };
597
587
 
598
- getRouteName = (routeInfo) => {
599
- const { moduleName } = routeInfo;
588
+ getRouteName = (rawRouteInfo) => {
589
+ const { moduleName } = rawRouteInfo;
600
590
  const { routeNameDuplicatesMap, templatesToRender } = this.config;
601
591
  const routeNameTemplate = templatesToRender.routeName;
602
592
 
603
593
  const routeNameFromTemplate = this.templates.renderTemplate(routeNameTemplate, {
604
- routeInfo: routeInfo,
594
+ routeInfo: rawRouteInfo,
605
595
  });
606
596
 
607
- const routeName = this.config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
597
+ const routeName = this.config.hooks.onFormatRouteName(rawRouteInfo, routeNameFromTemplate) || routeNameFromTemplate;
608
598
 
609
599
  const duplicateIdentifier = `${moduleName}|${routeName}`;
610
600
 
@@ -629,7 +619,7 @@ class SchemaRoutes {
629
619
  duplicate: duplicates > 1,
630
620
  };
631
621
 
632
- return this.config.hooks.onCreateRouteName(routeNameInfo, routeInfo) || routeNameInfo;
622
+ return this.config.hooks.onCreateRouteName(routeNameInfo, rawRouteInfo) || routeNameInfo;
633
623
  };
634
624
 
635
625
  parseRouteInfo = (rawRouteName, routeInfo, method, usageSchema, parsedSchemas) => {
@@ -675,6 +665,7 @@ class SchemaRoutes {
675
665
  const responseBodyInfo = this.getResponseBodyInfo(routeInfo, routeParams, parsedSchemas);
676
666
 
677
667
  const rawRouteInfo = {
668
+ ...otherInfo,
678
669
  pathArgs,
679
670
  operationId,
680
671
  method,
@@ -688,7 +679,6 @@ class SchemaRoutes {
688
679
  produces,
689
680
  requestBody,
690
681
  consumes,
691
- ...otherInfo,
692
682
  };
693
683
 
694
684
  const queryObjectSchema = this.convertRouteParamsIntoObject(routeParams.query);
@@ -707,8 +697,12 @@ class SchemaRoutes {
707
697
  routeName,
708
698
  });
709
699
 
710
- this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
711
- this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
700
+ if (this.config.extractResponseBody) {
701
+ this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeName);
702
+ }
703
+ if (this.config.extractResponseError) {
704
+ this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeName);
705
+ }
712
706
 
713
707
  const queryType = routeParams.query.length ? this.schemaParser.getInlineParseContent(queryObjectSchema) : null;
714
708
  const pathType = routeParams.path.length ? this.schemaParser.getInlineParseContent(pathObjectSchema) : null;
@@ -747,29 +741,6 @@ class SchemaRoutes {
747
741
  : void 0,
748
742
  };
749
743
 
750
- let routeArgs = _.compact([...pathArgs, specificArgs.query, specificArgs.body]);
751
-
752
- if (routeArgs.some((pathArg) => pathArg.optional)) {
753
- const { optionalArgs, requiredArgs } = _.reduce(
754
- [...routeArgs],
755
- (acc, pathArg) => {
756
- if (pathArg.optional) {
757
- acc.optionalArgs.push(pathArg);
758
- } else {
759
- acc.requiredArgs.push(pathArg);
760
- }
761
-
762
- return acc;
763
- },
764
- {
765
- optionalArgs: [],
766
- requiredArgs: [],
767
- },
768
- );
769
-
770
- routeArgs = [...requiredArgs, ...optionalArgs];
771
- }
772
-
773
744
  return {
774
745
  id: routeId,
775
746
  namespace: _.replace(moduleName, /^(\d)/, "v$1"),
@@ -819,10 +790,8 @@ class SchemaRoutes {
819
790
 
820
791
  _.forEach(routeInfosMap, (routeInfo, method) => {
821
792
  const parsedRouteInfo = this.parseRouteInfo(rawRouteName, routeInfo, method, usageSchema, parsedSchemas);
822
-
823
- const usageRouteData = this.config.hooks.onCreateRoute(parsedRouteInfo);
824
-
825
- const route = usageRouteData || parsedRouteInfo;
793
+ const processedRouteInfo = this.config.hooks.onCreateRoute(parsedRouteInfo);
794
+ const route = processedRouteInfo || parsedRouteInfo;
826
795
 
827
796
  if (!this.hasSecurityRoutes && route.security) {
828
797
  this.hasSecurityRoutes = route.security;
@@ -834,48 +803,50 @@ class SchemaRoutes {
834
803
  this.hasFormDataRoutes = route.hasFormDataParams;
835
804
  }
836
805
 
837
- this.routes.push(usageRouteData || parsedRouteInfo);
806
+ this.routes.push(route);
838
807
  });
839
808
  });
840
809
  };
841
810
 
842
811
  getGroupedRoutes = () => {
843
- return _.reduce(
844
- this.routes.reduce(
845
- (modules, route) => {
846
- if (route.namespace) {
847
- if (!modules[route.namespace]) {
848
- modules[route.namespace] = [];
849
- }
850
-
851
- modules[route.namespace].push(route);
852
- } else {
853
- modules.$outOfModule.push(route);
812
+ const groupedRoutes = this.routes.reduce(
813
+ (modules, route) => {
814
+ if (route.namespace) {
815
+ if (!modules[route.namespace]) {
816
+ modules[route.namespace] = [];
854
817
  }
855
818
 
856
- return modules;
857
- },
858
- {
859
- $outOfModule: [],
860
- },
861
- ),
862
- (acc, packRoutes, moduleName) => {
819
+ modules[route.namespace].push(route);
820
+ } else {
821
+ modules.$outOfModule.push(route);
822
+ }
823
+
824
+ return modules;
825
+ },
826
+ {
827
+ $outOfModule: [],
828
+ },
829
+ );
830
+
831
+ return _.reduce(
832
+ groupedRoutes,
833
+ (acc, routesGroup, moduleName) => {
863
834
  if (moduleName === "$outOfModule") {
864
- acc.outOfModule = packRoutes;
835
+ acc.outOfModule = routesGroup;
865
836
  } else {
866
837
  if (!acc.combined) acc.combined = [];
867
838
 
868
839
  acc.combined.push({
869
840
  moduleName,
870
- routes: _.map(packRoutes, (route) => {
841
+ routes: _.map(routesGroup, (route) => {
871
842
  const { original: originalName, usage: usageName } = route.routeName;
872
843
 
873
844
  // TODO: https://github.com/acacode/swagger-typescript-api/issues/152
874
845
  // TODO: refactor
875
846
  if (
876
- packRoutes.length > 1 &&
847
+ routesGroup.length > 1 &&
877
848
  usageName !== originalName &&
878
- !_.some(packRoutes, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
849
+ !_.some(routesGroup, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
879
850
  ) {
880
851
  return {
881
852
  ...route,
package/src/templates.js CHANGED
@@ -119,31 +119,31 @@ class Templates {
119
119
  );
120
120
  };
121
121
 
122
+ findTemplateWithExt = (path) => {
123
+ const raw = this.cropExtension(path);
124
+ const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
125
+ return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
126
+ };
127
+
122
128
  getTemplateContent = (path) => {
123
129
  const foundTemplatePathKey = _.keys(this.config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
124
130
 
125
- const findPathWithExt = (path) => {
126
- const raw = this.cropExtension(path);
127
- const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
128
- return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
129
- };
130
-
131
131
  const rawPath = resolve(
132
132
  _.replace(path, `@${foundTemplatePathKey}`, this.config.templatePaths[foundTemplatePathKey]),
133
133
  );
134
- const fixedPath = findPathWithExt(rawPath);
134
+ const fixedPath = this.findTemplateWithExt(rawPath);
135
135
 
136
136
  if (fixedPath) {
137
137
  return this.fileSystem.getFileContent(fixedPath);
138
138
  }
139
139
 
140
- const customPath = findPathWithExt(resolve(this.config.templatePaths.custom, path));
140
+ const customPath = this.findTemplateWithExt(resolve(this.config.templatePaths.custom, path));
141
141
 
142
142
  if (customPath) {
143
143
  return this.fileSystem.getFileContent(customPath);
144
144
  }
145
145
 
146
- const originalPath = findPathWithExt(resolve(this.config.templatePaths.original, path));
146
+ const originalPath = this.findTemplateWithExt(resolve(this.config.templatePaths.original, path));
147
147
 
148
148
  if (originalPath) {
149
149
  return this.fileSystem.getFileContent(originalPath);