Skip to content

Release 3.1.0 #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# next release


# 3.1.0

Features:
- `--moduleNameIndex` option. determines which path index should be used for routes separation (Thanks @nikalun)
Examples:
GET:api/v1/fruites/getFruit -> index:2 -> moduleName -> fruites
GET:api/v1/fruites/getFruit -> index:0 -> moduleName -> api

# 3.0.1

Fixes:
Expand Down
55 changes: 40 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,34 @@ Any questions you can ask [**here**](https://github.com/acacode/swagger-typescri

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

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

## 📄 Usage

```muse
Usage: sta [options]
Usage: swagger-typescript-api [options]

Options:
-v, --version output the current version
-p, --path <path> path/url to swagger scheme
-o, --output <output> output path of typescript api file (default: "./")
-n, --name <name> name of output typescript api file (default: "Api.ts")
-t, --templates <path> path to folder containing templates (default: "./src/templates")
-d, --default-as-success use "default" response status code as success response too.
some swagger schemas use "default" response status code
as success response type by default. (default: false)
-r, --responses generate additional information about request responses
also add typings for bad responses
--union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
--route-types generate type definitions for API routes (default: false)
--no-client do not generate an API class
--js generate js api module with declaration file (default: false)
-h, --help output usage information
-v, --version output the current version
-p, --path <path> path/url to swagger scheme
-o, --output <output> output path of typescript api file (default: "./")
-n, --name <name> name of output typescript api file (default: "Api.ts")
-t, --templates <path> path to folder containing templates
-d, --default-as-success use "default" response status code as success response too.
some swagger schemas use "default" response status code
as success response type by default. (default: false)
-r, --responses generate additional information about request responses
also add typings for bad responses (default: false)
--union-enums generate all "enum" types as union types (T1 | T2 | TN) (default: false)
--route-types generate type definitions for API routes (default: false)
--no-client do not generate an API class
--js generate js api module with declaration file (default: false)
--module-name-index <number> determines which path index should be used for routes separation (default: 0)
(example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)
-h, --help display help for command
```

Also you can use `npx`:
Expand Down Expand Up @@ -97,6 +103,25 @@ generateApi({

```


## 💎 options
### **`--templates`**
This option should be used in cases when you don't want to use default `swagger-typescript-api` output structure
How to use it:
1. copy [**swagger-typescript-api templates**](https://github.com/acacode/swagger-typescript-api/tree/mustache-latest/src/templates/defaults) into your place in project
1. add `--templates PATH_TO_YOUR_TEMPLATES` option
2. modify [Mustache](https://mustache.github.io/) templates as you like

### **`--module-name-index`**
This option should be used in cases when you have api with one global prefix like `/api`
Example:
`GET:/api/fruits/getFruits`
`POST:/api/fruits/addFruits`
`GET:/api/vegetables/addVegetable`
with `--module-name-index 0` Api class will have one property `api`
When we change it to `--module-name-index 1` then Api class have two properties `fruits` and `vegetables`


## 📄 Mass media

- [Why Swagger schemes are needed in frontend development ?](https://dev.to/js2me/why-swagger-schemes-are-needed-in-frontend-development-2cb4)
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ interface GenerateApiParams {
toJS?: boolean;

/**
* url index from paths used for merging into modules
* determines which path index should be used for routes separation
*/
moduleNameIndex?: number;
}
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ program
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
.option("--route-types", "generate type definitions for API routes", false)
.option("--no-client", "do not generate an API class", false)
.option("--js", "generate js api module with declaration file", false);
.option("--js", "generate js api module with declaration file", false)
.option(
"--module-name-index <number>",
"determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)",
0,
);

program.parse(process.argv);

Expand All @@ -48,6 +53,7 @@ const {
defaultAsSuccess,
responses,
js,
moduleNameIndex,
} = program;

generateApi({
Expand All @@ -65,4 +71,5 @@ generateApi({
templates || "./src/templates/defaults",
),
toJS: !!js,
moduleNameIndex: +(moduleNameIndex || 0),
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "swagger-typescript-api",
"version": "3.0.1",
"version": "3.1.0",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
"cli:debug:json": "node --nolazy index.js -p ./swagger-test-cli.json -n swagger-test-cli.ts --union-enums",
"cli:debug:yaml": "node --nolazy index.js -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
"cli:help": "node index.js -h",
"test:all": "npm-run-all generate validate test:routeTypes test:noClient test:defaultAsSuccess test:responses test:templates test:unionEnums test:specProperty test:js --continue-on-error",
"test:all": "npm-run-all generate validate test:routeTypes test:noClient test:defaultAsSuccess test:responses test:templates test:unionEnums test:specProperty test:js test:moduleNameIndex --continue-on-error",
"generate": "node tests/generate.js",
"generate:debug": "node --nolazy tests/generate.js",
"validate": "node tests/validate.js",
Expand All @@ -20,6 +20,7 @@
"test:unionEnums": "node tests/spec/unionEnums/test.js",
"test:responses": "node tests/spec/responses/test.js",
"test:specProperty": "node tests/spec/specProperty/test.js",
"test:moduleNameIndex": "node tests/spec/moduleNameIndex/test.js",
"test:js": "node tests/spec/js/test.js"
},
"author": "acacode",
Expand Down
Loading