mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-13 12:37:17 +08:00
Added dpd.js loader proxy
This commit is contained in:
11
clib/dpd-loader.js
Normal file
11
clib/dpd-loader.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var url = /*rootUrl*/ + '/dpd.js';
|
||||
|
||||
if (!document.readystate || document.readystate === "complete") {
|
||||
var head = document.getElementsByTagName('head')[0],
|
||||
script = document.createElement('script');
|
||||
|
||||
script.src = url;
|
||||
head.appendChild(script);
|
||||
} else {
|
||||
document.write('<script type="text/javascript" src="' + url + '"></script>')
|
||||
}
|
||||
133
clib/dpd.js
133
clib/dpd.js
@@ -1,12 +1,25 @@
|
||||
(function ($) {
|
||||
if(!$) throw 'dpd.js depends on jQuery.ajax - you must include it before loading dpd.js';
|
||||
if(!$) throw Error('dpd.js depends on jQuery.ajax - you must include it before loading dpd.js');
|
||||
|
||||
// global namespace
|
||||
window.dpd = {};
|
||||
|
||||
var r
|
||||
, resources = /*resources*/
|
||||
, contentType = 'application/json';
|
||||
, contentType = 'application/json'
|
||||
, rootUrl = /*rootUrl*/;
|
||||
|
||||
function errorCallback(fn) {
|
||||
return function (err) {
|
||||
fn && fn(null, JSON.parse(err.responseText));
|
||||
}
|
||||
}
|
||||
|
||||
function successCallback(fn) {
|
||||
return function (res) {
|
||||
fn && fn(res);
|
||||
}
|
||||
}
|
||||
|
||||
while(r = resources.shift()){
|
||||
if(~r.type.indexOf('Collection')) {
|
||||
@@ -22,76 +35,106 @@
|
||||
var q = (query && ('?q=' + JSON.stringify(query))) || '';
|
||||
|
||||
return $.ajax({
|
||||
url: r.path + q,
|
||||
url: rootUrl + r.path + q,
|
||||
type: 'GET',
|
||||
contentType: contentType,
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
fn && fn(res || []);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, JSON.parse(err));
|
||||
}
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r),
|
||||
first: (function (r) {
|
||||
getOne: (function (r) {
|
||||
return function (query, fn) {
|
||||
if(!fn) {
|
||||
fn = query;
|
||||
query = undefined;
|
||||
}
|
||||
|
||||
var q = (query && ('?q=' + JSON.stringify(query))) || '';
|
||||
var q = '';
|
||||
|
||||
if (typeof query === 'string') {
|
||||
q = '/' + query; //Id
|
||||
} else if (query) {
|
||||
q = '?q=' + JSON.stringify(query);
|
||||
}
|
||||
|
||||
return $.ajax({
|
||||
url: r.path + q,
|
||||
url: rootUrl + r.path + q,
|
||||
type: 'GET',
|
||||
contentType: contentType,
|
||||
success: function (res) {
|
||||
if(res && res.length) {
|
||||
fn && fn(res[0]);
|
||||
} else {
|
||||
} else if (typeof res.length !== 'undefined') {
|
||||
fn && fn(null, {message: 'not found', status: 404});
|
||||
} else {
|
||||
fn && fn(res);
|
||||
}
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(JSON.parse(err));
|
||||
}
|
||||
error: errorCallback(res)
|
||||
});
|
||||
}
|
||||
})(r),
|
||||
save: (function (r) {
|
||||
return function (obj, fn) {
|
||||
if (typeof obj === 'string') { throw Error("save() does not take an id. Did you mean to use put()?"); }
|
||||
return $.ajax({
|
||||
url: r.path + (obj._id ? ('/' + obj._id) : ''),
|
||||
url: rootUrl + r.path + (obj._id ? ('/' + obj._id) : ''),
|
||||
type: 'POST',
|
||||
contentType: contentType,
|
||||
data: JSON.stringify(obj),
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, err);
|
||||
}
|
||||
success: successCallback(fn),
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r),
|
||||
post: (function(r) {
|
||||
return function (obj, fn) {
|
||||
if (obj && obj._id) { throw Error("Cannot post() an object with an _id. Did you mean to use save() or put()?"); }
|
||||
return $.ajax({
|
||||
url: rootUrl + r.path,
|
||||
type: 'POST',
|
||||
contentType: contentType,
|
||||
data: JSON.stringify(obj),
|
||||
success: successCallback(fn),
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r),
|
||||
put: (function(r) {
|
||||
return function (id, obj, fn) {
|
||||
if (typeof id !== 'string') {
|
||||
obj = arguments[0]; //reorder parameters
|
||||
fn = arguments[1];
|
||||
id = obj && obj._id;
|
||||
}
|
||||
if (!id) { throw Error("id must be provided!"); }
|
||||
return $.ajax({
|
||||
url: rootUrl + r.path + '/' + id,
|
||||
type: 'PUT',
|
||||
contentType: contentType,
|
||||
data: JSON.stringify(obj),
|
||||
success: successCallback(fn),
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r),
|
||||
del: (function (r) {
|
||||
return function (query, fn) {
|
||||
if(!fn) fn = query;
|
||||
return function (id, fn) {
|
||||
if (typeof id !== 'string') {throw Error("id must be provided!");}
|
||||
|
||||
var q = query && ('?q=' + JSON.stringify(query));
|
||||
var q = '/' + id;
|
||||
|
||||
return $.ajax({
|
||||
url: r.path + q,
|
||||
url: rootUrl + r.path + q,
|
||||
type: 'DELETE',
|
||||
contentType: contentType,
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
success: function(res) {
|
||||
fn && fn(true);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, err);
|
||||
}
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r)
|
||||
@@ -102,16 +145,12 @@
|
||||
resource.login = (function (r) {
|
||||
return function (credentials, fn) {
|
||||
return $.ajax({
|
||||
url: r.path + '/login',
|
||||
url: rootUrl + r.path + '/login',
|
||||
type: 'POST',
|
||||
contentType: contentType,
|
||||
data: JSON.stringify(credentials),
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, err);
|
||||
}
|
||||
success: successCallback(fn),
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r);
|
||||
@@ -119,15 +158,13 @@
|
||||
resource.logout = (function (r) {
|
||||
return function (fn) {
|
||||
return $.ajax({
|
||||
url: r.path + '/logout',
|
||||
url: rootUrl + r.path + '/logout',
|
||||
type: 'POST',
|
||||
contentType: contentType,
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
success: function(res) {
|
||||
fn && fn(true);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, err);
|
||||
}
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r);
|
||||
@@ -135,15 +172,11 @@
|
||||
resource.me = (function (r) {
|
||||
return function (fn) {
|
||||
return $.ajax({
|
||||
url: r.path + '/me',
|
||||
url: rootUrl + r.path + '/me',
|
||||
type: 'GET',
|
||||
contentType: contentType,
|
||||
success: function (res) {
|
||||
fn && fn(res);
|
||||
},
|
||||
error: function (err) {
|
||||
fn && fn(null, err);
|
||||
}
|
||||
success: successCallback(fn),
|
||||
error: errorCallback(fn)
|
||||
});
|
||||
}
|
||||
})(r);
|
||||
|
||||
42
lib/clib.js
42
lib/clib.js
@@ -6,17 +6,39 @@ var resources = require('./collections/resources')
|
||||
, fs = require('fs');
|
||||
|
||||
module.exports = function (req, res, next) {
|
||||
resources.get(function (err, resources) {
|
||||
if(err) return next(err);
|
||||
|
||||
resources = resources || [];
|
||||
|
||||
fs.readFile(__dirname + '/../clib/dpd.js', function (err, data) {
|
||||
|
||||
function loadAndSendFile(fileName, callback) {
|
||||
fs.readFile(__dirname + fileName, function (err, data) {
|
||||
var src = data.toString();
|
||||
res.header('Content-Type', 'text/javascript');
|
||||
res.send(
|
||||
src.replace('/*resources*/', JSON.stringify(resources))
|
||||
);
|
||||
|
||||
if (callback) { src = callback(src); }
|
||||
|
||||
var rootUrl = process.url.href;
|
||||
if (rootUrl.lastIndexOf('/') === rootUrl.length - 1) { rootUrl = rootUrl.slice(0, -1); }
|
||||
|
||||
src = src.replace('/*rootUrl*/', '"' + rootUrl + '"');
|
||||
res.send(src);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
if (req.param('proxy')) {
|
||||
loadAndSendFile('/../clib/dpd-loader.js', function(src) {
|
||||
res.header('Content-Disposition', 'attachment; filename=dpd.js')
|
||||
return src;
|
||||
});
|
||||
} else {
|
||||
resources.get(function (err, resources) {
|
||||
if(err) return next(err);
|
||||
|
||||
resources = resources || [];
|
||||
|
||||
loadAndSendFile('/../clib/dpd.js', function(src) {
|
||||
return src.replace('/*resources*/', JSON.stringify(resources));
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user