@niledatabase/server 1.0.0-alpha.195

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1353 @@
1
+ import isEmpty from 'lodash-es/isEmpty';
2
+ import isObject from 'lodash-es/isObject';
3
+ import knex from 'knex';
4
+
5
+ function _regeneratorRuntime() {
6
+ _regeneratorRuntime = function () {
7
+ return e;
8
+ };
9
+ var t,
10
+ e = {},
11
+ r = Object.prototype,
12
+ n = r.hasOwnProperty,
13
+ o = Object.defineProperty || function (t, e, r) {
14
+ t[e] = r.value;
15
+ },
16
+ i = "function" == typeof Symbol ? Symbol : {},
17
+ a = i.iterator || "@@iterator",
18
+ c = i.asyncIterator || "@@asyncIterator",
19
+ u = i.toStringTag || "@@toStringTag";
20
+ function define(t, e, r) {
21
+ return Object.defineProperty(t, e, {
22
+ value: r,
23
+ enumerable: !0,
24
+ configurable: !0,
25
+ writable: !0
26
+ }), t[e];
27
+ }
28
+ try {
29
+ define({}, "");
30
+ } catch (t) {
31
+ define = function (t, e, r) {
32
+ return t[e] = r;
33
+ };
34
+ }
35
+ function wrap(t, e, r, n) {
36
+ var i = e && e.prototype instanceof Generator ? e : Generator,
37
+ a = Object.create(i.prototype),
38
+ c = new Context(n || []);
39
+ return o(a, "_invoke", {
40
+ value: makeInvokeMethod(t, r, c)
41
+ }), a;
42
+ }
43
+ function tryCatch(t, e, r) {
44
+ try {
45
+ return {
46
+ type: "normal",
47
+ arg: t.call(e, r)
48
+ };
49
+ } catch (t) {
50
+ return {
51
+ type: "throw",
52
+ arg: t
53
+ };
54
+ }
55
+ }
56
+ e.wrap = wrap;
57
+ var h = "suspendedStart",
58
+ l = "suspendedYield",
59
+ f = "executing",
60
+ s = "completed",
61
+ y = {};
62
+ function Generator() {}
63
+ function GeneratorFunction() {}
64
+ function GeneratorFunctionPrototype() {}
65
+ var p = {};
66
+ define(p, a, function () {
67
+ return this;
68
+ });
69
+ var d = Object.getPrototypeOf,
70
+ v = d && d(d(values([])));
71
+ v && v !== r && n.call(v, a) && (p = v);
72
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
73
+ function defineIteratorMethods(t) {
74
+ ["next", "throw", "return"].forEach(function (e) {
75
+ define(t, e, function (t) {
76
+ return this._invoke(e, t);
77
+ });
78
+ });
79
+ }
80
+ function AsyncIterator(t, e) {
81
+ function invoke(r, o, i, a) {
82
+ var c = tryCatch(t[r], t, o);
83
+ if ("throw" !== c.type) {
84
+ var u = c.arg,
85
+ h = u.value;
86
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
87
+ invoke("next", t, i, a);
88
+ }, function (t) {
89
+ invoke("throw", t, i, a);
90
+ }) : e.resolve(h).then(function (t) {
91
+ u.value = t, i(u);
92
+ }, function (t) {
93
+ return invoke("throw", t, i, a);
94
+ });
95
+ }
96
+ a(c.arg);
97
+ }
98
+ var r;
99
+ o(this, "_invoke", {
100
+ value: function (t, n) {
101
+ function callInvokeWithMethodAndArg() {
102
+ return new e(function (e, r) {
103
+ invoke(t, n, e, r);
104
+ });
105
+ }
106
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
107
+ }
108
+ });
109
+ }
110
+ function makeInvokeMethod(e, r, n) {
111
+ var o = h;
112
+ return function (i, a) {
113
+ if (o === f) throw new Error("Generator is already running");
114
+ if (o === s) {
115
+ if ("throw" === i) throw a;
116
+ return {
117
+ value: t,
118
+ done: !0
119
+ };
120
+ }
121
+ for (n.method = i, n.arg = a;;) {
122
+ var c = n.delegate;
123
+ if (c) {
124
+ var u = maybeInvokeDelegate(c, n);
125
+ if (u) {
126
+ if (u === y) continue;
127
+ return u;
128
+ }
129
+ }
130
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
131
+ if (o === h) throw o = s, n.arg;
132
+ n.dispatchException(n.arg);
133
+ } else "return" === n.method && n.abrupt("return", n.arg);
134
+ o = f;
135
+ var p = tryCatch(e, r, n);
136
+ if ("normal" === p.type) {
137
+ if (o = n.done ? s : l, p.arg === y) continue;
138
+ return {
139
+ value: p.arg,
140
+ done: n.done
141
+ };
142
+ }
143
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
144
+ }
145
+ };
146
+ }
147
+ function maybeInvokeDelegate(e, r) {
148
+ var n = r.method,
149
+ o = e.iterator[n];
150
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
151
+ var i = tryCatch(o, e.iterator, r.arg);
152
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
153
+ var a = i.arg;
154
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
155
+ }
156
+ function pushTryEntry(t) {
157
+ var e = {
158
+ tryLoc: t[0]
159
+ };
160
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
161
+ }
162
+ function resetTryEntry(t) {
163
+ var e = t.completion || {};
164
+ e.type = "normal", delete e.arg, t.completion = e;
165
+ }
166
+ function Context(t) {
167
+ this.tryEntries = [{
168
+ tryLoc: "root"
169
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
170
+ }
171
+ function values(e) {
172
+ if (e || "" === e) {
173
+ var r = e[a];
174
+ if (r) return r.call(e);
175
+ if ("function" == typeof e.next) return e;
176
+ if (!isNaN(e.length)) {
177
+ var o = -1,
178
+ i = function next() {
179
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
180
+ return next.value = t, next.done = !0, next;
181
+ };
182
+ return i.next = i;
183
+ }
184
+ }
185
+ throw new TypeError(typeof e + " is not iterable");
186
+ }
187
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
188
+ value: GeneratorFunctionPrototype,
189
+ configurable: !0
190
+ }), o(GeneratorFunctionPrototype, "constructor", {
191
+ value: GeneratorFunction,
192
+ configurable: !0
193
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
194
+ var e = "function" == typeof t && t.constructor;
195
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
196
+ }, e.mark = function (t) {
197
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
198
+ }, e.awrap = function (t) {
199
+ return {
200
+ __await: t
201
+ };
202
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
203
+ return this;
204
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
205
+ void 0 === i && (i = Promise);
206
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
207
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
208
+ return t.done ? t.value : a.next();
209
+ });
210
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
211
+ return this;
212
+ }), define(g, "toString", function () {
213
+ return "[object Generator]";
214
+ }), e.keys = function (t) {
215
+ var e = Object(t),
216
+ r = [];
217
+ for (var n in e) r.push(n);
218
+ return r.reverse(), function next() {
219
+ for (; r.length;) {
220
+ var t = r.pop();
221
+ if (t in e) return next.value = t, next.done = !1, next;
222
+ }
223
+ return next.done = !0, next;
224
+ };
225
+ }, e.values = values, Context.prototype = {
226
+ constructor: Context,
227
+ reset: function (e) {
228
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
229
+ },
230
+ stop: function () {
231
+ this.done = !0;
232
+ var t = this.tryEntries[0].completion;
233
+ if ("throw" === t.type) throw t.arg;
234
+ return this.rval;
235
+ },
236
+ dispatchException: function (e) {
237
+ if (this.done) throw e;
238
+ var r = this;
239
+ function handle(n, o) {
240
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
241
+ }
242
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
243
+ var i = this.tryEntries[o],
244
+ a = i.completion;
245
+ if ("root" === i.tryLoc) return handle("end");
246
+ if (i.tryLoc <= this.prev) {
247
+ var c = n.call(i, "catchLoc"),
248
+ u = n.call(i, "finallyLoc");
249
+ if (c && u) {
250
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
251
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
252
+ } else if (c) {
253
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
254
+ } else {
255
+ if (!u) throw new Error("try statement without catch or finally");
256
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
257
+ }
258
+ }
259
+ }
260
+ },
261
+ abrupt: function (t, e) {
262
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
263
+ var o = this.tryEntries[r];
264
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
265
+ var i = o;
266
+ break;
267
+ }
268
+ }
269
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
270
+ var a = i ? i.completion : {};
271
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
272
+ },
273
+ complete: function (t, e) {
274
+ if ("throw" === t.type) throw t.arg;
275
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
276
+ },
277
+ finish: function (t) {
278
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
279
+ var r = this.tryEntries[e];
280
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
281
+ }
282
+ },
283
+ catch: function (t) {
284
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
285
+ var r = this.tryEntries[e];
286
+ if (r.tryLoc === t) {
287
+ var n = r.completion;
288
+ if ("throw" === n.type) {
289
+ var o = n.arg;
290
+ resetTryEntry(r);
291
+ }
292
+ return o;
293
+ }
294
+ }
295
+ throw new Error("illegal catch attempt");
296
+ },
297
+ delegateYield: function (e, r, n) {
298
+ return this.delegate = {
299
+ iterator: values(e),
300
+ resultName: r,
301
+ nextLoc: n
302
+ }, "next" === this.method && (this.arg = t), y;
303
+ }
304
+ }, e;
305
+ }
306
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
307
+ try {
308
+ var info = gen[key](arg);
309
+ var value = info.value;
310
+ } catch (error) {
311
+ reject(error);
312
+ return;
313
+ }
314
+ if (info.done) {
315
+ resolve(value);
316
+ } else {
317
+ Promise.resolve(value).then(_next, _throw);
318
+ }
319
+ }
320
+ function _asyncToGenerator(fn) {
321
+ return function () {
322
+ var self = this,
323
+ args = arguments;
324
+ return new Promise(function (resolve, reject) {
325
+ var gen = fn.apply(self, args);
326
+ function _next(value) {
327
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
328
+ }
329
+ function _throw(err) {
330
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
331
+ }
332
+ _next(undefined);
333
+ });
334
+ };
335
+ }
336
+ function _defineProperties(target, props) {
337
+ for (var i = 0; i < props.length; i++) {
338
+ var descriptor = props[i];
339
+ descriptor.enumerable = descriptor.enumerable || false;
340
+ descriptor.configurable = true;
341
+ if ("value" in descriptor) descriptor.writable = true;
342
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
343
+ }
344
+ }
345
+ function _createClass(Constructor, protoProps, staticProps) {
346
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
347
+ if (staticProps) _defineProperties(Constructor, staticProps);
348
+ Object.defineProperty(Constructor, "prototype", {
349
+ writable: false
350
+ });
351
+ return Constructor;
352
+ }
353
+ function _extends() {
354
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
355
+ for (var i = 1; i < arguments.length; i++) {
356
+ var source = arguments[i];
357
+ for (var key in source) {
358
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
359
+ target[key] = source[key];
360
+ }
361
+ }
362
+ }
363
+ return target;
364
+ };
365
+ return _extends.apply(this, arguments);
366
+ }
367
+ function _inheritsLoose(subClass, superClass) {
368
+ subClass.prototype = Object.create(superClass.prototype);
369
+ subClass.prototype.constructor = subClass;
370
+ _setPrototypeOf(subClass, superClass);
371
+ }
372
+ function _setPrototypeOf(o, p) {
373
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
374
+ o.__proto__ = p;
375
+ return o;
376
+ };
377
+ return _setPrototypeOf(o, p);
378
+ }
379
+ function _assertThisInitialized(self) {
380
+ if (self === void 0) {
381
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
382
+ }
383
+ return self;
384
+ }
385
+ function _unsupportedIterableToArray(o, minLen) {
386
+ if (!o) return;
387
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
388
+ var n = Object.prototype.toString.call(o).slice(8, -1);
389
+ if (n === "Object" && o.constructor) n = o.constructor.name;
390
+ if (n === "Map" || n === "Set") return Array.from(o);
391
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
392
+ }
393
+ function _arrayLikeToArray(arr, len) {
394
+ if (len == null || len > arr.length) len = arr.length;
395
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
396
+ return arr2;
397
+ }
398
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
399
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
400
+ if (it) return (it = it.call(o)).next.bind(it);
401
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
402
+ if (it) o = it;
403
+ var i = 0;
404
+ return function () {
405
+ if (i >= o.length) return {
406
+ done: true
407
+ };
408
+ return {
409
+ done: false,
410
+ value: o[i++]
411
+ };
412
+ };
413
+ }
414
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
415
+ }
416
+ function _toPrimitive(input, hint) {
417
+ if (typeof input !== "object" || input === null) return input;
418
+ var prim = input[Symbol.toPrimitive];
419
+ if (prim !== undefined) {
420
+ var res = prim.call(input, hint || "default");
421
+ if (typeof res !== "object") return res;
422
+ throw new TypeError("@@toPrimitive must return a primitive value.");
423
+ }
424
+ return (hint === "string" ? String : Number)(input);
425
+ }
426
+ function _toPropertyKey(arg) {
427
+ var key = _toPrimitive(arg, "string");
428
+ return typeof key === "symbol" ? key : String(key);
429
+ }
430
+
431
+ var ApiConfig = /*#__PURE__*/function () {
432
+ function ApiConfig(_ref) {
433
+ var basePath = _ref.basePath,
434
+ cookieKey = _ref.cookieKey,
435
+ token = _ref.token;
436
+ this.basePath = basePath;
437
+ this.cookieKey = cookieKey;
438
+ this._token = token;
439
+ }
440
+ _createClass(ApiConfig, [{
441
+ key: "token",
442
+ get: function get() {
443
+ return this._token;
444
+ },
445
+ set: function set(value) {
446
+ this._token = value;
447
+ }
448
+ }]);
449
+ return ApiConfig;
450
+ }();
451
+ var niledatabase_ur = 'dev.khnum.thenile.dev';
452
+ var Config = /*#__PURE__*/function () {
453
+ function Config(_config) {
454
+ var _config$api$basePath, _config$api, _config$api$cookieKey, _config$api2, _config$api3, _config$db, _config$db2, _config$db3, _config$db4, _config$db5;
455
+ this.database = String(_config == null ? void 0 : _config.database);
456
+ this._tenantId = _config == null ? void 0 : _config.tenantId;
457
+ this.workspace = String(_config == null ? void 0 : _config.workspace);
458
+ this.api = new ApiConfig({
459
+ basePath: (_config$api$basePath = _config == null || (_config$api = _config.api) == null ? void 0 : _config$api.basePath) != null ? _config$api$basePath : "https://" + niledatabase_ur,
460
+ cookieKey: (_config$api$cookieKey = _config == null || (_config$api2 = _config.api) == null ? void 0 : _config$api2.cookieKey) != null ? _config$api$cookieKey : 'token',
461
+ token: _config == null || (_config$api3 = _config.api) == null ? void 0 : _config$api3.token
462
+ });
463
+ var host = _config != null && _config.db && _config.db.connection && typeof ((_config$db = _config.db) == null ? void 0 : _config$db.connection) !== 'string' && 'host' in _config.db.connection ? String(_config.db.connection.host) : niledatabase_ur;
464
+ var port = _config != null && (_config$db2 = _config.db) != null && _config$db2.connection && typeof ((_config$db3 = _config.db) == null ? void 0 : _config$db3.connection) !== 'string' && 'port' in _config.db.connection ? Number((_config$db4 = _config.db) == null || (_config$db4 = _config$db4.connection) == null ? void 0 : _config$db4.port) : 5432;
465
+ var connection = _extends({
466
+ host: host,
467
+ port: port,
468
+ database: _config == null ? void 0 : _config.database
469
+ }, typeof (_config == null || (_config$db5 = _config.db) == null ? void 0 : _config$db5.connection) === 'object' ? _config.db.connection : {});
470
+ this.db = _extends({}, _config == null ? void 0 : _config.db, {
471
+ connection: connection
472
+ });
473
+ }
474
+ _createClass(Config, [{
475
+ key: "tenantId",
476
+ get: function get() {
477
+ return this._tenantId;
478
+ },
479
+ set: function set(value) {
480
+ this._tenantId = value;
481
+ }
482
+ }]);
483
+ return Config;
484
+ }();
485
+
486
+ var ResponseError = function ResponseError(body, init) {
487
+ this.response = new Response(body, init);
488
+ };
489
+
490
+ var Eventer = /*#__PURE__*/function () {
491
+ function Eventer() {
492
+ this.events = {};
493
+ }
494
+ var _proto = Eventer.prototype;
495
+ _proto.publish = function publish(eventName, value) {
496
+ // Get all the callback functions of the current event
497
+ var callbackList = this.events[eventName];
498
+ // execute each callback function
499
+ if (callbackList) {
500
+ for (var _iterator = _createForOfIteratorHelperLoose(callbackList), _step; !(_step = _iterator()).done;) {
501
+ var callback = _step.value;
502
+ callback(value);
503
+ }
504
+ }
505
+ }
506
+ // Subscribe to events
507
+ ;
508
+ _proto.subscribe = function subscribe(eventName, callback) {
509
+ // initialize this event
510
+ if (!this.events[eventName]) {
511
+ this.events[eventName] = [];
512
+ }
513
+ // store the callback function of the subscriber
514
+ this.events[eventName].push(callback);
515
+ };
516
+ return Eventer;
517
+ }(); // tenantId manager
518
+ var tenantEvent = /*#__PURE__*/new Eventer();
519
+ var updateTenantId = function updateTenantId(tenantId) {
520
+ tenantEvent.publish('tenantId', tenantId);
521
+ };
522
+ var watchTenantId = function watchTenantId(cb) {
523
+ return tenantEvent.subscribe('tenantId', cb);
524
+ };
525
+
526
+ var X_NILE_TENANT = 'x-nile-tenantId';
527
+ function getTokenFromCookie(headers, cookieKey) {
528
+ var _headers$get;
529
+ var cookie = (_headers$get = headers.get('cookie')) == null ? void 0 : _headers$get.split('; ');
530
+ var _cookies = {};
531
+ if (cookie) {
532
+ for (var _iterator = _createForOfIteratorHelperLoose(cookie), _step; !(_step = _iterator()).done;) {
533
+ var parts = _step.value;
534
+ var cookieParts = parts.split('=');
535
+ var _cookie = cookieParts.slice(1).join('=');
536
+ var name = cookieParts[0];
537
+ _cookies[name] = _cookie;
538
+ }
539
+ }
540
+ if (cookie) {
541
+ for (var _iterator2 = _createForOfIteratorHelperLoose(cookie), _step2; !(_step2 = _iterator2()).done;) {
542
+ var _parts = _step2.value;
543
+ var _cookieParts = _parts.split('=');
544
+ var _cookie2 = _cookieParts.slice(1).join('=');
545
+ var _name = _cookieParts[0];
546
+ _cookies[_name] = _cookie2;
547
+ }
548
+ }
549
+ if (cookieKey) {
550
+ return _cookies[cookieKey];
551
+ }
552
+ return null;
553
+ }
554
+ function getTenantFromHttp(headers, config) {
555
+ var _ref;
556
+ var cookieTenant = getTokenFromCookie(headers, 'tenantId');
557
+ return (_ref = cookieTenant != null ? cookieTenant : headers == null ? void 0 : headers.get(X_NILE_TENANT)) != null ? _ref : config.tenantId;
558
+ }
559
+ function _fetch(_x, _x2, _x3) {
560
+ return _fetch2.apply(this, arguments);
561
+ }
562
+ function _fetch2() {
563
+ _fetch2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
564
+ var _config$api, _config$api2, _response$status;
565
+ var url, cookieKey, headers, basicHeaders, authHeader, token, _config$api3, tenantId, useableUrl, response, res, _res, message, _res2, _res2$errors, _message;
566
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
567
+ while (1) switch (_context.prev = _context.next) {
568
+ case 0:
569
+ url = "" + ((_config$api = config.api) == null ? void 0 : _config$api.basePath) + path;
570
+ cookieKey = (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey;
571
+ headers = new Headers(opts == null ? void 0 : opts.headers);
572
+ basicHeaders = new Headers();
573
+ basicHeaders.set('content-type', 'application/json; charset=utf-8');
574
+ authHeader = headers.get('Authorization');
575
+ if (!authHeader) {
576
+ token = getTokenFromCookie(headers, cookieKey);
577
+ if (token) {
578
+ basicHeaders.set('Authorization', "Bearer " + token);
579
+ } else {
580
+ basicHeaders.set('Authorization', "Bearer " + ((_config$api3 = config.api) == null ? void 0 : _config$api3.token));
581
+ }
582
+ }
583
+ tenantId = getTenantFromHttp(headers, config);
584
+ updateTenantId(tenantId);
585
+ if (!(url.includes('{tenantId}') && !tenantId)) {
586
+ _context.next = 11;
587
+ break;
588
+ }
589
+ return _context.abrupt("return", new ResponseError('tenantId is not set for request', {
590
+ status: 400
591
+ }));
592
+ case 11:
593
+ useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId)));
594
+ _context.next = 14;
595
+ return fetch(useableUrl, _extends({}, opts, {
596
+ headers: basicHeaders
597
+ }))["catch"](function (e) {
598
+ // eslint-disable-next-line no-console
599
+ console.log(e);
600
+ });
601
+ case 14:
602
+ response = _context.sent;
603
+ if (!(response && response.status >= 200 && response.status < 300)) {
604
+ _context.next = 17;
605
+ break;
606
+ }
607
+ return _context.abrupt("return", response);
608
+ case 17:
609
+ _context.prev = 17;
610
+ _context.next = 20;
611
+ return response == null ? void 0 : response.json();
612
+ case 20:
613
+ res = _context.sent;
614
+ _context.next = 25;
615
+ break;
616
+ case 23:
617
+ _context.prev = 23;
618
+ _context.t0 = _context["catch"](17);
619
+ case 25:
620
+ if (!(res && 'message' in res)) {
621
+ _context.next = 28;
622
+ break;
623
+ }
624
+ _res = res, message = _res.message;
625
+ return _context.abrupt("return", new ResponseError(message, {
626
+ status: 400
627
+ }));
628
+ case 28:
629
+ if (!(res && 'errors' in res)) {
630
+ _context.next = 31;
631
+ break;
632
+ }
633
+ _res2 = res, _res2$errors = _res2.errors, _message = _res2$errors[0];
634
+ return _context.abrupt("return", new ResponseError(_message, {
635
+ status: 400
636
+ }));
637
+ case 31:
638
+ return _context.abrupt("return", new ResponseError(null, {
639
+ status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
640
+ }));
641
+ case 32:
642
+ case "end":
643
+ return _context.stop();
644
+ }
645
+ }, _callee, null, [[17, 23]]);
646
+ }));
647
+ return _fetch2.apply(this, arguments);
648
+ }
649
+
650
+ var Requester = /*#__PURE__*/function (_Config) {
651
+ _inheritsLoose(Requester, _Config);
652
+ function Requester(config) {
653
+ var _this;
654
+ _this = _Config.call(this, config) || this;
655
+ _this.post = /*#__PURE__*/function () {
656
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, url, init) {
657
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
658
+ while (1) switch (_context.prev = _context.next) {
659
+ case 0:
660
+ _context.next = 2;
661
+ return _this.request('POST', url, req, init);
662
+ case 2:
663
+ return _context.abrupt("return", _context.sent);
664
+ case 3:
665
+ case "end":
666
+ return _context.stop();
667
+ }
668
+ }, _callee);
669
+ }));
670
+ return function (_x, _x2, _x3) {
671
+ return _ref.apply(this, arguments);
672
+ };
673
+ }();
674
+ _this.get = /*#__PURE__*/function () {
675
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, url, init) {
676
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
677
+ while (1) switch (_context2.prev = _context2.next) {
678
+ case 0:
679
+ _context2.next = 2;
680
+ return _this.request('GET', url, req, init);
681
+ case 2:
682
+ return _context2.abrupt("return", _context2.sent);
683
+ case 3:
684
+ case "end":
685
+ return _context2.stop();
686
+ }
687
+ }, _callee2);
688
+ }));
689
+ return function (_x4, _x5, _x6) {
690
+ return _ref2.apply(this, arguments);
691
+ };
692
+ }();
693
+ _this.put = /*#__PURE__*/function () {
694
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, url, init) {
695
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
696
+ while (1) switch (_context3.prev = _context3.next) {
697
+ case 0:
698
+ _context3.next = 2;
699
+ return _this.request('PUT', url, req, init);
700
+ case 2:
701
+ return _context3.abrupt("return", _context3.sent);
702
+ case 3:
703
+ case "end":
704
+ return _context3.stop();
705
+ }
706
+ }, _callee3);
707
+ }));
708
+ return function (_x7, _x8, _x9) {
709
+ return _ref3.apply(this, arguments);
710
+ };
711
+ }();
712
+ return _this;
713
+ }
714
+ var _proto = Requester.prototype;
715
+ _proto.rawRequest = /*#__PURE__*/function () {
716
+ var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(method, url, init, body) {
717
+ var _init, res;
718
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
719
+ while (1) switch (_context4.prev = _context4.next) {
720
+ case 0:
721
+ _init = _extends({}, init, {
722
+ body: body,
723
+ method: method
724
+ });
725
+ _context4.next = 3;
726
+ return _fetch(this, url, _init);
727
+ case 3:
728
+ res = _context4.sent;
729
+ if (!(res instanceof ResponseError)) {
730
+ _context4.next = 6;
731
+ break;
732
+ }
733
+ return _context4.abrupt("return", res.response);
734
+ case 6:
735
+ return _context4.abrupt("return", res);
736
+ case 7:
737
+ case "end":
738
+ return _context4.stop();
739
+ }
740
+ }, _callee4, this);
741
+ }));
742
+ function rawRequest(_x10, _x11, _x12, _x13) {
743
+ return _rawRequest.apply(this, arguments);
744
+ }
745
+ return rawRequest;
746
+ }()
747
+ /**
748
+ * three optios here
749
+ * 1) pass in headers for a server side request
750
+ * 2) pass in the payload that matches the api
751
+ * 3) pass in the request object sent by a browser
752
+ * @param method
753
+ * @param url
754
+ * @param req
755
+ * @param init
756
+ * @returns
757
+ */
758
+ ;
759
+ _proto.request =
760
+ /*#__PURE__*/
761
+ function () {
762
+ var _request = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(method, url, req, init) {
763
+ var headers, tenantId, cookie, _headers, _tenantId, _cookie, body, _init;
764
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
765
+ while (1) switch (_context5.prev = _context5.next) {
766
+ case 0:
767
+ // set the headers
768
+ headers = new Headers(init ? init == null ? void 0 : init.headers : {});
769
+ if (req instanceof Headers) {
770
+ tenantId = req.get(X_NILE_TENANT);
771
+ cookie = req.get('cookie');
772
+ if (tenantId) {
773
+ headers.set(X_NILE_TENANT, tenantId);
774
+ }
775
+ if (cookie) {
776
+ headers.set('cookie', cookie);
777
+ }
778
+ } else if (req instanceof Request) {
779
+ // pass back the X_NILE_TENANT
780
+ _headers = new Headers(req == null ? void 0 : req.headers);
781
+ _tenantId = _headers.get(X_NILE_TENANT);
782
+ _cookie = _headers.get('cookie');
783
+ if (_tenantId) {
784
+ headers.set(X_NILE_TENANT, _tenantId);
785
+ }
786
+ if (_cookie) {
787
+ headers.set('cookie', _cookie);
788
+ }
789
+ }
790
+ // default the body - may be the actual payload for the API
791
+ body = JSON.stringify(req); // comes from next/some server
792
+ if (!(method === 'GET')) {
793
+ _context5.next = 7;
794
+ break;
795
+ }
796
+ body = undefined;
797
+ _context5.next = 14;
798
+ break;
799
+ case 7:
800
+ if (!(req instanceof Request)) {
801
+ _context5.next = 13;
802
+ break;
803
+ }
804
+ _context5.next = 10;
805
+ return new Response(req.body).text();
806
+ case 10:
807
+ body = _context5.sent;
808
+ _context5.next = 14;
809
+ break;
810
+ case 13:
811
+ if (
812
+ // is just headers for a GET request
813
+ req instanceof Headers || isEmpty(req) || isObject(req) && Object.values(req).length === 0) {
814
+ body = undefined;
815
+ }
816
+ case 14:
817
+ _init = _extends({}, init, {
818
+ headers: headers
819
+ });
820
+ _context5.next = 17;
821
+ return this.rawRequest(method, url, _init, body);
822
+ case 17:
823
+ return _context5.abrupt("return", _context5.sent);
824
+ case 18:
825
+ case "end":
826
+ return _context5.stop();
827
+ }
828
+ }, _callee5, this);
829
+ }));
830
+ function request(_x14, _x15, _x16, _x17) {
831
+ return _request.apply(this, arguments);
832
+ }
833
+ return request;
834
+ }();
835
+ return Requester;
836
+ }(Config);
837
+
838
+ var Auth = /*#__PURE__*/function (_Config) {
839
+ _inheritsLoose(Auth, _Config);
840
+ function Auth(config) {
841
+ var _this;
842
+ _this = _Config.call(this, config) || this;
843
+ _this.login = /*#__PURE__*/function () {
844
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
845
+ var headers, _requester, params, sso, providerRes, providers, res, _this$api, token, cookie, tenants, tenant, tenantId, text;
846
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
847
+ while (1) switch (_context.prev = _context.next) {
848
+ case 0:
849
+ headers = new Headers({
850
+ 'content-type': 'application/json'
851
+ });
852
+ _requester = new Requester(_assertThisInitialized(_this));
853
+ params = req instanceof Request ? new URL(req.url).searchParams : new URLSearchParams();
854
+ sso = params.get('sso');
855
+ if (!(sso === 'true')) {
856
+ _context.next = 18;
857
+ break;
858
+ }
859
+ _context.next = 7;
860
+ return _this.listProviders(req.clone(), init);
861
+ case 7:
862
+ providerRes = _context.sent;
863
+ if (!(providerRes && providerRes.status >= 200 && providerRes.status < 300)) {
864
+ _context.next = 18;
865
+ break;
866
+ }
867
+ _context.next = 11;
868
+ return new Response(providerRes.body).json();
869
+ case 11:
870
+ providers = _context.sent;
871
+ if (!(providers.length > 0)) {
872
+ _context.next = 18;
873
+ break;
874
+ }
875
+ if (!(providers.length > 1)) {
876
+ _context.next = 15;
877
+ break;
878
+ }
879
+ return _context.abrupt("return", new Response(JSON.stringify(providers), {
880
+ status: 200
881
+ }));
882
+ case 15:
883
+ // is there a way to do this? probably not.
884
+ headers.set(X_NILE_TENANT, providers[0].tenantId);
885
+ headers.append('set-cookie', "tenantId=" + providers[0].tenantId + "; path=/; httponly;");
886
+ // const ssoResp = await this.loginSSO(req);
887
+ return _context.abrupt("return", new Response(JSON.stringify({
888
+ redirectURI: "" + _this.api.basePath + _this.loginSSOUrl('okta')
889
+ }), {
890
+ status: 200
891
+ }));
892
+ case 18:
893
+ _context.next = 20;
894
+ return _requester.post(req, _this.loginUrl, init)["catch"](function (e) {
895
+ // eslint-disable-next-line no-console
896
+ console.error(e);
897
+ return e;
898
+ });
899
+ case 20:
900
+ res = _context.sent;
901
+ if (!(res instanceof ResponseError)) {
902
+ _context.next = 23;
903
+ break;
904
+ }
905
+ return _context.abrupt("return", res.response);
906
+ case 23:
907
+ if (!(res && res.status >= 200 && res.status < 300)) {
908
+ _context.next = 35;
909
+ break;
910
+ }
911
+ _context.next = 26;
912
+ return res.json();
913
+ case 26:
914
+ token = _context.sent;
915
+ cookie = ((_this$api = _this.api) == null ? void 0 : _this$api.cookieKey) + "=" + token.token.jwt + "; path=/; samesite=lax; httponly;";
916
+ headers.append('set-cookie', cookie);
917
+ tenants = token.tenants;
918
+ tenant = tenants == null ? void 0 : tenants.values();
919
+ tenantId = tenant == null ? void 0 : tenant.next().value;
920
+ headers.set(X_NILE_TENANT, tenantId);
921
+ headers.append('set-cookie', "tenantId=" + tenantId + "; path=/; httponly;");
922
+ return _context.abrupt("return", new Response(JSON.stringify(token), {
923
+ status: 200,
924
+ headers: headers
925
+ }));
926
+ case 35:
927
+ _context.next = 37;
928
+ return res.text();
929
+ case 37:
930
+ text = _context.sent;
931
+ return _context.abrupt("return", new Response(text, {
932
+ status: res.status
933
+ }));
934
+ case 39:
935
+ case "end":
936
+ return _context.stop();
937
+ }
938
+ }, _callee);
939
+ }));
940
+ return function (_x, _x2) {
941
+ return _ref.apply(this, arguments);
942
+ };
943
+ }();
944
+ _this.loginSSO = function (redirectUrl) {
945
+ var ssoLogin = /*#__PURE__*/function () {
946
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req) {
947
+ var _this$api2;
948
+ var headers, body, accessToken, tenantId, cookie;
949
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
950
+ while (1) switch (_context2.prev = _context2.next) {
951
+ case 0:
952
+ headers = new Headers();
953
+ _context2.next = 3;
954
+ return req.formData();
955
+ case 3:
956
+ body = _context2.sent;
957
+ _context2.next = 6;
958
+ return body.get('access_token');
959
+ case 6:
960
+ accessToken = _context2.sent;
961
+ _context2.next = 9;
962
+ return body.get('tenantId');
963
+ case 9:
964
+ tenantId = _context2.sent;
965
+ cookie = ((_this$api2 = _this.api) == null ? void 0 : _this$api2.cookieKey) + "=" + accessToken + "; path=/; samesite=lax; httponly;";
966
+ headers.append('set-cookie', cookie);
967
+ headers.set(X_NILE_TENANT, tenantId);
968
+ headers.append('set-cookie', "tenantId=" + tenantId + "; path=/; httponly;");
969
+ headers.set('Location', redirectUrl);
970
+ return _context2.abrupt("return", new Response(null, {
971
+ headers: headers,
972
+ status: 303
973
+ }));
974
+ case 16:
975
+ case "end":
976
+ return _context2.stop();
977
+ }
978
+ }, _callee2);
979
+ }));
980
+ return function ssoLogin(_x3) {
981
+ return _ref2.apply(this, arguments);
982
+ };
983
+ }();
984
+ return ssoLogin;
985
+ };
986
+ _this.loginSSOUrl = function (provider) {
987
+ var _this$tenantId;
988
+ return "/workspaces/" + encodeURIComponent(_this.workspace) + "/databases/" + encodeURIComponent(_this.database) + "/tenants/" + ((_this$tenantId = _this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/auth/oidc/providers/" + provider + "/login";
989
+ };
990
+ _this.signUp = /*#__PURE__*/function () {
991
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
992
+ var _requester;
993
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
994
+ while (1) switch (_context3.prev = _context3.next) {
995
+ case 0:
996
+ _requester = new Requester(_assertThisInitialized(_this));
997
+ return _context3.abrupt("return", _requester.post(req, _this.signUpUrl, init));
998
+ case 2:
999
+ case "end":
1000
+ return _context3.stop();
1001
+ }
1002
+ }, _callee3);
1003
+ }));
1004
+ return function (_x4, _x5) {
1005
+ return _ref3.apply(this, arguments);
1006
+ };
1007
+ }();
1008
+ _this.listTenantProviders = /*#__PURE__*/function () {
1009
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
1010
+ var _requester;
1011
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1012
+ while (1) switch (_context4.prev = _context4.next) {
1013
+ case 0:
1014
+ _requester = new Requester(_assertThisInitialized(_this));
1015
+ return _context4.abrupt("return", _requester.get(req, _this.listTenantProvidersUrl, init));
1016
+ case 2:
1017
+ case "end":
1018
+ return _context4.stop();
1019
+ }
1020
+ }, _callee4);
1021
+ }));
1022
+ return function (_x6, _x7) {
1023
+ return _ref4.apply(this, arguments);
1024
+ };
1025
+ }();
1026
+ _this.createProvider = /*#__PURE__*/function () {
1027
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, init) {
1028
+ var _requester, providerName;
1029
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1030
+ while (1) switch (_context5.prev = _context5.next) {
1031
+ case 0:
1032
+ _requester = new Requester(_assertThisInitialized(_this));
1033
+ providerName = 'okta';
1034
+ return _context5.abrupt("return", _requester.post(req, _this.updateProviderUrl(providerName), init));
1035
+ case 3:
1036
+ case "end":
1037
+ return _context5.stop();
1038
+ }
1039
+ }, _callee5);
1040
+ }));
1041
+ return function (_x8, _x9) {
1042
+ return _ref5.apply(this, arguments);
1043
+ };
1044
+ }();
1045
+ _this.updateProvider = /*#__PURE__*/function () {
1046
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, init) {
1047
+ var _requester, providerName;
1048
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1049
+ while (1) switch (_context6.prev = _context6.next) {
1050
+ case 0:
1051
+ _requester = new Requester(_assertThisInitialized(_this));
1052
+ providerName = 'okta';
1053
+ return _context6.abrupt("return", _requester.put(req, _this.updateProviderUrl(providerName), init));
1054
+ case 3:
1055
+ case "end":
1056
+ return _context6.stop();
1057
+ }
1058
+ }, _callee6);
1059
+ }));
1060
+ return function (_x10, _x11) {
1061
+ return _ref6.apply(this, arguments);
1062
+ };
1063
+ }();
1064
+ _this.listProviders = /*#__PURE__*/function () {
1065
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(req, init) {
1066
+ var _body;
1067
+ var _requester, body;
1068
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
1069
+ while (1) switch (_context7.prev = _context7.next) {
1070
+ case 0:
1071
+ _requester = new Requester(_assertThisInitialized(_this));
1072
+ if (!(req && 'body' in req)) {
1073
+ _context7.next = 5;
1074
+ break;
1075
+ }
1076
+ _context7.next = 4;
1077
+ return new Response(req.body).json();
1078
+ case 4:
1079
+ body = _context7.sent;
1080
+ case 5:
1081
+ return _context7.abrupt("return", _requester.get(req, _this.providerUrl((_body = body) == null ? void 0 : _body.email), init));
1082
+ case 6:
1083
+ case "end":
1084
+ return _context7.stop();
1085
+ }
1086
+ }, _callee7);
1087
+ }));
1088
+ return function (_x12, _x13) {
1089
+ return _ref7.apply(this, arguments);
1090
+ };
1091
+ }();
1092
+ _this.getSSOCallbackUrl = function (param) {
1093
+ var tenantId;
1094
+ if (typeof tenantId === 'string') {
1095
+ tenantId = param;
1096
+ } else if (param instanceof Headers) {
1097
+ tenantId = getTenantFromHttp(param, _assertThisInitialized(_this));
1098
+ }
1099
+ return _this.api.basePath + "/workspaces/" + _this.workspace + "/databases/" + _this.database + "/tenants/" + tenantId + "/auth/oidc/callback";
1100
+ };
1101
+ return _this;
1102
+ }
1103
+ var _proto = Auth.prototype;
1104
+ _proto.updateProviderUrl = function updateProviderUrl(providerName) {
1105
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers/" + encodeURIComponent(providerName);
1106
+ };
1107
+ _proto.providerUrl = function providerUrl(email) {
1108
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/auth/oidc/providers" + (email ? "?email=" + encodeURIComponent(email) : '');
1109
+ };
1110
+ _createClass(Auth, [{
1111
+ key: "loginUrl",
1112
+ get: function get() {
1113
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/login";
1114
+ }
1115
+ }, {
1116
+ key: "signUpUrl",
1117
+ get: function get() {
1118
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users";
1119
+ }
1120
+ }, {
1121
+ key: "listTenantProvidersUrl",
1122
+ get: function get() {
1123
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers";
1124
+ }
1125
+ }]);
1126
+ return Auth;
1127
+ }(Config);
1128
+
1129
+ var Users = /*#__PURE__*/function (_Config) {
1130
+ _inheritsLoose(Users, _Config);
1131
+ function Users(config) {
1132
+ var _this;
1133
+ _this = _Config.call(this, config) || this;
1134
+ _this.createTenantUser = /*#__PURE__*/function () {
1135
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
1136
+ var _requester;
1137
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1138
+ while (1) switch (_context.prev = _context.next) {
1139
+ case 0:
1140
+ _requester = new Requester(_assertThisInitialized(_this));
1141
+ _context.next = 3;
1142
+ return _requester.post(req, _this.tenantUsersUrl, init);
1143
+ case 3:
1144
+ return _context.abrupt("return", _context.sent);
1145
+ case 4:
1146
+ case "end":
1147
+ return _context.stop();
1148
+ }
1149
+ }, _callee);
1150
+ }));
1151
+ return function (_x, _x2) {
1152
+ return _ref.apply(this, arguments);
1153
+ };
1154
+ }();
1155
+ _this.listTenantUsers = /*#__PURE__*/function () {
1156
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
1157
+ var _requester;
1158
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1159
+ while (1) switch (_context2.prev = _context2.next) {
1160
+ case 0:
1161
+ _requester = new Requester(_assertThisInitialized(_this));
1162
+ _context2.next = 3;
1163
+ return _requester.get(req, _this.tenantUsersUrl, init);
1164
+ case 3:
1165
+ return _context2.abrupt("return", _context2.sent);
1166
+ case 4:
1167
+ case "end":
1168
+ return _context2.stop();
1169
+ }
1170
+ }, _callee2);
1171
+ }));
1172
+ return function (_x3, _x4) {
1173
+ return _ref2.apply(this, arguments);
1174
+ };
1175
+ }();
1176
+ _this.me = /*#__PURE__*/function () {
1177
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
1178
+ var _requester;
1179
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1180
+ while (1) switch (_context3.prev = _context3.next) {
1181
+ case 0:
1182
+ _requester = new Requester(_assertThisInitialized(_this));
1183
+ _context3.next = 3;
1184
+ return _requester.get(req, _this.meUrl, init);
1185
+ case 3:
1186
+ return _context3.abrupt("return", _context3.sent);
1187
+ case 4:
1188
+ case "end":
1189
+ return _context3.stop();
1190
+ }
1191
+ }, _callee3);
1192
+ }));
1193
+ return function (_x5, _x6) {
1194
+ return _ref3.apply(this, arguments);
1195
+ };
1196
+ }();
1197
+ return _this;
1198
+ }
1199
+ _createClass(Users, [{
1200
+ key: "tenantUsersUrl",
1201
+ get: function get() {
1202
+ var _this$tenantId;
1203
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users";
1204
+ }
1205
+ }, {
1206
+ key: "meUrl",
1207
+ get: function get() {
1208
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/me";
1209
+ }
1210
+ }]);
1211
+ return Users;
1212
+ }(Config);
1213
+
1214
+ var Tenants = /*#__PURE__*/function (_Config) {
1215
+ _inheritsLoose(Tenants, _Config);
1216
+ function Tenants(config) {
1217
+ var _this;
1218
+ _this = _Config.call(this, config) || this;
1219
+ _this.createTenant = /*#__PURE__*/function () {
1220
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
1221
+ var _requester;
1222
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1223
+ while (1) switch (_context.prev = _context.next) {
1224
+ case 0:
1225
+ _requester = new Requester(_assertThisInitialized(_this));
1226
+ return _context.abrupt("return", _requester.post(req, _this.tenantsUrl, init));
1227
+ case 2:
1228
+ case "end":
1229
+ return _context.stop();
1230
+ }
1231
+ }, _callee);
1232
+ }));
1233
+ return function (_x, _x2) {
1234
+ return _ref.apply(this, arguments);
1235
+ };
1236
+ }();
1237
+ _this.getTenant = /*#__PURE__*/function () {
1238
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
1239
+ var _requester;
1240
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1241
+ while (1) switch (_context2.prev = _context2.next) {
1242
+ case 0:
1243
+ _requester = new Requester(_assertThisInitialized(_this));
1244
+ return _context2.abrupt("return", _requester.get(req, _this.tenantUrl, init));
1245
+ case 2:
1246
+ case "end":
1247
+ return _context2.stop();
1248
+ }
1249
+ }, _callee2);
1250
+ }));
1251
+ return function (_x3, _x4) {
1252
+ return _ref2.apply(this, arguments);
1253
+ };
1254
+ }();
1255
+ return _this;
1256
+ }
1257
+ _createClass(Tenants, [{
1258
+ key: "tenantsUrl",
1259
+ get: function get() {
1260
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants";
1261
+ }
1262
+ }, {
1263
+ key: "tenantUrl",
1264
+ get: function get() {
1265
+ var _this$tenantId;
1266
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}');
1267
+ }
1268
+ }]);
1269
+ return Tenants;
1270
+ }(Config);
1271
+
1272
+ var init = function init(config) {
1273
+ var auth = new Auth(config);
1274
+ var users = new Users(config);
1275
+ var tenants = new Tenants(config);
1276
+ var dbConfig = _extends({}, config.db, {
1277
+ client: 'pg'
1278
+ });
1279
+ return [{
1280
+ auth: auth,
1281
+ users: users,
1282
+ tenants: tenants
1283
+ }, knex(dbConfig)];
1284
+ };
1285
+ var Server = /*#__PURE__*/function () {
1286
+ function Server(config) {
1287
+ var _this = this;
1288
+ this.config = new Config(config);
1289
+ var _init = init(this.config),
1290
+ api = _init[0],
1291
+ knex = _init[1];
1292
+ this.api = api;
1293
+ this.db = knex;
1294
+ watchTenantId(function (tenantId) {
1295
+ _this.tenantId = tenantId;
1296
+ });
1297
+ }
1298
+ var _proto = Server.prototype;
1299
+ _proto.setConfig = function setConfig(cfg) {
1300
+ // if a config has changed, re init everything
1301
+ this.api = null;
1302
+ this.db = null;
1303
+ var _init2 = init(cfg),
1304
+ api = _init2[0],
1305
+ knex = _init2[1];
1306
+ this.api = api;
1307
+ this.db = knex;
1308
+ this.config = cfg;
1309
+ };
1310
+ _createClass(Server, [{
1311
+ key: "tenantId",
1312
+ get: function get() {
1313
+ return this.config.tenantId;
1314
+ },
1315
+ set: function set(tenantId) {
1316
+ if (tenantId) {
1317
+ this.config.tenantId = tenantId;
1318
+ if (this.api) {
1319
+ this.api.auth.tenantId = tenantId;
1320
+ this.api.users.tenantId = tenantId;
1321
+ this.api.tenants.tenantId = tenantId;
1322
+ }
1323
+ }
1324
+ }
1325
+ }, {
1326
+ key: "token",
1327
+ get: function get() {
1328
+ var _this$config;
1329
+ return (_this$config = this.config) == null || (_this$config = _this$config.api) == null ? void 0 : _this$config.token;
1330
+ },
1331
+ set: function set(token) {
1332
+ if (token) {
1333
+ this.config.api.token = token;
1334
+ if (this.api) {
1335
+ this.api.auth.api.token = token;
1336
+ this.api.users.api.token = token;
1337
+ this.api.tenants.api.token = token;
1338
+ }
1339
+ }
1340
+ }
1341
+ }]);
1342
+ return Server;
1343
+ }();
1344
+ var server = /*#__PURE__*/new Server();
1345
+ function Nile(config) {
1346
+ server.setConfig(new Config(config));
1347
+ return server;
1348
+ }
1349
+
1350
+ module.exports = Nile;
1351
+
1352
+ export default Nile;
1353
+ //# sourceMappingURL=server.esm.js.map