Skip to content

Commit

Permalink
Added custom params serializer support;
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed Oct 12, 2022
1 parent e757e0e commit 03c8b08
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 47 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Expand Up @@ -179,3 +179,42 @@
- [Luca Pizzini](https://github.com/lpizzinidev)
- [Willian Agostini](https://github.com/WillianAgostini)
- [Huyen Nguyen](https://github.com/huyenltnguyen)

## [1.1.0] - 2022-10-06

### Fixed

- Fixed missing exports in type definition index.d.ts [#5003](https://github.com/axios/axios/pull/5003)
- Fixed query params composing [#5018](https://github.com/axios/axios/pull/5018)
- Fixed GenericAbortSignal interface by making it more generic [#5021](https://github.com/axios/axios/pull/5021)
- Fixed adding "clear" to AxiosInterceptorManager [#5010](https://github.com/axios/axios/pull/5010)
- Fixed commonjs & umd exports [#5030](https://github.com/axios/axios/pull/5030)
- Fixed inability to access response headers when using axios 1.x with Jest [#5036](https://github.com/axios/axios/pull/5036)

### Contributors to this release

- [Trim21](https://github.com/trim21)
- [Dmitriy Mozgovoy](https://github.com/DigitalBrainJS)
- [shingo.sasaki](https://github.com/s-sasaki-0529)
- [Ivan Pepelko](https://github.com/ivanpepelko)
- [Richard Kořínek](https://github.com/risa)

## [1.1.1] - 2022-10-07

### Fixed

- Fixed broken exports for common js. This fix breaks a prior fix, I will fix both issues ASAP but the commonJS use is more impactful.

### Contributors to this release

- [Jason Saayman](https://github.com/jasonsaayman)

## [1.1.2] - 2022-10-07

### Fixed

- Fixed broken exports for UMD builds.

### Contributors to this release

- [Jason Saayman](https://github.com/jasonsaayman)
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -15,15 +15,18 @@

[![npm version](https://img.shields.io/npm/v/axios.svg?style=flat-square)](https://www.npmjs.org/package/axios)
[![CDNJS](https://img.shields.io/cdnjs/v/axios.svg?style=flat-square)](https://cdnjs.com/libraries/axios)
![Build status](https://github.com/axios/axios/actions/workflows/ci.yml/badge.svg)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/axios/axios)
[![Build status](https://img.shields.io/github/workflow/status/axios/axios/ci?label=CI&logo=github&style=flat-square)](https://github.com/axios/axios/actions/workflows/ci.yml)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod&style=flat-square)](https://gitpod.io/#https://github.com/axios/axios)
[![code coverage](https://img.shields.io/coveralls/mzabriskie/axios.svg?style=flat-square)](https://coveralls.io/r/mzabriskie/axios)
[![install size](https://packagephobia.now.sh/badge?p=axios)](https://packagephobia.now.sh/result?p=axios)
[![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=axios&query=$.install.pretty&label=install%20size&style=flat-square)](https://packagephobia.now.sh/result?p=axios)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/axios?style=flat-square)](https://bundlephobia.com/package/axios@latest)
[![npm downloads](https://img.shields.io/npm/dm/axios.svg?style=flat-square)](https://npm-stat.com/charts.html?package=axios)
[![gitter chat](https://img.shields.io/gitter/room/mzabriskie/axios.svg?style=flat-square)](https://gitter.im/mzabriskie/axios)
[![code helpers](https://www.codetriage.com/axios/axios/badges/users.svg)](https://www.codetriage.com/axios/axios)
[![Known Vulnerabilities](https://snyk.io/test/npm/axios/badge.svg)](https://snyk.io/test/npm/axios)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/axios)




</div>

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "axios",
"main": "./dist/axios.js",
"version": "1.0.0",
"version": "1.1.2",
"homepage": "https://axios-http.com",
"authors": [
"Matt Zabriskie"
Expand Down
6 changes: 3 additions & 3 deletions dist/axios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/axios.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/axios.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/axios.min.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/esm/axios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/esm/axios.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/axios.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/esm/axios.min.js.map

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions dist/node/axios.cjs
@@ -1,4 +1,4 @@
// Axios v1.0.0 Copyright (c) 2022 Matt Zabriskie and contributors
// Axios v1.1.2 Copyright (c) 2022 Matt Zabriskie and contributors
'use strict';

const FormData$1 = require('form-data');
Expand Down Expand Up @@ -158,7 +158,7 @@ const isPlainObject = (val) => {
}

const prototype = getPrototypeOf(val);
return prototype === null || prototype === Object.prototype;
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in val) && !(Symbol.iterator in val);
};

/**
Expand Down Expand Up @@ -1313,7 +1313,7 @@ function buildFullPath(baseURL, requestedURL) {
return requestedURL;
}

const VERSION = "1.0.0";
const VERSION = "1.1.2";

/**
* A `CanceledError` is an object that is thrown when an operation is canceled.
Expand Down Expand Up @@ -2251,9 +2251,14 @@ function httpAdapter(config) {

auth && headers.delete('authorization');

const path = parsed.pathname.concat(parsed.searchParams);
let path;

try {
buildURL(path, config.params, config.paramsSerializer).replace(/^\?/, '');
path = buildURL(
parsed.pathname + parsed.search,
config.params,
config.paramsSerializer
).replace(/^\?/, '');
} catch (err) {
const customErr = new Error(err.message);
customErr.config = config;
Expand All @@ -2265,7 +2270,7 @@ function httpAdapter(config) {
headers.set('Accept-Encoding', 'gzip, deflate, br', false);

const options = {
path: buildURL(path, config.params, config.paramsSerializer).replace(/^\?/, ''),
path,
method: method,
headers: headers.toJSON(),
agents: { http: config.httpAgent, https: config.httpsAgent },
Expand Down
2 changes: 1 addition & 1 deletion dist/node/axios.cjs.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions index.d.ts
Expand Up @@ -247,8 +247,13 @@ export interface ParamEncoder {
(value: any, defaultEncoder: (value: any) => any): any;
}

export interface CustomParamsSerializer {
(params: object, options?: ParamsSerializerOptions): string;
}

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

type MaxUploadRate = number;
Expand Down Expand Up @@ -413,6 +418,7 @@ export interface AxiosInterceptorOptions {
export interface AxiosInterceptorManager<V> {
use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any, options?: AxiosInterceptorOptions): number;
eject(id: number): void;
clear(): void;
}

export class Axios {
Expand Down
12 changes: 1 addition & 11 deletions lib/axios.js
Expand Up @@ -14,8 +14,6 @@ import toFormData from './helpers/toFormData.js';
import AxiosError from './core/AxiosError.js';
import spread from './helpers/spread.js';
import isAxiosError from './helpers/isAxiosError.js';
import AxiosHeaders from './core/AxiosHeaders.js';


/**
* Create an instance of Axios
Expand Down Expand Up @@ -75,12 +73,4 @@ axios.formToJSON = thing => {
return formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
};

export default axios;

export {
Axios,
AxiosHeaders,
AxiosError,
CanceledError,
};

export default axios
9 changes: 8 additions & 1 deletion lib/core/Axios.js
Expand Up @@ -47,7 +47,7 @@ class Axios {

config = mergeConfig(this.defaults, config);

const transitional = config.transitional;
const {transitional, paramsSerializer} = config;

if (transitional !== undefined) {
validator.assertOptions(transitional, {
Expand All @@ -57,6 +57,13 @@ class Axios {
}, false);
}

if (paramsSerializer !== undefined) {
validator.assertOptions(paramsSerializer, {
encode: validators.function,
serialize: validators.function
}, true);
}

// Set config.method
config.method = (config.method || this.defaults.method || 'get').toLowerCase();

Expand Down
2 changes: 1 addition & 1 deletion lib/env/data.js
@@ -1 +1 @@
export const VERSION = "1.0.0";
export const VERSION = "1.1.2";
18 changes: 13 additions & 5 deletions lib/helpers/buildURL.js
Expand Up @@ -44,12 +44,20 @@ export default function buildURL(url, params, options) {

const _encode = options && options.encode || encode;

const serializerParams = utils.isURLSearchParams(params) ?
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);
const serializeFn = options && options.serialize;

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

if (serializeFn) {
serializedParams = serializeFn(params, options);
} else {
serializedParams = utils.isURLSearchParams(params) ?
params.toString() :
new AxiosURLSearchParams(params, options).toString(_encode);
}

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

return url;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Expand Up @@ -132,7 +132,7 @@ const isPlainObject = (val) => {
}

const prototype = getPrototypeOf(val);
return prototype === null || prototype === Object.prototype;
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in val) && !(Symbol.iterator in val);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "axios",
"version": "1.0.0",
"version": "1.1.2",
"description": "Promise based HTTP client for the browser and node.js",
"main": "index.js",
"exports": {
Expand Down
16 changes: 16 additions & 0 deletions test/specs/helpers/buildURL.spec.js
Expand Up @@ -63,4 +63,20 @@ describe('helpers::buildURL', function () {
it('should support URLSearchParams', function () {
expect(buildURL('/foo', new URLSearchParams('bar=baz'))).toEqual('/foo?bar=baz');
});

it('should support custom serialize function', function () {
const params = {
x: 1
}

const options = {
serialize: (thisParams, thisOptions) => {
expect(thisParams).toEqual(params);
expect(thisOptions).toEqual(options);
return "rendered"
}
};

expect(buildURL('/foo', params, options)).toEqual('/foo?rendered');
});
});
6 changes: 5 additions & 1 deletion test/typescript/axios.ts
Expand Up @@ -21,7 +21,8 @@ const config: AxiosRequestConfig = {
params: { id: 12345 },
paramsSerializer: {
indexes: true,
encode: (value) => value
encode: (value) => value,
serialize: (value, options) => String(value)
},
data: { foo: 'bar' },
timeout: 10000,
Expand Down Expand Up @@ -326,6 +327,9 @@ axios.interceptors.response.eject(voidResponseInterceptorId);
axios.interceptors.response.use((response: AxiosResponse) => response);
axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(response));

axios.interceptors.request.clear();
axios.interceptors.response.clear();

// Adapters

const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
Expand Down

0 comments on commit 03c8b08

Please sign in to comment.