UNPKG

googleapis-nodejs-functions

Version:

Google Cloud Functions Client Library for Node.js (unofficial)

171 lines 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@google-cloud/common"); const promisify_1 = require("@google-cloud/promisify"); const is = require("is"); const request = require("request"); /** * Create a Function object to interact with a Cloud Functions. * * @param {GCF} gcf {@link GCF} instance. * @param {string} name The name of the function. * @param {object} [options] Configuration object. * @param {string} [options.userProject] User project. */ class CloudFunction extends common_1.ServiceObject { constructor(gcf, name, metadata, options) { options = options || {}; const methods = { create: true, delete: true, exists: true, get: true, getMetadata: true }; super({ parent: gcf, baseUrl: `projects/${gcf.projectId}/locations/${gcf.location}/functions`, id: name, createMethod: gcf.createCloudFunction.bind(gcf), methods, requestModule: request, }); this.gcf = gcf; this.name = name; this.metadata = metadata; this.userProject = options.userProject; } /** * Delete the function. * * @param {object} [options] Configuration options. * @param {string} [options.userProject] The ID of the project which will be * billed for the request. * @param {FunctionCallback} [callback] Callback function. * @returns {Promise<Operation>} */ delete(options, callback) { if (is.fn(options)) { callback = options; options = {}; } this.request({ method: 'DELETE', uri: '', qs: options, }, callback || common_1.util.noop); } /** * Check if the function exists. * * @param {object} [options] Configuration options. * @param {string} [options.userProject] The ID of the project which will be * billed for the request. * @param {FunctionCallback} [callback] Callback function. * @returns {Promise<boolean>} * */ exists(options, callback) { if (is.fn(options)) { callback = options; options = {}; } options = options || {}; this.get(options, err => { if (err) { if (err.code === 404) { callback(null, false); } else { callback(err); } return; } callback(null, true); }); } /** * Get a Function if it exists. * * @param {object} [options] Configuration options. * @param {string} [options.userProject] The ID of the project which will be * billed for the request. * @param {CloudFunctionCallback} [callback] Callback function. * @returns {Promise<CloudFunction>} * */ get(options, callback) { if (is.fn(options)) { callback = options; options = {}; } options = options || {}; const onCreate = (err, fn, apiResponse) => { if (err) { if (err.code === 409) { this.get(options, callback); return; } callback(err, null, apiResponse); return; } callback(null, fn, apiResponse); }; this.getMetadata(options, (err, metadata) => { if (err) { if (err.code === 404) { const args = []; if (!is.empty(options)) { args.push(options); } args.push(onCreate); this.create.apply(this, args); return; } callback(err, null, metadata); return; } callback(null, this, metadata); }); } /** * Get the functions's metadata. * * @param {object} [options] Configuration options. * @param {FunctionCallback} [callback] Callback function. * @returns {Promise<CloudFunctionMetadata>} */ getMetadata(options, callback) { if (is.fn(options)) { callback = options; options = {}; } this.request({ method: 'GET', uri: '', qs: options, }, (err, resp) => { if (err) { callback(err, null, resp); return; } this.metadata = resp; callback(null, this.metadata, resp); }); } } exports.CloudFunction = CloudFunction; /*! Developer Documentation * * These methods can be auto-paginated. */ // paginator.extend(Function, ''); /*! Developer Documentation * * All async methods (except for streams) will return a Promise in the event * that a callback is omitted. */ promisify_1.promisifyAll(CloudFunction, { exclude: [], }); //# sourceMappingURL=cloudfunction.js.map