Skip to content

Commit 14b6d3d

Browse files
js2meFl0pZzRafael Fakhreev
authored
Release 4.0.0 (#107)
* fix: handling x-omitempty property for definition properties (#68) * docs: update CHANGELOG * docs: update CHANGELOG * Migrate to eta (#88) * BREAKING CHANGE: migrate from mustache template engine to eta template engine * fix: merge conflicts after rebasing next Co-authored-by: Rafael Fakhreev <[email protected]> Co-authored-by: svolkov <[email protected]> * chore: update README (Contribution) * Refactor render layer (#90) * fix: handling x-omitempty property for definition properties (#68) * docs: update CHANGELOG * docs: update CHANGELOG; bump: up project version to 2.0.0 * BREAKING_CHANGE: renamed mustache templates, split client mustache template * BREAKING CHANGE: migrate from mustache template engine to eta template engine * feat: rewrite render layer - Respect single responsibility principle - Use Eta includeFile - Force use "No description" JSDoc comment * fix: handling x-omitempty property for definition properties (#68) * docs: update CHANGELOG * docs: update CHANGELOG * Migrate to eta (#88) * BREAKING CHANGE: migrate from mustache template engine to eta template engine * fix: merge conflicts after rebasing next Co-authored-by: Rafael Fakhreev <[email protected]> Co-authored-by: svolkov <[email protected]> * Sync code base * chore: delete unnecessary dir * Update index.js Fix default templates path Co-authored-by: svolkov <[email protected]> Co-authored-by: Rafael Fakhreev <[email protected]> * chore: revert back fix commit * BREAKING_CHANGE: return type of generateApi(); feat: modular option; chore: update HttpClient class (exportable) * fix: outOfModule modular * feat: complete data contracts imports for modular api modules, add prettier organize imports plugin * chore: remove console info * BREAKING_CHANGE: move generating route names into eta template * fix: problem of missing templates folder * fix: try to fix problem with not using prettier plugin * chore: refresh generated tests schemas * BREAKING_CHANGE: update all dependencies to latest * feat: improve types in index.d.ts file; refactor: processing templates folder, ETA templates, routes, schemas; feat: optional templates feature * chore: update CHANGELOG * feat: --extract-request-params option * fix: add missing generateApi params to .d.ts file; feat: add support x-enumNames field for enums * fix: enumNames getting from schema * feat: improve data-contracts.eta template, now data contracts formatting inside template * docs: update CHANGELOG * feat: hooks property for watching/modifying parsed parts of schema * fix: problem of complex types (`oneOf`, `allOf`) with `properties` field * chore: fix problem of wrong js docs comments for procedure call * feat: add pathArgs to routeInfo for routeName template * BREAKING_CHANGE: properties data and error in HttpResponse type have duck types * feat: add extraTemplate property for generateApi() * docs: update docs for --templates option * bump: up version to 4.0.0 * docs: update README * refactor: move all type identifiers\naming into constants like TS_KEYWORDS, SCHEMA_TYPES; chore: move prepareConfig to hooks.onPrepareConfig; feat: add onInit hook; feat: enumNamesAsValues * chore: try to fix travis yml * chore: remove description from modular api class * docs: update CHANGELOG Co-authored-by: Rafael Fakhreev <[email protected]> Co-authored-by: Rafael Fakhreev <[email protected]>
1 parent 4a5d7ea commit 14b6d3d

File tree

120 files changed

+13271
-4616
lines changed

Some content is hidden

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

120 files changed

+13271
-4616
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
.vscode
3+
.idea
34
swagger-test-cli.*
5+
swagger-test-cli
46
dist

.travis.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,8 @@ cache:
66
directories:
77
- "node_modules"
88

9-
before_install:
10-
- export PACKAGE_VERSION=$(node -p "require('./package.json').version")
11-
- export PACKAGE_NAME=$(node -p "require('./package.json').name")
12-
- export NPM_REMOTE_VERSION=$(npm view swagger-typescript-api version)
13-
- export BRANCH_IS_TAG=$(node -p "/^([0-9].[0-9].[0-9]+((-(alpha|beta))|))$/.test('${TRAVIS_BRANCH}')")
14-
- export GIT_LOG="$(git log --pretty=format:"* %s (%h)")"
15-
- export PROJECT_NAME="acacode/swagger-typescript-api"
16-
179
install:
18-
- npm ci
10+
- npm i
1911

2012
jobs:
2113
include:

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
"cwd": "${workspaceFolder}",
2828
"runtimeExecutable": "npm",
2929
"runtimeArgs": ["run-script", "cli:debug:yaml"]
30+
},
31+
{
32+
"name": "Debug Node",
33+
"type": "node",
34+
"request": "launch",
35+
"cwd": "${workspaceFolder}",
36+
"runtimeExecutable": "npm",
37+
"runtimeArgs": ["run-script", "node:debug"]
3038
}
3139
]
3240
}

