refactor(rest) sendResponse -> callback

This commit is contained in:
coderaiser 2014-08-13 08:54:50 -04:00
parent 275beb68a6
commit 1159d1f4c8

View file

@ -27,12 +27,6 @@
markdown = main.srvrequire('rest/markdown'),
JSONDIR = main.JSONDIR,
OK = 200,
sendError = main.sendError,
sendResponse= main.sendResponse,
Header = main.generateHeaders({
name:'api.json'
}),
DIR = './',
@ -40,10 +34,7 @@
pack = require(DIR + 'pack'),
mellow = require(DIR + 'mellow'),
format = require(DIR + 'format'),
isWin32 = process.platform === 'win32',
NOT_LOG = true;
isWin32 = process.platform === 'win32';
/**
@ -68,8 +59,24 @@
if (ret) {
params.name = Util.rmStrOnce(name, apiURL) || '/';
sendData(params, function(error) {
sendError(error, params);
sendData(params, function(error, options, data) {
params.gzip = !error;
if (!data) {
data = options;
options = {};
}
if (options.name)
params.name = options.name;
if (options.query)
params.query = options.query;
if (error)
main.sendError(error, params);
else
main.sendResponse(params, data, options.notLog);
});
}
}
@ -79,19 +86,6 @@
return ret;
};
/**
* send data
*
*/
function send(params) {
var res = params.response,
data = params.data,
str = Util.stringifyJSON(data);
res.writeHead(OK, Header);
res.end(str);
}
/**
* getting data on method and command
*
@ -110,10 +104,7 @@
onFS(params, callback);
else if (isMD)
markdown.operate(p.name, p.request, function(error, data) {
if (error)
callback(error);
else
sendResponse(p, data, NOT_LOG);
callback(error, {notLog: true}, data);
});
else {
if (p.name[0] === '/')
@ -153,7 +144,9 @@
switch (p.request.method) {
case 'GET':
onFSGet(query, path, function(error, data) {
var str,
var str,
options = {},
isFile = error && error.code === 'ENOTDIR',
isStr = Util.isString(data),
params = {
gzip: true,
@ -162,34 +155,28 @@
response: p.response,
};
if (error) {
if (error.code === 'ENOTDIR')
main.sendFile(params);
else
callback(error);
if (isFile) {
main.sendFile(params);
} else {
data.path = format.addSlashToEnd(p.name);
params.name += '.json';
params.query = query;
if (!error) {
data.path = format.addSlashToEnd(p.name);
options.name += '.json';
options.query = query;
if (isStr)
str = data;
else
str = Util.stringifyJSON(data);
}
if (isStr)
str = data;
else
str = Util.stringifyJSON(data);
sendResponse(params, str, NOT_LOG);
callback(error, options, str);
}
});
break;
case 'PUT':
onFSPut(query, path, p.request, function(error, msg) {
if (error)
callback(error);
else
sendResponse(params, msg);
});
onFSPut(query, path, p.request, callback);
break;
case 'DELETE':
@ -202,18 +189,18 @@
files = Util.parseJSON(body);
onDelete(query, path, files, function(error) {
var names;
var names, msg;
if (error) {
callback(error);
} else {
if (!error) {
if (files && files.length)
names = files;
else
names = p.name;
sendMsg(params, 'delete', names);
msg = formatMsg('delete', names);
}
callback(error, msg);
});
}
});
@ -237,10 +224,11 @@
switch(cmd) {
case '':
p.data = {
p.data = Util.stringifyJSON({
info: 'Cloud Commander API v1'
};
send(p);
});
callback(null, {name: 'api.json'}, p.data);
break;
case 'config':
@ -282,13 +270,9 @@
switch(cmd) {
case 'auth':
main.auth(p.body, function(error, token) {
if (error)
callback(error);
else
send({
response: p.response,
data: token
});
callback(error, Util.stringifyJSON({
data: token
}));
});
break;
@ -314,11 +298,10 @@
data = files;
copyFiles(files, flop.move, function(error) {
if (error)
callback(error);
else
sendMsg(params, 'move', data);
});
var msg = formatMsg('move', data);
callback(error, msg);
});
}
break;
@ -340,10 +323,9 @@
files.namesAll = Util.slice(files.names);
copyFiles(files, flop.copy, function(error) {
if (error)
callback(error);
else
sendMsg(params, 'copy', files.namesAll);
var msg = formatMsg('copy', files.namesAll);
callback(error, msg);
});
}
break;
@ -360,12 +342,10 @@
to = from + '.zip';
pack.gzip(from, to, function(error) {
var name = path.basename(files.from);
var name = path.basename(files.from),
msg = formatMsg('zip', name);
if (error)
callback(error);
else
sendMsg(params, 'zip', name);
callback(error, msg);
});
}
break;
@ -382,12 +362,10 @@
to = Util.rmStrOnce(files.from, ['.zip', '.gzip']);
pack.gunzip(from, to, function(error) {
var name = path.basename(files.from);
var name = path.basename(files.from),
data = formatMsg('unzip', name);
if (error)
callback(error);
else
sendMsg(params, 'unzip', name);
callback(error, data);
});
}
@ -410,16 +388,14 @@
json = Util.stringifyJSON(config) + '\n';
fs.writeFile(JSONDIR + 'config.json', json, function(error) {
if (error)
callback(error);
else
sendMsg(params, 'config', name);
data = formatMsg('config', name);
callback(error, data);
});
break;
default:
send(params);
callback();
break;
}
}
@ -477,7 +453,7 @@
return error;
}
function sendMsg(sendParam, msgParam, dataParam, status) {
function formatMsg(msgParam, dataParam, status) {
var msg, data,
isObj = Util.isObject(dataParam);
@ -488,7 +464,7 @@
msg = CloudFunc.formatMsg(msgParam, data, status);
sendResponse(sendParam, msg);
return msg;
}
})();