-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrowserMiddleware.js
216 lines (173 loc) · 5.28 KB
/
browserMiddleware.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
var window = require('global')
var utils = require('./middlewareUtils')
var querystringLite = require('./querystring-lite')
var randomString = require('random-string')
function middleware (name, fn) {
exports[name] = fn
fn.httpismMiddleware = {
name: name
}
}
middleware('exception', utils.exception)
middleware('querystring', utils.querystring)
middleware('expandUrl', utils.expandUrl)
middleware('json', function (request, next) {
if (request.body instanceof Object) {
request.body = JSON.stringify(request.body)
utils.setHeaderTo(request, 'content-type', 'application/json')
}
utils.setHeaderTo(request, 'accept', 'application/json')
return next().then(function (response) {
if (utils.shouldParseAs(response, 'json', request)) {
response.body = JSON.parse(response.body, request.options.jsonReviver)
}
return response
})
})
function randomGlobal (value) {
var name
do {
name = '_' + randomString({ length: 20 })
} while (typeof window[name] !== 'undefined')
window[name] = value
return name
}
middleware('jsonp', function (request, next) {
var jsonp = request.options.jsonp
if (jsonp) {
request.options.querystring = request.options.querystring || {}
return new Promise(function (resolve, reject) {
var callbackName = randomGlobal(function (v) {
delete window[callbackName]
document.head.removeChild(script)
resolve({
statusCode: 200,
headers: {},
body: v
})
})
request.options.querystring[jsonp] = callbackName
utils.mergeQueryString(request)
var script = document.createElement('script')
script.type = 'text/javascript'
script.src = request.url
script.onerror = function () {
reject(new Error('could not load script tag for JSONP request: ' + request.url))
}
document.head.appendChild(script)
})
}
return next()
})
middleware('text', function (request, next) {
if (typeof request.body === 'string') {
utils.setHeaderTo(request, 'content-type', 'text/plain;charset=UTF-8')
}
return next()
})
middleware('form', function (request, next) {
if (request.options.form && request.body instanceof Object) {
var querystring = request.options.qs || querystringLite
setBodyToString(request, querystring.stringify(request.body))
utils.setHeaderTo(request, 'content-type', 'application/x-www-form-urlencoded')
}
return next().then(function (response) {
var querystring = request.options.qs || querystringLite
if (utils.shouldParseAs(response, 'form', request)) {
response.body = querystring.parse(response.body)
}
return response
})
})
function setBodyToString (r, s) {
r.body = s
}
function parseHeaders (headers) {
var object = {}
var lines = headers.split('\n')
for (var n = 0; n < lines.length; n++) {
var line = lines[n]
var match = /^(.*?):(.*)/.exec(line)
if (match) {
object[match[1].toLowerCase()] = match[2].trim()
}
}
return object
}
function toUpperCase (x) {
return x.toUpperCase()
}
function formatHeaderName (name) {
return name.replace(/^([a-z])/, toUpperCase).replace(/-([a-z])/g, toUpperCase)
}
function setHeaders (headers, xhr) {
var headerNames = Object.keys(headers)
for (var n = 0; n < headerNames.length; n++) {
var key = headerNames[n]
var headerName = formatHeaderName(key)
xhr.setRequestHeader(headerName, headers[key])
}
}
function responseUrl (xhr, requestUrl) {
var origin = window.location.origin
var responseUrl = xhr.responseURL
if (responseUrl) {
if (responseUrl.substring(0, origin.length) === origin) {
return responseUrl.substring(origin.length)
} else {
return responseUrl
}
} else {
return requestUrl
}
}
middleware('send', function (request) {
var Xhr = request.options.xhr || window.XMLHttpRequest
var xhr = new Xhr()
var rejectPromise
var promise = new Promise(function (resolve, reject) {
rejectPromise = reject
xhr.open(request.method, request.url, true)
xhr.onload = function () {
var statusCode = xhr.status
var response = {
body: statusCode === 204 ? undefined : xhr.responseText,
headers: parseHeaders(xhr.getAllResponseHeaders()),
statusCode: statusCode,
url: responseUrl(xhr, request.url),
xhr: xhr,
statusText: xhr.statusText
}
resolve(response)
}
xhr.onerror = function () {
rejectPromise(new Error('failed to connect to ' + request.method + ' ' + request.url))
}
if (!isCrossDomain(request.url) && !request.headers['x-requested-with']) {
request.headers['x-requested-with'] = 'XMLHttpRequest'
}
setHeaders(request.headers, xhr)
xhr.withCredentials = !!request.options.withCredentials
xhr.send(request.body)
})
function abort () {
xhr.abort()
var error = new Error('aborted connection to ' + request.method + ' ' + request.url)
error.aborted = true
rejectPromise(error)
}
addAbortToPromise(promise, abort)
return promise
})
function isCrossDomain (url) {
return /^https?:\/\//.test(url)
}
function addAbortToPromise (promise, abort) {
var then = promise.then
promise.then = function () {
var p = then.apply(this, arguments)
p.abort = abort
addAbortToPromise(p, abort)
return p
}
}