1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | function nexus(system, config, clientAuth) {
|
13 | return __awaiter(this, void 0, void 0, function* () {
|
14 | let user = null;
|
15 | let baseUrl = config.url;
|
16 | if (baseUrl[baseUrl.length - 1] === '/') {
|
17 | baseUrl = baseUrl.slice(0, -1);
|
18 | }
|
19 | function getUrl() {
|
20 | return baseUrl;
|
21 | }
|
22 | function getUser() {
|
23 | return user;
|
24 | }
|
25 | function getLoginUrl(returnUrl) {
|
26 | if (typeof returnUrl !== 'string') {
|
27 | return `${baseUrl}/login`;
|
28 | }
|
29 | return `${baseUrl}/login?returnUrl=${returnUrl}`;
|
30 | }
|
31 | function disconnect() {
|
32 | return __awaiter(this, void 0, void 0, function* () {
|
33 | yield clientAuth.disconnect(system);
|
34 | if (clientAuth.afterDisconnect) {
|
35 | clientAuth.afterDisconnect(system);
|
36 | }
|
37 | });
|
38 | }
|
39 | function reconnect() {
|
40 | return __awaiter(this, void 0, void 0, function* () {
|
41 | yield disconnect();
|
42 | const result = yield clientAuth.reconnect(system);
|
43 | if (!result) {
|
44 | return null;
|
45 | }
|
46 | user = result;
|
47 | if (clientAuth.afterConnect) {
|
48 | clientAuth.afterConnect(system);
|
49 | }
|
50 | return result;
|
51 | });
|
52 | }
|
53 | const instance = {
|
54 | getUrl,
|
55 | getLoginUrl,
|
56 | disconnect,
|
57 | reconnect,
|
58 | };
|
59 | try {
|
60 | user = yield clientAuth.connect(baseUrl);
|
61 | if (!user) {
|
62 | throw new Error('was not able to authenticate the user');
|
63 | }
|
64 | return Object.freeze(Object.assign(Object.assign({}, instance), { getUser }));
|
65 | }
|
66 | catch (e) {
|
67 | throw new Error(`failed to establish a proper connection with a nexus node: ${e.message}`);
|
68 | }
|
69 | });
|
70 | }
|
71 | exports.default = nexus;
|