-
Notifications
You must be signed in to change notification settings - Fork 1
/
rx-request.js
225 lines (194 loc) · 6.09 KB
/
rx-request.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
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
217
218
219
220
221
222
223
224
225
var { Observable } = require("./rx");
function RequestError(url, xhr, message, reason = null) {
this.name = "RequestError";
this.url = url;
this.xhr = xhr;
this.code = xhr.status;
this.reason = reason;
this.message = `request: ${message} (${url})`;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, RequestError);
}
}
RequestError.prototype = new Error();
function RestCallMethodError(url, { code, method, message }) {
this.name = "RestCallMethodError";
this.url = url;
this.code = code;
this.message = `restmethodcall: webservice error status ${code} (${url})${method ? " (" + method + ")" : ""}${message ? "\n" + message : ""}`;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, RestCallMethodError);
}
}
RestCallMethodError.prototype = new Error();
function RestCallResult(response, url, scriptInfo) {
var restCallResult = response.querySelector("RestCallResult");
var status = +restCallResult.querySelector("Status").textContent;
if (status < 0)
throw new RestCallMethodError(url, { code: status, method: scriptInfo });
else
return {
output: restCallResult.querySelector("Output"),
status: status,
};
}
function toJSONForIE(blob) {
try {
return JSON.parse(blob);
} catch(e) {
return null;
}
}
function getResponseHeadersList(xhr, headersList) {
var headers = {}, header;
for (var i = 0; i < headersList.length; i++) {
header = headersList[i];
headers[header] = xhr.getResponseHeader(header);
}
return headers;
}
/**
* Creates an observable HTTP request.
* The options that can be passed are:
*
* - url Request's url
* - [method] HTTP method (defaults is "GET")
* - [data] Sent data for "POST", "UPDATE" or "PATCH" requests
* - [headers] Object containing headers key/value
* - [format] Format of the response, according to the XMLHttpRequest Level 2
* response type: "arraybuffer", "blob", "document", "json" or "text" (defaults)
*/
function request(options) {
if (options.format == "rest-call-method") {
return restCallMethod(options);
}
return Observable.create(observer => {
var {
url,
method,
data,
headers,
format,
withMetadata,
responseHeaders,
} = options;
var xhr = new XMLHttpRequest();
xhr.open(method || "GET", url, true);
// Special case for document format: some manifests may have a
// null response because of wrongly namespaced XML file. Also the
// document format rely on specific Content-Type headers which may
// erroneous. Therefore we use a text responseType and parse the
// document with DOMParser.
if (format == "document") {
xhr.responseType = "text";
} else {
xhr.responseType = format || "text";
}
if (headers) {
for (var name in headers) xhr.setRequestHeader(name, headers[name]);
}
xhr.addEventListener("load", onLoad, false);
xhr.addEventListener("error", onError, false);
var sent = Date.now();
xhr.send(data);
function onLoad(evt) {
var x = evt.target;
var s = x.status;
if (s < 200 || s >= 300) {
return observer.onError(new RequestError(url, x, x.statusText));
}
var duration = Date.now() - sent;
var blob;
if (format == "document") {
blob = new global.DOMParser().parseFromString(x.responseText, "text/xml");
} else {
blob = x.response;
}
if (format == "json" && typeof blob == "string") {
blob = toJSONForIE(blob);
}
if (blob == null) {
return observer.onError(new RequestError(url, x,
`null response with format "${format}" (error while parsing or wrong content-type)`));
}
// TODO(pierre): find a better API than this "withMetadata" flag
// (it is weird and collisions with responseHeaders)
if (withMetadata) {
var headers;
if (responseHeaders) {
headers = getResponseHeadersList(x, responseHeaders);
}
var size = evt.total;
observer.onNext({
blob,
size,
duration,
headers,
url: x.responseURL || url,
xhr: x,
});
}
else {
observer.onNext(blob);
}
observer.onCompleted();
}
function onError(e) {
observer.onError(new RequestError(url, e, "error event"));
}
return () => {
var { readyState } = xhr;
if (0 < readyState && readyState < 4) {
xhr.removeEventListener("load", onLoad);
xhr.removeEventListener("error", onError);
xhr.abort();
}
xhr = null;
};
});
}
var ENTITIES_REG = /[&<>]/g;
var ENTITIES = {
"&": "&",
"<": "<",
">": ">"
};
function escapeXml(xml) {
return (xml || "")
.toString()
.replace(ENTITIES_REG, tag => ENTITIES[tag]);
}
function objToXML(obj) {
var xml = "";
for (var attrName in obj) {
var attr = obj[attrName];
var inner = (typeof attr == "object")
? objToXML(attr)
: escapeXml(attr);
xml += `<${attrName}>${inner}</${attrName}>`;
}
return xml;
}
function getNodeTextContent(root, name) {
var item = root.querySelector(name);
return item && item.textContent;
}
var METHOD_CALL_XML = "<RestCallMethod xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">{payload}</RestCallMethod>";
function restCallMethod(options) {
options.method = "POST";
options.headers = { "Content-Type": "application/xml" };
options.data = METHOD_CALL_XML.replace("{payload}", objToXML(options.data));
options.format = "document";
// options.url = options.url.replace("RestPortalProvider", "JsonPortalProvider");
// options.headers = { "Content-Type": "application/json" };
// options.data = JSON.stringify(options.data);
// options.format = "json";
return request(options)
.map((data) => RestCallResult(data, options.url, options.ScriptInfo));
}
request.escapeXml = escapeXml;
request.RequestError = RequestError;
request.RestCallMethodError = RestCallMethodError;
request.RestCallResult = RestCallResult;
request.getNodeTextContent = getNodeTextContent;
module.exports = request;