Skip to content

Commit 5735a35

Browse files
JustinBeckwithjmdobry
authored andcommitted
Switch to TypeScript (googleapis#135)
1 parent d38f6c5 commit 5735a35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3580
-3825
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
node_modules/*
22
.idea
3+
.vscode
34
docs/
45
npm-debug.log
56
coverage
7+
lib/**/*.js
8+
test/**/*.js
9+
*.js.map

.jshintrc

Lines changed: 0 additions & 28 deletions
This file was deleted.

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ docs
44
node_modules
55
coverage
66
.github
7-
87
yarn.lock
98
jsdoc-conf.json
109
.travis.yml
1110
.gitignore
11+
*.js.map
12+
*.ts

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ sudo: false
22
language: node_js
33

44
node_js:
5-
- "0.10"
6-
- "0.12"
75
- "4"
86
- "6"
97
- "7"

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
# The email address is not required for organizations.
77
#
88
Google Inc.
9+

lib/auth/authclient.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

lib/utils.js renamed to lib/auth/authclient.ts

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

17-
'use strict';
17+
import DefaultTransporter from '../transporters';
18+
import Credentials from './credentials';
19+
20+
abstract class AuthClient {
21+
22+
public transporter = new DefaultTransporter();
23+
public credentials: Credentials;
1824

19-
/**
20-
* Export extend
21-
* @type {Object}
22-
*/
23-
module.exports = {
2425
/**
25-
* Copy key/values to obj from all other objects passed in
26-
*
27-
* @param {object} a the destination object.
28-
* @return {object} the destination object.
26+
* Provides an alternative request
27+
* implementations with auth credentials.
2928
*/
30-
extend: function(obj) {
31-
var source, prop;
32-
for (var i = 1, length = arguments.length; i < length; i++) {
33-
source = arguments[i];
34-
for (prop in source) {
35-
if (source.hasOwnProperty(prop)) {
36-
obj[prop] = source[prop];
37-
}
38-
}
39-
}
40-
return obj;
41-
}
42-
};
29+
public abstract request();
30+
}
31+
export default AuthClient;

lib/auth/computeclient.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

lib/auth/computeclient.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Copyright 2013 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as util from 'util';
18+
import Auth2Client from './oauth2client';
19+
20+
export default class Compute extends Auth2Client {
21+
22+
/**
23+
* Google Compute Engine metadata server token endpoint.
24+
*/
25+
protected static readonly _GOOGLE_OAUTH2_TOKEN_URL =
26+
'http://metadata.google.internal/computeMetadata/v1beta1/instance/service-accounts/default/token';
27+
28+
/**
29+
* Google Compute Engine service account credentials.
30+
*
31+
* Retrieve access token from the metadata server.
32+
* See: https://developers.google.com/compute/docs/authentication
33+
*/
34+
constructor() {
35+
super();
36+
// Start with an expired refresh token, which will automatically be refreshed
37+
// before the first API call is made.
38+
this.credentials = {
39+
expiry_date: 1,
40+
refresh_token: 'compute-placeholder'
41+
};
42+
}
43+
44+
/**
45+
* Indicates whether the credential requires scopes to be created by calling createdScoped before
46+
* use.
47+
* @return {object} The cloned instance.
48+
*/
49+
public createScopedRequired() {
50+
// On compute engine, scopes are specified at the compute instance's creation time,
51+
// and cannot be changed. For this reason, always return false.
52+
return false;
53+
}
54+
55+
/**
56+
* Refreshes the access token.
57+
* @param {object=} ignored_
58+
* @param {function=} opt_callback Optional callback.
59+
*/
60+
protected refreshToken(ignored, callback?) {
61+
const uri = this._opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL;
62+
// request for new token
63+
return this.transporter.request({
64+
method: 'GET',
65+
uri: uri,
66+
json: true
67+
}, (err, tokens, response) => {
68+
if (!err && tokens && tokens.expires_in) {
69+
tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000));
70+
delete tokens.expires_in;
71+
}
72+
if (callback) {
73+
callback(err, tokens, response);
74+
}
75+
});
76+
}
77+
78+
/**
79+
* Inserts a helpful error message guiding the user toward fixing common auth issues.
80+
* @param {object} err Error result.
81+
* @param {object} result The result.
82+
* @param {object} response The HTTP response.
83+
* @param {Function} callback The callback.
84+
*/
85+
protected postRequest(err, result, response, callback) {
86+
if (response && response.statusCode) {
87+
let helpfulMessage = null;
88+
if (response.statusCode === 403) {
89+
helpfulMessage = 'A Forbidden error was returned while attempting to retrieve an access ' +
90+
'token for the Compute Engine built-in service account. This may be because the Compute ' +
91+
'Engine instance does not have the correct permission scopes specified.';
92+
} else if (response.statusCode === 404) {
93+
helpfulMessage = 'A Not Found error was returned while attempting to retrieve an access' +
94+
'token for the Compute Engine built-in service account. This may be because the Compute ' +
95+
'Engine instance does not have any permission scopes specified.';
96+
}
97+
if (helpfulMessage) {
98+
if (err && err.message) {
99+
helpfulMessage += ' ' + err.message;
100+
}
101+
102+
if (err) {
103+
err.message = helpfulMessage;
104+
} else {
105+
err = new Error(helpfulMessage);
106+
err.code = response.statusCode;
107+
}
108+
}
109+
}
110+
callback(err, result, response);
111+
}
112+
}

0 commit comments

Comments
 (0)