Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #178 from data-provider/release
Browse files Browse the repository at this point in the history
Release v4.0.0
  • Loading branch information
javierbrea authored Jul 31, 2021
2 parents b9b14a4 + 8bf5d2b commit b0ed8cf
Show file tree
Hide file tree
Showing 10 changed files with 1,226 additions and 1,992 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### BREAKING CHANGES

## [4.0.0] - 2021-07-31

### Changed
- feat(#6): Use [query-string](https://www.npmjs.com/package/query-string) package to format query strings. Allow configuring query-string options using the `queryStringConfig` option of this package. This is a BREAKING CHANGE because query-string default options are used (read BREAKING CHANGES bellow for further info)
- chore(deps): Update devDependencies

### BREAKING CHANGES
- Arrays in query strings were converted into a list separated by "%2C" (url encoded comma). Now it uses the `query-string` package using its default options, so arrays are serialized duplicating keys (`?foo=1&foo=2`). This behaviour can be configured using the new `queryStringConfig` option.
- query string keys now are sorted by default.

## [3.1.0] - 2021-06-18
### Added
- feat(#140): Support body params in delete method
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Apart of the common Data Provider options, next ones are available:
* `error` _(Error)_: Error object produced by a failed request.
* Returns: Should return a rejected Promise, containing the new Error.
* `axiosConfig` _(Object)_: Options for the Axios request. If provided, this object is passed directly to Axios as [request configuration](https://github.com/axios/axios#request-config).
* `queryStringConfig` _(Object)_: Options for the [query-string library](https://github.com/sindresorhus/query-string#readme), which is used under the hood to serialize query string parameters. If provided, this object is passed directly to `query-string` as [options for the `stringify` method](https://github.com/sindresorhus/query-string#stringifyobject-options). Default options are the same from the library.

## Queries

Expand Down
3,103 changes: 1,146 additions & 1,957 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-provider/axios",
"version": "3.1.0",
"version": "4.0.0",
"description": "REST API Data Provider origin addon using Axios",
"keywords": [
"data-provider",
Expand Down Expand Up @@ -51,37 +51,38 @@
"dependencies": {
"axios": "0.21.1",
"axios-retry": "3.1.9",
"query-string": "7.0.1",
"path-to-regexp": "6.2.0"
},
"devDependencies": {
"@babel/core": "7.14.6",
"@babel/preset-env": "7.14.5",
"@babel/core": "7.14.8",
"@babel/preset-env": "7.14.8",
"@data-provider/core": "3.0.1",
"@mocks-server/admin-api-paths": "2.1.0",
"@mocks-server/main": "2.3.3",
"@mocks-server/main": "2.4.0",
"@rollup/plugin-babel": "5.3.0",
"@rollup/plugin-commonjs": "19.0.0",
"@rollup/plugin-commonjs": "19.0.2",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "13.0.0",
"@stryker-mutator/core": "5.0.1",
"@stryker-mutator/jest-runner": "5.0.1",
"@rollup/plugin-node-resolve": "13.0.4",
"@stryker-mutator/core": "5.2.2",
"@stryker-mutator/jest-runner": "5.2.2",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "27.0.2",
"babel-jest": "27.0.6",
"babel-polyfill": "6.26.0",
"cross-fetch": "3.1.4",
"eslint": "7.28.0",
"eslint": "7.31.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-prettier": "3.4.0",
"husky": "6.0.0",
"husky": "7.0.1",
"is-ci": "3.0.0",
"jest": "27.0.4",
"lint-staged": "11.0.0",
"prettier": "2.3.1",
"jest": "27.0.6",
"lint-staged": "11.1.1",
"prettier": "2.3.2",
"redux": "4.1.0",
"rollup": "2.51.2",
"rollup": "2.53.2",
"rollup-plugin-terser": "7.0.2",
"sinon": "11.1.1",
"start-server-and-test": "1.12.5"
"sinon": "11.1.2",
"start-server-and-test": "1.13.1"
},
"lint-staged": {
"*.js": "eslint",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const BASE_PLUGINS = [

const BASE_CONFIG = {
input: "src/index.js",
external: ["@data-provider/core", "axios", "axios-retry", "path-to-regexp"],
external: ["@data-provider/core", "axios", "axios-retry", "path-to-regexp", "query-string"],
plugins: [...BASE_PLUGINS, terser()],
};

Expand All @@ -42,6 +42,7 @@ const GLOBALS = {
axios: "axios",
"axios-retry": "axiosRetry",
"path-to-regexp": "pathToRegexp",
"query-string": "queryString",
};

module.exports = [
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
sonar.organization=data-provider
sonar.projectKey=data-provider_axios
sonar.projectVersion=3.1.0
sonar.projectVersion=4.0.0

sonar.javascript.file.suffixes=.js
sonar.sourceEncoding=UTF-8
Expand Down
21 changes: 9 additions & 12 deletions src/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Provider } from "@data-provider/core";
import { compile } from "path-to-regexp";
import axios from "axios";
import axiosRetry from "axios-retry";
import queryString from "query-string";

import {
once,
Expand Down Expand Up @@ -54,7 +55,8 @@ export class Axios extends Provider {
this._baseUrl = configuration.baseUrl;
this._onBeforeRequest = configuration.onBeforeRequest;
this._url = configuration.url;
this._axiosConfig = configuration.axiosConfig || {};
this._axiosConfig = configuration.axiosConfig;
this._queryStringConfig = configuration.queryStringConfig;

if (configuration.retries !== this._retries) {
this._retries = configuration.retries;
Expand All @@ -78,27 +80,22 @@ export class Axios extends Provider {
this._setUrl(this._baseUrl + this._url);
}

_getQueryString(queryString) {
if (!queryString || isEmpty(queryString)) {
_getQueryString(queryStringValue) {
if (!queryStringValue || isEmpty(queryStringValue)) {
return "";
}
return (
"?" +
Object.keys(queryString)
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(queryString[key])}`)
.join("&")
);
return `?${queryString.stringify(queryStringValue, this._queryStringConfig)}`;
}

_getUrl() {
const queryString = this.queryValue.queryString;
const queryStringValue = this.queryValue.queryString;
const urlParams = this.queryValue.urlParams;
if (urlParams && !isEmpty(urlParams)) {
return `${this.url.base}/${this.url.segment(urlParams)}${
this.url.trailingSlash
}${this._getQueryString(queryString)}`;
}${this._getQueryString(queryStringValue)}`;
}
return `${this.url.full}${this._getQueryString(queryString)}`;
return `${this.url.full}${this._getQueryString(queryStringValue)}`;
}

_setUrl(fullUrl) {
Expand Down
2 changes: 2 additions & 0 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export const defaultConfig = {
errorToReturn.data = error.response && error.response.data;
return Promise.reject(errorToReturn);
},
axiosConfig: {},
queryStringConfig: {},
};
4 changes: 2 additions & 2 deletions test/custom-queries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe("Axios queries", () => {
});
await queriedBooks.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual(
"/books/foo/cervantes?page=2&order=asc"
"/books/foo/cervantes?order=asc&page=2"
);
});

Expand All @@ -188,7 +188,7 @@ describe("Axios queries", () => {
});
await queriedBooks.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual(
"https://www.domain.com:3000/books/foo/cervantes?page=2&order=asc"
"https://www.domain.com:3000/books/foo/cervantes?order=asc&page=2"
);
});
});
Expand Down
37 changes: 35 additions & 2 deletions test/queries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ describe("Axios queries", () => {
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual("/books?author=foo");
});

it("should add query params to axios request as duplicated keys", async () => {
axios.stubs.instance.resetHistory();
const books = new Axios({
url: "/books",
}).query({
queryString: {
authors: ["foo", "foo2", "foo3"],
},
});
await books.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual(
"/books?authors=foo&authors=foo2&authors=foo3"
);
});

it("should add query params to axios request when url includes protocol", async () => {
axios.stubs.instance.resetHistory();
const books = new Axios({
Expand Down Expand Up @@ -161,7 +176,7 @@ describe("Axios queries", () => {
});
await books.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual(
"/books/foo/cervantes?page=2&order=asc"
"/books/foo/cervantes?order=asc&page=2"
);
});

Expand All @@ -181,11 +196,29 @@ describe("Axios queries", () => {
});
await books.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual(
"https://www.domain.com:3000/books/foo/cervantes?page=2&order=asc"
"https://www.domain.com:3000/books/foo/cervantes?order=asc&page=2"
);
});
});

describe("queryStringConfig option", () => {
it('when arrayFormat option is "comma", it should add query params to axios request as comma separated list', async () => {
axios.stubs.instance.resetHistory();
const books = new Axios({
url: "/books",
queryStringConfig: {
arrayFormat: "comma",
},
}).query({
queryString: {
authors: ["foo", "foo2", "foo3"],
},
});
await books.read();
expect(axios.stubs.instance.getCall(0).args[0].url).toEqual("/books?authors=foo,foo2,foo3");
});
});

describe("Loading property of a queried method", () => {
let books;

Expand Down

0 comments on commit b0ed8cf

Please sign in to comment.