CHANGELOG.md

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

3+
# 4.0.0
4+
5+
BREAKING_CHANGES:
6+
- Migrate from [mustache](https://mustache.github.io/) template engine to [ETA](https://eta.js.org/) template engine. (Thanks @Fl0pZz)
7+
- Critical change in `HttpResponse` type (Remove `D | null`, `E | null` unions)
8+
```diff
9+
interface HttpResponse<D extends unknown, E extends unknown = unknown> extends Response {
10+
- data: D | null;
11+
+ data: D;
12+
- error: E | null;
13+
+ error: E;
14+
}
15+
```
16+
17+
Features:
18+
- `--modular` option. Allows to generate api class per module name.
19+
Example: [here](./tests/spec/modular)
20+
- new templates on [ETA](https://eta.js.org/) (enhanced EJS) which can improve your templates! (Thanks @Fl0pZz)
21+
[ETA extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=shadowtime2000.eta-vscode) (thanks @shadowtime2000)
22+
Also moved out to templates:
23+
- `procedure-call.eta` (request api method template)
24+
- `route-name.eta` (api method name template)
25+
- `route-docs.eta` (api method docs template)
26+
27+
No worry about strange syntax it is very simple in usage :)
28+
- Optional templates feature (Except templates using in `includeFile` `ETA` directive)
29+
Now you can store only the `ETA` templates which you need to change for yourself.
30+
- `--extract-request-params` option. Generate path and query request params data contract and modify request payload args
31+
Example:
32+
![extract-request-params](./assets/changelog_assets/extractRequestParams.jpg)
33+
- Improve `data-contracts.eta` template. Added more power :)
34+
- Add `extraTemplates` property for `generateApi()`. Allows to generate extra files via this tool.
35+
- Add `hooks` property for `generateApi()`
36+
```ts
37+
hooks?: Partial<{
38+
onCreateComponent: (component: SchemaComponent) => SchemaComponent | void;
39+
onParseSchema: (originalSchema: any, parsedSchema: any) => any | void;
40+
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void;
41+
/** Start point of work this tool (after fetching schema) */
42+
onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
43+
/** Allows to customize configuration object before sending it to templates. */
44+
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
45+
}>;
46+
```
47+
```ts
48+
generateApi({
49+
input: "./schema.json",
50+
output: "./__generated__",
51+
hooks: {
52+
onCreateComponent(component) {
53+
// do something
54+
return component;
55+
},
56+
// ...
57+
}
58+
})
59+
```
60+
61+
Internal:
62+
- Update all dependencies to latest
63+
64+
Fixes:
65+
- `x-enumNames` support for enums
66+
- Problem of complex types (`oneOf`, `allOf`) with `properties` field
67+
- `additionalProperties: true` should make `[key: string]: any` for object types (Thanks @brookjordan for issue #103)
68+
69+
Common:
70+
- `HttpClient` is exportable by default
71+
- Improve typings when use `swagger-typescript-api` with NodeJS (`index.d.ts`)
72+
373
# 3.1.2
474
Fixes:
575
- axios vulnerability (#101 issue, thanks @Mvbraathen)

README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Greenkeeper badge](https://badges.greenkeeper.io/acacode/swagger-typescript-api.svg)](https://greenkeeper.io/)
44
[![NPM badge](https://img.shields.io/npm/v/swagger-typescript-api.svg)](https://www.npmjs.com/package/swagger-typescript-api)
5+
[![Build Status](https://travis-ci.org/acacode/swagger-typescript-api.svg?branch=master)](https://travis-ci.org/acacode/swagger-typescript-api)
56

67
<img src="https://raw.githubusercontent.com/acacode/swagger-typescript-api/master/assets/swagger-typescript-api-logo.png" align="left"
78
title="swagger-typescript-api logo by js2me" width="93" height="180">
@@ -23,9 +24,8 @@ Any questions you can ask [**here**](https://github.com/acacode/swagger-typescri
2324

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

26-
## 🛑 It is the latest version on mustache templates
27-
Next versions 4.0.0+ will use the [ETA](https://eta.js.org/docs/syntax) templates.
28-
If you want to create fork with `mustache` templates use `mustache-latest` branch
27+
## 🛑 It is new version with [ETA](https://eta.js.org/docs/syntax) templates
28+
Version with `mustache` templates is `>4.0.0`
2929

3030
## 📄 Usage
3131

@@ -47,9 +47,13 @@ Options:
4747
--union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
4848
--route-types generate type definitions for API routes (default: false)
4949
--no-client do not generate an API class
50+
--enum-names-as-values use values in 'x-enumNames' as enum values (not only as keys) (default: false)
5051
--js generate js api module with declaration file (default: false)
52+
--extract-request-params extract request params to data contract (default: false)
53+
Also combine path params and query params into one object
5154
--module-name-index <number> determines which path index should be used for routes separation (default: 0)
5255
(example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)
56+
--modular generate separated files for http client, data contracts, and routes (default: false)
5357
-h, --help display help for command
5458
```
5559

@@ -67,15 +71,23 @@ generateApi({
6771
name: "MySuperbApi.ts", // name of output typescript file
6872
url: 'http://api.com/swagger.json', // url where located swagger schema
6973
})
70-
.then(sourceFile => fs.writeFile(path, sourceFile))
74+
.then(({ files, configuration }) => {
75+
files.forEach(({ content, name }) => {
76+
fs.writeFile(path, content);
77+
});
78+
})
7179
.catch(e => console.error(e))
7280

7381
// example with local file
7482
generateApi({
7583
name: "ApiModule.ts", // name of output typescript file
7684
input: resolve(process.cwd(), './foo/swagger.json') // path to swagger schema
7785
})
78-
.then(sourceFile => fs.writeFile(path, sourceFile))
86+
.then(({ files, configuration }) => {
87+
files.forEach(({ content, name }) => {
88+
fs.writeFile(path, content);
89+
});
90+
})
7991
.catch(e => console.error(e))
8092

8193
// example with parsed schema
@@ -98,19 +110,36 @@ generateApi({
98110
// ...
99111
}
100112
})
101-
.then(sourceFile => fs.writeFile(path, sourceFile))
113+
.then(({ files, configuration }) => {
114+
files.forEach(({ content, name }) => {
115+
fs.writeFile(path, content);
116+
});
117+
})
102118
.catch(e => console.error(e))
103119

104120
```
105121

106122

107123
## 💎 options
108124
### **`--templates`**
109-
This option should be used in cases when you don't want to use default `swagger-typescript-api` output structure
125+
This option needed for cases when you don't want to use default `swagger-typescript-api` output structure
126+
127+
Templates:
128+
- `api.eta` - Api class module
129+
- `data-contracts.eta` - all types (data contracts) from swagger schema
130+
- `http-client.eta` - HttpClient class module
131+
- `procedure-call.eta` - route in Api class
132+
- `route-docs.eta` - documentation for route in Api class
133+
- `route-name.eta` - route name for route in Api class
134+
- `route-type.eta` - *(`--route-types` option)*
135+
- `route-types.eta` - *(`--route-types` option)*
136+
110137
How to use it:
111-
1. copy [**swagger-typescript-api templates**](https://github.com/acacode/swagger-typescript-api/tree/mustache-latest/src/templates/defaults) into your place in project
138+
1. copy swagger-typescript-api templates into your place in project
139+
- from [/templates/default](https://github.com/acacode/swagger-typescript-api/tree/next/templates/default) for single api file
140+
- from [/templates/modular](https://github.com/acacode/swagger-typescript-api/tree/next/templates/modular) for multiple api files (with `--modular` option)
112141
1. add `--templates PATH_TO_YOUR_TEMPLATES` option
113-
2. modify [Mustache](https://mustache.github.io/) templates as you like
142+
2. modify [ETA](https://eta.js.org/docs/syntax) templates as you like
114143

115144
### **`--module-name-index`**
116145
This option should be used in cases when you have api with one global prefix like `/api`
@@ -139,10 +168,10 @@ When we change it to `--module-name-index 1` then Api class have two properties
139168

140169
## 🛠️ Contribution
141170

142-
You can manually check your changes at schemas in `tests` folder before create a PR.
143-
To do that have scripts:
144-
- `npm run generate` - generate API modules from schemas in `tests` folder
145-
- `npm run validate` - validate generated API modules via TypeScript
171+
172+
❗❗❗ Please use the `next` branch :)
173+
174+
If you need to check your changes at schemas in `tests` folder before create a PR just run command `npm run test:all`
146175

147176
## 📝 License
148177
Licensed under the [MIT License](https://github.com/acacode/swagger-typescript-api/blob/master/LICENSE).
59.1 KB
Loading

0 commit comments

Comments
 (0)