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 +53 -48
- package/package.json +1 -1
- package/src/commands/generate-templates.js +0 -0
- package/src/schema-parser/schema-routes.js +44 -73
- package/src/templates.js +9 -9
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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-
|
|
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,
|
|
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]
|
|
550
|
+
_.assign(responseBodyInfo.responses[idx], successResponse.schema);
|
|
556
551
|
}
|
|
557
552
|
}
|
|
558
553
|
}
|
|
559
554
|
};
|
|
560
555
|
|
|
561
|
-
extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo,
|
|
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 = (
|
|
599
|
-
const { moduleName } =
|
|
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:
|
|
594
|
+
routeInfo: rawRouteInfo,
|
|
605
595
|
});
|
|
606
596
|
|
|
607
|
-
const routeName = this.config.hooks.onFormatRouteName(
|
|
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,
|
|
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.
|
|
711
|
-
|
|
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
|
|
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(
|
|
806
|
+
this.routes.push(route);
|
|
838
807
|
});
|
|
839
808
|
});
|
|
840
809
|
};
|
|
841
810
|
|
|
842
811
|
getGroupedRoutes = () => {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
(
|
|
846
|
-
if (route.namespace) {
|
|
847
|
-
|
|
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
|
-
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
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 =
|
|
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(
|
|
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
|
-
|
|
847
|
+
routesGroup.length > 1 &&
|
|
877
848
|
usageName !== originalName &&
|
|
878
|
-
!_.some(
|
|
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 =
|
|
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 =
|
|
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 =
|
|
146
|
+
const originalPath = this.findTemplateWithExt(resolve(this.config.templatePaths.original, path));
|
|
147
147
|
|
|
148
148
|
if (originalPath) {
|
|
149
149
|
return this.fileSystem.getFileContent(originalPath);
|