Skip to content

Instantly share code, notes, and snippets.

@darkyen
Created December 23, 2014 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkyen/830656185beffd9566b4 to your computer and use it in GitHub Desktop.
Save darkyen/830656185beffd9566b4 to your computer and use it in GitHub Desktop.
NetworkService for Hipster Delorean
var reqwest = require('reqwest');
var _ = require('lodash');
var Promise = require('bluebird');
class Network{
constructor(defs){
this._httpMethods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'];
this._defs = {
'GET': {},
'POST': {},
'OPTIONS': {},
'DELETE': {},
'PUT': {},
'ALL': defs || {}
};
this._request = reqwest;
this._headers = {
'GET': {},
'POST': {},
'PUT': {},
'DELETE': {},
'ALL': {}
};
this._transforms = {
'GET' : [],
'POST' : [],
'PUT' : [],
'DELETE': [],
'ALL' : []
};
this._filters = {
'GET' : [],
'POST' : [],
'PUT' : [],
'DELETE': [],
'ALL' : []
};
}
setHeaders(method, headers){
if( !headers ){
headers = method;
method = 'ALL';
}
for(var key in headers){
this._headers[method][key] = headers[key];
}
}
setOption(key, value, method){
method = method || 'ALL';
for(var key in headers){
this._defs[method][key] = value;
}
}
createRequest(options){
// Makes a fully qualified request.. adds callback etc etc blah blah
options['method'] = options['method'] || 'GET';
if( this._httpMethods.indexOf(options['method']) === -1 ){
throw new Error('Unknown HTTP method');
}
options['headers'] = options['headers'] || {};
// Perform various transformations
var transforms = _.flatten([ this._transforms['ALL'], this._transforms[options['method']] ]);
options = transforms.reduce((options, transform) => {
return transform(options);
}, options);
// Add options... this can be used to encorporate
// Default headers aswell
options = _.assign(options, this._defs['ALL']);
options = _.assign(options, this._defs[options['method']]);
// No transforms for now.
options['headers'] = _.assign(options['headers'], this._headers['ALL']);
options['headers'] = _.assign(options['headers'], this._headers[options['method']]);
// Just simple
var request = this._request;
var callbacks = _.flatten([this._filters['ALL'], this._filters['method']], true);
// Use bluebird promises
// Promises of reqwest are a terrible qwest.
var r = new Promise((resolve, reject) => {
options = _.assign(options, {
success: resolve,
error: reject
});
request(options);
});
// console.log(callbacks);
// Just Incredible
return callbacks.reduce((prev, curr) => {
return prev.then.apply(prev, curr);
}, r);
}
registerTransform(method, transform){
if( !transform ){
transform = method;
method = null;
}
method = method || 'ALL';
console.log(this, method, transform);
this._transforms[method].push(transform);
}
registerFilter(method, filter){
if( !filter ){
filter = method;
method = null;
}
method = method || 'ALL';
if( ! Array.isArray( filter )){
filter = [filter];
}
this._filters[method].push(filter);
}
get( url, data ){
return this.createRequest({
method : 'GET',
url : url,
data : data
});
}
post(url, data){
return this.createRequest({
url : url,
data : data,
method : 'POST',
});
}
put(url, data){
return this.createRequest({
url : url,
data : data,
method : 'PUT'
});
}
delete(url, data){
return this.createRequest({
method : 'DELETE',
url : url,
data : data
});
}
}
export default Network;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment