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

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-3",
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,17 @@ 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], {
551
+ ...successResponse.schema,
552
+ type: successResponse.type,
553
+ });
556
554
  }
557
555
  }
558
556
  }
559
557
  };
560
558
 
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
- ) {
559
+ extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo, routeName) => {
560
+ if (responseBodyInfo.responses.length && responseBodyInfo.error.schemas && responseBodyInfo.error.schemas.length) {
568
561
  const typeName = this.config.componentTypeNameResolver.resolve([
569
562
  pascalCase(`${routeName.usage} Error`),
570
563
  pascalCase(`${routeName.usage} Fail`),
@@ -595,16 +588,16 @@ class SchemaRoutes {
595
588
  }
596
589
  };
597
590
 
598
- getRouteName = (routeInfo) => {
599
- const { moduleName } = routeInfo;
591
+ getRouteName = (rawRouteInfo) => {
592
+ const { moduleName } = rawRouteInfo;
600
593
  const { routeNameDuplicatesMap, templatesToRender } = this.config;
601
594
  const routeNameTemplate = templatesToRender.routeName;
602
595
 
603
596
  const routeNameFromTemplate = this.templates.renderTemplate(routeNameTemplate, {
604
- routeInfo: routeInfo,
597
+ routeInfo: rawRouteInfo,
605
598
  });
606
599
 
607
- const routeName = this.config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
600
+ const routeName = this.config.hooks.onFormatRouteName(rawRouteInfo, routeNameFromTemplate) || routeNameFromTemplate;
608
601
 
609
602
  const duplicateIdentifier = `${moduleName}|${routeName}`;
610
603
 
@@ -629,7 +622,7 @@ class SchemaRoutes {
629
622
  duplicate: duplicates > 1,
630
623
  };
631
624
 
632
- return this.config.hooks.onCreateRouteName(routeNameInfo, routeInfo) || routeNameInfo;
625
+ return this.config.hooks.onCreateRouteName(routeNameInfo, rawRouteInfo) || routeNameInfo;
633
626
  };
634
627
 
635
628
  parseRouteInfo = (rawRouteName, routeInfo, method, usageSchema, parsedSchemas) => {
@@ -675,6 +668,7 @@ class SchemaRoutes {
675
668
  const responseBodyInfo = this.getResponseBodyInfo(routeInfo, routeParams, parsedSchemas);
676
669
 
677
670
  const rawRouteInfo = {
671
+ ...otherInfo,
678
672
  pathArgs,
679
673
  operationId,
680
674
  method,
@@ -688,7 +682,6 @@ class SchemaRoutes {
688
682
  produces,
689
683
  requestBody,
690
684
  consumes,
691
- ...otherInfo,
692
685
  };
693
686
 
694
687
  const queryObjectSchema = this.convertRouteParamsIntoObject(routeParams.query);
@@ -707,8 +700,12 @@ class SchemaRoutes {
707
700
  routeName,
708
701
  });
709
702
 
710
- this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
711
- this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
703
+ if (this.config.extractResponseBody) {
704
+ this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeName);
705
+ }
706
+ if (this.config.extractResponseError) {
707
+ this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeName);
708
+ }
712
709
 
713
710
  const queryType = routeParams.query.length ? this.schemaParser.getInlineParseContent(queryObjectSchema) : null;
714
711
  const pathType = routeParams.path.length ? this.schemaParser.getInlineParseContent(pathObjectSchema) : null;
@@ -747,29 +744,6 @@ class SchemaRoutes {
747
744
  : void 0,
748
745
  };
749
746
 
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
747
  return {
774
748
  id: routeId,
775
749
  namespace: _.replace(moduleName, /^(\d)/, "v$1"),
@@ -819,10 +793,8 @@ class SchemaRoutes {
819
793
 
820
794
  _.forEach(routeInfosMap, (routeInfo, method) => {
821
795
  const parsedRouteInfo = this.parseRouteInfo(rawRouteName, routeInfo, method, usageSchema, parsedSchemas);
822
-
823
- const usageRouteData = this.config.hooks.onCreateRoute(parsedRouteInfo);
824
-
825
- const route = usageRouteData || parsedRouteInfo;
796
+ const processedRouteInfo = this.config.hooks.onCreateRoute(parsedRouteInfo);
797
+ const route = processedRouteInfo || parsedRouteInfo;
826
798
 
827
799
  if (!this.hasSecurityRoutes && route.security) {
828
800
  this.hasSecurityRoutes = route.security;
@@ -834,48 +806,50 @@ class SchemaRoutes {
834
806
  this.hasFormDataRoutes = route.hasFormDataParams;
835
807
  }
836
808
 
837
- this.routes.push(usageRouteData || parsedRouteInfo);
809
+ this.routes.push(route);
838
810
  });
839
811
  });
840
812
  };
841
813
 
842
814
  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);
815
+ const groupedRoutes = this.routes.reduce(
816
+ (modules, route) => {
817
+ if (route.namespace) {
818
+ if (!modules[route.namespace]) {
819
+ modules[route.namespace] = [];
854
820
  }
855
821
 
856
- return modules;
857
- },
858
- {
859
- $outOfModule: [],
860
- },
861
- ),
862
- (acc, packRoutes, moduleName) => {
822
+ modules[route.namespace].push(route);
823
+ } else {
824
+ modules.$outOfModule.push(route);
825
+ }
826
+
827
+ return modules;
828
+ },
829
+ {
830
+ $outOfModule: [],
831
+ },
832
+ );
833
+
834
+ return _.reduce(
835
+ groupedRoutes,
836
+ (acc, routesGroup, moduleName) => {
863
837
  if (moduleName === "$outOfModule") {
864
- acc.outOfModule = packRoutes;
838
+ acc.outOfModule = routesGroup;
865
839
  } else {
866
840
  if (!acc.combined) acc.combined = [];
867
841
 
868
842
  acc.combined.push({
869
843
  moduleName,
870
- routes: _.map(packRoutes, (route) => {
844
+ routes: _.map(routesGroup, (route) => {
871
845
  const { original: originalName, usage: usageName } = route.routeName;
872
846
 
873
847
  // TODO: https://github.com/acacode/swagger-typescript-api/issues/152
874
848
  // TODO: refactor
875
849
  if (
876
- packRoutes.length > 1 &&
850
+ routesGroup.length > 1 &&
877
851
  usageName !== originalName &&
878
- !_.some(packRoutes, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
852
+ !_.some(routesGroup, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
879
853
  ) {
880
854
  return {
881
855
  ...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);