Added versioning to API functions

This commit is contained in:
nelson.silva 2013-02-16 11:51:10 +00:00
parent 7c03bc2610
commit f0bde9bf18
3 changed files with 36 additions and 7 deletions

View file

@ -192,7 +192,7 @@ Example returns:
{code: 0, message:"ok", data: {text:"Welcome <strong>Text</strong>"}} {code: 0, message:"ok", data: {text:"Welcome <strong>Text</strong>"}}
{code: 1, message:"padID does not exist", data: null} {code: 1, message:"padID does not exist", data: null}
*/ */
exports.getHTML = function(padID, rev, callback) function getHTML(padID, rev, wrapBody, callback)
{ {
if(typeof rev == "function") if(typeof rev == "function")
{ {
@ -243,8 +243,10 @@ exports.getHTML = function(padID, rev, callback)
exportHtml.getPadHTML(pad, rev, function(err, html) exportHtml.getPadHTML(pad, rev, function(err, html)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
if(wrapBody) {
html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head
html += "</body></html>"; html += "</body></html>";
}
data = {html: html}; data = {html: html};
callback(null, data); callback(null, data);
}); });
@ -255,8 +257,10 @@ exports.getHTML = function(padID, rev, callback)
exportHtml.getPadHTML(pad, undefined, function (err, html) exportHtml.getPadHTML(pad, undefined, function (err, html)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
if(wrapBody){
html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head html = "<!DOCTYPE HTML><html><body>" +html; // adds HTML head
html += "</body></html>"; html += "</body></html>";
}
data = {html: html}; data = {html: html};
callback(null, data); callback(null, data);
}); });
@ -264,6 +268,11 @@ exports.getHTML = function(padID, rev, callback)
}); });
} }
exports.getHTML = {
"<1.2.7": function(padID, rev, callback){ return getHTML(padID, rev, false, callback); },
">=1.2.7": function(padID, rev, callback){ return getHTML(padID, rev, true, callback); } // wrap the body
};
exports.setHTML = function(padID, html, callback) exports.setHTML = function(padID, html, callback)
{ {
//get the pad //get the pad

View file

@ -21,6 +21,7 @@
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var fs = require("fs"); var fs = require("fs");
var semver = require("semver");
var api = require("../db/API"); var api = require("../db/API");
var padManager = require("../db/PadManager"); var padManager = require("../db/PadManager");
var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString; var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
@ -326,6 +327,24 @@ function callAPI(apiVersion, functionName, fields, req, res)
} }
}); });
var fn = api[functionName];
// If this is not a funcion it's a version=>function obj where version uses semver
if(typeof fn != "function") {
// Get a proper version x.y.z
var v = apiVersion.split(".");
var apiVersion = [v[0]||'0', v[1]||'0', v[2]||'0'].join(".");
// lookup the first version that satisfies the required version
for(var fnVersion in fn) {
if (semver.satisfies(apiVersion, fnVersion)) {
fn = fn[fnVersion];
break;
}
}
}
//call the api function //call the api function
api[functionName](functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]); fn(functionParams[0],functionParams[1],functionParams[2],functionParams[3],functionParams[4]);
} }

View file

@ -36,7 +36,8 @@
"tinycon" : "0.0.1", "tinycon" : "0.0.1",
"underscore" : "1.3.1", "underscore" : "1.3.1",
"unorm" : "1.0.0", "unorm" : "1.0.0",
"languages4translatewiki" : "0.1.3" "languages4translatewiki" : "0.1.3",
"semver" : "1.1.3"
}, },
"bin": { "etherpad-lite": "./node/server.js" }, "bin": { "etherpad-lite": "./node/server.js" },
"devDependencies": { "devDependencies": {