swagger-typescript-api 12.0.3 → 12.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.
package/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- MIT License
2
-
3
- Copyright (c) 2019-present acacode
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- 'Software'), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1
+ MIT License
2
+
3
+ Copyright (c) 2019-present acacode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
22
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -130,6 +130,7 @@ generateApi({
130
130
  toJS: false,
131
131
  extractRequestParams: false,
132
132
  extractRequestBody: false,
133
+ extractEnums: false,
133
134
  unwrapResponseData: false,
134
135
  prettier: { // By default prettier config is load from your project
135
136
  printWidth: 120,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "12.0.3",
3
+ "version": "12.0.4",
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",
@@ -14,7 +14,6 @@
14
14
  "test-all-extended": "node --unhandled-rejections=strict ./scriptsRunner.js generate-extended validate generate validate test:*",
15
15
  "test-specific": "node ./scriptsRunner.js generate validate test:*",
16
16
  "test-specific-only": "node ./scriptsRunner.js test:*",
17
- "prepare": "npm run test-all-extended",
18
17
  "generate": "node tests/generate.js",
19
18
  "generate-extended": "node tests/generate-extended.js",
20
19
  "generate:debug": "node --nolazy tests/generate.js",
@@ -92,7 +92,7 @@ class SchemaParser {
92
92
  baseSchemaParsers = {
93
93
  [SCHEMA_TYPES.ENUM]: (schema, typeName) => {
94
94
  if (this.config.extractEnums && !typeName) {
95
- const generatedTypeName = this.config.componentTypeNameResolver.resolve([this.buildTypeNameFromPath()]);
95
+ const generatedTypeName = this.config.componentTypeNameResolver.resolve([this.buildTypeNameFromPath()], false);
96
96
  const schemaComponent = this.schemaComponentsMap.createComponent("schemas", generatedTypeName, { ...schema });
97
97
  return this.parseSchema(schemaComponent, generatedTypeName);
98
98
  }
@@ -391,9 +391,9 @@ class SchemaParser {
391
391
  _.merge(schema, this.config.hooks.onPreParseSchema(schema, typeName, schemaType));
392
392
  parsedSchema = this.baseSchemaParsers[schemaType](schema, typeName);
393
393
  schema.$parsed = this.config.hooks.onParseSchema(schema, parsedSchema) || parsedSchema;
394
- }
395
394
 
396
- this.$processingSchemaPath.pop();
395
+ this.$processingSchemaPath.pop();
396
+ }
397
397
 
398
398
  return schema.$parsed;
399
399
  };
@@ -383,7 +383,7 @@ class SchemaRoutes {
383
383
  [],
384
384
  );
385
385
 
386
- getResponseBodyInfo = (routeInfo, routeParams, parsedSchemas) => {
386
+ getResponseBodyInfo = (routeInfo, parsedSchemas) => {
387
387
  const { produces, operationId, responses } = routeInfo;
388
388
 
389
389
  const contentTypes = this.getContentTypes(responses, [...(produces || []), routeInfo["x-accepts"]]);
@@ -720,12 +720,13 @@ class SchemaRoutes {
720
720
  const pathArgs = routeParams.path.map((pathArgSchema) => ({
721
721
  name: pathArgSchema.name,
722
722
  optional: !pathArgSchema.required,
723
- type: this.schemaParser.getInlineParseContent(pathArgSchema.schema),
723
+ // mark it as any for now, because "getInlineParseContent" breaks type names of extracted enums
724
+ type: this.config.Ts.Keyword.Any,
724
725
  description: pathArgSchema.description,
725
726
  }));
726
727
  const pathArgsNames = pathArgs.map((arg) => arg.name);
727
728
 
728
- const responseBodyInfo = this.getResponseBodyInfo(routeInfo, routeParams, parsedSchemas);
729
+ const responseBodyInfo = this.getResponseBodyInfo(routeInfo, parsedSchemas);
729
730
 
730
731
  const rawRouteInfo = {
731
732
  ...otherInfo,
@@ -767,9 +768,22 @@ class SchemaRoutes {
767
768
  this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeName);
768
769
  }
769
770
 
770
- const queryType = routeParams.query.length ? this.schemaParser.getInlineParseContent(queryObjectSchema) : null;
771
- const pathType = routeParams.path.length ? this.schemaParser.getInlineParseContent(pathObjectSchema) : null;
772
- const headersType = routeParams.header.length ? this.schemaParser.getInlineParseContent(headersObjectSchema) : null;
771
+ const typeName = this.schemaUtils.resolveTypeName(
772
+ routeName.usage,
773
+ this.config.extractingOptions.requestParamsSuffix,
774
+ this.config.extractingOptions.requestParamsNameResolver,
775
+ false,
776
+ );
777
+
778
+ const queryType = routeParams.query.length
779
+ ? this.schemaParser.getInlineParseContent(queryObjectSchema, typeName)
780
+ : null;
781
+ const pathType = routeParams.path.length
782
+ ? this.schemaParser.getInlineParseContent(pathObjectSchema, typeName)
783
+ : null;
784
+ const headersType = routeParams.header.length
785
+ ? this.schemaParser.getInlineParseContent(headersObjectSchema, typeName)
786
+ : null;
773
787
 
774
788
  const nameResolver = new SpecificArgNameResolver(this.logger, pathArgsNames);
775
789
 
@@ -804,6 +818,10 @@ class SchemaRoutes {
804
818
  : void 0,
805
819
  };
806
820
 
821
+ pathArgs.forEach((pathArg, i) => {
822
+ pathArg.type = this.schemaParser.getInlineParseContent(routeParams.path[i].schema, typeName);
823
+ });
824
+
807
825
  return {
808
826
  id: routeId,
809
827
  namespace: _.replace(moduleName, /^(\d)/, "v$1"),
@@ -854,19 +872,21 @@ class SchemaRoutes {
854
872
  _.forEach(routeInfosMap, (routeInfo, method) => {
855
873
  const parsedRouteInfo = this.parseRouteInfo(rawRouteName, routeInfo, method, usageSchema, parsedSchemas);
856
874
  const processedRouteInfo = this.config.hooks.onCreateRoute(parsedRouteInfo);
857
- const route = processedRouteInfo || parsedRouteInfo;
858
-
859
- if (!this.hasSecurityRoutes && route.security) {
860
- this.hasSecurityRoutes = route.security;
861
- }
862
- if (!this.hasQueryRoutes && route.hasQuery) {
863
- this.hasQueryRoutes = route.hasQuery;
875
+ if (processedRouteInfo !== false) {
876
+ const route = processedRouteInfo || parsedRouteInfo;
877
+
878
+ if (!this.hasSecurityRoutes && route.security) {
879
+ this.hasSecurityRoutes = route.security;
880
+ }
881
+ if (!this.hasQueryRoutes && route.hasQuery) {
882
+ this.hasQueryRoutes = route.hasQuery;
883
+ }
884
+ if (!this.hasFormDataRoutes && route.hasFormDataParams) {
885
+ this.hasFormDataRoutes = route.hasFormDataParams;
886
+ }
887
+
888
+ this.routes.push(route);
864
889
  }
865
- if (!this.hasFormDataRoutes && route.hasFormDataParams) {
866
- this.hasFormDataRoutes = route.hasFormDataParams;
867
- }
868
-
869
- this.routes.push(route);
870
890
  });
871
891
  });
872
892
  };
@@ -145,7 +145,7 @@ class SchemaUtils {
145
145
  return _.uniq(_.filter(contents, (type) => filterFn(type)));
146
146
  };
147
147
 
148
- resolveTypeName = (typeName, suffixes, resolver) => {
148
+ resolveTypeName = (typeName, suffixes, resolver, shouldReserve = true) => {
149
149
  if (resolver) {
150
150
  return this.config.componentTypeNameResolver.resolve((reserved) => {
151
151
  const variant = resolver(pascalCase(typeName), reserved);
@@ -155,6 +155,7 @@ class SchemaUtils {
155
155
  } else {
156
156
  return this.config.componentTypeNameResolver.resolve(
157
157
  suffixes.map((suffix) => pascalCase(`${typeName} ${suffix}`)),
158
+ shouldReserve,
158
159
  );
159
160
  }
160
161
  };
@@ -40,7 +40,7 @@ class NameResolver {
40
40
  * @param {(string[]) | ((reserved: string[]) => string)} variantsOrResolver
41
41
  * @returns {string | null}
42
42
  */
43
- resolve(variantsOrResolver) {
43
+ resolve(variantsOrResolver, shouldReserve = true) {
44
44
  this.logger.debug("resolving name with using", variantsOrResolver);
45
45
  if (Array.isArray(variantsOrResolver)) {
46
46
  const variants = variantsOrResolver;
@@ -48,13 +48,13 @@ class NameResolver {
48
48
  const uniqVariants = _.uniq(_.compact(variants));
49
49
 
50
50
  _.forEach(uniqVariants, (variant) => {
51
- if (!usageName && !this.isReserved(variant)) {
51
+ if (!usageName && (!shouldReserve || !this.isReserved(variant))) {
52
52
  usageName = variant;
53
53
  }
54
54
  });
55
55
 
56
56
  if (usageName) {
57
- this.reserve([usageName]);
57
+ shouldReserve && this.reserve([usageName]);
58
58
  return usageName;
59
59
  }
60
60
 
@@ -74,7 +74,7 @@ class NameResolver {
74
74
  }
75
75
  }
76
76
 
77
- this.reserve([usageName]);
77
+ shouldReserve && this.reserve([usageName]);
78
78
  return usageName;
79
79
  }
80
80
 
@@ -1,37 +1,37 @@
1
- <%
2
- const { data, utils } = it;
3
- const { formatDescription, require, _ } = utils;
4
-
5
- const stringify = (value) => _.isObject(value) ? JSON.stringify(value) : _.isString(value) ? `"${value}"` : value
6
-
7
- const jsDocLines = _.compact([
8
- data.title,
9
- data.description && formatDescription(data.description),
10
- !_.isUndefined(data.deprecated) && data.deprecated && '@deprecated',
11
- !_.isUndefined(data.format) && `@format ${data.format}`,
12
- !_.isUndefined(data.minimum) && `@min ${data.minimum}`,
13
- !_.isUndefined(data.multipleOf) && `@multipleOf ${data.multipleOf}`,
14
- !_.isUndefined(data.exclusiveMinimum) && `@exclusiveMin ${data.exclusiveMinimum}`,
15
- !_.isUndefined(data.maximum) && `@max ${data.maximum}`,
16
- !_.isUndefined(data.minLength) && `@minLength ${data.minLength}`,
17
- !_.isUndefined(data.maxLength) && `@maxLength ${data.maxLength}`,
18
- !_.isUndefined(data.exclusiveMaximum) && `@exclusiveMax ${data.exclusiveMaximum}`,
19
- !_.isUndefined(data.maxItems) && `@maxItems ${data.maxItems}`,
20
- !_.isUndefined(data.minItems) && `@minItems ${data.minItems}`,
21
- !_.isUndefined(data.uniqueItems) && `@uniqueItems ${data.uniqueItems}`,
22
- !_.isUndefined(data.default) && `@default ${stringify(data.default)}`,
23
- !_.isUndefined(data.pattern) && `@pattern ${data.pattern}`,
24
- !_.isUndefined(data.example) && `@example ${stringify(data.example)}`
25
- ]).join('\n').split('\n');
26
- %>
27
- <% if (jsDocLines.every(_.isEmpty)) { %>
28
- <% } else if (jsDocLines.length === 1) { %>
29
- /** <%~ jsDocLines[0] %> */
30
- <% } else if (jsDocLines.length) { %>
31
- /**
32
- <% for (jsDocLine of jsDocLines) { %>
33
- * <%~ jsDocLine %>
34
-
35
- <% } %>
36
- */
37
- <% } %>
1
+ <%
2
+ const { data, utils } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+
5
+ const stringify = (value) => _.isObject(value) ? JSON.stringify(value) : _.isString(value) ? `"${value}"` : value
6
+
7
+ const jsDocLines = _.compact([
8
+ data.title,
9
+ data.description && formatDescription(data.description),
10
+ !_.isUndefined(data.deprecated) && data.deprecated && '@deprecated',
11
+ !_.isUndefined(data.format) && `@format ${data.format}`,
12
+ !_.isUndefined(data.minimum) && `@min ${data.minimum}`,
13
+ !_.isUndefined(data.multipleOf) && `@multipleOf ${data.multipleOf}`,
14
+ !_.isUndefined(data.exclusiveMinimum) && `@exclusiveMin ${data.exclusiveMinimum}`,
15
+ !_.isUndefined(data.maximum) && `@max ${data.maximum}`,
16
+ !_.isUndefined(data.minLength) && `@minLength ${data.minLength}`,
17
+ !_.isUndefined(data.maxLength) && `@maxLength ${data.maxLength}`,
18
+ !_.isUndefined(data.exclusiveMaximum) && `@exclusiveMax ${data.exclusiveMaximum}`,
19
+ !_.isUndefined(data.maxItems) && `@maxItems ${data.maxItems}`,
20
+ !_.isUndefined(data.minItems) && `@minItems ${data.minItems}`,
21
+ !_.isUndefined(data.uniqueItems) && `@uniqueItems ${data.uniqueItems}`,
22
+ !_.isUndefined(data.default) && `@default ${stringify(data.default)}`,
23
+ !_.isUndefined(data.pattern) && `@pattern ${data.pattern}`,
24
+ !_.isUndefined(data.example) && `@example ${stringify(data.example)}`
25
+ ]).join('\n').split('\n');
26
+ %>
27
+ <% if (jsDocLines.every(_.isEmpty)) { %>
28
+ <% } else if (jsDocLines.length === 1) { %>
29
+ /** <%~ jsDocLines[0] %> */
30
+ <% } else if (jsDocLines.length) { %>
31
+ /**
32
+ <% for (jsDocLine of jsDocLines) { %>
33
+ * <%~ jsDocLine %>
34
+
35
+ <% } %>
36
+ */
37
+ <% } %>
@@ -1,28 +1,28 @@
1
- <%
2
- const { modelTypes, utils, config } = it;
3
- const { formatDescription, require, _, Ts } = utils;
4
-
5
-
6
- const dataContractTemplates = {
7
- enum: (contract) => {
8
- return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
9
- },
10
- interface: (contract) => {
11
- return `interface ${contract.name} {\r\n${contract.content}}`;
12
- },
13
- type: (contract) => {
14
- return `type ${contract.name} = ${contract.content}`;
15
- },
16
- }
17
- %>
18
-
19
- <% if (config.internalTemplateOptions.addUtilRequiredKeysType) { %>
20
- type <%~ config.Ts.CodeGenKeyword.UtilRequiredKeys %><T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
21
- <% } %>
22
-
23
- <% modelTypes.forEach((contract) => { %>
24
- <%~ includeFile('@base/data-contract-jsdoc.ejs', { ...it, data: { ...contract, ...contract.typeData } }) %>
25
- export <%~ (dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type)(contract) %>
26
-
27
-
28
- <% }) %>
1
+ <%
2
+ const { modelTypes, utils, config } = it;
3
+ const { formatDescription, require, _, Ts } = utils;
4
+
5
+
6
+ const dataContractTemplates = {
7
+ enum: (contract) => {
8
+ return `enum ${contract.name} {\r\n${contract.content} \r\n }`;
9
+ },
10
+ interface: (contract) => {
11
+ return `interface ${contract.name} {\r\n${contract.content}}`;
12
+ },
13
+ type: (contract) => {
14
+ return `type ${contract.name} = ${contract.content}`;
15
+ },
16
+ }
17
+ %>
18
+
19
+ <% if (config.internalTemplateOptions.addUtilRequiredKeysType) { %>
20
+ type <%~ config.Ts.CodeGenKeyword.UtilRequiredKeys %><T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
21
+ <% } %>
22
+
23
+ <% modelTypes.forEach((contract) => { %>
24
+ <%~ includeFile('@base/data-contract-jsdoc.ejs', { ...it, data: { ...contract, ...contract.typeData } }) %>
25
+ export <%~ (dataContractTemplates[contract.typeIdentifier] || dataContractTemplates.type)(contract) %>
26
+
27
+
28
+ <% }) %>
@@ -1,12 +1,12 @@
1
- <%
2
- const { contract, utils, config } = it;
3
- const { formatDescription, require, _ } = utils;
4
- const { name, $content } = contract;
5
- %>
6
- <% if (config.generateUnionEnums) { %>
7
- export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
8
- <% } else { %>
9
- export enum <%~ name %> {
10
- <%~ _.map($content, ({ key, value }) => `${key} = ${value}`).join(",\n") %>
11
- }
12
- <% } %>
1
+ <%
2
+ const { contract, utils, config } = it;
3
+ const { formatDescription, require, _ } = utils;
4
+ const { name, $content } = contract;
5
+ %>
6
+ <% if (config.generateUnionEnums) { %>
7
+ export type <%~ name %> = <%~ _.map($content, ({ value }) => value).join(" | ") %>
8
+ <% } else { %>
9
+ export enum <%~ name %> {
10
+ <%~ _.map($content, ({ key, value }) => `${key} = ${value}`).join(",\n") %>
11
+ }
12
+ <% } %>
@@ -1,3 +1,3 @@
1
- <% const { config } = it; %>
2
- <% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
1
+ <% const { config } = it; %>
2
+ <% /* https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-clients/ */ %>
3
3
  <%~ includeFile(`@base/http-clients/${config.httpClientType}-http-client`, it) %>