celeritas
Version:
This is an API service framework which supports API requests over HTTP & WebSockets.
121 lines (107 loc) • 3.82 kB
JavaScript
var Router = {
routes: {},
get: function (args) {
args.method = "GET";
this._createRoute(args);
},
post: function (args) {
args.method = "POST";
this._createRoute(args);
},
put: function (args) {
args.method = "PUT";
this._createRoute(args);
},
delete: function (args) {
args.method = "DELETE";
this._createRoute(args);
},
/* This creates a URL route to access a given function.
arguments: {
*method: GET|POST|PUT|DELETE
permissions: null|true|{}
published: TRUE|FALSE; determines if this endpoint is available to be called.
paging: TRUE|FALSE; determines if this endpoint supports paging.
cache: int|false; defines if the results of this API call can be cached, and if it can, how long in seconds the data should be cached for.
type: "application/json", "text/csv", etc.
alphabetize: (defaults to app setting): true/false for if the api response should be auto-alphabetized for this route.
get: {} a schema defining what data can be passed via the query string
post: {} a schema defining what data can be passed via the body
*route: e.g. "users/create", url route of the API method
*execute: Function; a function that takes in parameter 'call' to execute this API.
}
*/
_createRoute: function (args) {
if (!args.route || typeof args.route != "string")
throw new Error("Error! You must define a 'route' for the API service to accessed from.");
if (!args.execute || typeof args.execute != "function")
throw new Error("Error! You must define an 'execute' for the API service to accessed from.");
var method = args.method.toUpperCase() || "GET";
var permissions = args.permissions;
if (typeof permissions === "undefined")
permissions = null;
var description = args.description || "";
var get = args.get || {};
var post = args.post || {};
var cache = (method == "GET") ? args.cache || false : false;
var mimeType = args.type || "application/json";
var paging = (typeof args.paging !== "undefined") ? args.paging : false;
var published = args.published;
if (typeof published == "undefined")
published = true;
if (published === null)
published = false;
var documented = args.documented;
if (typeof documented == "undefined")
documented = true;
if (documented === null)
documented = false;
let enforceSort = args.enforceSort || false;
var alphabetize = (args.alphabetize === true || args.alphabetize === false) ? args.alphabetize : null;
var timeout = args.timeout;
var path = args.route;
var execute = args.execute;
//If the last part of the API path looks like :id, then any value can be there, which will be associated as a property of param. E.g. call.param.id = value of :id
var last = path.split("/").pop();
if (last.indexOf(":") === 0) {
var param = last.substr(1);
path = path.substr(0, path.length - param.length-1) + "*";
}
this.routes[path.toLowerCase()] = this.routes[path.toLowerCase()] || {};
this.routes[path.toLowerCase()][method] = {
name: args.name || path,
path: path,
type: mimeType,
timeout: timeout,
published: published,
documented: documented,
permissions: permissions,
description: description,
cache: cache,
paging: paging,
enforceSort: enforceSort,
alphabetize: alphabetize,
get: get,
post: post,
execute: async (call) => {
try {
return await execute(call);
}
catch (err) {
let http_code = err.http_code || 500;
err = Object.assign({
message: err.message,
trace: err.stack,
type: err.constructor.name,
code: err.app_code
}, err.properties);
return call.emit(http_code, err);
}
}
};
if (typeof param !== "undefined")
this.routes[path.toLowerCase()][method].param = param;
}
}
module.exports = Router;