Skip to content

Commit cf82789

Browse files
committed
docs: jsdoc for the ajax module
1 parent da23137 commit cf82789

File tree

6 files changed

+253
-120
lines changed

6 files changed

+253
-120
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"presets": [["@babel/preset-env", { "loose": true }]],
3-
"plugins": [["add-module-exports", { "accessDefaultWithBracket": true }]]
3+
"plugins": [["add-module-exports", { "accessDefaultWithBracket": true }]],
4+
"comments": false
45
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ You can also see the older versions of API page on the [releases page](https://g
3131

3232
## 🎨 Features
3333

34+
* ajax
35+
* Send the Ajax request
3436
* array
3537
* Handle arrays
3638
* browser

ajax/index.js

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@ var _isUndefined = _interopRequireDefault(require("../type/isUndefined"));
2323

2424
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
2525

26-
/**
27-
* Serializer
28-
*
29-
* 1. Array format
30-
*
31-
* The default array format to serialize is 'bracket'.
32-
* However in case of nested array, only the deepest format follows the 'bracket', the rest follow 'indice' format.
33-
*
34-
* - basic
35-
* { a: [1, 2, 3] } => a[]=1&a[]=2&a[]=3
36-
* - nested
37-
* { a: [1, 2, [3]] } => a[]=1&a[]=2&a[2][]=3
38-
*
39-
* 2. Object format
40-
*
41-
* The default object format to serialize is 'bracket' notation and doesn't allow the 'dot' notation.
42-
*
43-
* - basic
44-
* { a: { b: 1, c: 2 } } => a[b]=1&a[c]=2
45-
*/
4626
function encodePairs(key, value) {
4727
return encodeURIComponent(key) + "=" + encodeURIComponent((0, _isNull["default"])(value) || (0, _isUndefined["default"])(value) ? '' : value);
4828
}
@@ -72,26 +52,6 @@ function serialize(params) {
7252
});
7353
return serializedList.join('&');
7454
}
75-
/**
76-
* The default configurations
77-
* @namespace defaults
78-
* @property {string} baseURL - baseURL
79-
* @property {object} headers - request headers
80-
* @property {object<string,string>} headers.common - Common configs regardless of the type of request
81-
* @property {object<string,string>} headers.get - Common configs for the GET method
82-
* @property {object<string,string>} headers.post - Common configs for the POST method
83-
* @property {object<string,string>} headers.put - Common configs for the PUT method
84-
* @property {object<string,string>} headers.delete - Common configs for the DELETE method
85-
* @property {object<string,string>} headers.patch - Common configs for the PATCH method
86-
* @property {object<string,string>} headers.options - Common configs for the OPTIONS method
87-
* @property {object<string,string>} headers.head - Common configs for the HEAD method
88-
* @property {function} serializer - determine how to serialize the data
89-
* @property {function} beforeRequest - callback function executed before the request is sent
90-
* @property {function} success - callback function executed when the request is success
91-
* @property {function} error - callback function executed when the request is failed
92-
* @property {function} complete - callback function executed when the request is completed
93-
*/
94-
9555

9656
var getDefaultOptions = function getDefaultOptions() {
9757
return {
@@ -111,13 +71,6 @@ var getDefaultOptions = function getDefaultOptions() {
11171
};
11272

11373
var HTTP_PROTOCOL_REGEXP = /^(http|https):\/\//i;
114-
/**
115-
* Combine an absolute URL string (baseURL) and a relative URL string (url).
116-
* @param {string} baseURL - An absolute URL string
117-
* @param {string} url - An relative URL string
118-
* @returns {string}
119-
* @private
120-
*/
12174

12275
function combineURL(baseURL, url) {
12376
if (HTTP_PROTOCOL_REGEXP.test(url)) {
@@ -130,14 +83,6 @@ function combineURL(baseURL, url) {
13083

13184
return baseURL + url;
13285
}
133-
/**
134-
* Get merged options by its priorities.
135-
* defaults.common < defaults[method] < custom options
136-
* @param {object} defaultOptions - The default options
137-
* @param {object} customOptions - The custom options
138-
* @returns {object}
139-
*/
140-
14186

14287
function getComputedOptions(defaultOptions, customOptions) {
14388
var baseURL = defaultOptions.baseURL,
@@ -233,7 +178,7 @@ function handleReadyStateChange(xhr, options) {
233178
resolve = options.resolve,
234179
error = options.error,
235180
reject = options.reject,
236-
complete = options.complete; // eslint-disable-next-line eqeqeq
181+
complete = options.complete;
237182

238183
if (readyState != XMLHttpRequest.DONE) {
239184
return;
@@ -274,7 +219,6 @@ function open(xhr, options) {
274219
var requestUrl = url;
275220

276221
if (!hasRequestBody(method) && params) {
277-
// serialize query string
278222
var qs = (QS_DELIM_REGEXP.test(url) ? '&' : '?') + serializer(params);
279223
requestUrl = "" + url + qs;
280224
}
@@ -288,12 +232,11 @@ function applyConfig(xhr, options) {
288232
mimeType = options.mimeType,
289233
headers = options.headers,
290234
_options$withCredenti = options.withCredentials,
291-
withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti; // set withCredentials (IE10+)
235+
withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti;
292236

293237
if (withCredentials) {
294238
xhr.withCredentials = withCredentials;
295-
} // override MIME type (IE11+)
296-
239+
}
297240

298241
if (mimeType) {
299242
xhr.overrideMimeType(mimeType);
@@ -307,8 +250,7 @@ function applyConfig(xhr, options) {
307250

308251
if (hasRequestBody(method)) {
309252
xhr.setRequestHeader('Content-Type', contentType + "; charset=UTF-8");
310-
} // set 'x-requested-with' header to prevent CSRF in old browser
311-
253+
}
312254

313255
xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
314256
}
@@ -324,7 +266,6 @@ function send(xhr, options) {
324266
var body = null;
325267

326268
if (hasRequestBody(method)) {
327-
// The space character '%20' is replaced to '+', because application/x-www-form-urlencoded follows rfc-1866
328269
body = contentType.indexOf('application/x-www-form-urlencoded') > -1 ? serializer(params).replace(ENCODED_SPACE_REGEXP, '+') : JSON.stringify(params);
329270
}
330271

@@ -335,14 +276,6 @@ function send(xhr, options) {
335276
executeCallback(beforeRequest, xhr);
336277
xhr.send(body);
337278
}
338-
/**
339-
* A module for the Ajax request.
340-
* If the browser supports Promise, return the Promise object. If not, return nothing.
341-
* @param {object} options Options for the Ajax request
342-
* @returns {[Promise]}
343-
* @memberof module:ajax
344-
*/
345-
346279

347280
function ajax(options) {
348281
var xhr = new XMLHttpRequest();

0 commit comments

Comments
 (0)