Skip to content

Commit

Permalink
fix: config merge (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Mar 12, 2020
1 parent 0a56948 commit a111ffd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
include:
- stage: test
node_js: lts/*
if: branch = master AND type = push AND fork = false
if: fork = false
script:
- npm run test:e2e
- stage: release
Expand Down
42 changes: 42 additions & 0 deletions e2e/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import axios from "axios";
import { elevation } from "../src/elevation";
import { Client, defaultHttpsAgent, defaultAxiosInstance } from "../src";

test("client should work with defaults", async () => {
const location = { lat: 10, lng: 20 };

const params = {
locations: [location, location],
key: process.env.GOOGLE_MAPS_API_KEY
};
const client = new Client();
const r = await client.elevation({ params: params });

expect(r.data.results.length).toEqual(2);
});

test("client should work with modified config", async () => {
const location = { lat: 10, lng: 20 };

const params = {
locations: [location, location],
key: process.env.GOOGLE_MAPS_API_KEY
};
const client = new Client({ config: { timeout: 30000, httpsAgent: defaultHttpsAgent } });
const r = await client.elevation({ params: params });

expect(r.data.results.length).toEqual(2);
});

test("client should work with instance", async () => {
const location = { lat: 10, lng: 20 };

const params = {
locations: [location, location],
key: process.env.GOOGLE_MAPS_API_KEY
};
const client = new Client({ axiosInstance: defaultAxiosInstance });
const r = await client.elevation({ params: params });

expect(r.data.results.length).toEqual(2);
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"scripts": {
"prepare": "tsc",
"test": "jest src",
"test:e2e": "jest e2e"
"test:e2e": "jest e2e",
"test:all": "jest"
},
"dependencies": {
"agentkeepalive": "^4.1.0",
"axios": "^0.19.0",
"query-string": "^6.11.0",
"deepmerge": "^4.2.2"
"query-string": "^6.11.0"
},
"devDependencies": {
"@semantic-release/changelog": "^5.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ test("client can be instantiated with axiosInstance has correct defaults", () =>
expect(client["axiosInstance"].defaults.headers["User-Agent"]).toEqual(
userAgent
);
expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBeUndefined();
expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBe(
acceptEncoding
);
expect(client["axiosInstance"].defaults.timeout).toEqual(
axios.defaults.timeout
);
Expand Down
11 changes: 7 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios, { AxiosRequestConfig, AxiosInstance } from "axios";
import * as merge from "deepmerge";
import { version } from "./index";
import { HttpsAgent } from "agentkeepalive";
import {
Expand Down Expand Up @@ -103,10 +102,14 @@ export class Client {

if (axiosInstance) {
this.axiosInstance = axiosInstance;
// Avoid changing this instance in any way that affects its behavior
this.axiosInstance.defaults.headers["User-Agent"] = userAgent;
this.axiosInstance.defaults.headers = {
...defaultConfig.headers,
...this.axiosInstance.defaults.headers
};
} else if (config) {
this.axiosInstance = axios.create(merge(defaultConfig, config));
config = { ...defaultConfig, ...config };
config.headers = { ...defaultConfig.headers, ...(config.headers || {}) };
this.axiosInstance = axios.create(config);
} else {
this.axiosInstance = defaultAxiosInstance;
}
Expand Down

0 comments on commit a111ffd

Please sign in to comment.