req_partner_manager.js
6.94 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
let _ = require('lodash')
module.exports = function(reqBody, partnerConfig) {
this.reqBody = reqBody;
this.partnerConfig = JSON.parse(JSON.stringify(partnerConfig));
this.requestApi = function() {
try {
let body;
let headers = getReqHeader(this.partnerConfig.request.header, this.reqBody);
this.partnerConfig.request.header = headers
if(this.partnerConfig.request.ContentType == 'application/json') {
body = getReqBodyJson(this.partnerConfig.request.body, this.reqBody)
} else if(this.partnerConfig.request.ContentType == 'text/xml'){
body = getReqBodyXml(this.partnerConfig.request.body, this.reqBody)
} else {
throw {
status : 'unsupport',
cause : 'unsupport content-type ' + this.partnerConfig.request.ContentType
}
}
// console.log('body', body)
if(body) {
this.partnerConfig.request.body = body
}
return this.partnerConfig.request;
} catch (err) {
console.log(err)
return new Error(JSON.stringify(err));
}
}
}
function getReqHeader(headers, dataHeaders) {
let headersObject = {};
headers = replaceSpace(headers).split(',');
//replce headers
for(let headerIndex in headers) {
let header = headers[headerIndex];
let key = header.split('=')[0]
if(header.indexOf('$') !== -1) {
let keyValue = header.split('$')[1];
headers[headerIndex] = header.replace('$'+keyValue,replaceValueMadatory(keyValue,dataHeaders));
}
headersObject[key] = headers[headerIndex].split('=')[1];
}
return headersObject;
}
function getReqBodyJson(bodyJson, dataBodyJson) {
let bodyObject = {};
bodyJson = replaceSpace(bodyJson).split(',')
//replce body
for(let bodyIndex in bodyJson) {
let body = bodyJson[bodyIndex];
let key = body.split('=')[0];
let keyValue = bodyJson[bodyIndex].split('=')[1]
let insertKeyFlag = true;
if(key.indexOf('@') !== -1) { // case array
insertKeyFlag = false
// let tempObjectArray = {}
let keyArray = searchKeyArray(key);
let keyValueArray = searchKeyArray(keyValue);
if(searchValueArray(keyValue, keyValueArray, dataBodyJson)) {
let index = 0;
let breakWhile = true;
while(breakWhile) {
let findKey = keyValue.replace(keyValue,keyValueArray.substring(1) + '['+index+']');
let keyObject = keyArray.replace(keyArray,keyArray.substring(1) + '['+index+']');
if(_.has(dataBodyJson, findKey)) {
bodyObject[keyObject] = _.get(dataBodyJson, findKey)
} else {
breakWhile = false
}
// bodyObject[findKey] = bodyJson[bodyIndex].split('=')[1];
index++
}
} else {
throw {
status : 'missing',
cause : 'missing parameter ' + key
}
}
} else { // case object
if(body.indexOf('$') !== -1) {
let keyValue = body.split('$')[1];
bodyJson[bodyIndex] = body.replace('$'+keyValue,replaceValueMadatory(keyValue,dataBodyJson));
} else if(body.indexOf('?') !== -1) {
let keyValue = body.split('?')[1];
if(checkValueOptional(keyValue, dataBodyJson)) {
bodyJson[bodyIndex] = body.replace('?'+keyValue,replaceValueOptional(keyValue,dataBodyJson));
} else {
insertKeyFlag = false;
}
}
}
if(insertKeyFlag) {
bodyObject[key] = bodyJson[bodyIndex].split('=')[1];
} else {
insertKeyFlag = true;
}
}
return buildDataToObject(bodyObject);
}
function getReqBodyXml(bodyXml, dataBodyXml) {
let listKeyReplace = {};
let tempXml = bodyXml;
let index = 0;
while (true) {
index = tempXml.indexOf('>$')
if(index === -1) {
index = tempXml.indexOf('>?')
if(index === -1) {
break;
}
}
let first = tempXml.substring(tempXml.substring(0, index).lastIndexOf('<'),index+1)
let last = tempXml.substring(index+1).substring(0, tempXml.substring(index+1).indexOf('>')+1)
let valueKey = last.substring(0, last.indexOf('<'));
let fullKey = first + last;
listKeyReplace[fullKey] = valueKey;
tempXml = tempXml.substring(tempXml.indexOf(fullKey) + fullKey.length);
// console.log(tempXml);
}
let valueXml = getValueXml(listKeyReplace, dataBodyXml)
console.log(valueXml)
for(let valueXmlKey in valueXml) {
if(valueXml[valueXmlKey] === undefined) {
bodyXml = bodyXml.replace(valueXmlKey, '')
} else {
bodyXml = bodyXml.replace(valueXmlKey, valueXml[valueXmlKey])
}
}
return bodyXml
}
function getValueXml(listKeyXml, dataBodyXml) {
for(let key in listKeyXml) {
if(listKeyXml[key].startsWith('$')) {
listKeyXml[key] = key.replace(listKeyXml[key], replaceValueMadatory(listKeyXml[key].substring(1), dataBodyXml))
} else if(listKeyXml[key].startsWith('?')){
if(checkValueOptional(listKeyXml[key].substring(1), dataBodyXml)) {
listKeyXml[key] = key.replace(listKeyXml[key], replaceValueOptional(listKeyXml[key].substring(1), dataBodyXml))
} else {
listKeyXml[key] = ''
}
}
}
return listKeyXml;
}
function replaceSpace(data) {
return data && typeof data == 'string' ? data.replace(/ /g, '') : data;
}
function replaceValueMadatory(key, value) {
if(_.has(value, key)) {
return _.get(value, key);
} else {
throw {
status : 'missing',
cause : 'missing parameter ' + key
};
}
}
function replaceValueOptional(key, value) {
if(_.has(value, key)) {
return _.get(value, key);
} else {
return;
}
}
function checkValueOptional(key, value) {
return _.has(value, key) ? true : false;
}
function buildDataToObject(dataObject) {
let newObject = {}
for(let key in dataObject) {
_.set(newObject,key,dataObject[key]);
}
return newObject;
}
function searchKeyArray(key) {
let indexArr = key.indexOf('@')
let lastIndexArr = key.substring(indexArr).indexOf('.')
let keyArray = key.substring(indexArr, lastIndexArr !== -1 ? lastIndexArr + indexArr : key.length)
return keyArray;
}
function searchValueArray(key, idenKey, value) {
let findKey = key.replace(key,idenKey.substring(1) + '[0]')
console.log('findKeyArr', findKey)
return _.has(value,findKey)
}