jonsamwell / angular-http-batcher

Enables HTTP batch request with AngularJS
MIT License
96 stars 28 forks source link

Allow custom send function #38

Open timt-unity3d opened 7 years ago

timt-unity3d commented 7 years ago

I'm overriding the send function to use oboe to allow streaming processing of batches as they are returned from the server. This allows me to override the base send function and decorate the standard nodeJsMultiFetchAdapter like so:

angular
  .module('shared.adapter', ['jcs.angular-http-batch'])
  .decorator('nodeJsMultiFetchAdapter', function($delegate) {

    $delegate.send = function() {
      var self = this,
        adapter = self.getAdapter(),
        httpBatchConfig = adapter.buildRequest(self.requests, self.config);

      self.sendCallback();
      httpBatchConfig.withCredentials = true;
      oboe(httpBatchConfig)
        .node('!.*', function(node, path) {

         if (self.requests[path]) {
            self.requests[path].callback(
              node.statusCode,
              node.body,
              convertHeadersToString(node.headers),
              node.statusText
            );
          }
        });
    };

    return $delegate;
  });