googleapis-nodejs-functions
Version:
Google Cloud Functions Client Library for Node.js (unofficial)
169 lines • 6.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const arrify = require("arrify");
const common_1 = require("@google-cloud/common");
const paginator_1 = require("@google-cloud/paginator");
const promisify_1 = require("@google-cloud/promisify");
const extend = require("extend");
const request = require("request");
const cloudfunction_1 = require("./cloudfunction");
exports.CloudFunction = cloudfunction_1.CloudFunction;
var CloudFunctionStatus;
(function (CloudFunctionStatus) {
CloudFunctionStatus[CloudFunctionStatus["CLOUD_FUNCTION_STATUS_UNSPECIFIED"] = 0] = "CLOUD_FUNCTION_STATUS_UNSPECIFIED";
CloudFunctionStatus[CloudFunctionStatus["ACTIVE"] = 1] = "ACTIVE";
CloudFunctionStatus[CloudFunctionStatus["OFFLINE"] = 2] = "OFFLINE";
CloudFunctionStatus[CloudFunctionStatus["DEPLOY_IN_PROGRESS"] = 3] = "DEPLOY_IN_PROGRESS";
CloudFunctionStatus[CloudFunctionStatus["DELETE_IN_PROGRESS"] = 4] = "DELETE_IN_PROGRESS";
CloudFunctionStatus[CloudFunctionStatus["UNKNOWN"] = 5] = "UNKNOWN";
})(CloudFunctionStatus = exports.CloudFunctionStatus || (exports.CloudFunctionStatus = {}));
var CloudFunctionRuntimes;
(function (CloudFunctionRuntimes) {
CloudFunctionRuntimes[CloudFunctionRuntimes["nodejs6"] = 0] = "nodejs6";
CloudFunctionRuntimes[CloudFunctionRuntimes["nodejs8"] = 1] = "nodejs8";
CloudFunctionRuntimes[CloudFunctionRuntimes["python3"] = 2] = "python3";
})(CloudFunctionRuntimes = exports.CloudFunctionRuntimes || (exports.CloudFunctionRuntimes = {}));
/*!
* @param {ConfigurationObject} [options] Configuration options.
*/
class GCF extends common_1.Service {
constructor(options = {}, location) {
const config = {
baseUrl: 'https://cloudfunctions.googleapis.com/v1',
projectIdRequired: false,
scopes: [
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/cloudfunctions'
],
packageJson: require('../../package.json'),
requestModule: request,
};
super(config, options);
this.location = location || 'us-central1';
}
/**
* Get a reference to a Cloud Functions function.
*
* @param {string} name Name of the function.
* @returns {CloudFunction}
*/
cloudFunction(name, metadata) {
if (!name) {
throw new Error('A function name is needed to use Cloud Functions.');
}
return new cloudfunction_1.CloudFunction(this, name, metadata);
}
createCloudFunction(name, metadataOrCallback, callback) {
if (!name) {
throw new Error('A name is required to create a function.');
}
let metadata;
if (!callback) {
callback = metadataOrCallback;
metadata = {};
}
else {
metadata = metadataOrCallback;
}
// @developer @archelogos
// & in a type position means type intersection.
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types
const body = extend({}, metadata, { name });
// @developer @archelogos
// @TODO business logic here -> runtimes check
const location = metadata.location || this.location; // GCP region
// @developer @archelogos
// @TODO business logic here -> name check
body.name = `projects/${this.projectId}/locations/${location}/functions/${name}`;
this.request({
method: 'POST',
uri: `/projects/${this.projectId}/locations/${location}/functions`,
qs: '',
json: body,
}, (err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const operation = resp;
// @developer @archelogos
// That's the non-null assertion operator (https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci)
callback(null, operation, resp);
});
}
// @TODO @archelogos
getLocations() {
}
/**
* Get Function objects for all of the functions in your project.
*
* @param {CloudFunctionQuery} [query] Query object for listing function.
* @param {CloudFunctionCallback} [callback] Callback function.
* @returns {Promise<CloudFunction[]>}
*/
getCloudFunctions(query = {}, callback) {
const location = query.location || this.location; // GCP region
// @developer @archelogos
// @TODO check if parent is valid (this.getLocations)
// -> https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations/list
this.request({
method: 'GET',
uri: `/projects/${this.projectId}/locations/${location}/functions`,
qs: ''
}, (err, resp) => {
if (err) {
callback(err, null, null, resp);
return;
}
const cloudFunctions = arrify(resp.functions).map(fn => {
const cloudFunctionInstance = this.cloudFunction(fn.name, fn);
return cloudFunctionInstance;
});
let nextQuery;
if (resp.nextPageToken) {
nextQuery = extend({}, query, { pageToken: resp.nextPageToken });
}
callback(null, cloudFunctions, nextQuery, resp);
});
}
operation(name, callback) {
// @TODO validate name /operations/some/unique/name
this.request({
method: 'GET',
uri: `/${name}`,
qs: ''
}, (err, resp) => {
if (err) {
callback(err, null, resp);
return;
}
const operation = resp;
// @developer @archelogos
// That's the non-null assertion operator (https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci)
callback(null, operation, resp);
});
}
}
/**
* {@link CloudFunction} class.
*
* @name GCF.CloudFunction
* @see CloudFunction
* @type {Constructor}
*/
GCF.CloudFunction = cloudfunction_1.CloudFunction;
exports.GCF = GCF;
/*! Developer Documentation
*
* These methods can be auto-paginated.
*/
paginator_1.paginator.extend(GCF, 'getCloudFunctions');
/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
promisify_1.promisifyAll(GCF, {
exclude: ['cloudFunction'],
});
//# sourceMappingURL=index.js.map