slouch/scripts/request-wrapper.js
Geoff Cox 54a60e7a21 feat(auth): cookie authentication in node (#27)
* 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
2017-09-23 04:49:07 -07:00

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;