Skip to content

Commit

Permalink
Updated typings;
Browse files Browse the repository at this point in the history
Added TS test;
  • Loading branch information
DigitalBrainJS committed May 20, 2022
1 parent a2188b3 commit 68fde57
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
22 changes: 17 additions & 5 deletions index.d.ts
Expand Up @@ -90,22 +90,34 @@ export interface GenericAbortSignal {
}

export interface FormDataVisitorHelpers {
defaultVisitor: FormDataVisitor;
defaultVisitor: SerializerVisitor;
convertValue: (value: any) => any;
isVisitable: (value: any) => boolean;
}

export interface FormDataVisitor {
export interface SerializerVisitor {
(value: any, key: string | number, path: null | Array<string | number>, helpers: FormDataVisitorHelpers): boolean;
}

export interface FormSerializerOptions {
visitor?: FormDataVisitor;
export interface SerializerOptions {
visitor?: SerializerVisitor;
dots?: boolean;
metaTokens?: boolean;
indexes?: boolean;
}

// tslint:disable-next-line
export interface FormSerializerOptions extends SerializerOptions {
}

export interface ParamEncoder {
(value: any, defaultEncoder: (value: any) => any): any;
}

export interface ParamsSerializerOptions extends SerializerOptions {
encode?: ParamEncoder;
}

export interface AxiosRequestConfig<D = any> {
url?: string;
method?: Method | string;
Expand All @@ -114,7 +126,7 @@ export interface AxiosRequestConfig<D = any> {
transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
headers?: AxiosRequestHeaders;
params?: any;
paramsSerializer?: (params: any) => string;
paramsSerializer?: ParamsSerializerOptions;
data?: D;
timeout?: number;
timeoutErrorMessage?: string;
Expand Down
6 changes: 3 additions & 3 deletions lib/helpers/AxiosURLSearchParams.js
Expand Up @@ -18,23 +18,23 @@ function encode(str) {
}

function AxiosURLSearchParams(params, options) {
this.pairs = [];
this._pairs = [];

params && toFormData(params, this, options);
}

var prototype = AxiosURLSearchParams.prototype;

prototype.append = function append(name, value) {
this.pairs.push([name, value]);
this._pairs.push([name, value]);
};

prototype.toString = function toString(encoder) {
var _encode = encoder ? function(value) {
return encoder.call(this, value, encode);
} : encode;

return this.pairs.map(function each(pair) {
return this._pairs.map(function each(pair) {
return _encode(pair[0]) + '=' + _encode(pair[1]);
}, '').join('&');
};
Expand Down
12 changes: 11 additions & 1 deletion lib/helpers/buildURL.js
@@ -1,5 +1,6 @@
'use strict';

var utils = require('../utils');
var AxiosURLSearchParams = require('../helpers/AxiosURLSearchParams');

function encode(val) {
Expand Down Expand Up @@ -27,11 +28,20 @@ module.exports = function buildURL(url, params, options) {
}

var hashmarkIndex = url.indexOf('#');

if (hashmarkIndex !== -1) {
url = url.slice(0, hashmarkIndex);
}

url += (url.indexOf('?') === -1 ? '?' : '&') + new AxiosURLSearchParams(params, options).toString(encode);
var _encode = options && options.encode || encode;

var serializerParams = utils.isURLSearchParams(params) ?
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);

if (serializerParams) {
url += (url.indexOf('?') === -1 ? '?' : '&') + serializerParams;
}

return url;
};
11 changes: 1 addition & 10 deletions test/specs/helpers/buildURL.spec.js
Expand Up @@ -17,7 +17,7 @@ describe('helpers::buildURL', function () {
foo: {
bar: 'baz'
}
})).toEqual('/foo?foo=' + encodeURI('{"bar":"baz"}'));
})).toEqual('/foo?foo[bar]=baz');
});

it('should support date params', function () {
Expand Down Expand Up @@ -60,15 +60,6 @@ describe('helpers::buildURL', function () {
})).toEqual('/foo?foo=bar&query=baz');
});

it('should use serializer if provided', function () {
serializer = sinon.stub();
params = {foo: 'bar'};
serializer.returns('foo=bar');
expect(buildURL('/foo', params, serializer)).toEqual('/foo?foo=bar');
expect(serializer.calledOnce).toBe(true);
expect(serializer.calledWith(params)).toBe(true);
});

it('should support URLSearchParams', function () {
expect(buildURL('/foo', new URLSearchParams('bar=baz'))).toEqual('/foo?bar=baz');
});
Expand Down
5 changes: 4 additions & 1 deletion test/typescript/axios.ts
Expand Up @@ -20,7 +20,10 @@ const config: AxiosRequestConfig = {
],
headers: { 'X-FOO': 'bar' },
params: { id: 12345 },
paramsSerializer: (params: any) => 'id=12345',
paramsSerializer: {
indexes: true,
encode: (value) => value
},
data: { foo: 'bar' },
timeout: 10000,
withCredentials: true,
Expand Down

0 comments on commit 68fde57

Please sign in to comment.