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 +53 -48
- package/package.json +1 -1
- package/src/commands/generate-templates.js +0 -0
- package/src/schema-parser/schema-routes.js +47 -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-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,
|
|
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]
|
|
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,
|
|
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 = (
|
|
599
|
-
const { moduleName } =
|
|
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:
|
|
597
|
+
routeInfo: rawRouteInfo,
|
|
605
598
|
});
|
|
606
599
|
|
|
607
|
-
const routeName = this.config.hooks.onFormatRouteName(
|
|
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,
|
|
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.
|
|
711
|
-
|
|
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
|
|
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(
|
|
809
|
+
this.routes.push(route);
|
|
838
810
|
});
|
|
839
811
|
});
|
|
840
812
|
};
|
|
841
813
|
|
|
842
814
|
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);
|
|
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
|
-
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
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 =
|
|
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(
|
|
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
|
-
|
|
850
|
+
routesGroup.length > 1 &&
|
|
877
851
|
usageName !== originalName &&
|
|
878
|
-
!_.some(
|
|
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 =
|
|
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);
|