Skip to content

Commit 4c3901f

Browse files
authored
Release 6.0.0 (#183)
* BREAKING_CHANGE: fully rewrite render Eta templates process, overriding only one template(#166), template path prefixes * docs: update README.md * docs: update README * docs: update README * docs: add extra readmes for template folders * docs: remove words about mustache version in README * BREAKING_CHANGE: removed TPromise type * feat: --axios option (axios based HttpClient class) * bump: up version to 6.0.0 * fix: support of older templates; fix: spec_templates test; chore: apply comments for axios http client
1 parent f162595 commit 4c3901f

File tree

108 files changed

+30554
-780
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+30554
-780
lines changed

.vscode/launch.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535
"cwd": "${workspaceFolder}",
3636
"runtimeExecutable": "npm",
3737
"runtimeArgs": ["run-script", "node:debug"]
38+
},
39+
{
40+
"name": "Debug partialTemplates test",
41+
"type": "node",
42+
"request": "launch",
43+
"cwd": "${workspaceFolder}",
44+
"runtimeExecutable": "npm",
45+
"runtimeArgs": ["run-script", "test:partialBaseTemplate"]
46+
},
47+
{
48+
"name": "Debug test:--templates",
49+
"type": "node",
50+
"request": "launch",
51+
"cwd": "${workspaceFolder}",
52+
"runtimeExecutable": "npm",
53+
"runtimeArgs": ["run-script", "test:--templates"]
3854
}
3955
]
4056
}

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# next release
22

