This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.js
executable file
·94 lines (85 loc) · 3.49 KB
/
convert.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
89
90
91
92
93
94
var Path = require('path');
var pd = require('pretty-data').pd;
var soap = require('soap');
var uuid = require('uuid');
var converter = {
parseFile: function (url, callback) {
soap.createClient(url, (err, client) => this.convertResource(err, client, callback).bind(this));
},
convertResource: function (err, client, callback) {
let collection = {};
let wsdlBasename = Path.basename(client.wsdl.uri);
let wsdlExtension = Path.extname(wsdlBasename);
collection.info = {
_postman_id: uuid.v4(),
name: wsdlBasename.substr(0, wsdlBasename.indexOf(wsdlExtension)),
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
};
let clientDescription = client.describe();
collection.item = Object.keys(clientDescription).sort()
.map((serviceName) => {
return {
id: uuid.v4(),
name: serviceName,
item: this.parseService(client, serviceName, clientDescription[serviceName])
};
});
callback(collection);
},
parseService: function (client, serviceName, serviceDescription) {
return Object.keys(serviceDescription).sort()
.map((portName) => {
return {
id: uuid.v4(),
name: portName,
item: this.parsePort(client, serviceName, portName, serviceDescription[portName])
};
});
},
parsePort: function (client, serviceName, portName, portDescription) {
return Object.keys(portDescription).sort()
.map((operationName) => {
let operationItem = {
id: uuid.v4(),
name: operationName,
request: {
method: 'POST',
body: { mode: 'raw' },
description: ''
}
};
client.httpClient = {
request: function (rurl, data, callback, exheaders, exoptions) {
operationItem.request.body.raw = pd.xml(data);
operationItem.request.header = Object.keys(exheaders).map((headerKey) => {
return {
key: headerKey,
value: exheaders[headerKey]
};
});
operationItem.request.url = rurl;
}
};
let requestParameter = this.expand(portDescription[operationName].input);
client[serviceName][portName][operationName](requestParameter);
return operationItem;
});
},
expand: function (input) {
let keys = Object.keys(input);
if (keys.length === 0) return '?';
let obj = {};
keys.filter((fieldName) => fieldName !== 'targetNSAlias' && fieldName !== 'targetNamespace')
.forEach((fieldName) => {
let parameterValue = typeof input[fieldName] === 'object' ? this.expand(input[fieldName]) : '?';
if (fieldName.endsWith('[]')) {
obj[fieldName.substr(0, fieldName.length - 2)] = [parameterValue];
}
else {
obj[fieldName] = parameterValue;
}
});
return obj;
}
};
module.exports = converter;