|
var config = { |
|
headers: {}, |
|
timeout: 30000, |
|
domain: 'http://localhost', |
|
port: 0 |
|
}; |
|
|
|
var NoCache = 1, |
|
CacheElseNetwork = 2, |
|
NetworkElseCache = 4, |
|
CacheThenNetwork = 8, |
|
CacheOnly = 16, |
|
NetworkOnly = 32; |
|
|
|
var SDK = module.exports = { |
|
/* |
|
Public API. |
|
*/ |
|
getModel: function (name, failIfMissing) { |
|
name = String(name || '').replace('connector/', ''); |
|
var result = this.models[name]; |
|
if (!result) { |
|
for (var modelName in this.models) { |
|
if (this.models.hasOwnProperty(modelName) && name === modelName.split('/').pop()) { |
|
result = this.models[modelName]; |
|
break; |
|
} |
|
} |
|
} |
|
if (!result && failIfMissing) { |
|
throw new Error('Couldn\'t find model named: ' + name); |
|
} |
|
return result; |
|
}, |
|
getAPI: function (pathOrNickname, method) { |
|
for (var i = 0; i < this.apis.length; i++) { |
|
var API = this.apis[i]; |
|
if (1 === arguments.length) { |
|
if (API.nickname === pathOrNickname) { |
|
return API; |
|
} |
|
} |
|
else { |
|
if (API.path === pathOrNickname && API.method === method) { |
|
return API; |
|
} |
|
} |
|
} |
|
return null; |
|
}, |
|
|
|
/* |
|
Constants. |
|
*/ |
|
Policy: { |
|
/** |
|
* No caches are used or saved. This is the default caching policy for models. |
|
*/ |
|
NoCache: 1, |
|
/** |
|
* If a local cache of the results is available, it will be used; otherwise, the network will be used. Results will |
|
* be cached. |
|
*/ |
|
CacheElseNetwork: 2, |
|
/** |
|
* If the network is available, it will be used for results; otherwise, the local cache will be used. Results will |
|
* be cached. |
|
*/ |
|
NetworkElseCache: 4, |
|
/** |
|
* If a local cache of the result is available, it will be used immediately, and then the network will be used. The |
|
* callback will, thus, be called twice. Results will be cached. |
|
*/ |
|
CacheThenNetwork: 8, |
|
/** |
|
* If a local cache of results is available, it will be used. |
|
*/ |
|
CacheOnly: 16, |
|
/** |
|
* If the network is available, it will be used for results. Results will be cached. |
|
*/ |
|
NetworkOnly: 32 |
|
}, |
|
|
|
/* |
|
Configuration. |
|
*/ |
|
cachePolicy: {type: NoCache}, |
|
get domain() { return config.domain; }, |
|
set domain(val) { config.domain = val; }, |
|
get port() { return config.port; }, |
|
set port(val) { config.port = val; }, |
|
get timeout() { return config.timeout; }, |
|
set timeout(val) { config.timeout = val; }, |
|
get APIKey() { return config.APIKey; }, |
|
set APIKey(val) { |
|
config.APIKey = val; |
|
config.headers.APIKey = val; |
|
}, |
|
get Authorization() { return config.Authorization; }, |
|
set Authorization(val) { |
|
config.Authorization = val; |
|
if (0 === val.indexOf('Basic ')) { |
|
config.headers.Authorization = val; |
|
} |
|
else if (':' === val.slice(-1)) { |
|
config.headers.Authorization = 'Basic ' + Ti.Utils.base64encode(val); |
|
} |
|
else { |
|
config.headers.Authorization = 'Basic ' + Ti.Utils.base64encode(val + ':'); |
|
} |
|
}, |
|
|
|
/* |
|
Utility. |
|
*/ |
|
isFunction: function isFunction(val) { |
|
return 'function' === typeof val; |
|
}, |
|
|
|
appendQuery: function appendQuery(url, query) { |
|
for (var queryName in query) { |
|
if (query.hasOwnProperty(queryName)) { |
|
url += (url.indexOf('?') === -1 ? '?' : '&') + queryName; |
|
if (query[queryName] !== '') { |
|
url += '=' + encodeURIComponent(query[queryName]); |
|
} |
|
} |
|
} |
|
return url; |
|
}, |
|
|
|
constructHTTP: function constructHTTP(method, url, onLoad, onError) { |
|
var client = Ti.Network.createHTTPClient({ |
|
onload: onLoad, |
|
onerror: onError, |
|
timeout: config.timeout |
|
}); |
|
client.open(method, url); |
|
|
|
// Add headers. |
|
for (var headerName in config.headers) { |
|
if (config.headers.hasOwnProperty(headerName)) { |
|
client.setRequestHeader(headerName, config.headers[headerName]); |
|
} |
|
} |
|
|
|
return client; |
|
}, |
|
|
|
getURL: function getURL(path) { |
|
var url = config.domain; |
|
if (config.port && config.port !== 80) { |
|
url += ':' + config.port; |
|
} |
|
url += path; |
|
return url; |
|
}, |
|
|
|
/** |
|
* Turns a url and method in to a hash that can uniquely and securely identify it. |
|
*/ |
|
getCacheHash: function getCacheHash(method, url) { |
|
var key = method + ':' + url + '\n'; |
|
// Add headers. |
|
for (var headerName in config.headers) { |
|
if (config.headers.hasOwnProperty(headerName)) { |
|
key += headerName + ':' + config.headers[headerName] + '\n'; |
|
} |
|
} |
|
return Ti.Utils.sha256(key); |
|
}, |
|
|
|
/** |
|
* Gets the cache db, ensuring it has the proper schema set up. |
|
* @returns {Window} |
|
*/ |
|
getCacheDB: function () { |
|
if (this.cacheDB) { |
|
return Ti.Database.open(this.cacheDB); |
|
} |
|
this.cacheDB = 'MyArrowAppCache'; |
|
var db = Ti.Database.open(this.cacheDB); |
|
db.execute('CREATE TABLE IF NOT EXISTS cache(hash TEXT PRIMARY KEY, until INTEGER, result BLOB);'); |
|
db.file.setRemoteBackup(false); |
|
return db; |
|
}, |
|
/** |
|
* Checks the cache to see if results have been cached for the specific request. |
|
* @param hash A string that uniquely identifies the request. |
|
* @returns The cached results, or undefined. |
|
*/ |
|
readCache: function (hash) { |
|
var db = this.getCacheDB(); |
|
db.execute('DELETE FROM cache WHERE until < ?', Date.now()); |
|
var result = db.execute('SELECT result FROM cache WHERE hash = ? LIMIT 1', hash), |
|
retVal; |
|
if (result.isValidRow()) { |
|
retVal = JSON.parse(result.fieldByName('result')); |
|
} |
|
result.close(); |
|
db.close(); |
|
return retVal; |
|
}, |
|
/** |
|
* |
|
* @param hash |
|
* @param duration |
|
* @param results |
|
*/ |
|
writeCache: function (hash, duration, results) { |
|
var db = this.getCacheDB(); |
|
db.execute('INSERT OR REPLACE INTO cache (hash, until, result) VALUES (?, ?, ?);', |
|
hash, |
|
Date.now() + duration, |
|
JSON.stringify(results)); |
|
db.close(); |
|
}, |
|
|
|
/* |
|
Definitions. |
|
*/ |
|
models: { |
|
'employee': { |
|
cachePolicy: { |
|
duration: 360000, |
|
type: NoCache |
|
}, |
|
'deleteAll': function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
}, |
|
'create': function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
}, |
|
'findAll': function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
}, |
|
'count': function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/count'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
}, |
|
'query': function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/query'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
}, |
|
'upsert': function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/upsert'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
}, |
|
'findAndModify': function method(query, body, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
body = {}; |
|
} |
|
else if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/findAndModify'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = body = callback = url = client = null; |
|
} |
|
}, |
|
'delete': function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
}, |
|
'update': function method(id, body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = body = callback = url = client = null; |
|
} |
|
}, |
|
'findByID': function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
}, |
|
'distinct': function method(field, query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/distinct/:field'); |
|
url = url.replace(':field', field); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
field = query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
'testuser': { |
|
cachePolicy: { |
|
duration: 360000, |
|
type: NoCache |
|
}, |
|
'query': function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/query'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
}, |
|
'count': function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/count'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
}, |
|
'findAll': function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
}, |
|
'deleteAll': function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
}, |
|
'create': function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
}, |
|
'upsert': function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/upsert'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
}, |
|
'findAndModify': function method(query, body, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
body = {}; |
|
} |
|
else if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/findAndModify'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = body = callback = url = client = null; |
|
} |
|
}, |
|
'update': function method(id, body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = body = callback = url = client = null; |
|
} |
|
}, |
|
'delete': function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
}, |
|
'findByID': function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
}, |
|
'distinct': function method(field, query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/distinct/:field'); |
|
url = url.replace(':field', field); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
field = query = callback = url = client = null; |
|
} |
|
} |
|
} |
|
}, |
|
apis: [ |
|
{ |
|
nickname: 'GET /api/testapi', |
|
path: '/api/testapi/:id', |
|
method: 'GET', |
|
execute: function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/testapi/:id'); |
|
url = url.replace(':id', id); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Query', |
|
path: '/api/testuser/query', |
|
method: 'GET', |
|
execute: function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/query'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Count', |
|
path: '/api/testuser/count', |
|
method: 'GET', |
|
execute: function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/count'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find All', |
|
path: '/api/testuser', |
|
method: 'GET', |
|
execute: function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Delete All', |
|
path: '/api/testuser', |
|
method: 'DELETE', |
|
execute: function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Create', |
|
path: '/api/testuser', |
|
method: 'POST', |
|
execute: function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Upsert', |
|
path: '/api/testuser/upsert', |
|
method: 'POST', |
|
execute: function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/upsert'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find and Modify', |
|
path: '/api/testuser/findAndModify', |
|
method: 'PUT', |
|
execute: function method(query, body, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
body = {}; |
|
} |
|
else if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/findAndModify'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Update', |
|
path: '/api/testuser/:id', |
|
method: 'PUT', |
|
execute: function method(id, body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Delete One', |
|
path: '/api/testuser/:id', |
|
method: 'DELETE', |
|
execute: function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find By ID', |
|
path: '/api/testuser/:id', |
|
method: 'GET', |
|
execute: function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/testuser/:id'); |
|
url = url.replace(':id', id); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Distinct', |
|
path: '/api/testuser/distinct/:field', |
|
method: 'GET', |
|
execute: function method(field, query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/testuser/distinct/:field'); |
|
url = url.replace(':field', field); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
field = query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Delete All', |
|
path: '/api/employee', |
|
method: 'DELETE', |
|
execute: function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Create', |
|
path: '/api/employee', |
|
method: 'POST', |
|
execute: function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find All', |
|
path: '/api/employee', |
|
method: 'GET', |
|
execute: function method(callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee'); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Count', |
|
path: '/api/employee/count', |
|
method: 'GET', |
|
execute: function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/count'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Query', |
|
path: '/api/employee/query', |
|
method: 'GET', |
|
execute: function method(query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/query'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Upsert', |
|
path: '/api/employee/upsert', |
|
method: 'POST', |
|
execute: function method(body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/upsert'); |
|
|
|
|
|
var client = SDK.constructHTTP('POST', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find and Modify', |
|
path: '/api/employee/findAndModify', |
|
method: 'PUT', |
|
execute: function method(query, body, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
body = {}; |
|
} |
|
else if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/findAndModify'); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
query = body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Delete One', |
|
path: '/api/employee/:id', |
|
method: 'DELETE', |
|
execute: function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Update', |
|
path: '/api/employee/:id', |
|
method: 'PUT', |
|
execute: function method(id, body, callback, options) { |
|
if (SDK.isFunction(body)) { |
|
callback = body; |
|
body = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
|
|
var client = SDK.constructHTTP('PUT', url, onLoad, onError); |
|
client.send(body); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = body = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Find By ID', |
|
path: '/api/employee/:id', |
|
method: 'GET', |
|
execute: function method(id, callback, options) { |
|
|
|
var url = SDK.getURL('/api/employee/:id'); |
|
url = url.replace(':id', id); |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
id = callback = url = client = null; |
|
} |
|
} |
|
}, |
|
{ |
|
nickname: 'Distinct', |
|
path: '/api/employee/distinct/:field', |
|
method: 'GET', |
|
execute: function method(field, query, callback, options) { |
|
if (SDK.isFunction(query)) { |
|
callback = query; |
|
query = {}; |
|
} |
|
|
|
var url = SDK.getURL('/api/employee/distinct/:field'); |
|
url = url.replace(':field', field); |
|
if (query) { |
|
url = SDK.appendQuery(url, query); |
|
} |
|
|
|
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, |
|
cacheHash = SDK.getCacheHash('GET', url); |
|
// If we're using one of the policies that can use the cache... |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { |
|
var cached = SDK.readCache(cacheHash); |
|
if (cached) { |
|
callback && callback(null, cached, null); |
|
// CacheElseNetwork or CacheOnly? Then we can return. |
|
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { |
|
clean(); |
|
return cached; |
|
} |
|
// CacheThenNetwork would carry on. |
|
} |
|
// CacheOnly will never hit the network; it wants to return, even if we don't find results. |
|
if (cachePolicy.type === SDK.Policy.CacheOnly) { |
|
callback && callback(new Error('No results have been cached yet.'), null, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
|
|
var client = SDK.constructHTTP('GET', url, onLoad, onError); |
|
client.send(); |
|
|
|
function onLoad() { |
|
if (!callback) { |
|
return; |
|
} |
|
var body = this.responseText; |
|
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { |
|
try { |
|
body = JSON.parse(body); |
|
if (body.key && body[body.key] !== undefined) { |
|
body = body[body.key]; |
|
} |
|
} |
|
catch (e) { |
|
console.error('Failed to parse the body from the server:'); |
|
console.error(body); |
|
console.error(e); |
|
} |
|
} |
|
if (this.status >= 200 && this.status <= 299) { |
|
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { |
|
SDK.writeCache(cacheHash, cachePolicy.duration, body); |
|
// TODO: When we do a findAll, cache findByID, too. |
|
// TODO: Non-GETs should bust the cache for find, right? |
|
} |
|
callback(null, body, client); |
|
} |
|
else { |
|
callback(body, null, client); |
|
} |
|
clean(); |
|
} |
|
|
|
function onError(e) { |
|
if (!callback) { |
|
return; |
|
} |
|
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { |
|
var cached = SDK.readCache(url); |
|
if (cached) { |
|
callback(null, cached, null); |
|
clean(); |
|
return; |
|
} |
|
} |
|
callback(e.error, null, client); |
|
clean(); |
|
} |
|
|
|
function clean() { |
|
field = query = callback = url = client = null; |
|
} |
|
} |
|
} |
|
] |
|
}; |
Thanks for this. What should be the domain/port be set to for development/production when testing on a physical device, or when the app is deployed to an app store, to use a published ArrowDB ?