UNPKG

@mkeen/rxcouch

Version:

Real Time RxJs Based CouchDB Client

180 lines 8.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CouchDBSession = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var rxhttp_1 = require("@mkeen/rxhttp"); var types_1 = require("./types"); var CouchDBSession = /** @class */ (function () { function CouchDBSession(authorizationBehavior, sessionUrl, credentials) { var _this = this; if (sessionUrl === void 0) { sessionUrl = ''; } this.authorizationBehavior = authorizationBehavior; this.sessionUrl = sessionUrl; this.credentials = credentials; this.authenticated = new rxjs_1.BehaviorSubject(false); this.loginAttemptMade = new rxjs_1.BehaviorSubject(false); this.userSession = new rxjs_1.BehaviorSubject(null); this.cookie = new rxjs_1.BehaviorSubject(''); this.context = new rxjs_1.BehaviorSubject(null); this.saveCookie = function (access_token) { return function (httpResponse) { var response = httpResponse.response, headers = httpResponse.headers; if (response.ok) { if (access_token) { _this.cookie.next(access_token); } else if (typeof process === 'object') { var cookie = headers.get('set-cookie'); if (cookie) { _this.cookie.next(cookie); } } } }; }; this.extractResponse = function (httpResponse) { return httpResponse.response; }; if (this.credentials) { this.credentials.subscribe(function (couchDbCreds) { _this.authenticate(couchDbCreds).pipe(operators_1.take(1)).subscribe(function (_) { }); }); } } CouchDBSession.prototype.authenticate = function (providedCredentials) { var _this = this; return rxjs_1.Observable.create(function (observer) { if ([types_1.AuthorizationBehavior.cookie, types_1.AuthorizationBehavior.jwt].includes(_this.authorizationBehavior)) { if (providedCredentials) { _this.lastCredentials = providedCredentials; _this.attemptNewAuthentication(providedCredentials).pipe(operators_1.take(1)).subscribe(function (authResponse) { if (!_this.loginAttemptMade.value) { _this.loginAttemptMade.next(true); } if (_this.authenticated.value !== true) { _this.authenticated.next(true); } _this.context.next(authResponse); observer.next(true); }, function (_error) { if (!_this.loginAttemptMade.value) { _this.loginAttemptMade.next(true); } _this.authenticated.next(false); console.warn(_error); _this.context.next(null); observer.error(false); observer.complete(); }); } else { if (_this.loginAttemptMade.value === false) { _this.get().pipe(operators_1.take(1)).subscribe(function (session) { var ok = session.ok, userCtx = session.userCtx; var authenticated = !!userCtx.name; if (_this.authenticated.value !== authenticated) { _this.authenticated.next(authenticated); if (ok) { _this.context.next(session.userCtx); observer.next(true); } else { _this.context.next(null); observer.error(false); } } else { _this.context.next(null); observer.error(false); } }); } else { _this.context.next(null); observer.error(false); } } } }); }; CouchDBSession.prototype.reauthenticate = function () { return this.authenticate(this.lastCredentials); }; CouchDBSession.prototype.get = function () { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.httpRequest(_this.sessionUrl, rxhttp_1.FetchBehavior.simpleWithHeaders).fetch().pipe(operators_1.tap(_this.saveCookie), operators_1.map(_this.extractResponse)).subscribe(function (response) { if (!_this.loginAttemptMade.value) { _this.loginAttemptMade.next(true); } if (response.ok && response.info.authenticated) { _this.context.next(response.userCtx); if (!_this.authenticated.value) { _this.authenticated.next(true); } } else { _this.context.next(null); if (!!_this.authenticated.value) { _this.authenticated.next(false); } } observer.next(response); }, function (err) { observer.error(err); }); }); }; CouchDBSession.prototype.attemptNewAuthentication = function (credentials) { var _this = this; return this.httpRequest(this.sessionUrl, rxhttp_1.FetchBehavior.simpleWithHeaders, 'POST', JSON.stringify(!credentials.access_token ? { name: credentials.name, password: credentials.password } : undefined), credentials.access_token ? { Authorization: "Bearer " + credentials.access_token } : undefined).fetch().pipe(operators_1.tap(this.saveCookie(credentials.access_token)), operators_1.map(this.extractResponse), operators_1.tap(function (_response) { _this.loginAttemptMade.next(true); })); }; CouchDBSession.prototype.httpRequest = function (url, behavior, method, body, headers) { var _a; if (behavior === void 0) { behavior = rxhttp_1.FetchBehavior.simpleWithHeaders; } if (method === void 0) { method = 'GET'; } if (body === void 0) { body = undefined; } if (headers === void 0) { headers = undefined; } return new rxhttp_1.HttpRequest(url, this.httpRequestOptions((_a = this.cookie) === null || _a === void 0 ? void 0 : _a.value, method, body, headers), behavior); }; CouchDBSession.prototype.httpRequestOptions = function (cookie, method, body, headers) { var httpOptions = { method: method }; if (body) { httpOptions.body = body; } if (headers && headers.length) { httpOptions.headers = headers; } if (cookie !== null) { if (cookie.length && typeof process === 'object') { // Todo: Type hint and length check really necessary? httpOptions['headers'] = { 'Cookie': this.cookieForRequestHeader(cookie) // Todo: Why is type hint needed when inside the null check? }; } } return httpOptions; }; CouchDBSession.prototype.cookieForRequestHeader = function (cookie) { return cookie.split(';')[0].trim(); }; CouchDBSession.prototype.destroy = function () { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.httpRequest(_this.sessionUrl, rxhttp_1.FetchBehavior.simple, 'delete').fetch().subscribe(function (response) { if (response.ok) { _this.authenticated.next(false); _this.cookie.next(''); } observer.next(response); }); }); }; return CouchDBSession; }()); exports.CouchDBSession = CouchDBSession; //# sourceMappingURL=couchdbsession.js.map