Skip to content

Commit 689fadf

Browse files
PR feedback
1 parent 244cb85 commit 689fadf

File tree

9 files changed

+118
-123
lines changed

9 files changed

+118
-123
lines changed

ts/lib/auth/computeclient.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
import * as request from 'request';
1818

19-
import {RequestCallback, RequestError} from './../transporters';
19+
import {BodyResponseCallback, RequestError} from './../transporters';
2020
import Auth2Client from './oauth2client';
2121

22-
export interface Tokens {
22+
export interface Token {
2323
expires_in: number;
2424
expiry_date: number;
2525
}
2626

27-
export interface RefreshTokenCallback {
28-
(err: Error, tokens: Tokens, response: request.RequestResponse): void;
29-
}
27+
export declare type RefreshTokenCallback =
28+
(err: Error, token: Token, response: request.RequestResponse) => void;
3029

3130
export default class Compute extends Auth2Client {
3231
/**
@@ -63,22 +62,22 @@ export default class Compute extends Auth2Client {
6362
/**
6463
* Refreshes the access token.
6564
* @param {object=} ignored_
66-
* @param {function=} opt_callback Optional callback.
65+
* @param {function=} callback Optional callback.
6766
*/
6867
protected refreshToken(ignored: any, callback?: RefreshTokenCallback):
6968
request.Request {
7069
const uri = this._opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
7170
// request for new token
7271
return this.transporter.request(
7372
{method: 'GET', uri: uri, json: true}, (err, body, response) => {
74-
const tokens = body as Tokens;
75-
if (!err && tokens && tokens.expires_in) {
76-
tokens.expiry_date =
77-
((new Date()).getTime() + (tokens.expires_in * 1000));
78-
delete tokens.expires_in;
73+
const token = body as Token;
74+
if (!err && token && token.expires_in) {
75+
token.expiry_date =
76+
((new Date()).getTime() + (token.expires_in * 1000));
77+
delete token.expires_in;
7978
}
8079
if (callback) {
81-
callback(err, tokens, response);
80+
callback(err, token, response);
8281
}
8382
});
8483
}
@@ -93,7 +92,7 @@ export default class Compute extends Auth2Client {
9392
*/
9493
protected postRequest(
9594
err: Error, result: any, response: request.RequestResponse,
96-
callback: RequestCallback) {
95+
callback: BodyResponseCallback) {
9796
if (response && response.statusCode) {
9897
let helpfulMessage = null;
9998
if (response.statusCode === 403) {

ts/lib/auth/googleauth.ts

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as util from 'util';
2323

2424
import {DefaultTransporter, Transporter} from '../transporters';
2525

26-
import {RequestCallback} from './../transporters';
26+
import {BodyResponseCallback} from './../transporters';
2727
import Compute from './computeclient';
2828
import IAMAuth from './iam';
2929
import JWTAccess from './jwtaccess';
@@ -95,7 +95,7 @@ export default class GoogleAuth {
9595

9696
/**
9797
* Obtains the default project ID for the application..
98-
* @param {function=} opt_callback Optional callback.
98+
* @param {function=} callback Optional callback.
9999
*/
100100
public getDefaultProjectId(callback: ProjectIdCallback) {
101101
// In implicit case, supports three environments. In order of precedence,
@@ -161,15 +161,15 @@ export default class GoogleAuth {
161161

162162
/**
163163
* Obtains the default service-level credentials for the application..
164-
* @param {function=} opt_callback Optional callback.
164+
* @param {function=} callback Optional callback.
165165
*/
166166
public getApplicationDefault(
167-
opt_callback: (err: Error, credential: any, projectId: string) => void) {
167+
callback?: (err: Error, credential: any, projectId: string) => void) {
168168
// If we've already got a cached credential, just return it.
169169
if (this.cachedCredential) {
170170
setImmediate(() => {
171171
this.callback(
172-
opt_callback, null, this.cachedCredential, this._cachedProjectId);
172+
callback, null, this.cachedCredential, this._cachedProjectId);
173173
});
174174
} else {
175175
// Inject our own callback routine, which will cache the credential once
@@ -181,12 +181,12 @@ export default class GoogleAuth {
181181
this.getDefaultProjectId((err2, projectId) => {
182182
setImmediate(() => {
183183
// Ignore default project error
184-
this.callback(opt_callback, null, result, projectId);
184+
this.callback(callback, null, result, projectId);
185185
});
186186
});
187187
} else {
188188
setImmediate(() => {
189-
this.callback(opt_callback, err, result);
189+
this.callback(callback, err, result);
190190
});
191191
}
192192
};
@@ -246,12 +246,12 @@ export default class GoogleAuth {
246246

247247
/**
248248
* Attempts to load default credentials from the environment variable path..
249-
* @param {function=} opt_callback Optional callback.
249+
* @param {function=} callback Optional callback.
250250
* @return {boolean} Returns true if the callback has been executed; false otherwise.
251251
* @api private
252252
*/
253253
public _tryGetApplicationCredentialsFromEnvironmentVariable(
254-
opt_callback: (err: Error, result: any) => void) {
254+
callback?: (err: Error, result: any) => void) {
255255
const credentialsPath = this._getEnv('GOOGLE_APPLICATION_CREDENTIALS');
256256
if (!credentialsPath || credentialsPath.length === 0) {
257257
return false;
@@ -264,19 +264,19 @@ export default class GoogleAuth {
264264
'environment variable.',
265265
err);
266266
}
267-
this.callback(opt_callback, wrappedError, result);
267+
this.callback(callback, wrappedError, result);
268268
});
269269
return true;
270270
}
271271

272272
/**
273273
* Attempts to load default credentials from a well-known file location
274-
* @param {function=} opt_callback Optional callback.
274+
* @param {function=} callback Optional callback.
275275
* @return {boolean} Returns true if the callback has been executed; false otherwise.
276276
* @api private
277277
*/
278278
public _tryGetApplicationCredentialsFromWellKnownFile(
279-
opt_callback?: (err: Error, result: any) => void) {
279+
callback?: (err: Error, result?: any) => void) {
280280
// First, figure out the location of the file, depending upon the OS type.
281281
let location = null;
282282
if (this._isWindows()) {
@@ -305,18 +305,18 @@ export default class GoogleAuth {
305305
return false;
306306
}
307307
// The file seems to exist. Try to use it.
308-
this._getApplicationCredentialsFromFilePath(location, opt_callback);
308+
this._getApplicationCredentialsFromFilePath(location, callback);
309309
return true;
310310
}
311311

312312
/**
313313
* Attempts to load default credentials from a file at the given path..
314314
* @param {string=} filePath The path to the file to read.
315-
* @param {function=} opt_callback Optional callback.
315+
* @param {function=} callback Optional callback.
316316
* @api private
317317
*/
318318
public _getApplicationCredentialsFromFilePath(
319-
filePath: string, opt_callback: (err: Error, result?: any) => void) {
319+
filePath: string, callback: (err: Error, result?: any) => void) {
320320
let error = null;
321321
// Make sure the path looks like a string.
322322
if (!filePath || filePath.length === 0) {
@@ -346,27 +346,27 @@ export default class GoogleAuth {
346346
if (!error) {
347347
try {
348348
const stream = this._createReadStream(filePath);
349-
this.fromStream(stream, opt_callback);
349+
this.fromStream(stream, callback);
350350
} catch (err) {
351351
error = this.createError(
352352
util.format('Unable to read the file at %s.', filePath), err);
353353
}
354354
}
355355
if (error) {
356-
this.callback(opt_callback, error);
356+
this.callback(callback, error);
357357
}
358358
}
359359

360360
/**
361361
* Create a credentials instance using the given input options.
362362
* @param {object=} json The input object.
363-
* @param {function=} opt_callback Optional callback.
363+
* @param {function=} callback Optional callback.
364364
*/
365-
public fromJSON(json: any, opt_callback: (err: Error, client?: any) => void) {
365+
public fromJSON(json: any, callback?: (err: Error, client?: any) => void) {
366366
let client: UserRefreshClient|JWTClient = null;
367367
if (!json) {
368368
this.callback(
369-
opt_callback,
369+
callback,
370370
new Error(
371371
'Must pass in a JSON object containing the Google auth settings.'));
372372
return;
@@ -379,25 +379,24 @@ export default class GoogleAuth {
379379

380380
client.fromJSON(json, (err: Error) => {
381381
if (err) {
382-
this.callback(opt_callback, err);
382+
this.callback(callback, err);
383383
} else {
384-
this.callback(opt_callback, null, client);
384+
this.callback(callback, null, client);
385385
}
386386
});
387387
}
388388

389389
/**
390390
* Create a credentials instance using the given input stream.
391391
* @param {object=} stream The input stream.
392-
* @param {function=} opt_callback Optional callback.
392+
* @param {function=} callback Optional callback.
393393
*/
394394
public fromStream(
395-
stream: stream.Readable,
396-
opt_callback: (err: Error, result?: any) => void) {
395+
stream: stream.Readable, callback?: (err: Error, result?: any) => void) {
397396
if (!stream) {
398397
setImmediate(() => {
399398
this.callback(
400-
opt_callback,
399+
callback,
401400
new Error(
402401
'Must pass in a stream containing the Google auth settings.'));
403402
});
@@ -411,9 +410,9 @@ export default class GoogleAuth {
411410
stream.on('end', () => {
412411
try {
413412
const data = JSON.parse(s);
414-
this.fromJSON(data, opt_callback);
413+
this.fromJSON(data, callback);
415414
} catch (err) {
416-
this.callback(opt_callback, err);
415+
this.callback(callback, err);
417416
}
418417
});
419418
}
@@ -424,13 +423,13 @@ export default class GoogleAuth {
424423
* @param {function=} - Optional callback function
425424
*/
426425
public fromAPIKey(
427-
apiKey: string, opt_callback: (err: Error, client?: JWTClient) => void) {
426+
apiKey: string, callback?: (err: Error, client?: JWTClient) => void) {
428427
const client = new this.JWTClient();
429428
client.fromAPIKey(apiKey, (err) => {
430429
if (err) {
431-
this.callback(opt_callback, err);
430+
this.callback(callback, err);
432431
} else {
433-
this.callback(opt_callback, null, client);
432+
this.callback(callback, null, client);
434433
}
435434
});
436435
}
@@ -499,7 +498,7 @@ export default class GoogleAuth {
499498
}
500499

501500
// Executes the given callback if it is not null.
502-
private callback(c: Function, ...args: any[]) {
501+
private callback(c: Function, err?: Error, ...args: any[]) {
503502
if (c) {
504503
return c.apply(null, Array.prototype.slice.call(arguments, 1));
505504
}
@@ -600,7 +599,7 @@ export default class GoogleAuth {
600599
* @param {function} _callback Callback.
601600
* @api private
602601
*/
603-
private _getGCEProjectId(_callback: RequestCallback) {
602+
private _getGCEProjectId(_callback: BodyResponseCallback) {
604603
if (!this.transporter) {
605604
this.transporter = new DefaultTransporter();
606605
}

ts/lib/auth/iam.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default class IAMAuth {
5252
*/
5353
public getRequestMetadata(
5454
unused_uri: string,
55-
metadataFn: (err: Error, metadata: RequestMetadata) => void) {
55+
metadataFn: (err: Error, metadata?: RequestMetadata) => void) {
5656
metadataFn(null, {
5757
'x-goog-iam-authority-selector': this.selector,
5858
'x-goog-iam-authorization-token': this.token

ts/lib/auth/jwtaccess.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class JWTAccess {
5858
* request metadata.
5959
*/
6060
public getRequestMetadata(
61-
authURI: string, metadataCb: (err: Error, headers: any) => void) {
61+
authURI: string, metadataCb: (err: Error, headers?: any) => void) {
6262
const iat = Math.floor(new Date().getTime() / 1000);
6363
const exp = iat + 3600; // 3600 seconds = 1 hour
6464

@@ -86,10 +86,10 @@ export default class JWTAccess {
8686
/**
8787
* Create a JWTAccess credentials instance using the given input options.
8888
* @param {object=} json The input object.
89-
* @param {function=} opt_callback Optional callback.
89+
* @param {function=} callback Optional callback.
9090
*/
91-
public fromJSON(json: any, opt_callback: (err: Error) => void) {
92-
const done = opt_callback || noop;
91+
public fromJSON(json: any, callback?: (err: Error) => void) {
92+
const done = callback || noop;
9393
if (!json) {
9494
done(new Error(
9595
'Must pass in a JSON object containing the service account auth settings.'));
@@ -115,11 +115,10 @@ export default class JWTAccess {
115115
/**
116116
* Create a JWTAccess credentials instance using the given input stream.
117117
* @param {object=} stream The input stream.
118-
* @param {function=} opt_callback Optional callback.
118+
* @param {function=} callback Optional callback.
119119
*/
120-
public fromStream(
121-
stream: stream.Readable, opt_callback: (err: Error) => void) {
122-
const done = opt_callback || noop;
120+
public fromStream(stream: stream.Readable, callback?: (err: Error) => void) {
121+
const done = callback || noop;
123122
if (!stream) {
124123
setImmediate(() => {
125124
done(new Error(
@@ -135,7 +134,7 @@ export default class JWTAccess {
135134
stream.on('end', () => {
136135
try {
137136
const data = JSON.parse(s);
138-
this.fromJSON(data, opt_callback);
137+
this.fromJSON(data, callback);
139138
} catch (err) {
140139
done(err);
141140
}

0 commit comments

Comments
 (0)