forked from postmanlabs/swagger2-postman2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert-zuora-swagger.js
88 lines (75 loc) · 2.62 KB
/
convert-zuora-swagger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require("fs");
const refParser = require("json-schema-ref-parser");
const swagger2Postman = require("./index.js");
var specURL = "https://www.zuora.com/developer/yaml/swagger.yaml";
console.log(`Downloading Swagger spec...`);
refParser.dereference(specURL, {}, (err, spec) => {
if (err) {
console.error("ERROR: Cannot dereference Swagger spec");
return;
}
var specVersion = spec.info.version;
console.log(`Swagger spec version is ${specVersion}`);
// Prepare the Swagger spec
delete spec.definitions;
spec.info.title = `Zuora REST API (${specVersion})`;
spec.info.description = `See https://www.zuora.com/developer/api-reference/ for the latest REST API documentation
This collection was generated using https://github.com/davidwzuora/swagger2-postman2`;
spec.host = "{{zuora_host}}";
// Convert the Swagger spec
console.log("Converting Swagger spec to a Postman collection...");
var collection = swagger2Postman.convert(spec).collection;
// Define a variable for the host
collection.variables = [{
key: "zuora_host",
value: "rest.apisandbox.zuora.com",
type: "string"
}];
// Set the authorization method to Bearer Token
collection.auth = {
type: "bearer",
bearer: [{
key: "token",
value: "{{bearer_token}}",
type: "string"
}]
};
// Modify POST /oauth/token
collection.item.forEach(item => {
if (
item.hasOwnProperty("request") &&
item.request.method == "POST" &&
item.request.url.path.length == 2 &&
item.request.url.path[0] == "oauth" &&
item.request.url.path[1] == "token"
) {
// Don't use Bearer Token authorization
item.request.auth = {
type: "noauth"
};
// Define a test script that saves the generated token
item.event = [{
listen: "test",
script: {
type: "text/javascript",
exec: [
"tests[\"OAuth token generated\"] = (",
" responseCode.code === 200 &&",
" JSON.parse(responseBody).hasOwnProperty(\"access_token\")",
");",
"",
"if (tests[\"OAuth token generated\"]) {",
" var body = JSON.parse(responseBody);",
" postman.setEnvironmentVariable(\"bearer_token\", body.access_token);",
"}"
]
}
}];
}
});
// Save the Postman collection
var postmanFilename = `zuora-postman-${specVersion}.json`;
var postmanJSON = JSON.stringify(collection, null, 2);
fs.writeFileSync(postmanFilename, postmanJSON, "utf8");
console.log(`Saved Postman collection as ${postmanFilename}`);
});