built.io
Version:
SDK for Built.io Backend
236 lines (218 loc) • 8.17 kB
JavaScript
var R = require('ramda');
var utility = require('./utilities/utility');
var instanceMethodBuilder = require('./utilities/instanceMethodBuilder')();
/*
Constants
*/
var extensionPath = '/functions';
var allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
/**
* @class Extension
* @classdesc App.Extension allows you to add your own business logic to Built.io Backend. The business logic is written in JavaScript, and executed on the cloud.
* @instance
* @description Use this constructor to get an instance of Extension
* @param extension_key {String} The extension key of the extension application
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
*
* var extion = Built.App('blt5d4sample2633b').Extension('blt_extKey'); // blt_extKey is a extension key.
* @return {Extension}
*/
var extensionCons = module.exports = R.curry(function(app, extension_key) {
if(!extension_key){
extension_key = app.getExtensionKey()
}
var returnObj = {
extension_key : extension_key,
app : app,
toJSON : function() {
return extension_key
}
}
installPluginsOnInst(returnObj)
return instanceMethodBuilder.build(module.exports, returnObj)
})
/*
pluginsHelper is global variable declared in built.js
*/
function installPluginsOnInst(ext){
pluginsHelper.plugins.map(function(plugin){
if(plugin.realtime && plugin.realtime.onExtensionsInstance){
plugin.realtime.onExtensionsInstance(ext);
}
});
}
/**
Use the function to execute a function call deployed on Built.io Backend Extension.
* @function executeFunction
* @param method {String} The HTTP method of the function that needs to be executed (GET, POST, PUT, DELETE)
* @param fnUrl {String} The url of the function that needs to be executed
* @param reqBody {object} The request body that needs to be sent with the request (Optional)
* @param reqHeaders {object} Headers that need to be sent along with the request (Optional)
* @instance
* @memberof Extension
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var app = Built.App('blt5d4sample2633b')
* app.Extension()
* .executeFunction('GET', '/submit', body, headers)
* .then(function(result){
* // do somethings
* });
* @return {Promise<Variable>}
*/
var execute = module.exports.executeFunction = R.curry(function(method, fnUrl, reqBody, reqHeaders, extension){
if(!R.contains(method, allowedHttpMethods))
throw new Error("invalid http method")
var app = extension.app
method = method || "POST"
return makeFunctionCall(app.options.adaptor, method, getURL(fnUrl, extension.getExtensionKey(),app), getCombinedHeaders(extension, reqHeaders), reqBody, null);
});
instanceMethodBuilder.define('executeFunction', 5, [{}, {}]);
/**
Use the function to execute a get function call deployed on Built.io Backend Extension.
* @function get
* @param fnUrl {String} The url of the function that needs to be executed
* @param reqBody {object} The request body that needs to be sent with the request (Optional)
* @param reqHeaders {object} Headers that need to be sent along with the request (Optional)
* @instance
* @memberof Extension
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var app = Built.App('blt5d4sample2633b')
* app.Extension()
* .get('/getUser', body, headers)
* .then(function(result){
* // do somethings
* });
* @return {Promise<Variable>}
*/
module.exports.get = execute('GET')
instanceMethodBuilder.define('get', 4, [{}, {}]);
/**
Use the function to execute a post function call deployed on Built.io Backend Extension.
* @function post
* @param fnUrl {String} The url of the function that needs to be executed
* @param reqBody {object} The request body that needs to be sent with the request (Optional)
* @param reqHeaders {object} Headers that need to be sent along with the request (Optional)
* @instance
* @memberof Extension
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var app = Built.App('blt5d4sample2633b')
* app.Extension()
* .post('/submit', body, headers)
* .then(function(result){
* // do somethings
* });
* @return {Promise<Variable>}
*/
module.exports.post = execute('POST')
instanceMethodBuilder.define('post', 4, [{}, {}]);
/**
Use the function to execute a put function call deployed on Built.io Backend Extension.
* @function put
* @param fnUrl {String} The url of the function that needs to be executed
* @param reqBody {object} The request body that needs to be sent with the request (Optional)
* @param reqHeaders {object} Headers that need to be sent along with the request (Optional)
* @instance
* @memberof Extension
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var app = Built.App('blt5d4sample2633b')
* app.Extension()
* .put('/submit', body, headers)
* .then(function(result){
* // do somethings
* });
* @return {Promise<Variable>}
*/
module.exports.put = execute('PUT')
instanceMethodBuilder.define('put', 4, [{}, {}]);
/**
Use the function to execute a delete function call deployed on Built.io Backend Extension.
* @function delete
* @param fnUrl {String} The url of the function that needs to be executed
* @param reqBody {object} The request body that needs to be sent with the request (Optional)
* @param reqHeaders {object} Headers that need to be sent along with the request (Optional)
* @instance
* @memberof Extension
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var app = Built.App('blt5d4sample2633b')
* app.Extension()
* .delete('/submit', body, headers)
* .then(function(result){
* // do somethings
* });
* @return {Promise<Variable>}
*/
module.exports.delete = execute('DELETE')
instanceMethodBuilder.define('delete', 4, [{}, {}]);
/**
* Sets the extension key for an extension
* @memberof Extension
* @function setExtensionKey
* @param {String} extensionKey The Extension key
* @instance
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var extension = Built.App('blt5d4sample2633b').Extension().setExtensionKey('bltdb28fsaMplEf1b8');
* @return {Extension}
*/
module.exports.setExtensionKey = R.curry(function(extension_key, extension){
return extensionCons(extension.app, extension_key)
})
instanceMethodBuilder.define('setExtensionKey', 2);
/**
* Gets the extension key for an extension
* @memberof Extension
* @function getExtensionKey
* @instance
* @example
* // 'blt5d4sample2633b' is a dummy Application API key
* var extension = Built.App('blt5d4sample2633b').Extension().setExtensionKey('bltdb28fsaMplEf1b8');
* console.log(extension.getExtensionKey());
* @return {String}
*/
module.exports.getExtensionKey = R.curry(function(extension){
return extension.extension_key
})
instanceMethodBuilder.define('getExtensionKey', 1);
/**
* Determines whether extension_key is assigned to this Extension
* @function isNew
* @instance
* @memberof Extension
* @example
* //'blt5d4sample2633b' is a dummy Application API key
* // 'local_extension' is a dummy extension_key
* var extApp = Built.App('blt5d4sample2633b').Extension('local_extension')
* var boolean = extApp.isNew();
* @return {Boolean}
*/
module.exports.isNew = function(extension){
return !! extension.getExtensionKey()
}
instanceMethodBuilder.define('isNew',1);
function makeFunctionCall(adaptor, method, path, headers, entity, params) {
if(method == "GET") {
params = entity
entity = null
}
var requestObject = utility.getAdaptorObj(method, path, headers, entity, params);
return adaptor.makeCall(requestObject).then(function(response) {
return response.entity
});
}
function getURL (fnUrl, extension_key, app) {
return app.getURL() + "/extensions/" + extension_key + extensionPath + fnUrl;
}
function getCombinedHeaders(extension, reqHeaders){
reqHeaders = reqHeaders || {};
reqHeaders = R.mixin({
extension_key: extension.extension_key
}, reqHeaders)
var newHeaders = R.mixin(extension.app.getHeaders(),reqHeaders);
return newHeaders;
}