Skip to content

Commit

Permalink
chore: upgrade to Axios 0.25 (#437)
Browse files Browse the repository at this point in the history
* chore(deps-dev): bump axios from 0.21.1 to 0.24.0

Bumps [axios](https://github.com/axios/axios) from 0.21.1 to 0.24.0.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/master/CHANGELOG.md)
- [Commits](axios/axios@v0.21.1...v0.24.0)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: remove charset from tests

axios v0.21.2 [removes the charset from JSON Content-Type
headers](axios/axios#2154). This adjusts the
tests to compensate for that change.

* test: change axios mocking

use 'axios-mock-adapter' over 'moxios' due to library changes that cause
incompatibilites with new versions of axios (over 0.21).

* build: update axios to 0.24

* fix: upgrade to axios 0.25.0

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: James Bourne <jamesmbourne@users.noreply.github.com>
Co-authored-by: James Bourne <jamesbourne@outlook.com>
  • Loading branch information
4 people committed Feb 4, 2022
1 parent bbfd934 commit 9ae8eb6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 66 deletions.
11 changes: 5 additions & 6 deletions package.json
Expand Up @@ -23,7 +23,7 @@
"lodash": "4.17.15",
"mixin-deep": "2.0.1",
"set-value": "3.0.1",
"axios": "0.21.1"
"axios": "0.25.0"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -42,22 +42,21 @@
"@aws-sdk/client-cloudformation": "^3.4.1",
"@aws-sdk/client-sts": "^3.4.1",
"@types/jest": "^27.0.1",
"@types/moxios": "^0.4.9",
"@types/node": "^14.14.21",
"@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0",
"axios": "^0.21.0",
"axios": "^0.25.0",
"axios-mock-adapter": "^1.20.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^8.3.0",
"husky": "^7.0.1",
"jest": "^26.6.3",
"lint-staged": "^11.1.2",
"moxios": "^0.4.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"semantic-release": "^17.3.3",
"ts-jest": "^26.5.0",
"serverless": "^2.22.0",
"ts-jest": "^26.5.0",
"typescript": "^4.1.3"
},
"dependencies": {
Expand All @@ -66,7 +65,7 @@
"aws4": "^1.9.1"
},
"peerDependencies": {
"axios": ">0.19.2 < 0.22.0"
"axios": ">0.25.0"
},
"husky": {
"hooks": {
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/apiGateway.it.ts
Expand Up @@ -199,9 +199,7 @@ describe("with credentials from environment variables", () => {

expect(error).toBe(undefined);
expect(result?.status).toEqual(200);
expect(result?.data.headers["content-type"]).toBe(
"application/json;charset=utf-8"
);
expect(result?.data.headers["content-type"]).toBe("application/json");
});
});

Expand Down
40 changes: 19 additions & 21 deletions src/axiosInterceptor.test.ts
@@ -1,14 +1,15 @@
import moxios from "moxios";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import aws4Interceptor from ".";

describe("axios interceptor", () => {
beforeEach(() => {
moxios.install();
let mock: MockAdapter;
beforeAll(() => {
mock = new MockAdapter(axios);
});

afterEach(() => {
moxios.uninstall();
mock.reset();
});

it("should not mutate request config object", async () => {
Expand All @@ -23,13 +24,14 @@ describe("axios interceptor", () => {
params: { foo: "bar" },
};

moxios.stubOnce("GET", /./, {});
mock.onGet().replyOnce(200, {});

// Act
await client.get(url, config);

// Assert
const request = moxios.requests.first();
const request = mock.history.get[0];
// const request = moxios.requests.first();
expect(request.url).toBe(`${url}?foo=bar`);
expect(config.params).toStrictEqual({ foo: "bar" });
});
Expand All @@ -44,18 +46,18 @@ describe("axios interceptor", () => {

const url = "https://localhost/foo";

moxios.stubOnce("POST", url, {});
mock.onPost(url).replyOnce(200, {});

// Act
await client.post(url, data, {
headers: { "X-Custom-Header": "foo", "Content-Type": "application/json" },
});

// Assert
const request = moxios.requests.first();
expect(request.headers["Content-Type"]).toEqual("application/json");
expect(request.headers["X-Custom-Header"]).toEqual("foo");
expect(request.headers["Authorization"]).toContain("AWS");
const request = mock.history.post[0];
expect(request.headers?.["Content-Type"]).toEqual("application/json");
expect(request.headers?.["X-Custom-Header"]).toEqual("foo");
expect(request.headers?.["Authorization"]).toContain("AWS");
});

it("should preserve default headers - without interceptor", async () => {
Expand All @@ -66,16 +68,14 @@ describe("axios interceptor", () => {

const url = "https://localhost/foo";

moxios.stubOnce("POST", url, {});
mock.onPost(url).replyOnce(200, {});

// Act
await client.post(url, data, {});

// Assert
const request = moxios.requests.first();
expect(request.headers["Content-Type"]).toEqual(
"application/json;charset=utf-8"
);
const request = mock.history.post[0];
expect(request.headers?.["Content-Type"]).toEqual("application/json");
});

it("should preserve default headers - with interceptor", async () => {
Expand All @@ -88,15 +88,13 @@ describe("axios interceptor", () => {

const url = "https://localhost/foo";

moxios.stubOnce("POST", url, {});
mock.onPost(url).replyOnce(200, {});

// Act
await client.post(url, data, {});

// Assert
const request = moxios.requests.first();
expect(request.headers["Content-Type"]).toEqual(
"application/json;charset=utf-8"
);
const request = mock.history.post[0];
expect(request.headers?.["Content-Type"]).toEqual("application/json");
});
});
20 changes: 10 additions & 10 deletions src/interceptor.test.ts
@@ -1,5 +1,5 @@
import { sign } from "aws4";
import axios, { AxiosRequestConfig } from "axios";
import axios, { AxiosRequestConfig, AxiosRequestHeaders } from "axios";
import { aws4Interceptor } from ".";
import { CredentialsProvider } from "./credentials/credentialsProvider";

Expand All @@ -25,14 +25,14 @@ const mockCustomProvider: CredentialsProvider = {
},
};

const getDefaultHeaders = () => ({
common: { Accept: "application/json, text/plain, */*" },
delete: {},
get: {},
head: {},
post: { "Content-Type": "application/x-www-form-urlencoded" },
put: { "Content-Type": "application/x-www-form-urlencoded" },
patch: { "Content-Type": "application/x-www-form-urlencoded" },
const getDefaultHeaders = (): AxiosRequestHeaders => ({
// common: { Accept: "application/json, text/plain, */*" },
// delete: {},
// get: {},
// head: {},
// post: { "Content-Type": "application/x-www-form-urlencoded" },
// put: { "Content-Type": "application/x-www-form-urlencoded" },
// patch: { "Content-Type": "application/x-www-form-urlencoded" },
});

const getDefaultTransformRequest = () => axios.defaults.transformRequest;
Expand Down Expand Up @@ -165,7 +165,7 @@ describe("interceptor", () => {
region: "local",
host: "example.com",
body: '{"foo":"bar"}',
headers: { "Content-Type": "application/json;charset=utf-8" },
headers: { "Content-Type": "application/json" },
},
undefined
);
Expand Down
14 changes: 10 additions & 4 deletions src/interceptor.ts
@@ -1,4 +1,4 @@
import { AxiosRequestConfig } from "axios";
import { AxiosRequestConfig, AxiosRequestHeaders, Method } from "axios";
import { sign } from "aws4";
import buildUrl from "axios/lib/helpers/buildURL";
import combineURLs from "axios/lib/helpers/combineURLs";
Expand Down Expand Up @@ -37,7 +37,7 @@ export interface InterceptorOptions {

export interface SigningOptions {
host?: string;
headers?: unknown;
headers?: AxiosRequestHeaders;
path?: string;
body?: unknown;
region?: string;
Expand All @@ -52,6 +52,11 @@ export interface Credentials {
sessionToken?: string;
}

export type InternalAxiosHeaders = Record<
Method | "common",
Record<string, string>
>;

/**
* Create an interceptor to add to the Axios request chain. This interceptor
* will sign requests with the AWSv4 signature.
Expand Down Expand Up @@ -117,7 +122,8 @@ export const aws4Interceptor = (
put,
patch,
...headersToSign
} = headers;
} = headers as any as InternalAxiosHeaders;
// Axios type definitions do not match the real shape of this object

const signingOptions: SigningOptions = {
method: method && method.toUpperCase(),
Expand All @@ -127,7 +133,7 @@ export const aws4Interceptor = (
service: options?.service,
signQuery: options?.signQuery,
body: transformedData,
headers: headersToSign,
headers: headersToSign as any,
};

const resolvedCredentials = await credentialsProvider.getCredentials();
Expand Down
51 changes: 29 additions & 22 deletions yarn.lock
Expand Up @@ -2140,13 +2140,6 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==

"@types/moxios@^0.4.9":
version "0.4.10"
resolved "https://registry.yarnpkg.com/@types/moxios/-/moxios-0.4.10.tgz#0762de3157714f8078a3c79ba65dcec4da701bfe"
integrity sha512-OGXB0kvKJT4KAdy4OzdGkBhNJ3f1x3FsqUq6elUBLcaBsVMy09hErlyhq2+zwEwcNbv1DGmksASW06XRISnxUQ==
dependencies:
axios "^0.21.1"

"@types/node@*", "@types/node@>= 8", "@types/node@^14.14.21":
version "14.14.25"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.25.tgz#15967a7b577ff81383f9b888aa6705d43fbbae93"
Expand Down Expand Up @@ -2755,12 +2748,21 @@ aws4@^1.8.0, aws4@^1.9.1:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==

axios@0.21.1, axios@^0.21.0, axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
axios-mock-adapter@^1.20.0:
version "1.20.0"
resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.20.0.tgz#21f5b4b625306f43e8c05673616719da86e20dcb"
integrity sha512-shZRhTjLP0WWdcvHKf3rH3iW9deb3UdKbdnKUoHmmsnBhVXN3sjPJM6ZvQ2r/ywgvBVQrMnjrSyQab60G1sr2w==
dependencies:
follow-redirects "^1.10.0"
fast-deep-equal "^3.1.3"
is-blob "^2.1.0"
is-buffer "^2.0.5"

axios@0.25.0, axios@^0.21.1, axios@^0.25.0:
version "0.25.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.25.0.tgz#349cfbb31331a9b4453190791760a8d35b093e0a"
integrity sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==
dependencies:
follow-redirects "^1.14.7"

babel-jest@^26.6.3:
version "26.6.3"
Expand Down Expand Up @@ -4846,7 +4848,7 @@ fast-base64-decode@^1.0.0:
resolved "https://registry.yarnpkg.com/fast-base64-decode/-/fast-base64-decode-1.0.0.tgz#b434a0dd7d92b12b43f26819300d2dafb83ee418"
integrity sha512-qwaScUgUGBYeDNRnbc/KyllVU88Jk1pRHPStuF/lO7B0/RTRLj7U0lkdTAutlBblY08rwZDff6tNU9cjv6j//Q==

fast-deep-equal@^3.1.1:
fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==
Expand Down Expand Up @@ -5084,10 +5086,10 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

follow-redirects@^1.10.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.1.tgz#5f69b813376cee4fd0474a3aba835df04ab763b7"
integrity sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==
follow-redirects@^1.14.7:
version "1.14.7"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685"
integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==

forever-agent@~0.6.1:
version "0.6.1"
Expand Down Expand Up @@ -5946,11 +5948,21 @@ is-binary-path@~2.1.0:
dependencies:
binary-extensions "^2.0.0"

is-blob@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-blob/-/is-blob-2.1.0.tgz#e36cd82c90653f1e1b930f11baf9c64216a05385"
integrity sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==

is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==

is-buffer@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.5.tgz#ebc252e400d22ff8d77fa09888821a24a658c191"
integrity sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==

is-callable@^1.1.4, is-callable@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.2.tgz#c7c6715cd22d4ddb48d3e19970223aceabb080d9"
Expand Down Expand Up @@ -7791,11 +7803,6 @@ move-concurrently@^1.0.1:
rimraf "^2.5.4"
run-queue "^1.0.3"

moxios@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/moxios/-/moxios-0.4.0.tgz#fc0da2c65477d725ca6b9679d58370ed0c52f53b"
integrity sha1-/A2ixlR31yXKa5Z51YNw7QxS9Ts=

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 9ae8eb6

Please sign in to comment.