/
Http.js
186 lines (148 loc) · 4.17 KB
/
Http.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
/*
import Dict exposing (empty, update)
import Elm.Kernel.Scheduler exposing (binding, fail, rawSpawn, succeed)
import Elm.Kernel.Utils exposing (Tuple2)
import Http exposing (BadUrl_, Timeout_, NetworkError_, BadStatus_, GoodStatus_, Sending, Receiving)
import Maybe exposing (Just, Nothing, isJust)
import Platform exposing (sendToApp, sendToSelf)
import Result exposing (map, isOk)
*/
// SEND REQUEST
var _Http_toTask = F3(function(router, toTask, request)
{
return __Scheduler_binding(function(callback)
{
function done(response) {
callback(toTask(request.__$expect.__toValue(response)));
}
var xhr = new XMLHttpRequest();
xhr.addEventListener('error', function() { done(__Http_NetworkError_); });
xhr.addEventListener('timeout', function() { done(__Http_Timeout_); });
xhr.addEventListener('load', function() { done(_Http_toResponse(request.__$expect.__toBody, xhr)); });
__Maybe_isJust(request.__$tracker) && _Http_track(router, xhr, request.__$tracker.a);
try {
xhr.open(request.__$method, request.__$url, true);
} catch (e) {
return done(__Http_BadUrl_(request.__$url));
}
_Http_configureRequest(xhr, request);
request.__$body.a && xhr.setRequestHeader('Content-Type', request.__$body.a);
xhr.send(request.__$body.b);
return function() { xhr.__isAborted = true; xhr.abort(); };
});
});
// CONFIGURE
function _Http_configureRequest(xhr, request)
{
for (var headers = request.__$headers; headers.b; headers = headers.b) // WHILE_CONS
{
xhr.setRequestHeader(headers.a.a, headers.a.b);
}
xhr.timeout = request.__$timeout.a || 0;
xhr.responseType = request.__$expect.__type;
xhr.withCredentials = request.__$allowCookiesFromOtherDomains;
}
// RESPONSES
function _Http_toResponse(toBody, xhr)
{
return A2(
200 <= xhr.status && xhr.status < 300 ? __Http_GoodStatus_ : __Http_BadStatus_,
_Http_toMetadata(xhr),
toBody(xhr.response)
);
}
// METADATA
function _Http_toMetadata(xhr)
{
return {
__$url: xhr.responseURL,
__$statusCode: xhr.status,
__$statusText: xhr.statusText,
__$headers: _Http_parseHeaders(xhr.getAllResponseHeaders())
};
}
// HEADERS
function _Http_parseHeaders(rawHeaders)
{
if (!rawHeaders)
{
return __Dict_empty;
}
var headers = __Dict_empty;
var headerPairs = rawHeaders.split('\r\n');
for (var i = headerPairs.length; i--; )
{
var headerPair = headerPairs[i];
var index = headerPair.indexOf(': ');
if (index > 0)
{
var key = headerPair.substring(0, index);
var value = headerPair.substring(index + 2);
headers = A3(__Dict_update, key, function(oldValue) {
return __Maybe_Just(__Maybe_isJust(oldValue)
? value + ', ' + oldValue.a
: value
);
}, headers);
}
}
return headers;
}
// EXPECT
var _Http_expect = F3(function(type, toBody, toValue)
{
return {
$: 0,
__type: type,
__toBody: toBody,
__toValue: toValue
};
});
var _Http_mapExpect = F2(function(func, expect)
{
return {
$: 0,
__type: expect.__type,
__toBody: expect.__toBody,
__toValue: function(x) { return func(expect.__toValue(x)); }
};
});
function _Http_toDataView(arrayBuffer)
{
return new DataView(arrayBuffer);
}
// BODY and PARTS
var _Http_emptyBody = { $: 0 };
var _Http_pair = F2(function(a, b) { return { $: 0, a: a, b: b }; });
function _Http_toFormData(parts)
{
for (var formData = new FormData(); parts.b; parts = parts.b) // WHILE_CONS
{
var part = parts.a;
formData.append(part.a, part.b);
}
return formData;
}
var _Http_bytesToBlob = F2(function(mime, bytes)
{
return new Blob([bytes], { type: mime });
});
// PROGRESS
function _Http_track(router, xhr, tracker)
{
// TODO check out lengthComputable on loadstart event
xhr.upload.addEventListener('progress', function(event) {
if (xhr.__isAborted) { return; }
__Scheduler_rawSpawn(A2(__Platform_sendToSelf, router, __Utils_Tuple2(tracker, __Http_Sending({
__$sent: event.loaded,
__$size: event.total
}))));
});
xhr.addEventListener('progress', function(event) {
if (xhr.__isAborted) { return; }
__Scheduler_rawSpawn(A2(__Platform_sendToSelf, router, __Utils_Tuple2(tracker, __Http_Receiving({
__$received: event.loaded,
__$size: event.lengthComputable ? __Maybe_Just(event.total) : __Maybe_Nothing
}))));
});
}