Skip to content
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

feat: export static GoogleAuth #249

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,16 @@ For example, a JWT auth client will be created when your code is running on your
The code below shows how to retrieve a default credential type, depending upon the runtime environment. The createScopedRequired must be called to determine when you need to pass in the scopes manually, and when they have been set for you automatically based on the configured runtime environment.

```js
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth();
const {auth} = require('google-auth-library');

/**
* Acquire a client, and make a request to an API that's enabled by default.
*/
async function main() {
try {
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;
const res = await adc.client.request({url});
console.log(res.data);
} catch (e) {
console.error('Error making request.');
console.error(e);
}
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;
const res = await adc.client.request({url});
console.log(res.data);
}

/**
Expand Down Expand Up @@ -98,7 +92,7 @@ async function getADC() {
}
}

main();
main().catch(console.error);
```

#### OAuth2 client
Expand Down Expand Up @@ -275,7 +269,7 @@ $ export CREDS='{
Now you can create a new client from the credentials:

```js
const {GoogleAuth} = require('google-auth-library');
const {auth} = require('google-auth-library');

// load the environment variable with our keys
const keysEnvVar = process.env['CREDS'];
Expand All @@ -285,7 +279,6 @@ if (!keysEnvVar) {
const keys = JSON.parse(keysEnvVar);

async function main() {
const auth = new GoogleAuth();
// load the JWT or UserRefreshClient from the keys
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
Expand Down
18 changes: 6 additions & 12 deletions examples/adc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@
/**
* Import the GoogleAuth library, and create a new GoogleAuth client.
*/
const { GoogleAuth } = require('google-auth-library');
const auth = new GoogleAuth();
const { auth } = require('google-auth-library');

/**
* Acquire a client, and make a request to an API that's enabled by default.
*/
async function main() {
try {
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;
const res = await adc.client.request({ url });
console.log(res.data);
} catch (e) {
console.error('Error making request.');
console.error(e);
}
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;
const res = await adc.client.request({ url });
console.log(res.data);
}

/**
Expand Down Expand Up @@ -65,4 +59,4 @@ async function getADC() {
};
}

main();
main().catch(console.error);
3 changes: 1 addition & 2 deletions examples/fromJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'use strict';

const { GoogleAuth } = require('google-auth-library');
const { auth } = require('google-auth-library');

/**
* Instead of loading credentials from a key file, you can also provide
Expand Down Expand Up @@ -47,7 +47,6 @@ if (!keysEnvVar) {
const keys = JSON.parse(keysEnvVar);

async function main() {
const auth = new GoogleAuth();
const client = auth.fromJSON(keys);
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
await client.authorize();
Expand Down
42 changes: 18 additions & 24 deletions examples/keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,32 @@
/**
* Import the GoogleAuth library, and create a new GoogleAuth client.
*/
const { GoogleAuth } = require('google-auth-library');
const auth = new GoogleAuth();
const { auth } = require('google-auth-library');
const https = require('https');

/**
* Acquire a client, and make a request to an API that's enabled by default.
*/
async function main() {
try {
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;
const adc = await getADC();
const url = `https://www.googleapis.com/dns/v1/projects/${adc.projectId}`;

// create a new agent with keepAlive enabled
const agent = new https.Agent({ keepAlive: true });
// create a new agent with keepAlive enabled
const agent = new https.Agent({ keepAlive: true });

// use the agent as an Axios config param to make the request
const res = await adc.client.request({
url,
httpsAgent: agent
});
console.log(res.data);
// use the agent as an Axios config param to make the request
const res = await adc.client.request({
url,
httpsAgent: agent
});
console.log(res.data);

// Re-use the same agent to make the next request over the same connection
const res2 = await adc.client.request({
url,
httpsAgent: agent
});
console.log(res2.data);
} catch (e) {
console.error('Error making request.');
console.error(e);
}
// Re-use the same agent to make the next request over the same connection
const res2 = await adc.client.request({
url,
httpsAgent: agent
});
console.log(res2.data);
}

/**
Expand Down Expand Up @@ -82,4 +76,4 @@ async function getADC() {
};
}

main();
main().catch(console.error);
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {GoogleAuth} from './auth/googleauth';

export {Compute} from './auth/computeclient';
export {GoogleAuth} from './auth/googleauth';
export {IAMAuth} from './auth/iam';
export {JWTAccess} from './auth/jwtaccess';
export {JWT} from './auth/jwtclient';
export {CodeChallengeMethod, OAuth2Client} from './auth/oauth2client';
export {UserRefreshClient} from './auth/refreshclient';
export {DefaultTransporter} from './transporters';

const auth = new GoogleAuth();
export {auth, GoogleAuth};
11 changes: 6 additions & 5 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import * as http from 'http';
import * as nock from 'nock';
import * as path from 'path';
import * as stream from 'stream';
import {PassThrough} from 'stream';

import {DefaultTransporter, GoogleAuth, JWT, UserRefreshClient} from '../src/index';
import {auth, DefaultTransporter, GoogleAuth, JWT, UserRefreshClient} from '../src/index';
import {BodyResponseCallback} from '../src/transporters';

nock.disableNetConnect();
Expand Down Expand Up @@ -89,8 +87,6 @@ class MockTransporter extends DefaultTransporter {
}
}



// Creates a standard JSON auth object for testing.
function createJwtJSON() {
return {
Expand Down Expand Up @@ -156,6 +152,11 @@ function insertWellKnownFilePathIntoAuth(
}

describe('GoogleAuth', () => {
it('should support the instantiated named export', () => {
const result = auth.fromJSON(createJwtJSON());
assert(result);
});

describe('.fromJson', () => {
it('should error on null json', () => {
const auth = new GoogleAuth();
Expand Down