@google-cloud/common
Advanced tools
Comparing version 0.6.0 to 0.7.0
{ | ||
"name": "@google-cloud/common", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"author": "Google Inc.", | ||
@@ -5,0 +5,0 @@ "description": "Common components for Google Cloud APIs Node.js Client Libraries", |
@@ -137,2 +137,19 @@ /*! | ||
/** | ||
* Wraps the `complete` and `error` events in a Promise. | ||
* | ||
* @return {promise} | ||
*/ | ||
GrpcOperation.prototype.promise = function() { | ||
var self = this; | ||
return new self.Promise(function(resolve, reject) { | ||
self | ||
.on('error', reject) | ||
.on('complete', function(metadata) { | ||
resolve([metadata]); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Begin listening for events on the operation. This method keeps track of how | ||
@@ -139,0 +156,0 @@ * many "complete" listeners are registered and removed, making sure polling is |
@@ -134,2 +134,10 @@ /*! | ||
/*! Developer Documentation | ||
* | ||
* All async methods (except for streams) will return a Promise in the event | ||
* that a callback is omitted. | ||
*/ | ||
util.promisifyAll(GrpcServiceObject); | ||
module.exports = GrpcServiceObject; |
@@ -36,2 +36,8 @@ /*! | ||
/** | ||
* @type {module:common/paginator} | ||
* @private | ||
*/ | ||
exports.paginator = require('./paginator.js'); | ||
/** | ||
* @type {module:common/service} | ||
@@ -49,8 +55,2 @@ * @private | ||
/** | ||
* @type {module:common/streamRouter} | ||
* @private | ||
*/ | ||
exports.streamRouter = require('./stream-router.js'); | ||
/** | ||
* @type {module:common/util} | ||
@@ -57,0 +57,0 @@ * @private |
@@ -74,2 +74,3 @@ /*! | ||
this.interceptors = []; | ||
this.Promise = this.parent.Promise; | ||
@@ -82,3 +83,3 @@ if (config.methods) { | ||
// All ServiceObjects need `request`. | ||
methodName !== 'request' && | ||
!/^request/.test(methodName) && | ||
@@ -306,3 +307,3 @@ // The ServiceObject didn't redefine the method. | ||
*/ | ||
ServiceObject.prototype.request = function(reqOpts, callback) { | ||
ServiceObject.prototype.request_ = function(reqOpts, callback) { | ||
reqOpts = extend(true, {}, reqOpts); | ||
@@ -335,5 +336,41 @@ | ||
return this.parent.request(reqOpts, callback); | ||
if (!callback) { | ||
return this.parent.requestStream(reqOpts); | ||
} | ||
this.parent.request(reqOpts, callback); | ||
}; | ||
/** | ||
* Make an authenticated API request. | ||
* | ||
* @private | ||
* | ||
* @param {object} reqOpts - Request options that are passed to `request`. | ||
* @param {string} reqOpts.uri - A URI relative to the baseUrl. | ||
* @param {function} callback - The callback function passed to `request`. | ||
*/ | ||
ServiceObject.prototype.request = function(reqOpts, callback) { | ||
ServiceObject.prototype.request_.call(this, reqOpts, callback); | ||
}; | ||
/** | ||
* Make an authenticated API request. | ||
* | ||
* @private | ||
* | ||
* @param {object} reqOpts - Request options that are passed to `request`. | ||
* @param {string} reqOpts.uri - A URI relative to the baseUrl. | ||
*/ | ||
ServiceObject.prototype.requestStream = function(reqOpts) { | ||
return ServiceObject.prototype.request_.call(this, reqOpts); | ||
}; | ||
/*! Developer Documentation | ||
* | ||
* All async methods (except for streams) will return a Promise in the event | ||
* that a callback is omitted. | ||
*/ | ||
util.promisifyAll(ServiceObject); | ||
module.exports = ServiceObject; |
@@ -63,2 +63,3 @@ /*! | ||
this.projectIdRequired = config.projectIdRequired !== false; | ||
this.Promise = options.promise || Promise; | ||
} | ||
@@ -75,3 +76,3 @@ | ||
*/ | ||
Service.prototype.request = function(reqOpts, callback) { | ||
Service.prototype.request_ = function(reqOpts, callback) { | ||
reqOpts = extend(true, {}, reqOpts); | ||
@@ -127,2 +128,27 @@ | ||
/** | ||
* Make an authenticated API request. | ||
* | ||
* @private | ||
* | ||
* @param {object} reqOpts - Request options that are passed to `request`. | ||
* @param {string} reqOpts.uri - A URI relative to the baseUrl. | ||
* @param {function} callback - The callback function passed to `request`. | ||
*/ | ||
Service.prototype.request = function(reqOpts, callback) { | ||
Service.prototype.request_.call(this, reqOpts, callback); | ||
}; | ||
/** | ||
* Make an authenticated API request. | ||
* | ||
* @private | ||
* | ||
* @param {object} reqOpts - Request options that are passed to `request`. | ||
* @param {string} reqOpts.uri - A URI relative to the baseUrl. | ||
*/ | ||
Service.prototype.requestStream = function(reqOpts) { | ||
return Service.prototype.request_.call(this, reqOpts); | ||
}; | ||
module.exports = Service; |
@@ -634,1 +634,83 @@ /** | ||
util.getUserAgentFromPackageJson = getUserAgentFromPackageJson; | ||
/** | ||
* Wraps a callback style function to conditionally return a promise. | ||
* | ||
* @param {function} originalMethod - The method to promisify. | ||
* @return {function} wrapped | ||
*/ | ||
function promisify(originalMethod) { | ||
if (originalMethod.promisified_) { | ||
return originalMethod; | ||
} | ||
var slice = Array.prototype.slice; | ||
var wrapper = function() { | ||
var args = slice.call(arguments); | ||
var hasCallback = is.fn(args[args.length - 1]); | ||
var context = this; | ||
if (hasCallback) { | ||
return originalMethod.apply(context, args); | ||
} | ||
var PromiseCtor = Promise; | ||
// Because dedupe will likely create a single install of | ||
// @google-cloud/common to be shared amongst all modules, we need to | ||
// localize it at the Service level. | ||
if (context && context.Promise) { | ||
PromiseCtor = context.Promise; | ||
} | ||
return new PromiseCtor(function(resolve, reject) { | ||
args.push(function() { | ||
var callbackArgs = slice.call(arguments); | ||
var err = callbackArgs.shift(); | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(callbackArgs); | ||
}); | ||
originalMethod.apply(context, args); | ||
}); | ||
}; | ||
wrapper.promisified_ = true; | ||
return wrapper; | ||
} | ||
util.promisify = promisify; | ||
/** | ||
* Promisifies certain Class methods. This will not promisify private or | ||
* streaming methods. | ||
* | ||
* @param {module:common/service} Class - Service class. | ||
* @param {object=} options - Configuration object. | ||
*/ | ||
function promisifyAll(Class, options) { | ||
var exclude = options && options.exclude || []; | ||
var methods = Object | ||
.keys(Class.prototype) | ||
.filter(function(methodName) { | ||
return is.fn(Class.prototype[methodName]) && // is it a function? | ||
!/(^\_|(Stream|\_)$)/.test(methodName) && // is it public/non-stream? | ||
exclude.indexOf(methodName) === -1; // is it blacklisted? | ||
}); | ||
methods.forEach(function(methodName) { | ||
var originalMethod = Class.prototype[methodName]; | ||
if (!originalMethod.promisified_) { | ||
Class.prototype[methodName] = util.promisify(originalMethod); | ||
} | ||
}); | ||
} | ||
util.promisifyAll = promisifyAll; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86692
12
2292