Skip to content

Commit

Permalink
feat: allow customizing the http client (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
forty authored Jun 28, 2022
1 parent 8072bae commit 408ad04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import * as fs from 'fs';
import {GaxiosError, request} from 'gaxios';
import {GaxiosOptions, GaxiosPromise, request, GaxiosError} from 'gaxios';
import * as jws from 'jws';
import * as path from 'path';
import {promisify} from 'util';
Expand All @@ -32,6 +32,10 @@ const GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token';
const GOOGLE_REVOKE_TOKEN_URL =
'https://accounts.google.com/o/oauth2/revoke?token=';

export interface Transporter {
request<T>(opts: GaxiosOptions): GaxiosPromise<T>;
}

export type GetTokenCallback = (err: Error | null, token?: TokenData) => void;

export interface Credentials {
Expand Down Expand Up @@ -59,6 +63,7 @@ export interface TokenOptions {
// milliseconds from expiring".
// Defaults to 0
eagerRefreshThresholdMillis?: number;
transporter?: Transporter;
}

export interface GetTokenOptions {
Expand Down Expand Up @@ -97,6 +102,9 @@ export class GoogleToken {
email?: string;
additionalClaims?: {};
eagerRefreshThresholdMillis?: number;
transporter: Transporter = {
request: opts => request(opts),
};

private inFlightRequest?: undefined | Promise<TokenData>;

Expand Down Expand Up @@ -274,7 +282,7 @@ export class GoogleToken {
throw new Error('No token to revoke.');
}
const url = GOOGLE_REVOKE_TOKEN_URL + this.accessToken;
await request({url});
await this.transporter.request({url});
this.configure({
email: this.iss,
sub: this.sub,
Expand Down Expand Up @@ -302,6 +310,9 @@ export class GoogleToken {
this.scope = options.scope;
}
this.eagerRefreshThresholdMillis = options.eagerRefreshThresholdMillis;
if (options.transporter) {
this.transporter = options.transporter;
}
}

/**
Expand All @@ -327,7 +338,7 @@ export class GoogleToken {
secret: this.key,
});
try {
const r = await request<TokenData>({
const r = await this.transporter.request<TokenData>({
method: 'POST',
url: GOOGLE_TOKEN_URL,
data: {
Expand Down
22 changes: 22 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as assert from 'assert';
import {describe, it} from 'mocha';
import * as fs from 'fs';
import * as nock from 'nock';
import {request} from 'gaxios';
import {GoogleToken} from '../src';

const EMAIL = 'example@developer.gserviceaccount.com';
Expand Down Expand Up @@ -521,6 +522,27 @@ describe('.getToken()', () => {
});
});

it('should use a custom transporter if one is provided', done => {
let customTransporterWasUsed = false;
const gtoken = new GoogleToken({
...TESTDATA,
transporter: {
request: opts => {
customTransporterWasUsed = true;
return request(opts);
},
},
});
const fakeToken = 'nodeftw';
const scope = createGetTokenMock(200, {access_token: fakeToken});
gtoken.getToken((err, token) => {
scope.done();
assert.strictEqual(err, null);
assert(customTransporterWasUsed);
done();
});
});

it('should set and return correct properties on success', done => {
const gtoken = new GoogleToken(TESTDATA);
const RESPBODY = {
Expand Down

0 comments on commit 408ad04

Please sign in to comment.