Last active
December 11, 2015 15:46
-
-
Save StephanHoyer/30ae9cbe7c0d0117ed7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var omit = require('lodash/object/omit'); | |
var extend = require('lodash/object/extend'); | |
var m = require('mithril'); | |
function apiUrl(type) { | |
return '/api/v1/' + type; | |
} | |
var defaultOmitAttrs = ['onChange', 'omitOnSave', 'type', '_data']; | |
function xhrConfig(xhr) { | |
var session = require('client/utils/session'); | |
xhr.setRequestHeader('sid', session.sid); | |
} | |
function save(instance) { | |
var data = instance; | |
if (!instance.type) { | |
throw new Error('no type provided to save model'); | |
} | |
if (instance.omitOnSave) { | |
data = omit(data, instance.omitOnSave); | |
} | |
data = omit(data, defaultOmitAttrs); | |
return m.request({ | |
method: instance.id ? 'PUT' : 'POST', | |
url: apiUrl(instance.type) + (instance.id ? '/' + instance.id : ''), | |
data: data, | |
config: xhrConfig | |
}).then(function(result) { | |
return extend(instance, result); | |
}); | |
} | |
function load(type, id, model, options) { | |
if (!type) { | |
throw new Error('no type provided to load model'); | |
} | |
if (!id) { | |
throw new Error('no id provided to load model'); | |
} | |
return m.request({ | |
method: 'GET', | |
url: apiUrl(type + '/' + id), | |
config: xhrConfig, | |
data: options | |
}).then(model); | |
} | |
function remove(instance) { | |
if (!instance) { | |
throw new Error('no instance given to remove'); | |
} | |
if (!instance.type) { | |
throw new Error('no type provided to remove instance'); | |
} | |
if (!instance.id) { | |
throw new Error('no id provided to remove instance'); | |
} | |
return m.request({ | |
method: 'DELETE', | |
config: xhrConfig, | |
url: apiUrl(instance.type + '/' + instance.id) | |
}); | |
} | |
function loadWhere(type, query, model) { | |
if (!type) { | |
throw new Error('no type provided to load model'); | |
} | |
query = query || {}; | |
return m.request({ | |
method: 'GET', | |
url: apiUrl(type), | |
config: xhrConfig, | |
data: query | |
}).then(function(data) { | |
if (!model) { | |
return data; | |
} | |
return data.map(model); | |
}); | |
} | |
function request(options) { | |
options.url = apiUrl(options.url); | |
return m.request(options); | |
} | |
module.exports = { | |
save: save, | |
load: load, | |
loadWhere: loadWhere, | |
remove: remove, | |
request: request | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var debug = require('debug')('store:server'); | |
var m = require('mithril'); | |
var resources = require('server/rest/resources'); | |
var request = require('supertest'); | |
function noop(){} | |
function load(type, id, model, options) { | |
debug('load', type, id, options); | |
if (!resources[type]) { | |
throw Error('Resource with type "' + type + '" does not exist'); | |
} | |
return new Promise(function(resolve) { | |
request(resources[type]) | |
.get('/' + id) | |
.query(options) | |
.expect(function(res) { | |
resolve(model(res.body)); | |
}) | |
.end(noop); | |
}); | |
} | |
function identity(thing) { | |
return thing; | |
} | |
function loadWhere(type, where, model, options) { | |
if (typeof model !== 'function') { | |
options = model; | |
model = identity; | |
} | |
debug('loadWhere', type, where, options); | |
return new Promise(function(resolve) { | |
request(resources[type]) | |
.get('/') | |
.query(options) | |
.query({where: where}) | |
.expect(function(res) { | |
resolve(res.body.map(model)); | |
}) | |
.end(noop); | |
}); | |
} | |
var store = { | |
load: load, | |
loadWhere: loadWhere | |
}; | |
// these two functions are only for testing purposes. NEVER use them in normal code! | |
store._mock = function _mock(fnName, cb, result, err) { | |
var origFn = store[fnName]; | |
// cb is optional argument | |
// if left out, cb parameter is result | |
if (typeof cb !== 'function') { | |
err = result; | |
result = cb; | |
cb = null; | |
} | |
store[fnName] = function() { | |
store[fnName] = origFn; | |
if (cb) { | |
var cbResult = cb.apply(null, arguments); | |
result = result || cbResult; | |
} | |
var deferred = m.deferred(); | |
if (err) { | |
deferred.reject(err); | |
} else { | |
deferred.resolve(result); | |
} | |
return deferred.promise; | |
}; | |
}; | |
store._resetAll = function _resetAll() { | |
store.save = save; | |
store.load = load; | |
store.remove = remove; | |
store.loadWhere = loadWhere; | |
}; | |
module.exports = store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment