/
Http.js
127 lines (127 loc) · 3.52 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var Option_1 = require("fp-ts/lib/Option");
var Either_1 = require("fp-ts/lib/Either");
var Task_1 = require("fp-ts/lib/Task");
var Task_2 = require("./Task");
var function_1 = require("fp-ts/lib/function");
function expectJson(decoder) {
return decoder.decode;
}
exports.expectJson = expectJson;
var BadUrl = /** @class */ (function () {
function BadUrl(value) {
this.value = value;
this._tag = 'BadUrl';
}
return BadUrl;
}());
exports.BadUrl = BadUrl;
var Timeout = /** @class */ (function () {
function Timeout() {
this._tag = 'Timeout';
}
return Timeout;
}());
exports.Timeout = Timeout;
var NetworkError = /** @class */ (function () {
function NetworkError(value) {
this.value = value;
this._tag = 'NetworkError';
}
return NetworkError;
}());
exports.NetworkError = NetworkError;
var BadStatus = /** @class */ (function () {
function BadStatus(response) {
this.response = response;
this._tag = 'BadStatus';
}
return BadStatus;
}());
exports.BadStatus = BadStatus;
var BadPayload = /** @class */ (function () {
function BadPayload(value, response) {
this.value = value;
this.response = response;
this._tag = 'BadPayload';
}
return BadPayload;
}());
exports.BadPayload = BadPayload;
function axiosResponseToResponse(res) {
return {
url: res.config.url,
status: {
code: res.status,
message: res.statusText
},
headers: res.headers,
body: res.request.responseText
};
}
function axiosResponseToEither(res, expect) {
return expect(res.data).mapLeft(function (errors) { return new BadPayload(errors, axiosResponseToResponse(res)); });
}
function axiosErrorToEither(e) {
if (e instanceof Error) {
if (e.code === 'ECONNABORTED') {
return Either_1.left(new Timeout());
}
return Either_1.left(new NetworkError(e.message));
}
var res = e.response;
switch (res.status) {
case 404:
return Either_1.left(new BadUrl(res.config.url));
default:
return Either_1.left(new BadStatus(axiosResponseToResponse(res)));
}
}
function getPromiseAxiosResponse(config) {
return axios_1.default(config);
}
function requestToTask(req) {
return new Task_1.Task(function () {
return getPromiseAxiosResponse({
method: req.method,
headers: req.headers,
url: req.url,
data: req.body,
timeout: req.timeout.fold(function () { return undefined; }, function_1.identity),
withCredentials: req.withCredentials
})
.then(function (res) { return axiosResponseToEither(res, req.expect); })
.catch(function (e) { return axiosErrorToEither(e); });
});
}
function send(f, req) {
return Task_2.attempt(f, requestToTask(req));
}
exports.send = send;
function get(url, decoder) {
return {
method: 'GET',
headers: {},
url: url,
body: undefined,
expect: expectJson(decoder),
timeout: Option_1.none,
withCredentials: false
};
}
exports.get = get;
function post(url, body, decoder) {
return {
method: 'POST',
headers: {},
url: url,
body: body,
expect: expectJson(decoder),
timeout: Option_1.none,
withCredentials: false
};
}
exports.post = post;
//# sourceMappingURL=Http.js.map