-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathNetwork.jsx
188 lines (167 loc) · 6.08 KB
/
Network.jsx
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
import $ from 'jquery';
import _ from 'underscore';
import {isWindowAvailable} from './utils';
/**
* Adds our API command to the URL so the API call is more easily identified in the
* network tab of the JS console
*
* @param {string} command
* @param {string} url
* @returns {string}
*/
function addCommandToUrl(command, url) {
let newUrl = url;
if (command) {
// Add a ? to the end of the URL if there isn't one already
if (newUrl.indexOf('?') === -1) {
newUrl = `${newUrl}?`;
}
newUrl = `${newUrl}&command=${command}`;
}
return newUrl;
}
/**
* @param {String} endpoint
*
* @returns {Object}
*/
export default function Network(endpoint) {
// A flag that turns true when the user navigates away
let isNavigatingAway = false;
// If URL ends in `/` we're using /api/{command} format.
const isNewURLFormat = endpoint[endpoint.length - 1] === '/';
if (!endpoint) {
throw new Error('Cannot instantiate Network without an url endpoint');
}
// Attach a listener to the event indicating that we're leaving a page
if (isWindowAvailable()) {
window.onbeforeunload = () => {
isNavigatingAway = true;
};
}
return {
/**
* @param {String} url to fetch
* @returns {Deferred}
*/
get(url) {
return $.get(url);
},
/**
* @param {Object} parameters
*
* @returns {$.Deferred}
*/
post(parameters) {
// Build request
let newURL = endpoint;
if (isNewURLFormat) {
// Remove command from parameters and use it in the URL
const command = parameters.command;
// eslint-disable-next-line no-param-reassign
delete parameters.command;
newURL = `${endpoint}${command}`;
}
const settings = {
url: newURL,
type: 'POST',
data: parameters,
};
const formData = new FormData();
let shouldUseFormData = false;
// Add the API command to our URL (for console debugging purposes)
// Note that parameters.command is empty if we're using the new API format and this will do nothing.
settings.url = addCommandToUrl(parameters.command, settings.url);
// Check to see if parameters contains a File or Blob object
// If it does, we should use formData instead of parameters and update
// the ajax settings accordingly
_(parameters).each((value, key) => {
if (!value) {
return;
}
// Populate formData in case we need it
formData.append(key, value);
if (value instanceof File || value instanceof Blob) {
shouldUseFormData = true;
}
});
if (shouldUseFormData) {
settings.processData = false;
settings.contentType = false;
settings.data = formData;
}
return $.ajax(settings);
},
/**
* Uses the fetch API to send a keepalive request that will complete
* even if the browser is closed
*
* Note: This method ONLY provides partial support for sending complex data structures.
* It supports a single level array of objects with no nested properties
* eg. [{one: 1}, {two:2}]
*
* @param {Object} parameters
* @returns {$.Deferred}
*/
keepalive(parameters) {
// Build request
const settings = {
method: 'POST',
keepalive: true,
credentials: 'same-origin',
};
let url = endpoint;
// Add the API command to our URL (for console debugging purposes)
url = addCommandToUrl(parameters.command, url);
// Add our data as form data
const formData = new FormData();
_(parameters).each((value, key) => {
if (_.isUndefined(value)) {
return;
}
if (_.isArray(value)) {
_.each(value, (valueItem, i) => {
if (_.isObject(valueItem)) {
_.each(valueItem, (valueItemObjectValue, valueItemObjectKey) => {
formData.append(`${key}[${i}][${valueItemObjectKey}]`, valueItemObjectValue);
});
} else {
formData.append(`${key}[${i}]`, valueItem);
}
});
} else {
formData.append(key, value);
}
});
settings.body = formData;
// Make our request via the fetch API but return it in the form of a jQuery promise
// so that our API can be consistent
const promise = new $.Deferred();
fetch(url, settings)
.then(() => {
// No need to return a response since there is no script to process it
// due to the browser being closed
promise.resolve();
})
.catch((error) => {
promise.reject(error);
});
return promise;
},
/**
* @param {Function} callback
*/
registerNetworkFailureHandler(callback) {
$(document).ajaxError((event, jqxhr) => {
// Some browsers invoke the ajax error callbacks when navigating away during a pending ajax call
// Ignore this if navigating away
if (isNavigatingAway) {
return;
}
const response = jqxhr.responseJSON;
const jsonCode = response ? parseInt(response.jsonCode, 10) : null;
callback(jsonCode, response);
});
},
};
}