2014-09-20 30 views
0

我想添加一些添加一些額外的邏輯(日誌記錄,跟蹤的東西)到SuperAgent的主要功能:https://github.com/visionmedia/superagent/blob/master/lib/client.js#L444延伸,以與自己的邏輯/旁路構造函數庫

所以我需要延長的SuperAgent,並希望提供相同的API,通過所有功能。我試圖通過不同的機制解決它:Object.create,原型,深拷貝,但我沒有得到它的工作。

我不想操縱superagent的源代碼,只需要它幷包裝它,添加我額外的邏輯並調用,通過原函數。我認爲這是一種面向方面。

//編輯 所以什麼都不用爲我工作是爲了繞過請求構造:

function Request(method, url) { 
    var self = this; 
    Emitter.call(this); 
    this._query = this._query || []; 
    this.method = method; 
    this.url = url; 
    this.header = {}; 
    this._header = {}; 
    this.on('end', function(){ 
    try { 
     var res = new Response(self); 
     if ('HEAD' == method) res.text = null; 
     self.callback(null, res); 
    } catch(e) { 
     var err = new Error('Parser is unable to parse the response'); 
     err.parse = true; 
     err.original = e; 
     self.callback(err); 
    } 
    }); 
} 
+0

請出示你已經嘗試了(整體)的代碼,不(只)原構造函數來發送。同時向我們展示*什麼*多餘的邏輯你想添加一個*當*應該成爲活動,最好與你包裝的構造函數和期望的輸出的示例調用。 – Bergi 2014-09-20 14:53:01

+0

您可以在我提供的鏈接中看到整個代碼(整個文件),突出顯示構造函數的行。 – timaschew 2014-09-20 19:20:45

+0

但這是原始代碼,不是嗎?我的意思是你的方法中的代碼不起作用:「* Object.create,prototype,deep copy *」 - 你做了什麼?他們怎麼沒有工作?沒有告訴我們你想要注入哪種邏輯,這甚至不是一個真正的問題。 – Bergi 2014-09-20 19:47:23

回答

0

我得到了它幾乎與此代碼的工作:

var superagent = require('superagent'); 
var uuid = require('uuid'); 

var map = {}; 

var init = function() { 

    var supderdebug = function(method, url) { 
     console.log("pass through: root"); 
     return superagent.apply(this, arguments); 
    } 

    var methods = ['get', 'head', 'del', 'patch','post', 'put']; 
    methods.forEach(function(method) { 
     var origin = superagent[method]; 
     supderdebug[method] = function(url) { 
      console.log("pass through: "+method+"('"+url+"')"); 
      var request = origin.apply(this, arguments); 
      var id = uuid(); 
      map[id] = request; 
      return request; 
     } 

    }); 

    _end = superagent.Request.prototype.end; 
    superagent.Request.prototype.end = function(fn) { 
     console.log("pass through: end"); 
     return _end.apply(this, arguments); 
    } 

    _callback = superagent.Request.prototype.callback; 
    superagent.Request.prototype.callback = function(err, res) { 
     console.log("pass through: callback"); 
     if (err) { 
      console.log(err); 
     } 
     var response = _callback.apply(this, arguments); 
     return response; 
    } 

    return supderdebug; 
} 

module.exports.init = init 

用法:

var sd = require("supderdebug").init(); 

然後,當我需要它時,我得到與superagent提供的相同的API:var superagent = require("superagent")

但我不能這樣做superagent.Request和sa.Response。當我不工作:

當需要我的兩個庫和SuperAgent的,則:

superagent.Request.prototype.constructor = function(method, url) 
    // my hook 
} 

而且,還有另一個副作用,如果沒有這種副作用的解決方案這將是很好超越性不再是原點,因爲我重寫了超絕性的功能。

0

您需要在現有的功能

superagent.Request.prototype.end = function(end) { 
    return function() { 
     console.log("before end");   
     var request = end.apply(this, arguments); 
     console.log("after end"); 
     return request; 
    }; 
}(superagent.Request.prototype.end);