Repository: googlemaps/google-maps-services-js
Pull request #354 (2020-03-08 07:23:13)

fix: make ClientOptions optional in Client constructor
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
- [ ] Make sure to open a GitHub issue as a bug/feature request before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [ ] Ensure the tests and linter pass
- [ ] Code coverage does not decrease (if any source code was changed)
- [ ] Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕


googlebot: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fgooglemaps%2Fgoogle-maps-services-js%2Fpull%2F354) for more info**.

<!-- need_sender_cla -->
codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=h1) Report
> Merging [#354](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=desc) into [master](https://codecov.io/gh/googlemaps/google-maps-services-js/commit/4dff0300d268d569c1d446a854e8c67b455062c3&el=desc) will **decrease** coverage by `0.30%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354/graphs/tree.svg?width=650&height=150&src=pr&token=wXidzRKqFM)](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##            master     #354      +/-   ##
===========================================
- Coverage   100.00%   99.69%   -0.31%     
===========================================
  Files           21       21              
  Lines          326      327       +1     
  Branches        84       84              
===========================================
  Hits           326      326              
- Misses           0        1       +1     
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=footer). Last update [4dff030...c722c54](https://codecov.io/gh/googlemaps/google-maps-services-js/pull/354?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

avin-kavish: @googlebot I signed it!

Patch:
diff --git a/src/client.ts b/src/client.ts
index 7776f946d..8737daa92 100644
--- a/src/client.ts
+++ b/src/client.ts
@@ -70,12 +70,16 @@ import {
 export const defaultHttpsAgent = new HttpsAgent({ keepAlive: true });
 export const defaultTimeout = 10000;
 export const userAgent = `google-maps-services-node-${version}`;
+export const acceptEncoding = "gzip";
 export const X_GOOG_MAPS_EXPERIENCE_ID = "X-GOOG-MAPS-EXPERIENCE-ID";
 
 const defaultConfig = {
   timeout: defaultTimeout,
   httpsAgent: defaultHttpsAgent,
-  headers: { "User-Agent": userAgent }
+  headers: {
+    "User-Agent": userAgent,
+    "Accept-Encoding": acceptEncoding
+  }
 };
 
 export const defaultAxiosInstance = axios.create(defaultConfig);
@@ -92,7 +96,7 @@ export class Client {
   private axiosInstance: AxiosInstance;
   private experienceId: string[];
 
-  constructor({ axiosInstance, config, experienceId }: ClientOptions) {
+  constructor({ axiosInstance, config, experienceId }: ClientOptions = {}) {
     if (axiosInstance && config) {
       throw new Error("Provide one of axiosInstance or config.");
     }


Test patch:
diff --git a/e2e/compression.test.ts b/e2e/compression.test.ts
new file mode 100644
index 000000000..078df5271
--- /dev/null
+++ b/e2e/compression.test.ts
@@ -0,0 +1,29 @@
+import { directions } from "../src/directions";
+
+test("server responds with compressed content", async () => {
+  const params = {
+    origin: "Seattle, WA",
+    destination: "San Francisco, CA",
+    waypoints: [{ lat: 40, lng: -120 }],
+    key: process.env.GOOGLE_MAPS_API_KEY
+  };
+
+  // Use of directions here is entirely arbitrary and any API that supports
+  // result compression could be substituted.
+
+  const r = await directions({ params: params });
+  expect(r.data.status).toEqual("OK");
+
+  // Axios removes conent-encoding from the response header set once it takes
+  // care of piping the stream through a zlib unzip instance.  So verifying
+  // that the server responds with a compressed response must be done via the
+  // raw headers.
+
+  const {rawHeaders} = r.request.res;
+  const contentEncodingIndex = rawHeaders
+      .findIndex(i => i.toLowerCase() === "content-encoding");
+
+  expect(contentEncodingIndex).not.toBe(-1);
+  expect(rawHeaders[contentEncodingIndex + 1]).toBe("gzip");
+  expect(r.headers["Content-Encoding"]).toBeUndefined();
+});
diff --git a/src/client.test.ts b/src/client.test.ts
index 759c9fc24..0055a6df9 100644
--- a/src/client.test.ts
+++ b/src/client.test.ts
@@ -1,6 +1,7 @@
 import {
   Client,
   userAgent,
+  acceptEncoding,
   DirectionsRequest,
   DistanceMatrixRequest,
   ElevationRequest,
@@ -38,6 +39,7 @@ 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.timeout).toEqual(
     axios.defaults.timeout
   );
@@ -59,6 +61,21 @@ test("client can be instantiated with header options", () => {
   expect(client["axiosInstance"].defaults.headers["User-Agent"]).toEqual(
     userAgent
   );
+  expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBe(
+      acceptEncoding
+  );
+});
+
+test("client can be override Accept-Encoding with header options", () => {
+  const client = new Client({ config: { headers: { "x-foo": "bar", "Accept-Encoding": "identity" } } });
+  expect(client["axiosInstance"]).toBeDefined();
+  expect(client["axiosInstance"].defaults.headers["x-foo"]).toEqual("bar");
+  expect(client["axiosInstance"].defaults.headers["User-Agent"]).toEqual(
+      userAgent
+  );
+  expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBe(
+      "identity"
+  );
 });
 
 test("client can be instantiated without header options", () => {
@@ -68,6 +85,9 @@ test("client can be instantiated without header options", () => {
   expect(client["axiosInstance"].defaults.headers["User-Agent"]).toEqual(
     userAgent
   );
+  expect(client["axiosInstance"].defaults.headers["Accept-Encoding"]).toBe(
+      acceptEncoding
+  );
 });
 
 test("client can be instantiated with experienceId", () => {