2015-05-29 19 views
7

Questo issue nel repository SuperAgent menziona il metodo .use per aggiungere la logica su ogni richiesta. Ad esempio, l'aggiunta di un Authorization intestazione per JWT quando un token è disponibile:Metodo ".use" per aggiungere la logica su ciascuna richiesta di SuperAgent

superagent.use(bearer); 

function bearer (request) { 
    var token = sessionStorage.get('token'); 

    if (token) request.set('Authorization', 'Bearer ' + token); 
} 

Anche se l'ultimo commento informa che questa funzione è lavorare di nuovo, non posso farlo funzionare.

Il seguente codice di prova:

var request = require('superagent'); 

request.use(bearer); 

function bearer (request) 
{ 
    // "config" is a global var where token and other stuff resides 
    if (config.token) request.set('Authorization', 'Bearer ' + config.token); 
} 

restituisce questo errore:

request.use(bearer); 
     ^
TypeError: undefined is not a function 
+1

Per favore, spiegate i motivi per cui avete downvoted la domanda: http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes –

+0

la funzione 'request' che viene esportata da superagent non avere un metodo 'use'. tuttavia, l'istanza di 'Request' che viene restituita eseguendo' request'. Non è chiaro se questo sia un bug o possa essere risolto senza una richiesta di pull. –

+0

In altre parole, l'errore che si sta ottenendo è previsto, in base alla visualizzazione dell'origine. Il readme inoltre non mostra un esempio del tuo utilizzo, quindi (il tuo caso d'uso) potrebbe non essere un uso previsto. –

risposta

8

Il problema si è collegato a non è legata ad alcun commit, quindi tutto ciò che possiamo fare è speculare sulla necessità o non quella caratteristica è stata implementata e quindi rimossa, o mai implementata in primo luogo.

Se read through the src, vedrete che use è sempre e solo definito sul prototipo del costruttore Request, che significa che può sempre e solo essere utilizzato dopo aver iniziato la costruzione di una richiesta, as shown in the readme.

In altre parole, il problema sembra parlare di una funzionalità che è stata rimossa o non è mai esistita. Dovresti invece usare la sintassi menzionata nel readme.

var request = require('superagent'); 

request 
.get('/some-url') 
.use(bearer) // affects **only** this request 
.end(function(err, res){ 
    // Do something 
}); 

function bearer (request){ 
    // "config" is a global var where token and other stuff resides 
    if (config.token) { 
     request.set('Authorization', 'Bearer ' + config.token); 
    } 
} 

Si potrebbe ovviamente creare il proprio wrapper in modo da non doverlo fare per ogni richiesta.

var superagent = require('superagent'); 

function request(method, url) { 
    // callback 
    if ('function' == typeof url) { 
     return new superagent.Request('GET', method).end(url).use(bearer); 
    } 

    // url first 
    if (1 == arguments.length) { 
     return new superagent.Request('GET', method).use(bearer); 
    } 

    return new superagent.Request(method, url).use(bearer); 
} 
// re-implement the .get and .post helpers if you feel they're important.. 

function bearer (request){ 
    // "config" is a global var where token and other stuff resides 
    if (config.token) { 
     request.set('Authorization', 'Bearer ' + config.token); 
    } 
} 

request('GET', '/some-url') 
.end(function(err, res){ 
    // Do something 
}); 
1

recenti pacchetto superagent-use è stato rilasciato per facilitare l'impostazione uso globale per le richieste superagent.