Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. class Network{
  2.  
  3. constructor(defs){
  4. this._httpMethods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'];
  5.  
  6. this._defs = {
  7. 'GET': {},
  8. 'POST': {},
  9. 'OPTIONS': {},
  10. 'DELETE': {},
  11. 'PUT': {},
  12. 'ALL': defs || {}
  13. };
  14.  
  15. this._request = reqwest;
  16. this._headers = {
  17. 'GET': {},
  18. 'POST': {},
  19. 'PUT': {},
  20. 'DELETE': {},
  21. 'ALL': {}
  22. };
  23.  
  24. this._transforms = {
  25. 'GET' : [],
  26. 'POST' : [],
  27. 'PUT' : [],
  28. 'DELETE': [],
  29. 'ALL' : []
  30. };
  31.  
  32. this._filters = {
  33. 'GET' : [],
  34. 'POST' : [],
  35. 'PUT' : [],
  36. 'DELETE': [],
  37. 'ALL' : []
  38. };
  39. }
  40.  
  41. setHeader(method, headers){
  42. method = method || 'ALL';
  43. for(var key in headers){
  44. this._headers[method][key] = headers[key];
  45. }
  46. }
  47.  
  48. setOption(key, value, method){
  49. method = method || 'ALL';
  50.  
  51. for(var key in headers){
  52. this._defs[method][key] = value;
  53. }
  54.  
  55. }
  56.  
  57. createRequest(options){
  58.  
  59. // Makes a fully qualified request.. adds callback etc etc blah blah
  60. options['method'] = options['method'] || 'GET';
  61.  
  62. if( this._httpMethods.indexOf(options['method']) ){
  63. throw new Error('Unknown HTTP method');
  64. }
  65.  
  66. options['headers'] = options['headers'] || {};
  67.  
  68. // Perform various transformations
  69. transforms = _.flatten(this._transforms['ALL'], this._transforms[method] );
  70. options = this._transforms[method].reduce((options, transform) => {
  71. return transform(options);
  72. }, options);
  73.  
  74.  
  75. // Add options... this can be used to encorporate
  76. // Default headers aswell
  77. _.assign(options, this._defs['ALL']);
  78. _.assign(options, this._defs[options['method']]);
  79.  
  80. // No transforms for now.
  81. options['headers'] = _.assign(options['headers'], this._headers['ALL']);
  82. options['headers'] = _.assign(options['headers'], this._headers[options['method']]);
  83.  
  84. // Just simple
  85. var request = this._request;
  86. var callbacks = _.flatten(this._filters['ALL'], this._filters['method']);
  87. var r = request(options);
  88.  
  89. // Just Incredible
  90. return callbacks.reduce((prev, curr) => {
  91. return prev.then.apply(prev, curr);
  92. }, r);
  93.  
  94. }
  95.  
  96. registerFilter(method, filter){
  97.  
  98. method = method || 'ALL';
  99.  
  100. if( ! Array.isArray( filter )){
  101. filter = [filter];
  102. }
  103.  
  104. this._filters[method].push(filter);
  105.  
  106. }
  107.  
  108. get( url, data ){
  109. return this.createRequest({
  110. method : 'GET',
  111. url : url,
  112. data : data
  113. });
  114. }
  115.  
  116. post(url, data){
  117. return this.createRequest({
  118. url : url,
  119. data : data,
  120. method : 'POST',
  121. });
  122. }
  123.  
  124. put(url, data){
  125. return this.createRequest({
  126. url : url,
  127. data : data,
  128. method : 'POST'
  129. });
  130. }
  131.  
  132. delete(url, data){
  133. return this.createRequest({
  134. url : url,
  135. data : data
  136. });
  137. }
  138. }
  139.  
  140.  
  141. export default Network;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement