-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: switch to axios #182
Conversation
@ofrobots all the tests are passing, this is ready for a pass. Apologies for the size of the review, couldn't really find a way to split it up. |
package.json
Outdated
}, | ||
"devDependencies": { | ||
"@types/jws": "^3.1.0", |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/authclient.ts
Outdated
@@ -28,5 +34,5 @@ export abstract class AuthClient { | |||
* Provides an alternative request | |||
* implementations with auth credentials. | |||
*/ | |||
abstract request(opts: request.Options): request.Request|void; | |||
abstract request(opts: AxiosRequestConfig): Promise<BodyResponse>; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/transporters.ts
Outdated
request(opts: request.Options, callback?: BodyResponseCallback) { | ||
request(opts: AxiosRequestConfig): AxiosPromise; | ||
request(opts: AxiosRequestConfig, callback?: BodyResponseCallback): void; | ||
request(opts: AxiosRequestConfig, callback?: BodyResponseCallback): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/authclient.ts
Outdated
|
||
import {DefaultTransporter} from '../transporters'; | ||
|
||
import {Credentials} from './credentials'; | ||
|
||
export interface BodyResponse { | ||
// tslint:disable-next-line no-any | ||
body: any; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/transporters.ts
Outdated
body = JSON.parse(body as string); | ||
} catch (err) { | ||
/* no op */ | ||
private processError(e: AxiosError): RequestError { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@ofrobots I think I covered all the feedback so far. Ready for the next wave :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some initial comments. Still want going through the rest of the file, but ran out of time.
src/auth/computeclient.ts
Outdated
try { | ||
const url = this.opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL; | ||
// request for new token | ||
const res = await this.transporter.request({url}); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/computeclient.ts
Outdated
}); | ||
protected async refreshToken(refreshToken?: string| | ||
null): Promise<GetTokenResponse> { | ||
try { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/credentials.ts
Outdated
@@ -19,6 +19,7 @@ export interface Credentials { | |||
expiry_date?: number; | |||
access_token?: string; | |||
token_type?: string; | |||
expires_in?: number; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
getDefaultProjectId(callback: ProjectIdCallback) { | ||
getDefaultProjectId(): Promise<string>; | ||
getDefaultProjectId(callback: ProjectIdCallback): void; | ||
getDefaultProjectId(callback?: ProjectIdCallback): Promise<string|null>|void { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
return; | ||
} | ||
// environment variable | ||
projectId = this.getProductionProjectId(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
callback: | ||
(error: Error|null, stdout: string, stderr: string|null) => void) { | ||
exec('gcloud -q config list core/project --format=json', callback); | ||
_getSDKDefaultProjectId(): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still more to come
src/auth/googleauth.ts
Outdated
if (gce) { | ||
// For GCE, just return a default ComputeClient. It will take care of | ||
// the rest. | ||
return {projectId: null, credential: new Compute()}; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
let client: UserRefreshClient|JWT; | ||
if (!json) { | ||
try { | ||
if (!json) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
json: JWTInput, | ||
callback?: (err: Error|null, client?: UserRefreshClient|JWT) => void) { | ||
fromJSON(json: JWTInput): JWT|UserRefreshClient; | ||
fromJSON(json: JWTInput, callback: CredentialCallback): void; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -453,15 +486,23 @@ export class GoogleAuth { | |||
* @param {function=} - Optional callback function | |||
*/ | |||
fromAPIKey( | |||
apiKey: string, callback?: (err?: Error|null, client?: JWT) => void) { | |||
apiKey: string, |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
(err: Error|null, headers?: http.IncomingHttpHeaders|null) => void) { | ||
const iat = Math.floor(new Date().getTime() / 1000); | ||
const exp = iat + 3600; // 3600 seconds = 1 hour | ||
getRequestMetadata(authURI: string): RequestMetadataResponse; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/jwtclient.ts
Outdated
} else { | ||
return newGToken.getToken((err2?: Error|null, token?: string|null) => { | ||
return done(err2, { | ||
async refreshToken(refreshToken?: string|null) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/jwtclient.ts
Outdated
}); | ||
}); | ||
} | ||
} as Credentials; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
done(new Error( | ||
'The incoming JSON object does not contain a private_key field')); | ||
return; | ||
fromJSON(json: JWTInput, callback?: (err?: Error) => void): void { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/jwtclient.ts
Outdated
done(err); | ||
} | ||
|
||
private async fromStreamAsync(inputStream: stream.Readable) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -215,37 +233,36 @@ export class JWT extends OAuth2Client { | |||
* @param {function=} callback - Optional callback to be invoked after | |||
* initialization. | |||
*/ | |||
fromAPIKey(apiKey: string, callback?: (err: Error) => void) { | |||
const done = callback || noop; | |||
fromAPIKey(apiKey: string, callback?: (err?: Error) => void): void { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/oauth2client.ts
Outdated
const res = await this.transporter.request<CredentialRequest>( | ||
{method: 'POST', url, data}); | ||
const tokens = res.data as Credentials; | ||
if (res.data && res.data.expires_in) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/oauth2client.ts
Outdated
} else { | ||
return callback(null, this.credentials.access_token, null); | ||
return {token: this.credentials.access_token, res: undefined}; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/oauth2client.ts
Outdated
const headers = { | ||
Authorization: credentials.token_type + ' ' + tokens.access_token | ||
}; | ||
return {headers, res: r ? r.res : null}; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if (callback) { | ||
callback(new Error( | ||
'Must pass in a JSON object containing the user refresh token')); | ||
try { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/options.ts
Outdated
@@ -0,0 +1,33 @@ | |||
import {AxiosRequestConfig} from 'axios'; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -643,7 +612,7 @@ describe('.fromAPIKey', () => { | |||
it('should set the .apiKey property on the instance', (done) => { | |||
jwt.fromAPIKey(KEY, (err) => { | |||
assert.strictEqual(jwt.apiKey, KEY); | |||
assert.strictEqual(err, null); | |||
assert.equal(err, null); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
test/test.oauth2.ts
Outdated
@@ -855,7 +854,7 @@ describe('OAuth2 client', () => { | |||
const auth = new GoogleAuth(); | |||
const oauth2client = | |||
new auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI); | |||
oauth2client.request(({} as request.OptionsWithUri), (err, result) => { | |||
oauth2client.request(({} as AxiosRequestConfig), (err, result) => { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
test/test.oauth2.ts
Outdated
@@ -996,7 +995,10 @@ describe('OAuth2 client', () => { | |||
oauth2client.credentials = {access_token: 'abc', refresh_token: 'abc'}; | |||
oauth2client.revokeCredentials((err, result) => { | |||
assert.equal(err, null); | |||
assert.equal(result.success, true); | |||
assert(result); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
test/test.oauth2.ts
Outdated
assert(tokens.expiry_date >= now + (10 * 1000)); | ||
assert(tokens.expiry_date <= now + (15 * 1000)); | ||
assert(tokens); | ||
if (tokens && tokens.expiry_date) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
test/test.transporters.ts
Outdated
const defaultUserAgentRE = 'google-api-nodejs-client/\\d+.\\d+.\\d+'; | ||
const transporter = new DefaultTransporter(); | ||
|
||
it('should set default client user agent if none is set', () => { | ||
const opts = transporter.configure(({} as request.OptionsWithUrl)); | ||
const opts = transporter.configure(({} as AxiosRequestConfig)); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Switching out request for axios, and making the API support callback and async styles.