Skip to content

Commit 14067a2

Browse files
committed
feat: add postman support
1 parent 75ec836 commit 14067a2

File tree

7 files changed

+380
-13
lines changed

7 files changed

+380
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"axios": ">=0.27",
3838
"chalk": "^4.1.2",
3939
"js-yaml": "^4.1.0",
40+
"postman-to-openapi": "^2.4.1",
4041
"qs": "^6.11.0",
4142
"recursive-readdir": "^2.2.2",
4243
"swagger2openapi": "^7.0.8",

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
# Swagger-Typescript: Generate ts/js code from swagger/openApi JSON
88

9-
Support OpenApi v3 and swagger v2
9+
Support OpenApi v3, swagger v2 and postman collection
1010

11-
An auto typescript/javascript code generator from swagger.
11+
An auto typescript/javascript code generator from APIs doc.
1212
Each endpoint will be created as a function, full type base.
1313
Supported
1414

@@ -81,7 +81,7 @@ For Example:
8181

8282
| [`Key`] | [`default`] | Comment |
8383
| -------------------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
84-
| `url` | Required | Address of swagger.json ([specific branch](#specific-branch)) |
84+
| `url` | Required | swagger or postman collection Address. can be online or local (json/yaml) ([specific branch](#specific-branch)) |
8585
| `dir` | Required | Address of output |
8686
| `language` | `typescript` | export to "javascript" or "typescript" |
8787
| `methodName` | `{method}{path}` | Supported mixed of "{method}{path}{operationId}". for Example: 'service{method}{path}' |

src/getJson.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import Axios from "axios";
2+
import { readFileSync } from "fs";
23
import yaml from "js-yaml";
34

45
async function getJson(url: string) {
5-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
6+
let input;
7+
if (url.startsWith("http")) {
8+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
9+
const { data } = await Axios.get(url);
610

7-
const { data } = await Axios.get(url);
11+
// if url is yaml file convert it to json
12+
input = data;
13+
} else {
14+
const data = readFileSync(url).toString();
15+
input = data;
16+
}
817

9-
// if url is yaml file convert it to json
10-
if (typeof data === "object") {
11-
return data;
18+
if (typeof input === "object") {
19+
return input;
20+
} else if (url.endsWith("json")) {
21+
return JSON.parse(input);
1222
}
13-
return yaml.load(data);
23+
return yaml.load(input);
1424
}
1525

1626
export { getJson };

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { getCurrentUrl, majorVersionsCheck } from "./utils";
66
import { swaggerToOpenApi } from "./utilities/swaggerToOpenApi";
77
import chalk from "chalk";
88
import { partialUpdateJson } from "./updateJson";
9+
import postmanToOpenApi from "postman-to-openapi";
10+
import yaml from "js-yaml";
911

1012
/** @param config If isn't defined will be use swagger.config.json instead */
1113
async function generate(config?: SwaggerConfig, cli?: Partial<Config>) {
@@ -53,8 +55,12 @@ const generateService = async (config: Config, cli?: Partial<Config>) => {
5355
// convert swagger v2 to openApi v3
5456
config._isSwagger2 = true;
5557
input = await swaggerToOpenApi(input);
56-
} else {
58+
} else if (input.openapi) {
5759
majorVersionsCheck("3.0.0", input.openapi);
60+
} else {
61+
input = yaml.load(
62+
await postmanToOpenApi(JSON.stringify(input), undefined),
63+
) as SwaggerJson;
5864
}
5965
}
6066

@@ -111,8 +117,7 @@ function getLocalJson(dir: string) {
111117
const swaggerJsonPath = `${dir}/swagger.json`;
112118

113119
try {
114-
const old = readFileSync(swaggerJsonPath).toString();
115-
return JSON.parse(old);
120+
return readJson(swaggerJsonPath);
116121
} catch (error) {
117122
chalk.red(
118123
"swagger.json file not found. You should set keepJson true to save json then run swag-ts without tag to save that",
@@ -121,4 +126,9 @@ function getLocalJson(dir: string) {
121126
}
122127
}
123128

129+
function readJson(path: string) {
130+
const old = readFileSync(path).toString();
131+
return JSON.parse(old);
132+
}
133+
124134
export { generate };

swagger.config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,17 @@
3434
"terminalId"
3535
]
3636
}
37+
},
38+
{
39+
"url": "./testFiles/Wms.postman_collection.json",
40+
"dir": "./test/postman",
41+
"language": "typescript",
42+
"ignore": {
43+
"headerParams": [
44+
"Accept",
45+
"Content-Encoding",
46+
"Content-Type"
47+
]
48+
}
3749
}
3850
]

0 commit comments

Comments
 (0)