Skip to content

Commit f4f058b

Browse files
authored
[Features] - response declarations, "default" status codes as success response types (#17)
* feat: add @returns keywords into each request comments description (request responses) * feat: prettify descriptions * feat: add response declarations in request description; feat: typed bad responses * feat: -d, --default-as-success option (ability to use "default" success response status code) * chore: add common test schemas * fix: description of defaultResponseAsSuccess option * chore: rename setConfig to addToConfig * chore: add specific comments for calling src/index.js on unix like machines via nodejs * feat: move responses declarations into flag '-r, --responses'; chore(test): add manual test for new option; docs: update CHANGELOG + README
1 parent bd304fd commit f4f058b

Some content is hidden

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

61 files changed

+9521
-5416
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# next release
22
Breaking Changes:
3-
- Rename default typescript api file name (prev `api.ts`, now `Api.ts`)
3+
- Rename default typescript output api file name (prev `api.ts`, now `Api.ts`)
44
Features:
5+
- `-d, --default-as-success` option. Allows to use "default" status codes as success response type
6+
- `-r, --responses` option. Response declarations in request rescription
7+
This option adds comments of the possible responses from request
8+
![responses comments](./assets/changelog_assets/responses-comments.jpg)
9+
Also typings for `.catch()` callback
10+
![responses catch types](./assets/changelog_assets/responses-catch-types.jpg)
511
- Improve response body type definitions
12+
- Types for bad responses
613
Changes:
714
- \[minor\] fix jsdoc comments space
8-
![api description](./assets/changelog_assets/right-comments-space.jpg)
15+
![right comments space](./assets/changelog_assets/right-comments-space.jpg)
916

1017
# 1.3.0
1118
Features:

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,23 @@ All examples you can find [**here**](https://github.com/acacode/swagger-typescri
2525

2626
## 📄 Usage
2727

28-
```cool
28+
```muse
2929
Usage: sta [options]
3030
Usage: swagger-typescript-api [options]
3131
3232
Options:
33-
-v, --version output the current version
34-
-p, --path <path> path/url to swagger scheme
35-
-o, --output <output> output path of typescript api file (default: "./")
36-
-n, --name <name> name of output typescript api file (default: "Api.ts")
37-
--route-types generate type definitions for API routes (default: false)
38-
--no-client do not generate an API class
39-
-h, --help output usage information
33+
-v, --version output the current version
34+
-p, --path <path> path/url to swagger scheme
35+
-o, --output <output> output path of typescript api file (default: "./")
36+
-n, --name <name> name of output typescript api file (default: "Api.ts")
37+
-d, --default-as-success use "default" response status code as success response too.
38+
some swagger schemas use "default" response status code
39+
as success response type by default. (default: false)
40+
-r, --responses generate additional information about request responses
41+
also add typings for bad responses
42+
--route-types generate type definitions for API routes (default: false)
43+
--no-client do not generate an API class
44+
-h, --help output usage information
4045
```
4146

4247
Also you can use `npx`:
22.8 KB
Loading
52 KB
Loading

index.d.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11

2-
interface BaseGenerateApiParams {
2+
interface GenerateApiParams {
3+
4+
/**
5+
* path to swagger schema
6+
*/
7+
input: string;
8+
9+
/**
10+
* url to swagger schema
11+
*/
12+
url: string;
313

414
/**
515
* default 'api.ts'
@@ -10,23 +20,29 @@ interface BaseGenerateApiParams {
1020
* path to folder where will been located the created api module
1121
*/
1222
output?: string;
13-
}
14-
15-
interface LocalFileGenerateApiParams extends BaseGenerateApiParams {
23+
24+
/**
25+
* generate type definitions for API routes (default: false)
26+
*/
27+
generateRouteTypes?: boolean;
1628

1729
/**
18-
* path to swagger schema
30+
* do not generate an API class
1931
*/
20-
input: string;
21-
}
32+
generateClient?: boolean;
2233

23-
interface UrlGenerateApiParams extends BaseGenerateApiParams {
34+
/**
35+
* use "default" response status code as success response too.
36+
* some swagger schemas use "default" response status code as success response type by default.
37+
*/
38+
defaultResponseAsSuccess?: boolean;
2439

2540
/**
26-
* url to swagger schema
41+
* generate additional information about request responses
42+
* also add typings for bad responses
2743
*/
28-
url: string;
44+
generateResponses?: boolean;
2945
}
3046

31-
export declare function generateApi(params: LocalFileGenerateApiParams): Promise<string>
32-
export declare function generateApi(params: UrlGenerateApiParams): Promise<string>
47+
export declare function generateApi(params: Omit<GenerateApiParams, "url">): Promise<string>
48+
export declare function generateApi(params: Omit<GenerateApiParams, "input">): Promise<string>

index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,40 @@ program
1717
.requiredOption('-p, --path <path>', 'path/url to swagger scheme')
1818
.option('-o, --output <output>', 'output path of typescript api file', './')
1919
.option('-n, --name <name>', 'name of output typescript api file', 'Api.ts')
20+
.option(
21+
'-d, --default-as-success',
22+
'use "default" response status code as success response too.\n' +
23+
'some swagger schemas use "default" response status code as success response type by default.',
24+
false
25+
)
26+
.option(
27+
'-r, --responses',
28+
'generate additional information about request responses\n' +
29+
'also add typings for bad responses',
30+
false,
31+
)
2032
.option('--route-types', 'generate type definitions for API routes', false)
2133
.option('--no-client', 'do not generate an API class', false);
2234

2335
program.parse(process.argv);
2436

25-
const { path, output, name, routeTypes, client } = program;
37+
const {
38+
path,
39+
output,
40+
name,
41+
routeTypes,
42+
client,
43+
defaultAsSuccess,
44+
responses,
45+
} = program;
2646

2747
generateApi({
2848
name,
2949
url: path,
3050
generateRouteTypes: routeTypes,
3151
generateClient: client,
52+
defaultResponseAsSuccess: defaultAsSuccess,
53+
generateResponses: responses,
3254
input: resolve(process.cwd(), path),
3355
output: resolve(process.cwd(), output || '.')
3456
})

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
"version": "1.3.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
6-
"cli": "node index.js -p ./tests/schemas/v3.0/personal-api-example.json -n swagger-test-cli.ts",
6+
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
77
"cli:debug": "node --nolazy --inspect-brk=9229 index.js -p ./tests/schemas/v2.0/adafruit.yaml -n swagger-test-cli.ts",
88
"cli:help": "node index.js -h",
9-
"test:all": "npm-run-all generate validate test:routeTypes test:noClient --continue-on-error",
9+
"test:all": "npm-run-all generate validate test:routeTypes test:noClient test:defaultAsSuccess test:responses --continue-on-error",
1010
"generate": "node tests/generate.js",
1111
"generate:debug": "node --nolazy --inspect-brk=9229 tests/generate.js",
1212
"validate": "node tests/validate.js",
1313
"validate:debug": "node --nolazy --inspect-brk=9229 tests/validate.js",
1414
"test:routeTypes": "node tests/spec/routeTypes/test.js",
15-
"test:noClient": "node tests/spec/noClient/test.js"
15+
"test:noClient": "node tests/spec/noClient/test.js",
16+
"test:defaultAsSuccess": "node tests/spec/defaultAsSuccess/test.js",
17+
"test:responses": "node tests/spec/responses/test.js"
1618
},
1719
"author": "acacode",
1820
"license": "MIT",

src/apiConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const _ = require("lodash")
1+
const { formatDescription } = require("./common");
22

33
const createApiConfig = ({ info, servers, }) => {
44
const server = (servers && servers[0]) || { url: '' }
@@ -29,7 +29,7 @@ const createApiConfig = ({ info, servers, }) => {
2929
baseUrl: server.url,
3030
title: info.title,
3131
version: info.version,
32-
description: _.replace(info.description, /\*\//g, "*\/"),
32+
description: formatDescription(info.description),
3333
}
3434
}
3535

src/common.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const _ = require("lodash");
2+
3+
module.exports = {
4+
formatDescription: (description, inline) => {
5+
let prettified = description;
6+
7+
prettified = _.replace(prettified, /\*\//g, "*\/")
8+
9+
if (inline && _.includes(prettified, '\n')) {
10+
prettified = _(prettified).split(/\n/g)
11+
.map(part => _.trim(part))
12+
.compact()
13+
.join(' ')
14+
.valueOf()
15+
}
16+
17+
return prettified;
18+
}
19+
}

src/config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

22
const config = {
3+
/** CLI flag */
4+
generateResponses: false,
5+
/** CLI flag */
6+
defaultResponseAsSuccess: false,
37
/** CLI flag */
48
generateRouteTypes: false,
59
/** CLI flag */
@@ -14,4 +18,4 @@ const config = {
1418
module.exports = {
1519
addToConfig: configParts => Object.assign(config, configParts),
1620
config,
17-
}
21+
}

0 commit comments

Comments
 (0)