Skip to content

Commit 1c014d8

Browse files
stephenplusplusJustinBeckwith
authored andcommitted
refactor(deps): use gaxios for HTTP requests instead of axios (#593)
1 parent 06da209 commit 1c014d8

File tree

8 files changed

+72
-100
lines changed

8 files changed

+72
-100
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"client library"
1818
],
1919
"dependencies": {
20-
"axios": "^0.18.0",
2120
"base64-js": "^1.3.0",
2221
"fast-text-encoding": "^1.0.0",
22+
"gaxios": "^1.1.1",
2323
"gcp-metadata": "^0.9.3",
2424
"gtoken": "^2.3.2",
2525
"https-proxy-agent": "^2.2.1",

src/auth/authclient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AxiosPromise, AxiosRequestConfig} from 'axios';
1817
import {EventEmitter} from 'events';
18+
import {GaxiosOptions, GaxiosPromise} from 'gaxios';
1919

2020
import {DefaultTransporter} from '../transporters';
2121

@@ -30,9 +30,9 @@ export abstract class AuthClient extends EventEmitter {
3030
credentials: Credentials = {};
3131

3232
/**
33-
* Provides an alternative Axios request implementation with auth credentials
33+
* Provides an alternative Gaxios request implementation with auth credentials
3434
*/
35-
abstract request<T>(opts: AxiosRequestConfig): AxiosPromise<T>;
35+
abstract request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
3636

3737
/**
3838
* Sets the auth credentials.

src/auth/computeclient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios';
17+
import {GaxiosError, GaxiosOptions, GaxiosPromise} from 'gaxios';
1818
import * as gcpMetadata from 'gcp-metadata';
1919
import * as messages from '../messages';
2020
import {CredentialRequest, Credentials} from './credentials';
@@ -82,10 +82,10 @@ export class Compute extends OAuth2Client {
8282
return {tokens, res: null};
8383
}
8484

85-
protected requestAsync<T>(opts: AxiosRequestConfig, retry = false):
86-
AxiosPromise<T> {
85+
protected requestAsync<T>(opts: GaxiosOptions, retry = false):
86+
GaxiosPromise<T> {
8787
return super.requestAsync<T>(opts, retry).catch(e => {
88-
const res = (e as AxiosError).response;
88+
const res = (e as GaxiosError).response;
8989
if (res && res.status) {
9090
let helpfulMessage = null;
9191
if (res.status === 403) {

src/auth/googleauth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AxiosRequestConfig, AxiosResponse} from 'axios';
1817
import {exec} from 'child_process';
1918
import * as fs from 'fs';
19+
import {GaxiosOptions, GaxiosResponse} from 'gaxios';
2020
import * as gcpMetadata from 'gcp-metadata';
2121
import * as os from 'os';
2222
import * as path from 'path';
@@ -732,7 +732,7 @@ export class GoogleAuth {
732732
* @param opts Axios request options for the HTTP request.
733733
*/
734734
// tslint:disable-next-line no-any
735-
async request<T = any>(opts: AxiosRequestConfig): Promise<AxiosResponse<T>> {
735+
async request<T = any>(opts: GaxiosOptions): Promise<GaxiosResponse<T>> {
736736
const client = await this.getClient();
737737
return client.request<T>(opts);
738738
}

src/auth/oauth2client.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios';
17+
import {GaxiosError, GaxiosOptions, GaxiosPromise, GaxiosResponse} from 'gaxios';
1818
import * as querystring from 'querystring';
1919
import * as stream from 'stream';
2020

@@ -264,53 +264,53 @@ export interface GenerateAuthUrlOpts {
264264
}
265265

266266
export interface GetTokenCallback {
267-
(err: AxiosError|null, token?: Credentials|null,
268-
res?: AxiosResponse|null): void;
267+
(err: GaxiosError|null, token?: Credentials|null,
268+
res?: GaxiosResponse|null): void;
269269
}
270270

271271
export interface GetTokenResponse {
272272
tokens: Credentials;
273-
res: AxiosResponse|null;
273+
res: GaxiosResponse|null;
274274
}
275275

276276
export interface GetAccessTokenCallback {
277-
(err: AxiosError|null, token?: string|null, res?: AxiosResponse|null): void;
277+
(err: GaxiosError|null, token?: string|null, res?: GaxiosResponse|null): void;
278278
}
279279

280280
export interface GetAccessTokenResponse {
281281
token?: string|null;
282-
res?: AxiosResponse|null;
282+
res?: GaxiosResponse|null;
283283
}
284284

285285
export interface RefreshAccessTokenCallback {
286-
(err: AxiosError|null, credentials?: Credentials|null,
287-
res?: AxiosResponse|null): void;
286+
(err: GaxiosError|null, credentials?: Credentials|null,
287+
res?: GaxiosResponse|null): void;
288288
}
289289

290290
export interface RefreshAccessTokenResponse {
291291
credentials: Credentials;
292-
res: AxiosResponse|null;
292+
res: GaxiosResponse|null;
293293
}
294294

295295
export interface RequestMetadataResponse {
296296
headers: Headers;
297-
res?: AxiosResponse<void>|null;
297+
res?: GaxiosResponse<void>|null;
298298
}
299299

300300
export interface RequestMetadataCallback {
301-
(err: AxiosError|null, headers?: Headers,
302-
res?: AxiosResponse<void>|null): void;
301+
(err: GaxiosError|null, headers?: Headers,
302+
res?: GaxiosResponse<void>|null): void;
303303
}
304304

305305
export interface GetFederatedSignonCertsCallback {
306-
(err: AxiosError|null, certs?: Certificates,
307-
response?: AxiosResponse<void>|null): void;
306+
(err: GaxiosError|null, certs?: Certificates,
307+
response?: GaxiosResponse<void>|null): void;
308308
}
309309

310310
export interface FederatedSignonCertsResponse {
311311
certs: Certificates;
312312
format: CertificateFormat;
313-
res?: AxiosResponse<void>|null;
313+
res?: GaxiosResponse<void>|null;
314314
}
315315

316316
export interface RevokeCredentialsResult {
@@ -715,7 +715,7 @@ export class OAuth2Client extends AuthClient {
715715
r = await this.refreshToken(thisCreds.refresh_token);
716716
tokens = r.tokens;
717717
} catch (err) {
718-
const e = err as AxiosError;
718+
const e = err as GaxiosError;
719719
if (e.response &&
720720
(e.response.status === 403 || e.response.status === 404)) {
721721
e.message = 'Could not refresh access token.';
@@ -747,14 +747,17 @@ export class OAuth2Client extends AuthClient {
747747
* @param token The existing token to be revoked.
748748
* @param callback Optional callback fn.
749749
*/
750-
revokeToken(token: string): AxiosPromise<RevokeCredentialsResult>;
750+
revokeToken(token: string): GaxiosPromise<RevokeCredentialsResult>;
751751
revokeToken(
752752
token: string,
753753
callback: BodyResponseCallback<RevokeCredentialsResult>): void;
754754
revokeToken(
755755
token: string, callback?: BodyResponseCallback<RevokeCredentialsResult>):
756-
AxiosPromise<RevokeCredentialsResult>|void {
757-
const opts = {url: OAuth2Client.getRevokeTokenUrl(token), method: 'POST'};
756+
GaxiosPromise<RevokeCredentialsResult>|void {
757+
const opts: GaxiosOptions = {
758+
url: OAuth2Client.getRevokeTokenUrl(token),
759+
method: 'POST'
760+
};
758761
if (callback) {
759762
this.transporter.request<RevokeCredentialsResult>(opts).then(
760763
r => callback(null, r), callback);
@@ -768,11 +771,11 @@ export class OAuth2Client extends AuthClient {
768771
* Revokes access token and clears the credentials object
769772
* @param callback callback
770773
*/
771-
revokeCredentials(): AxiosPromise<RevokeCredentialsResult>;
774+
revokeCredentials(): GaxiosPromise<RevokeCredentialsResult>;
772775
revokeCredentials(callback: BodyResponseCallback<RevokeCredentialsResult>):
773776
void;
774777
revokeCredentials(callback?: BodyResponseCallback<RevokeCredentialsResult>):
775-
AxiosPromise<RevokeCredentialsResult>|void {
778+
GaxiosPromise<RevokeCredentialsResult>|void {
776779
if (callback) {
777780
this.revokeCredentialsAsync().then(res => callback(null, res), callback);
778781
} else {
@@ -798,10 +801,10 @@ export class OAuth2Client extends AuthClient {
798801
* @param callback callback.
799802
* @return Request object
800803
*/
801-
request<T>(opts: AxiosRequestConfig): AxiosPromise<T>;
802-
request<T>(opts: AxiosRequestConfig, callback: BodyResponseCallback<T>): void;
803-
request<T>(opts: AxiosRequestConfig, callback?: BodyResponseCallback<T>):
804-
AxiosPromise<T>|void {
804+
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
805+
request<T>(opts: GaxiosOptions, callback: BodyResponseCallback<T>): void;
806+
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>):
807+
GaxiosPromise<T>|void {
805808
if (callback) {
806809
this.requestAsync<T>(opts).then(r => callback(null, r), e => {
807810
return callback(e, e.response);
@@ -811,9 +814,9 @@ export class OAuth2Client extends AuthClient {
811814
}
812815
}
813816

814-
protected async requestAsync<T>(opts: AxiosRequestConfig, retry = false):
815-
Promise<AxiosResponse<T>> {
816-
let r2: AxiosResponse;
817+
protected async requestAsync<T>(opts: GaxiosOptions, retry = false):
818+
Promise<GaxiosResponse<T>> {
819+
let r2: GaxiosResponse;
817820
try {
818821
const r = await this.getRequestMetadataAsync(opts.url);
819822
if (r.headers && r.headers.Authorization) {
@@ -826,7 +829,7 @@ export class OAuth2Client extends AuthClient {
826829
}
827830
r2 = await this.transporter.request<T>(opts);
828831
} catch (e) {
829-
const res = (e as AxiosError).response;
832+
const res = (e as GaxiosError).response;
830833
if (res) {
831834
const statusCode = res.status;
832835
// Retry the request for metadata if the following criteria are true:
@@ -944,7 +947,7 @@ export class OAuth2Client extends AuthClient {
944947
this.certificateCacheFormat === format) {
945948
return {certs: this.certificateCache, format};
946949
}
947-
let res: AxiosResponse;
950+
let res: GaxiosResponse;
948951
let url: string;
949952
switch (format) {
950953
case CertificateFormat.PEM:

src/transporters.ts

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
import axios, {AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios';
17+
import {GaxiosError, GaxiosOptions, GaxiosPromise, GaxiosResponse, request} from 'gaxios';
1818

1919
import {isBrowser} from './isbrowser';
2020
import {validate} from './options';
2121

22-
// tslint:disable-next-line variable-name
23-
const HttpsProxyAgent = require('https-proxy-agent');
24-
2522
// tslint:disable-next-line no-var-requires
2623
const pkg = require('../../package.json');
2724
const PRODUCT_NAME = 'google-api-nodejs-client';
2825

2926
export interface Transporter {
30-
request<T>(opts: AxiosRequestConfig): AxiosPromise<T>;
31-
request<T>(opts: AxiosRequestConfig, callback?: BodyResponseCallback<T>):
32-
void;
33-
request<T>(opts: AxiosRequestConfig, callback?: BodyResponseCallback<T>):
34-
AxiosPromise|void;
27+
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
28+
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>): void;
29+
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>):
30+
GaxiosPromise|void;
3531
}
3632

3733
export interface BodyResponseCallback<T> {
3834
// The `body` object is a truly dynamic type. It must be `any`.
39-
(err: Error|null, res?: AxiosResponse<T>|null): void;
35+
(err: Error|null, res?: GaxiosResponse<T>|null): void;
4036
}
4137

42-
export interface RequestError extends AxiosError {
38+
export interface RequestError extends GaxiosError {
4339
errors: Error[];
4440
}
4541

46-
/**
47-
* Axios will use XHR if it is available. In the case of Electron,
48-
* since XHR is there it will try to use that. This leads to OPTIONS
49-
* preflight requests which googleapis DOES NOT like. This line of
50-
* code pins the adapter to ensure it uses node.
51-
* https://github.com/google/google-api-nodejs-client/issues/1083
52-
*/
53-
axios.defaults.adapter = require('axios/lib/adapters/http');
54-
5542
export class DefaultTransporter {
5643
/**
5744
* Default user agent.
@@ -60,10 +47,10 @@ export class DefaultTransporter {
6047

6148
/**
6249
* Configures request options before making a request.
63-
* @param opts AxiosRequestConfig options.
50+
* @param opts GaxiosOptions options.
6451
* @return Configured options.
6552
*/
66-
configure(opts: AxiosRequestConfig = {}): AxiosRequestConfig {
53+
configure(opts: GaxiosOptions = {}): GaxiosOptions {
6754
opts.headers = opts.headers || {};
6855
if (!isBrowser()) {
6956
// set transporter user agent if not in browser
@@ -79,16 +66,15 @@ export class DefaultTransporter {
7966
}
8067

8168
/**
82-
* Makes a request using Axios with given options.
83-
* @param opts AxiosRequestConfig options.
84-
* @param callback optional callback that contains AxiosResponse object.
85-
* @return AxiosPromise, assuming no callback is passed.
69+
* Makes a request using Gaxios with given options.
70+
* @param opts GaxiosOptions options.
71+
* @param callback optional callback that contains GaxiosResponse object.
72+
* @return GaxiosPromise, assuming no callback is passed.
8673
*/
87-
request<T>(opts: AxiosRequestConfig): AxiosPromise<T>;
88-
request<T>(opts: AxiosRequestConfig, callback?: BodyResponseCallback<T>):
89-
void;
90-
request<T>(opts: AxiosRequestConfig, callback?: BodyResponseCallback<T>):
91-
AxiosPromise|void {
74+
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
75+
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>): void;
76+
request<T>(opts: GaxiosOptions, callback?: BodyResponseCallback<T>):
77+
GaxiosPromise|void {
9278
// ensure the user isn't passing in request-style options
9379
opts = this.configure(opts);
9480
try {
@@ -101,24 +87,16 @@ export class DefaultTransporter {
10187
}
10288
}
10389

104-
// If the user configured an `HTTPS_PROXY` environment variable, create
105-
// a custom agent to proxy the request.
106-
const proxy = process.env.HTTPS_PROXY || process.env.https_proxy;
107-
if (proxy) {
108-
opts.httpsAgent = new HttpsProxyAgent(proxy);
109-
opts.proxy = false;
110-
}
111-
11290
if (callback) {
113-
axios(opts).then(
91+
request<T>(opts).then(
11492
r => {
11593
callback(null, r);
11694
},
11795
e => {
11896
callback(this.processError(e));
11997
});
12098
} else {
121-
return axios(opts).catch(e => {
99+
return request<T>(opts).catch(e => {
122100
throw this.processError(e);
123101
});
124102
}
@@ -127,7 +105,7 @@ export class DefaultTransporter {
127105
/**
128106
* Changes the error to include details from the body.
129107
*/
130-
private processError(e: AxiosError): RequestError {
108+
private processError(e: GaxiosError): RequestError {
131109
const res = e.response;
132110
const err = e as RequestError;
133111
const body = res ? res.data : null;

0 commit comments

Comments
 (0)