3+
# 6.0.0
4+
5+
BREAKING_CHANGES:
6+
- Ability to override only one template (issue #166, thanks @Nihisil)
7+
- removed `TPromise` type for `--responses` options (perf. problem, issue #182, thanks @mixalbl4-127)
8+
- breaking changes in `http-client.eta`
9+
- `securityWorker` now can return `Promise<RequestParams | void> | RequestParams | void`
10+
11+
Features:
12+
- template path prefixes `@base`, `@default`, `@modular` (using in Eta templates, `includeFile()`, see README.md)
13+
- `--axios` option for axios http client (issue #142, thanks @msklvsk, @mixalbl4-127 )
14+
315
# 5.1.7
416

517
Fixes:

README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ Any questions you can ask [**here**](https://github.com/acacode/swagger-typescri
2626

2727
All examples you can find [**here**](https://github.com/acacode/swagger-typescript-api/tree/master/tests)
2828

29-
## 🛑 It is new version with [ETA](https://eta.js.org/docs/syntax) templates
30-
Version with `mustache` templates is `>4.0.0`
31-
3229
## 📄 Usage
3330

3431
```muse
@@ -58,6 +55,7 @@ Options:
5855
--modular generate separated files for http client, data contracts, and routes (default: false)
5956
--disableStrictSSL disabled strict SSL (default: false)
6057
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
58+
--axios generate axios http client (default: false)
6159
--single-http-client Ability to send HttpClient instance to Api constructor (default: false)
6260
--default-response <type> default type for empty response schema (default: "void")
6361
-h, --help display help for command
@@ -128,25 +126,43 @@ generateApi({
128126

129127
## 💎 options
130128
### **`--templates`**
131-
This option needed for cases when you don't want to use default `swagger-typescript-api` output structure
129+
This option needed for cases when you don't want to use the default `swagger-typescript-api` output structure
132130

133131
Templates:
134-
- `api.eta` - Api class module
135-
- `data-contracts.eta` - all types (data contracts) from swagger schema
136-
- `http-client.eta` - HttpClient class module
137-
- `procedure-call.eta` - route in Api class
138-
- `route-docs.eta` - documentation for route in Api class
139-
- `route-name.eta` - route name for route in Api class
140-
- `route-type.eta` - *(`--route-types` option)*
141-
- `route-types.eta` - *(`--route-types` option)*
132+
- `api.eta` - Api class module (locations: [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/api.eta), [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/api.eta))
133+
- `data-contracts.eta` - all types (data contracts) from swagger schema (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/data-contracts.eta))
134+
- `http-client.eta` - HttpClient class module (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/http-client.eta))
135+
- `procedure-call.eta` - route in Api class (locations: [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default/procedure-call.eta), [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular/procedure-call.eta))
136+
- `route-docs.eta` - documentation for route in Api class (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-docs.eta))
137+
- `route-name.eta` - route name for route in Api class (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-name.eta))
138+
- `route-type.eta` - *(`--route-types` option)* (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-type.eta))
139+
- `route-types.eta` - *(`--route-types` option)* (locations: [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base/route-types.eta))
142140

143141
How to use it:
144-
1. copy swagger-typescript-api templates into your place in project
142+
1. copy `swagger-typescript-api` templates into your place in project
145143
- from [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default) for single api file
146144
- from [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular) for multiple api files (with `--modular` option)
145+
- from [/templates/base](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base) for base templates (templates using both in default and modular)
147146
1. add `--templates PATH_TO_YOUR_TEMPLATES` option
148147
2. modify [ETA](https://eta.js.org/docs/syntax) templates as you like
149148

149+
NOTE:
150+
Eta has special directive to render template in your Eta templates - `includeFile(pathToTemplate, payload)`
151+
If you want to use some default templates from this tool you can use path prefixes: `@base`, `@default`, `@modular`.
152+
`@base` - [path to base templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/base)
153+
`@default` - [path to single api file templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default)
154+
`@modular` - [path to multiple api files templates](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular)
155+
Examples:
156+
- `includeFile("@base/data-contracts.eta", configuration)`
157+
- `includeFile("@default/api.eta", configuration)`
158+
- `includeFile("@default/procedure-call.eta", configuration)`
159+
- `includeFile("@modular/api.eta", configuration)`
160+
- `includeFile("@modular/procedure-call.eta", configuration)`
161+
- `includeFile("@base/route-docs.eta", configuration)`
162+
- `includeFile("@base/route-name.eta", configuration)`
163+
- `includeFile("@base/route-type.eta", configuration)`
164+
- `includeFile("@base/route-types.eta", configuration)`
165+
150166
### **`--module-name-index`**
151167
This option should be used in cases when you have api with one global prefix like `/api`
152168
Example:

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { Command } = require("commander");
1010
const { resolve } = require("path");
1111
const { generateApi } = require("./src");
1212
const { version, name: packageName } = require("./package.json");
13-
const { TS_KEYWORDS } = require("./src/constants");
13+
const { TS_KEYWORDS, HTTP_CLIENT } = require("./src/constants");
1414

1515
const program = new Command(packageName);
1616

@@ -62,6 +62,7 @@ program
6262
0,
6363
)
6464
.option("--disableStrictSSL", "disabled strict SSL", false)
65+
.option("--axios", "generate axios http client", false)
6566
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
6667
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
6768
.option(
@@ -91,13 +92,15 @@ const {
9192
cleanOutput,
9293
defaultResponse,
9394
singleHttpClient,
95+
axios,
9496
} = program;
9597

9698
generateApi({
9799
name,
98100
url: path,
99101
generateRouteTypes: routeTypes,
100-
generateClient: client,
102+
generateClient: !!(axios || client),
103+
httpClientType: axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
101104
defaultResponseAsSuccess: defaultAsSuccess,
102105
defaultResponseType: defaultResponse,
103106
generateUnionEnums: unionEnums,

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "5.1.7",
3+
"version": "6.0.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
@@ -29,7 +29,10 @@
2929
"test:--extract-request-params": "node tests/spec/extractRequestParams/test.js",
3030
"test:--enum-names-as-values": "node tests/spec/enumNamesAsValues/test.js",
3131
"test:--default-response": "node tests/spec/defaultResponse/test.js",
32-
"test:--js": "node tests/spec/js/test.js"
32+
"test:--js": "node tests/spec/js/test.js",
33+
"test:--axios": "node tests/spec/axios/test.js",
34+
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
35+
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js"
3336
},
3437
"author": "acacode",
3538
"license": "MIT",

src/config.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const constants = require("./constants");
1+
const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");
22

33
const config = {
44
/** CLI flag */
@@ -37,7 +37,7 @@ const config = {
3737
outOfModuleApi: "Common",
3838
},
3939
routeNameDuplicatesMap: new Map(),
40-
prettierOptions: constants.PRETTIER_OPTIONS,
40+
prettierOptions: PRETTIER_OPTIONS,
4141
hooks: {
4242
onCreateComponent: (schema) => schema,
4343
onParseSchema: (originalSchema, parsedSchema) => parsedSchema,
@@ -47,8 +47,23 @@ const config = {
4747
onCreateRequestParams: (rawType) => {},
4848
onCreateRouteName: () => {},
4949
},
50-
defaultResponseType: constants.TS_KEYWORDS.VOID,
50+
defaultResponseType: TS_KEYWORDS.VOID,
5151
singleHttpClient: false,
52+
httpClientType: HTTP_CLIENT.FETCH,
53+
templatePaths: {
54+
/** `templates/base` */
55+
base: "",
56+
/** `templates/default` */
57+
default: "",
58+
/** `templates/modular` */
59+
modular: "",
60+
/** usage path if `--templates` option is not set */
61+
original: "",
62+
/** custom path to templates (`--templates`) */
63+
custom: "",
64+
},
65+
/** Record<templateName, templateContent> */
66+
templatesToRender: {},
5267
};
5368

5469
/** needs to use data everywhere in project */

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ const SCHEMA_TYPES = {
3131
COMPLEX_NOT: "not",
3232
};
3333

34+
const HTTP_CLIENT = {
35+
FETCH: "fetch",
36+
AXIOS: "axios",
37+
};
38+
3439
module.exports = {
3540
DEFAULT_BODY_ARG_NAME: "data",
3641
SUCCESS_RESPONSE_STATUS_RANGE: [200, 300],
3742
JS_PRIMITIVE_TYPES,
3843
JS_EMPTY_TYPES,
3944
TS_KEYWORDS,
4045
SCHEMA_TYPES,
46+
HTTP_CLIENT,
4147
PRETTIER_OPTIONS: {
4248
printWidth: 120,
4349
tabWidth: 2,

src/files.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const { resolve } = require("path");
44
const { filePrefix } = require("./filePrefix");
55
const makeDir = require("make-dir");
66

7-
const getFileContent = (path) => fs.readFileSync(path, { encoding: "UTF-8" });
7+
const getFileContent = (path) => {
8+
return fs.readFileSync(path, { encoding: "UTF-8" });
9+
};
810

911
const pathIsDir = (path) => {
1012
if (!path) return false;

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const { getSwaggerObject, fixSwaggerScheme, convertSwaggerObject } = require("./
1515
const { createComponentsMap, filterComponentsMap } = require("./components");
1616
const { createFile, pathIsExist, pathIsDir, createDir, cleanDir } = require("./files");
1717
const { addToConfig, config } = require("./config");
18-
const { getTemplates } = require("./templates");
18+
const { getTemplates, getTemplatePaths } = require("./templates");
1919
const constants = require("./constants");
2020
const { generateOutputFiles } = require("./output");
2121

@@ -33,6 +33,7 @@ module.exports = {
3333
defaultResponseAsSuccess = config.defaultResponseAsSuccess,
3434
generateRouteTypes = config.generateRouteTypes,
3535
generateClient = config.generateClient,
36+
httpClientType = config.httpClientType,
3637
generateUnionEnums = config.generateUnionEnums,
3738
moduleNameIndex = config.moduleNameIndex,
3839
extractRequestParams = config.extractRequestParams,
@@ -50,6 +51,7 @@ module.exports = {
5051
defaultResponseAsSuccess,
5152
generateRouteTypes,
5253
generateClient,
54+
httpClientType,
5355
generateResponses,
5456
templates,
5557
generateUnionEnums,
@@ -63,9 +65,14 @@ module.exports = {
6365
cleanOutput,
6466
defaultResponseType,
6567
singleHttpClient,
68+
constants,
6669
});
6770
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url, disableStrictSSL))
6871
.then(({ usageSchema, originalSchema }) => {
72+
const templatePaths = getTemplatePaths(config);
73+
74+
addToConfig({ templatePaths });
75+
6976
const templatesToRender = getTemplates(config);
7077

7178
console.log("☄️ start generating your typescript api");

0 commit comments

Comments
 (0)