Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/axios/lib/helpers/buildURL.js
1126 views
1
'use strict';
2
3
var utils = require('./../utils');
4
5
function encode(val) {
6
return encodeURIComponent(val).
7
replace(/%3A/gi, ':').
8
replace(/%24/g, '$').
9
replace(/%2C/gi, ',').
10
replace(/%20/g, '+').
11
replace(/%5B/gi, '[').
12
replace(/%5D/gi, ']');
13
}
14
15
/**
16
* Build a URL by appending params to the end
17
*
18
* @param {string} url The base of the url (e.g., http://www.google.com)
19
* @param {object} [params] The params to be appended
20
* @returns {string} The formatted url
21
*/
22
module.exports = function buildURL(url, params, paramsSerializer) {
23
/*eslint no-param-reassign:0*/
24
if (!params) {
25
return url;
26
}
27
28
var serializedParams;
29
if (paramsSerializer) {
30
serializedParams = paramsSerializer(params);
31
} else if (utils.isURLSearchParams(params)) {
32
serializedParams = params.toString();
33
} else {
34
var parts = [];
35
36
utils.forEach(params, function serialize(val, key) {
37
if (val === null || typeof val === 'undefined') {
38
return;
39
}
40
41
if (utils.isArray(val)) {
42
key = key + '[]';
43
} else {
44
val = [val];
45
}
46
47
utils.forEach(val, function parseValue(v) {
48
if (utils.isDate(v)) {
49
v = v.toISOString();
50
} else if (utils.isObject(v)) {
51
v = JSON.stringify(v);
52
}
53
parts.push(encode(key) + '=' + encode(v));
54
});
55
});
56
57
serializedParams = parts.join('&');
58
}
59
60
if (serializedParams) {
61
var hashmarkIndex = url.indexOf('#');
62
if (hashmarkIndex !== -1) {
63
url = url.slice(0, hashmarkIndex);
64
}
65
66
url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
67
}
68
69
return url;
70
};
71
72