1 | 'use strict';
|
---|
2 |
|
---|
3 | import toFormData from './toFormData.js';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * It encodes a string by replacing all characters that are not in the unreserved set with
|
---|
7 | * their percent-encoded equivalents
|
---|
8 | *
|
---|
9 | * @param {string} str - The string to encode.
|
---|
10 | *
|
---|
11 | * @returns {string} The encoded string.
|
---|
12 | */
|
---|
13 | function encode(str) {
|
---|
14 | const charMap = {
|
---|
15 | '!': '%21',
|
---|
16 | "'": '%27',
|
---|
17 | '(': '%28',
|
---|
18 | ')': '%29',
|
---|
19 | '~': '%7E',
|
---|
20 | '%20': '+',
|
---|
21 | '%00': '\x00'
|
---|
22 | };
|
---|
23 | return encodeURIComponent(str).replace(/[!'()~]|%20|%00/g, function replacer(match) {
|
---|
24 | return charMap[match];
|
---|
25 | });
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * It takes a params object and converts it to a FormData object
|
---|
30 | *
|
---|
31 | * @param {Object<string, any>} params - The parameters to be converted to a FormData object.
|
---|
32 | * @param {Object<string, any>} options - The options object passed to the Axios constructor.
|
---|
33 | *
|
---|
34 | * @returns {void}
|
---|
35 | */
|
---|
36 | function AxiosURLSearchParams(params, options) {
|
---|
37 | this._pairs = [];
|
---|
38 |
|
---|
39 | params && toFormData(params, this, options);
|
---|
40 | }
|
---|
41 |
|
---|
42 | const prototype = AxiosURLSearchParams.prototype;
|
---|
43 |
|
---|
44 | prototype.append = function append(name, value) {
|
---|
45 | this._pairs.push([name, value]);
|
---|
46 | };
|
---|
47 |
|
---|
48 | prototype.toString = function toString(encoder) {
|
---|
49 | const _encode = encoder ? function(value) {
|
---|
50 | return encoder.call(this, value, encode);
|
---|
51 | } : encode;
|
---|
52 |
|
---|
53 | return this._pairs.map(function each(pair) {
|
---|
54 | return _encode(pair[0]) + '=' + _encode(pair[1]);
|
---|
55 | }, '').join('&');
|
---|
56 | };
|
---|
57 |
|
---|
58 | export default AxiosURLSearchParams;
|
---|