/
xhr-request.js
142 lines (126 loc) · 3.73 KB
/
xhr-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
import Deferred from "../utils/deferred"
import xhr from "xhr"
import url from "url"
import Msgpack from "msgpack"
import HTTPHeaderField from "./http-header-field"
import Error from "../model/error"
import Transformer from "./transformer"
export default class XhrRequest {
constructor(manager, url, method, body, options) {
this.url = url;
this.body = body;
this.manager = manager;
this.method = method;
this.options = options;
this.isBackground = options.isBackground || false;
this.deferred = new Deferred();
this.transformer = new Transformer();
}
result() {
return this.deferred;
}
resendable() {
return this.method === "GET" || this.isBackground;
}
send() {
const config = this.buildConfig();
if (!this.isBackground) this.manager.startLoading();
this.sendRequest(config).then(
this.onSuccess.bind(this),
this.onFail.bind(this));
}
onSuccess(response) {
if (this.canceled) return;
if (!this.isBackground) this.manager.endLoading();
this.manager.handleResponse(this, response.data);
}
onFail(response) {
if (this.canceled) return;
if (!this.isBackground) this.manager.endLoading();
this.manager.handleError(this, this.convertError(response));
}
addAuthorizationHeader(headers) {
if (!this.manager.sessionManager.isLoggedIn) return;
headers[HTTPHeaderField.AUTHORIZATION] =
"X-JIJI-AUTHENTICATE " + this.manager.sessionManager.getToken();
}
buildConfig() {
const base = Object.assign({
url: this.url,
method: this.method,
params: this.params,
body: this.transformRequest(this.body),
withCredentials: false,
headers: this.options.multipart ? {} : {
"Content-Type": "application/x-msgpack"
},
responseType: "arraybuffer"
}, this.options);
this.addAuthorizationHeader(base.headers);
return base;
}
transformRequest(data) {
if (data == null) return null;
if (this.options.multipart) return data;
let transformed = this.transformer.transformRequest(data);
return new Uint8Array(Msgpack.msgpack.pack(transformed));
}
transformResponse(arrayBuffer) {
if (!arrayBuffer) return null
let data = Msgpack.msgpack.unpack(new Uint8Array(arrayBuffer));
return this.transformer.transformResponse(data);
}
cancel() {
this.canceled = true;
this.manager.handleError(this, {
code: Error.Code.CANCELED
});
}
sendRequest(setting) {
const d = new Deferred();
this.start = new Date().getTime();
xhr(setting, (err, resp, body) => {
if (err) {
d.reject(err);
} else {
if (resp.statusCode >= 400) {
d.reject(resp);
} else {
resp.data = this.transformResponse(resp.body);
d.resolve(resp);
}
}
});
return d;
}
isAuthRequest() {
return (
(this.url.match(/\/api\/authenticator$/) && this.method === "POST")
|| (this.url.match(/\/api\/settings\/user\/password$/) && this.method === "PUT")
|| (this.url.match(/\/api\/settings\/password-resetter$/))
);
}
convertError(response) {
return Error.create({
response: response,
code: this.convertErrorCode(response)
});
}
convertErrorCode(response) {
if (response.statusCode == null) return Error.Code.NETWORK_ERROR;
switch (response.statusCode) {
case 400:
return Error.Code.INVALID_VALUE;
case 401:
return Error.Code.UNAUTHORIZED;
case 403:
return Error.Code.OPERATION_NOT_ALLOWED;
case 404:
return Error.Code.NOT_FOUND;
case 406:
return Error.Code.INVALID_VALUE;
default:
return Error.Code.SERVER_BUSY;
}
}
}