swagger-typescript-api 12.0.2 → 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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![NPM badge](https://img.shields.io/npm/v/swagger-typescript-api.svg)](https://www.npmjs.com/package/swagger-typescript-api)
4
4
  [![CI](https://github.com/acacode/swagger-typescript-api/actions/workflows/main.yml/badge.svg?branch=next)](https://github.com/acacode/swagger-typescript-api/actions/workflows/main.yml) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
5
- [![All Contributors](https://img.shields.io/badge/all_contributors-32-orange.svg)](#contributors)
5
+ [![All Contributors](https://img.shields.io/badge/all_contributors-33-orange.svg)](#contributors)
6
6
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
7
7
 
8
8
  <img src="https://raw.githubusercontent.com/acacode/swagger-typescript-api/master/assets/swagger-typescript-api-logo.png" align="left"
@@ -21,9 +21,15 @@ Any questions you can ask [**here**](https://github.com/acacode/swagger-typescri
21
21
 
22
22
  ![](https://raw.githubusercontent.com/acacode/swagger-typescript-api/master/assets/components-converter-example.jpg)
23
23
 
24
- 👀 This project is looking for a code maintainer 👀
25
24
  P.S. If you are creating the PR, please check your changes with using command `npm run prepare`
26
- P.S. If you want to contribute please use branch `next`. All PRs which will have target `master` will be declined
25
+ P.S. If you want to contribute please use the `next` branch. All PRs that has target `master` will be declined!
26
+
27
+
28
+ Thanks to [Jetbrains](https://www.jetbrains.com/?from=swaggertypescriptapi) for providing a free license for their excellent Webstorm IDE.
29
+ <a href="https://www.jetbrains.com/?from=swaggertypescriptapi">
30
+ <img src="https://upload.wikimedia.org/wikipedia/commons/1/1a/JetBrains_Logo_2016.svg" alt="Jetbrains">
31
+ </a>
32
+
27
33
 
28
34
  ## 👀 Examples
29
35
 
@@ -124,6 +130,7 @@ generateApi({
124
130
  toJS: false,
125
131
  extractRequestParams: false,
126
132
  extractRequestBody: false,
133
+ extractEnums: false,
127
134
  unwrapResponseData: false,
128
135
  prettier: { // By default prettier config is load from your project
129
136
  printWidth: 120,
@@ -529,6 +536,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
529
536
  <td align="center"><a href="https://github.com/ApacheEx"><img src="https://avatars.githubusercontent.com/u/1918108?v=4?s=100" width="100px;" alt="Oleg Kuzava"/><br /><sub><b>Oleg Kuzava</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=ApacheEx" title="Code">💻</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AApacheEx" title="Bug reports">🐛</a></td>
530
537
  <td align="center"><a href="http://nikz.se"><img src="https://avatars.githubusercontent.com/u/7352072?v=4?s=100" width="100px;" alt="Niklas Frank"/><br /><sub><b>Niklas Frank</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=nksfrank" title="Code">💻</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Anksfrank" title="Bug reports">🐛</a></td>
531
538
  <td align="center"><a href="https://quentinbrunet.com"><img src="https://avatars.githubusercontent.com/u/20137632?v=4?s=100" width="100px;" alt="Quentin Brunet"/><br /><sub><b>Quentin Brunet</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=qboot" title="Code">💻</a></td>
539
+ <td align="center"><a href="https://github.com/Soarc"><img src="https://avatars.githubusercontent.com/u/3385495?v=4?s=100" width="100px;" alt="Gor Rustamyan"/><br /><sub><b>Gor Rustamyan</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=Soarc" title="Code">💻</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3ASoarc" title="Bug reports">🐛</a></td>
540
+ <td align="center"><a href="https://github.com/JochenDiekenbrock"><img src="https://avatars.githubusercontent.com/u/1625222?v=4?s=100" width="100px;" alt="Jochen Diekenbrock"/><br /><sub><b>Jochen Diekenbrock</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=JochenDiekenbrock" title="Code">💻</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AJochenDiekenbrock" title="Bug reports">🐛</a></td>
532
541
  </tr>
533
542
  </tbody>
534
543
  </table>
package/index.d.ts CHANGED
@@ -144,20 +144,28 @@ interface GenerateApiParamsBase {
144
144
 
145
145
  /** extract all enums from nested types\interfaces to `enum` construction */
146
146
  extractEnums?: boolean;
147
+
147
148
  /** prefix string value needed to fix invalid type names (default: 'Type') */
148
149
  fixInvalidTypeNamePrefix?: string;
150
+
149
151
  /** prefix string value needed to fix invalid enum keys (default: 'Value') */
150
152
  fixInvalidEnumKeyPrefix?: string;
153
+
151
154
  /** prefix string value for enum keys */
152
155
  enumKeyPrefix?: string;
156
+
153
157
  /** suffix string value for enum keys */
154
158
  enumKeySuffix?: string;
159
+
155
160
  /** prefix string value for type names */
156
161
  typePrefix?: string;
162
+
157
163
  /** suffix string value for type names */
158
164
  typeSuffix?: string;
165
+
159
166
  /** extra configuration for extracting type names operations */
160
167
  extractingOptions?: Partial<ExtractingOptions>;
168
+
161
169
  /** configuration for fetching swagger schema requests */
162
170
  requestOptions?: null | Partial<import("node-fetch").RequestInit>;
163
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swagger-typescript-api",
3
- "version": "12.0.2",
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",
@@ -80,7 +80,7 @@
80
80
  "@types/swagger-schema-official": "2.0.22",
81
81
  "cosmiconfig": "7.0.1",
82
82
  "didyoumean": "^1.2.2",
83
- "eta": "1.12.3",
83
+ "eta": "^2.0.0",
84
84
  "js-yaml": "4.1.0",
85
85
  "lodash": "4.17.21",
86
86
  "make-dir": "3.1.0",
@@ -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,17 +1,17 @@
1
- # swagger-typescript-api
2
-
3
- # templates
4
-
5
- Templates:
6
- - `api.ejs` - *(generates file)* Api class module (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.ejs))
7
- - `data-contracts.ejs` - *(generates file)* all types (data contracts) from swagger schema (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.ejs))
8
- - `http-client.ejs` - *(generates file)* HttpClient class module (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.ejs))
9
- - `procedure-call.ejs` - *(subtemplate)* route in Api class (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.ejs))
10
- - `route-docs.ejs` - *(generates file)* documentation for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.ejs))
11
- - `route-name.ejs` - *(subtemplate)* route name for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.ejs))
12
- - `route-type.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.ejs))
13
- - `route-types.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.ejs)) - `data-contract-jsdoc.ejs` - *(subtemplate)* generates JSDOC for data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contract-jsdoc.ejs))
14
-
15
- [//]: # (- `enum-data-contract.ejs` - *&#40;subtemplate&#41;* generates `enum` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/enum-data-contract.ejs&#41;&#41;)
16
- [//]: # (- `interface-data-contract.ejs` - *&#40;subtemplate&#41;* generates `interface` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/interface-data-contract.ejs&#41;&#41;)
17
- [//]: # (- `type-data-contract.ejs` - *&#40;subtemplate&#41;* generates `type` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/type-data-contract.ejs&#41;&#41;)
1
+ # swagger-typescript-api
2
+
3
+ # templates
4
+
5
+ Templates:
6
+ - `api.ejs` - *(generates file)* Api class module (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.ejs))
7
+ - `data-contracts.ejs` - *(generates file)* all types (data contracts) from swagger schema (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.ejs))
8
+ - `http-client.ejs` - *(generates file)* HttpClient class module (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.ejs))
9
+ - `procedure-call.ejs` - *(subtemplate)* route in Api class (locations: [default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.ejs), [modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.ejs))
10
+ - `route-docs.ejs` - *(generates file)* documentation for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.ejs))
11
+ - `route-name.ejs` - *(subtemplate)* route name for route in Api class (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.ejs))
12
+ - `route-type.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.ejs))
13
+ - `route-types.ejs` - *(`--route-types` option)* *(subtemplate)* (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.ejs)) - `data-contract-jsdoc.ejs` - *(subtemplate)* generates JSDOC for data contract (locations: [base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contract-jsdoc.ejs))
14
+
15
+ [//]: # (- `enum-data-contract.ejs` - *&#40;subtemplate&#41;* generates `enum` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/enum-data-contract.ejs&#41;&#41;)
16
+ [//]: # (- `interface-data-contract.ejs` - *&#40;subtemplate&#41;* generates `interface` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/interface-data-contract.ejs&#41;&#41;)
17
+ [//]: # (- `type-data-contract.ejs` - *&#40;subtemplate&#41;* generates `type` data contract &#40;locations: [base]&#40;https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/type-data-contract.ejs&#41;&#41;)
@@ -1,8 +1,8 @@
1
- # swagger-typescript-api
2
-
3
- # templates/base
4
-
5
- This templates use both for multiple api files and single api file
6
-
7
-
1
+ # swagger-typescript-api
2
+
3
+ # templates/base
4
+
5
+ This templates use both for multiple api files and single api file
6
+
7
+
8
8
  path prefix `@base`
@@ -1,7 +1,7 @@
1
- # swagger-typescript-api
2
-
3
- # templates/default
4
-
5
- This templates use for single api file (without `--modular` option)
6
-
1
+ # swagger-typescript-api
2
+
3
+ # templates/default
4
+
5
+ This templates use for single api file (without `--modular` option)
6
+
7
7
  path prefix `@default`
@@ -1,7 +1,7 @@
1
- # swagger-typescript-api
2
-
3
- # templates/modular
4
-
5
- This templates use for multiple api files (`--modular` option)
6
-
1
+ # swagger-typescript-api
2
+
3
+ # templates/modular
4
+
5
+ This templates use for multiple api files (`--modular` option)
6
+
7
7
  path prefix `@modular`