mirror of
https://github.com/donl/slouch.git
synced 2026-05-25 22:07:24 -06:00
* feat(cookie): request-wrapper * test(request-wrapper): 100% coverage * test(browser): 100% coverage * refactor(request-class): rename to enhanced-request * feat(get-session) * feat(enhanced-request): full response * refactor(enhanced-request): enhanced opts * feat(example): authentication
34 lines
1,016 B
JavaScript
34 lines
1,016 B
JavaScript
'use strict';
|
|
|
|
var request = require('request');
|
|
|
|
// A construct used to associate a cookie with a request. We include the cookie at this layer so
|
|
// that we have a single place to modify later when we migrate to using fetch instead of request.
|
|
//
|
|
// TODO: when EnhancedRequest is used in quelle then we should be able to move the logic from
|
|
// RequestWrapper into EnhancedRequest and remove RequestWrapper.
|
|
var RequestWrapper = function () {};
|
|
|
|
RequestWrapper.prototype._setCookieHeader = function (opts) {
|
|
if ((!opts.headers || !opts.headers.cookie) && this._cookie) {
|
|
if (!opts.headers) {
|
|
opts.headers = {};
|
|
}
|
|
opts.headers.cookie = this._cookie;
|
|
}
|
|
return opts;
|
|
};
|
|
|
|
RequestWrapper.prototype.requestFactory = function () {
|
|
var self = this;
|
|
return function (opts) {
|
|
arguments[0] = self._setCookieHeader(opts);
|
|
return request.apply(request, arguments);
|
|
};
|
|
};
|
|
|
|
RequestWrapper.prototype.setCookie = function (cookie) {
|
|
this._cookie = cookie;
|
|
};
|
|
|
|
module.exports = RequestWrapper;
|