@niledatabase/server 3.0.0-alpha.0 → 3.0.0-alpha.10

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.
Files changed (42) hide show
  1. package/dist/Api.d.ts +22 -0
  2. package/dist/Server.d.ts +1 -21
  3. package/dist/api/handlers/DELETE.d.ts +1 -1
  4. package/dist/api/handlers/GET.d.ts +1 -1
  5. package/dist/api/handlers/POST.d.ts +1 -1
  6. package/dist/api/handlers/PUT.d.ts +1 -1
  7. package/dist/api/handlers/index.d.ts +4 -4
  8. package/dist/api/routes/auth/csrf.d.ts +1 -1
  9. package/dist/api/routes/auth/error.d.ts +1 -1
  10. package/dist/api/routes/auth/providers.d.ts +1 -1
  11. package/dist/api/routes/auth/session.d.ts +2 -1
  12. package/dist/api/routes/auth/signin.d.ts +1 -1
  13. package/dist/api/routes/auth/signout.d.ts +1 -1
  14. package/dist/api/routes/me/index.d.ts +1 -1
  15. package/dist/api/routes/tenants/GET.d.ts +1 -1
  16. package/dist/api/routes/tenants/POST.d.ts +1 -1
  17. package/dist/api/routes/tenants/[tenantId]/DELETE.d.ts +1 -1
  18. package/dist/api/routes/tenants/[tenantId]/users/GET.d.ts +1 -1
  19. package/dist/api/routes/tenants/[tenantId]/users/POST.d.ts +1 -1
  20. package/dist/api/routes/tenants/[tenantId]/users/PUT.d.ts +2 -3
  21. package/dist/api/routes/tenants/[tenantId]/users/[userId]/DELETE.d.ts +3 -3
  22. package/dist/api/routes/tenants/[tenantId]/users/index.d.ts +1 -1
  23. package/dist/api/routes/tenants/index.d.ts +1 -1
  24. package/dist/api/routes/users/GET.d.ts +2 -2
  25. package/dist/api/routes/users/POST.d.ts +1 -1
  26. package/dist/api/routes/users/[userId]/PUT.d.ts +1 -1
  27. package/dist/api/routes/users/index.d.ts +1 -1
  28. package/dist/api/utils/auth.d.ts +8 -0
  29. package/dist/api/utils/request.d.ts +2 -1
  30. package/dist/db/isUUID.d.ts +1 -0
  31. package/dist/server.cjs.development.js +1663 -1535
  32. package/dist/server.cjs.development.js.map +1 -1
  33. package/dist/server.cjs.production.min.js +1 -1
  34. package/dist/server.cjs.production.min.js.map +1 -1
  35. package/dist/server.esm.js +1663 -1535
  36. package/dist/server.esm.js.map +1 -1
  37. package/dist/tenants/index.d.ts +7 -3
  38. package/dist/types.d.ts +1 -0
  39. package/dist/users/index.d.ts +7 -7
  40. package/dist/utils/Requester/index.d.ts +5 -5
  41. package/dist/utils/Requester/types.d.ts +1 -1
  42. package/package.json +2 -2
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('dotenv/config');
6
- var jose = require('jose');
7
6
  var pg = require('pg');
7
+ var jose = require('jose');
8
8
 
9
9
  function _regeneratorRuntime() {
10
10
  _regeneratorRuntime = function () {
@@ -609,11 +609,19 @@ var getControlPlane = function getControlPlane(cfg) {
609
609
  logger = cfg.logger;
610
610
  var _Logger9 = Logger(config, '[basePath]'),
611
611
  info = _Logger9.info;
612
+ if (config != null && config.configureUrl) {
613
+ logger && info(logger, 'config', config.configureUrl);
614
+ return config.configureUrl;
615
+ }
612
616
  if (process.env.NILEDB_CONFIGURE) {
613
617
  logger && info(logger, 'NILEDB_CONFIGURE', process.env.NILEDB_CONFIGURE);
618
+ // backwards compatible, but not really
619
+ if (!process.env.NILEDB_CONFIGURE.startsWith('http')) {
620
+ return "https://" + process.env.NILEDB_CONFIGURE;
621
+ }
614
622
  return process.env.NILEDB_CONFIGURE;
615
623
  }
616
- logger && info(logger, 'default', process.env.NILEDB_CONFIGURE);
624
+ logger && info(logger, 'default', 'https://global.thenile.dev');
617
625
  return 'https://global.thenile.dev';
618
626
  };
619
627
  function getDbHost(cfg) {
@@ -899,11 +907,6 @@ var Config = /*#__PURE__*/function () {
899
907
  }]);
900
908
  }();
901
909
 
902
- var ResponseError = function ResponseError(body, init) {
903
- this.response = void 0;
904
- this.response = new Response(body, init);
905
- };
906
-
907
910
  var Events;
908
911
  (function (Events) {
909
912
  Events["User"] = "userId";
@@ -974,886 +977,179 @@ var evictPool = function evictPool(val) {
974
977
  eventer.publish(Events.EvictPool, val);
975
978
  };
976
979
 
977
- var X_NILE_TENANT = 'niledb-tenant-id';
978
- var X_NILE_USER_ID = 'niledb-user-id';
979
- function getTokenFromCookie(headers, cookieKey) {
980
- var _headers$get;
981
- var cookie = (_headers$get = headers.get('cookie')) == null ? void 0 : _headers$get.split('; ');
982
- var _cookies = {};
983
- if (cookie) {
984
- for (var _iterator = _createForOfIteratorHelperLoose(cookie), _step; !(_step = _iterator()).done;) {
985
- var parts = _step.value;
986
- var cookieParts = parts.split('=');
987
- var _cookie = cookieParts.slice(1).join('=');
988
- var name = cookieParts[0];
989
- _cookies[name] = _cookie;
990
- }
991
- }
992
- if (cookie) {
993
- for (var _iterator2 = _createForOfIteratorHelperLoose(cookie), _step2; !(_step2 = _iterator2()).done;) {
994
- var _parts = _step2.value;
995
- var _cookieParts = _parts.split('=');
996
- var _cookie2 = _cookieParts.slice(1).join('=');
997
- var _name = _cookieParts[0];
998
- _cookies[_name] = _cookie2;
980
+ function createProxyForPool(pool, config) {
981
+ var _Logger = Logger(config, '[pool]'),
982
+ info = _Logger.info,
983
+ error = _Logger.error;
984
+ return new Proxy(pool, {
985
+ get: function get(target, property) {
986
+ if (property === 'query') {
987
+ // give connection string a pass for these problems
988
+ if (!config.db.connectionString) {
989
+ if (!config.user || !config.password) {
990
+ error('Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev');
991
+ } else if (!config.db.database) {
992
+ error('Database name is missing from the config. Call `nile.init()` or set NILEDB_ID in your .env');
993
+ }
994
+ }
995
+ var caller = target[property];
996
+ return function query() {
997
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
998
+ args[_key] = arguments[_key];
999
+ }
1000
+ info.apply(void 0, args);
1001
+ // @ts-expect-error - not mine
1002
+ var called = caller.apply(this, args);
1003
+ return called;
1004
+ };
1005
+ }
1006
+ return target[property];
999
1007
  }
1000
- }
1001
- if (cookieKey) {
1002
- return _cookies[cookieKey];
1003
- }
1004
- return null;
1005
- }
1006
- function getTenantFromHttp(headers, config) {
1007
- var _ref;
1008
- var cookieTenant = getTokenFromCookie(headers, X_NILE_TENANT);
1009
- return (_ref = cookieTenant != null ? cookieTenant : headers == null ? void 0 : headers.get(X_NILE_TENANT)) != null ? _ref : config == null ? void 0 : config.tenantId;
1008
+ });
1010
1009
  }
1011
- function getUserFromHttp(headers, config) {
1012
- var _headers$get2;
1013
- var token = getTokenFromCookie(headers, config.api.cookieKey);
1014
- if (token) {
1015
- var jwt = jose.decodeJwt(token);
1016
- return jwt.sub;
1010
+
1011
+ function isUUID(value) {
1012
+ if (!value) {
1013
+ return false;
1017
1014
  }
1018
- return (_headers$get2 = headers == null ? void 0 : headers.get(X_NILE_USER_ID)) != null ? _headers$get2 : config.userId;
1019
- }
1020
- function _fetch(_x, _x2, _x3) {
1021
- return _fetch2.apply(this, arguments);
1015
+ var regex = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
1016
+ return regex.test(value);
1022
1017
  }
1023
- function _fetch2() {
1024
- _fetch2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
1025
- var _config$api, _config$api2, _response$status;
1026
- var _Logger, info, error, url, cookieKey, headers, basicHeaders, creds, authHeader, token, tenantId, userId, useableUrl, response, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1027
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1028
- while (1) switch (_context.prev = _context.next) {
1029
- case 0:
1030
- _Logger = Logger(config, '[server]'), info = _Logger.info, error = _Logger.error;
1031
- url = "" + ((_config$api = config.api) == null ? void 0 : _config$api.basePath) + path;
1032
- cookieKey = (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey;
1033
- headers = new Headers(opts == null ? void 0 : opts.headers);
1034
- basicHeaders = new Headers(opts == null ? void 0 : opts.headers);
1035
- basicHeaders.set('content-type', 'application/json; charset=utf-8');
1036
- creds = Buffer.from(process.env.NILEDB_USER + ":" + process.env.NILEDB_PASSWORD, 'utf8').toString('base64');
1037
- basicHeaders.set('niledb-creds', creds);
1038
- authHeader = headers.get('Authorization');
1039
- if (!authHeader) {
1040
- token = getTokenFromCookie(headers, cookieKey);
1041
- if (token) {
1042
- basicHeaders.set('Authorization', "Bearer " + token);
1043
- } else if (getToken({
1044
- config: config
1045
- })) {
1046
- basicHeaders.set('Authorization', "Bearer " + getToken({
1047
- config: config
1048
- }));
1049
- }
1050
- }
1051
- tenantId = getTenantFromHttp(headers, config);
1052
- updateTenantId(tenantId);
1053
- userId = getUserFromHttp(headers, config);
1054
- updateUserId(userId);
1055
- if (!(url.includes('{tenantId}') && !tenantId)) {
1056
- _context.next = 16;
1057
- break;
1058
- }
1059
- return _context.abrupt("return", new ResponseError('tenantId is not set for request', {
1060
- status: 400
1061
- }));
1062
- case 16:
1063
- useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId))).replace('{userId}', encodeURIComponent(String(userId)));
1064
- info('[fetch]', useableUrl);
1065
- _context.next = 20;
1066
- return fetch(useableUrl, _extends({}, opts, {
1067
- headers: basicHeaders
1068
- }))["catch"](function (e) {
1069
- error('[fetch]', '[response]', e);
1070
- });
1071
- case 20:
1072
- response = _context.sent;
1073
- if (!(response && response.status >= 200 && response.status < 300)) {
1074
- _context.next = 39;
1075
- break;
1076
- }
1077
- if (!(typeof response.clone === 'function')) {
1078
- _context.next = 38;
1079
- break;
1080
- }
1081
- _context.prev = 23;
1082
- _context.t0 = info;
1083
- _context.next = 27;
1084
- return response.clone().json();
1085
- case 27:
1086
- _context.t1 = _context.sent;
1087
- (0, _context.t0)('[fetch]', '[response]', _context.t1);
1088
- _context.next = 38;
1089
- break;
1090
- case 31:
1091
- _context.prev = 31;
1092
- _context.t2 = _context["catch"](23);
1093
- _context.t3 = info;
1094
- _context.next = 36;
1095
- return response.clone().text();
1096
- case 36:
1097
- _context.t4 = _context.sent;
1098
- (0, _context.t3)('[fetch]', '[response]', _context.t4);
1099
- case 38:
1100
- return _context.abrupt("return", response);
1101
- case 39:
1102
- if (!((response == null ? void 0 : response.status) === 401)) {
1103
- _context.next = 41;
1104
- break;
1018
+
1019
+ var _excluded$1 = ["afterCreate"];
1020
+ var NileDatabase = /*#__PURE__*/function () {
1021
+ function NileDatabase(config, id) {
1022
+ var _this = this;
1023
+ this.pool = void 0;
1024
+ this.tenantId = void 0;
1025
+ this.userId = void 0;
1026
+ this.id = void 0;
1027
+ this.config = void 0;
1028
+ this.timer = void 0;
1029
+ var _Logger = Logger(config, '[NileInstance]'),
1030
+ warn = _Logger.warn,
1031
+ info = _Logger.info;
1032
+ this.id = id;
1033
+ var poolConfig = _extends({
1034
+ min: 0,
1035
+ max: 10,
1036
+ idleTimeoutMillis: 30000
1037
+ }, config.db);
1038
+ var afterCreate = poolConfig.afterCreate,
1039
+ remaining = _objectWithoutPropertiesLoose(poolConfig, _excluded$1);
1040
+ config.db = poolConfig;
1041
+ this.config = config;
1042
+ info(this.config.db);
1043
+ this.pool = createProxyForPool(new pg.Pool(remaining), this.config);
1044
+ if (typeof afterCreate === 'function') {
1045
+ warn('Providing an pool configuration will stop automatic tenant context setting.');
1046
+ }
1047
+ // start the timer for cleanup
1048
+ this.startTimeout();
1049
+ this.pool.on('connect', /*#__PURE__*/function () {
1050
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(client) {
1051
+ var afterCreate;
1052
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1053
+ while (1) switch (_context.prev = _context.next) {
1054
+ case 0:
1055
+ info('pool connected');
1056
+ afterCreate = makeAfterCreate(config);
1057
+ afterCreate(client, function (err) {
1058
+ var _Logger2 = Logger(config, '[after create callback]'),
1059
+ error = _Logger2.error;
1060
+ if (err) {
1061
+ error('after create failed', err);
1062
+ evictPool(_this.id);
1063
+ }
1064
+ });
1065
+ _this.startTimeout();
1066
+ case 4:
1067
+ case "end":
1068
+ return _context.stop();
1105
1069
  }
1106
- return _context.abrupt("return", new ResponseError('Unauthorized', {
1107
- status: 401
1108
- }));
1109
- case 41:
1110
- if (!((response == null ? void 0 : response.status) === 405)) {
1111
- _context.next = 43;
1112
- break;
1113
- }
1114
- return _context.abrupt("return", new ResponseError('Method not allowed', {
1115
- status: 405
1116
- }));
1117
- case 43:
1118
- errorHandler = typeof (response == null ? void 0 : response.clone) === 'function' ? response.clone() : null;
1119
- msg = '';
1120
- _context.prev = 45;
1121
- _context.next = 48;
1122
- return response == null ? void 0 : response.json();
1123
- case 48:
1124
- res = _context.sent;
1125
- _context.next = 59;
1126
- break;
1127
- case 51:
1128
- _context.prev = 51;
1129
- _context.t5 = _context["catch"](45);
1130
- if (!errorHandler) {
1131
- _context.next = 58;
1132
- break;
1133
- }
1134
- _context.next = 56;
1135
- return errorHandler.text();
1136
- case 56:
1137
- msg = _context.sent;
1138
- if (msg) {
1139
- error('[fetch]', '[response]', "[status: " + errorHandler.status + "]", msg);
1140
- }
1141
- case 58:
1142
- if (!msg) {
1143
- error('[fetch]', '[response]', _context.t5);
1144
- }
1145
- case 59:
1146
- if (!msg) {
1147
- _context.next = 61;
1148
- break;
1149
- }
1150
- return _context.abrupt("return", new ResponseError(msg, {
1151
- status: errorHandler == null ? void 0 : errorHandler.status
1152
- }));
1153
- case 61:
1154
- if (!(res && 'message' in res)) {
1155
- _context.next = 65;
1156
- break;
1157
- }
1158
- _res = res, message = _res.message;
1159
- error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", message);
1160
- return _context.abrupt("return", new ResponseError(message, {
1161
- status: 400
1162
- }));
1163
- case 65:
1164
- if (!(res && 'errors' in res)) {
1165
- _context.next = 69;
1166
- break;
1167
- }
1168
- _res2 = res, _res2$errors = _res2.errors, _message = _res2$errors[0];
1169
- error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", _message);
1170
- return _context.abrupt("return", new ResponseError(_message, {
1171
- status: 400
1172
- }));
1173
- case 69:
1174
- error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", 'UNHANDLED ERROR');
1175
- return _context.abrupt("return", new ResponseError(null, {
1176
- status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
1177
- }));
1178
- case 71:
1179
- case "end":
1180
- return _context.stop();
1181
- }
1182
- }, _callee, null, [[23, 31], [45, 51]]);
1183
- }));
1184
- return _fetch2.apply(this, arguments);
1185
- }
1186
-
1187
- var Requester = /*#__PURE__*/function (_Config) {
1188
- function Requester(config) {
1189
- var _this;
1190
- _this = _Config.call(this, config) || this;
1191
- _this.post = /*#__PURE__*/function () {
1192
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, url, init) {
1193
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1194
- while (1) switch (_context.prev = _context.next) {
1195
- case 0:
1196
- _context.next = 2;
1197
- return _this.request('POST', url, req, init);
1198
- case 2:
1199
- return _context.abrupt("return", _context.sent);
1200
- case 3:
1201
- case "end":
1202
- return _context.stop();
1203
- }
1204
- }, _callee);
1205
- }));
1206
- return function (_x, _x2, _x3) {
1207
- return _ref.apply(this, arguments);
1208
- };
1209
- }();
1210
- _this.get = /*#__PURE__*/function () {
1211
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, url, init) {
1212
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1213
- while (1) switch (_context2.prev = _context2.next) {
1214
- case 0:
1215
- _context2.next = 2;
1216
- return _this.request('GET', url, req, init);
1217
- case 2:
1218
- return _context2.abrupt("return", _context2.sent);
1219
- case 3:
1220
- case "end":
1221
- return _context2.stop();
1070
+ }, _callee);
1071
+ }));
1072
+ return function (_x) {
1073
+ return _ref.apply(this, arguments);
1074
+ };
1075
+ }());
1076
+ this.pool.on('error', /*#__PURE__*/function () {
1077
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(e) {
1078
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1079
+ while (1) switch (_context2.prev = _context2.next) {
1080
+ case 0:
1081
+ info('pool failed', e);
1082
+ if (_this.timer) {
1083
+ clearTimeout(_this.timer);
1084
+ }
1085
+ evictPool(_this.id);
1086
+ case 3:
1087
+ case "end":
1088
+ return _context2.stop();
1222
1089
  }
1223
1090
  }, _callee2);
1224
1091
  }));
1225
- return function (_x4, _x5, _x6) {
1092
+ return function (_x2) {
1226
1093
  return _ref2.apply(this, arguments);
1227
1094
  };
1228
- }();
1229
- _this.put = /*#__PURE__*/function () {
1230
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, url, init) {
1231
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1232
- while (1) switch (_context3.prev = _context3.next) {
1233
- case 0:
1234
- _context3.next = 2;
1235
- return _this.request('PUT', url, req, init);
1236
- case 2:
1237
- return _context3.abrupt("return", _context3.sent);
1238
- case 3:
1239
- case "end":
1240
- return _context3.stop();
1241
- }
1242
- }, _callee3);
1243
- }));
1244
- return function (_x7, _x8, _x9) {
1245
- return _ref3.apply(this, arguments);
1246
- };
1247
- }();
1248
- _this["delete"] = /*#__PURE__*/function () {
1249
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, url, init) {
1250
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1251
- while (1) switch (_context4.prev = _context4.next) {
1252
- case 0:
1253
- _context4.next = 2;
1254
- return _this.request('DELETE', url, req, init);
1255
- case 2:
1256
- return _context4.abrupt("return", _context4.sent);
1257
- case 3:
1258
- case "end":
1259
- return _context4.stop();
1260
- }
1261
- }, _callee4);
1262
- }));
1263
- return function (_x10, _x11, _x12) {
1264
- return _ref4.apply(this, arguments);
1265
- };
1266
- }();
1267
- return _this;
1095
+ }());
1268
1096
  }
1269
- _inheritsLoose(Requester, _Config);
1270
- var _proto = Requester.prototype;
1271
- _proto.rawRequest = /*#__PURE__*/function () {
1272
- var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(method, url, init, body) {
1273
- var _init, res;
1274
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1275
- while (1) switch (_context5.prev = _context5.next) {
1097
+ var _proto = NileDatabase.prototype;
1098
+ _proto.startTimeout = function startTimeout() {
1099
+ var _this2 = this;
1100
+ var _Logger3 = Logger(this.config, '[NileInstance]'),
1101
+ info = _Logger3.info;
1102
+ if (this.timer) {
1103
+ clearTimeout(this.timer);
1104
+ }
1105
+ this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
1106
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1107
+ while (1) switch (_context3.prev = _context3.next) {
1276
1108
  case 0:
1277
- _init = _extends({}, init, {
1278
- body: body,
1279
- method: method
1109
+ info('Pool reached idleTimeoutMillis.', _this2.id, 'evicted after', _this2.config.db.idleTimeoutMillis, 'ms');
1110
+ _context3.next = 3;
1111
+ return _this2.pool.end(function () {
1112
+ info('Pool end called');
1113
+ // something odd going on here. Without the callback, pool.end() is flakey
1280
1114
  });
1281
- _context5.next = 3;
1282
- return _fetch(this, url, _init);
1283
1115
  case 3:
1284
- res = _context5.sent;
1285
- if (!(res instanceof ResponseError)) {
1286
- _context5.next = 6;
1287
- break;
1288
- }
1289
- return _context5.abrupt("return", res.response);
1290
- case 6:
1291
- return _context5.abrupt("return", res);
1292
- case 7:
1116
+ evictPool(_this2.id);
1117
+ case 4:
1293
1118
  case "end":
1294
- return _context5.stop();
1119
+ return _context3.stop();
1295
1120
  }
1296
- }, _callee5, this);
1297
- }));
1298
- function rawRequest(_x13, _x14, _x15, _x16) {
1299
- return _rawRequest.apply(this, arguments);
1300
- }
1301
- return rawRequest;
1302
- }()
1303
- /**
1304
- * three optios here
1305
- * 1) pass in headers for a server side request
1306
- * 2) pass in the payload that matches the api
1307
- * 3) pass in the request object sent by a browser
1308
- * @param method
1309
- * @param url
1310
- * @param req
1311
- * @param init
1312
- * @returns
1313
- */
1314
- ;
1315
- _proto.request =
1316
- /*#__PURE__*/
1317
- function () {
1318
- var _request = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(method, url, req, init) {
1319
- var headers, tenantId, cookie, _headers, _tenantId, _cookie, body, _init;
1320
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1321
- while (1) switch (_context6.prev = _context6.next) {
1322
- case 0:
1323
- // set the headers
1324
- headers = new Headers(init ? init == null ? void 0 : init.headers : {});
1325
- if (req instanceof Headers) {
1326
- tenantId = req.get(X_NILE_TENANT);
1327
- cookie = req.get('cookie');
1328
- if (tenantId) {
1329
- headers.set(X_NILE_TENANT, tenantId);
1330
- }
1331
- if (cookie) {
1332
- headers.set('cookie', cookie);
1333
- }
1334
- } else if (req instanceof Request) {
1335
- // pass back the X_NILE_TENANT
1336
- _headers = new Headers(req == null ? void 0 : req.headers);
1337
- _tenantId = _headers.get(X_NILE_TENANT);
1338
- _cookie = _headers.get('cookie');
1339
- if (_tenantId) {
1340
- headers.set(X_NILE_TENANT, _tenantId);
1341
- }
1342
- if (_cookie) {
1343
- headers.set('cookie', _cookie);
1344
- }
1345
- }
1346
- // default the body - may be the actual payload for the API
1347
- body = JSON.stringify(req); // comes from next/some server
1348
- if (!(method === 'GET')) {
1349
- _context6.next = 7;
1350
- break;
1351
- }
1352
- body = undefined;
1353
- _context6.next = 14;
1354
- break;
1355
- case 7:
1356
- if (!(req instanceof Request)) {
1357
- _context6.next = 13;
1358
- break;
1359
- }
1360
- _context6.next = 10;
1361
- return new Response(req.body).text();
1362
- case 10:
1363
- body = _context6.sent;
1364
- _context6.next = 14;
1365
- break;
1366
- case 13:
1367
- if (
1368
- // is just headers for a GET request
1369
- req instanceof Headers || JSON.stringify(req) === '{}' || req && typeof req === 'object' && Object.values(req).length === 0) {
1370
- body = undefined;
1371
- }
1372
- case 14:
1373
- _init = _extends({}, init, {
1374
- headers: headers
1375
- });
1376
- _context6.next = 17;
1377
- return this.rawRequest(method, url, _init, body);
1378
- case 17:
1379
- return _context6.abrupt("return", _context6.sent);
1380
- case 18:
1381
- case "end":
1382
- return _context6.stop();
1121
+ }, _callee3);
1122
+ })), this.config.db.idleTimeoutMillis);
1123
+ };
1124
+ return NileDatabase;
1125
+ }();
1126
+ function makeAfterCreate(config) {
1127
+ var _Logger4 = Logger(config, '[afterCreate]'),
1128
+ warn = _Logger4.warn,
1129
+ info = _Logger4.info;
1130
+ return function (conn, done) {
1131
+ conn.on('error', function errorHandler(error) {
1132
+ warn('Connection was terminated by server', error);
1133
+ done(error, conn);
1134
+ });
1135
+ if (isUUID(config.tenantId)) {
1136
+ var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1137
+ if (isUUID(config.userId)) {
1138
+ if (!config.tenantId) {
1139
+ warn('A user id cannot be set in context without a tenant id');
1383
1140
  }
1384
- }, _callee6, this);
1385
- }));
1386
- function request(_x17, _x18, _x19, _x20) {
1387
- return _request.apply(this, arguments);
1388
- }
1389
- return request;
1390
- }();
1391
- return Requester;
1392
- }(Config);
1393
-
1394
- var Users = /*#__PURE__*/function (_Config) {
1395
- function Users(config, headers) {
1396
- var _this;
1397
- _this = _Config.call(this, config) || this;
1398
- _this.headers = void 0;
1399
- _this.createUser = /*#__PURE__*/function () {
1400
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
1401
- var _requester, _init;
1402
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1403
- while (1) switch (_context.prev = _context.next) {
1404
- case 0:
1405
- _requester = new Requester(_this);
1406
- _init = _this.handleHeaders(init);
1407
- _context.next = 4;
1408
- return _requester.post(req, _this.usersUrl, _init);
1409
- case 4:
1410
- return _context.abrupt("return", _context.sent);
1411
- case 5:
1412
- case "end":
1413
- return _context.stop();
1414
- }
1415
- }, _callee);
1416
- }));
1417
- return function (_x, _x2) {
1418
- return _ref.apply(this, arguments);
1419
- };
1420
- }();
1421
- _this.updateUser = /*#__PURE__*/function () {
1422
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(userId, req, init) {
1423
- var _requester, _init;
1424
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1425
- while (1) switch (_context2.prev = _context2.next) {
1426
- case 0:
1427
- _requester = new Requester(_this);
1428
- _init = _this.handleHeaders(init);
1429
- _context2.next = 4;
1430
- return _requester.put(req, _this.usersUrl + "/" + userId, _init);
1431
- case 4:
1432
- return _context2.abrupt("return", _context2.sent);
1433
- case 5:
1434
- case "end":
1435
- return _context2.stop();
1436
- }
1437
- }, _callee2);
1438
- }));
1439
- return function (_x3, _x4, _x5) {
1440
- return _ref2.apply(this, arguments);
1441
- };
1442
- }();
1443
- _this.listUsers = /*#__PURE__*/function () {
1444
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
1445
- var _requester, _init;
1446
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1447
- while (1) switch (_context3.prev = _context3.next) {
1448
- case 0:
1449
- _requester = new Requester(_this);
1450
- _init = _this.handleHeaders(init);
1451
- _context3.next = 4;
1452
- return _requester.get(req, _this.tenantUsersUrl, _init);
1453
- case 4:
1454
- return _context3.abrupt("return", _context3.sent);
1455
- case 5:
1456
- case "end":
1457
- return _context3.stop();
1458
- }
1459
- }, _callee3);
1460
- }));
1461
- return function (_x6, _x7) {
1462
- return _ref3.apply(this, arguments);
1463
- };
1464
- }();
1465
- _this.linkUser = /*#__PURE__*/function () {
1466
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
1467
- var _requester, _init;
1468
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1469
- while (1) switch (_context4.prev = _context4.next) {
1470
- case 0:
1471
- _requester = new Requester(_this);
1472
- _init = _this.handleHeaders(init);
1473
- _context4.next = 4;
1474
- return _requester.put(req, _this.tenantUsersUrl, _init);
1475
- case 4:
1476
- return _context4.abrupt("return", _context4.sent);
1477
- case 5:
1478
- case "end":
1479
- return _context4.stop();
1480
- }
1481
- }, _callee4);
1482
- }));
1483
- return function (_x8, _x9) {
1484
- return _ref4.apply(this, arguments);
1485
- };
1486
- }();
1487
- _this.tenantUsersDeleteUrl = function (userId) {
1488
- var _this$tenantId;
1489
- return "/tenants/" + ((_this$tenantId = _this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users/" + (userId != null ? userId : '{userId}');
1490
- };
1491
- _this.getUserId = /*#__PURE__*/function () {
1492
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req) {
1493
- var body;
1494
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1495
- while (1) switch (_context5.prev = _context5.next) {
1496
- case 0:
1497
- if (!(req instanceof Request)) {
1498
- _context5.next = 6;
1499
- break;
1500
- }
1501
- _context5.next = 3;
1502
- return new Response(req == null ? void 0 : req.body).json();
1503
- case 3:
1504
- body = _context5.sent;
1505
- if (!body) {
1506
- _context5.next = 6;
1507
- break;
1508
- }
1509
- return _context5.abrupt("return", body.id);
1510
- case 6:
1511
- if (!('id' in req)) {
1512
- _context5.next = 8;
1513
- break;
1514
- }
1515
- return _context5.abrupt("return", req.id);
1516
- case 8:
1517
- return _context5.abrupt("return", null);
1518
- case 9:
1519
- case "end":
1520
- return _context5.stop();
1521
- }
1522
- }, _callee5);
1523
- }));
1524
- return function (_x10) {
1525
- return _ref5.apply(this, arguments);
1526
- };
1527
- }();
1528
- _this.unlinkUser = /*#__PURE__*/function () {
1529
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, init) {
1530
- var _requester, userId, _init;
1531
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1532
- while (1) switch (_context6.prev = _context6.next) {
1533
- case 0:
1534
- _requester = new Requester(_this);
1535
- _context6.next = 3;
1536
- return _this.getUserId(req);
1537
- case 3:
1538
- userId = _context6.sent;
1539
- _init = _this.handleHeaders(init);
1540
- _context6.next = 7;
1541
- return _requester["delete"](req, _this.tenantUsersDeleteUrl(userId), _init);
1542
- case 7:
1543
- return _context6.abrupt("return", _context6.sent);
1544
- case 8:
1545
- case "end":
1546
- return _context6.stop();
1547
- }
1548
- }, _callee6);
1549
- }));
1550
- return function (_x11, _x12) {
1551
- return _ref6.apply(this, arguments);
1552
- };
1553
- }();
1554
- _this.me = /*#__PURE__*/function () {
1555
- var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(req, init) {
1556
- var _requester, _init;
1557
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
1558
- while (1) switch (_context7.prev = _context7.next) {
1559
- case 0:
1560
- _requester = new Requester(_this);
1561
- _init = _this.handleHeaders(init);
1562
- _context7.next = 4;
1563
- return _requester.get(req, _this.meUrl, _init);
1564
- case 4:
1565
- return _context7.abrupt("return", _context7.sent);
1566
- case 5:
1567
- case "end":
1568
- return _context7.stop();
1569
- }
1570
- }, _callee7);
1571
- }));
1572
- return function (_x13, _x14) {
1573
- return _ref7.apply(this, arguments);
1574
- };
1575
- }();
1576
- _this.headers = headers;
1577
- return _this;
1578
- }
1579
- _inheritsLoose(Users, _Config);
1580
- var _proto = Users.prototype;
1581
- _proto.handleHeaders = function handleHeaders(init) {
1582
- if (this.headers) {
1583
- if (init) {
1584
- var _init2;
1585
- init.headers = new Headers(_extends({}, this.headers, (_init2 = init) == null ? void 0 : _init2.headers));
1586
- return init;
1587
- } else {
1588
- init = {
1589
- headers: this.headers
1590
- };
1591
- return init;
1592
- }
1593
- }
1594
- return undefined;
1595
- };
1596
- return _createClass(Users, [{
1597
- key: "usersUrl",
1598
- get: function get() {
1599
- return '/users';
1600
- }
1601
- }, {
1602
- key: "tenantUsersUrl",
1603
- get: function get() {
1604
- var _this$tenantId2;
1605
- return "/tenants/" + ((_this$tenantId2 = this.tenantId) != null ? _this$tenantId2 : '{tenantId}') + "/users";
1606
- }
1607
- }, {
1608
- key: "meUrl",
1609
- get: function get() {
1610
- return '/me';
1611
- }
1612
- }]);
1613
- }(Config);
1614
-
1615
- var Tenants = /*#__PURE__*/function (_Config) {
1616
- function Tenants(config, headers) {
1617
- var _this;
1618
- _this = _Config.call(this, config) || this;
1619
- _this.headers = void 0;
1620
- _this.createTenant = /*#__PURE__*/function () {
1621
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
1622
- var _requester, _init;
1623
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1624
- while (1) switch (_context.prev = _context.next) {
1625
- case 0:
1626
- _requester = new Requester(_this);
1627
- _init = _this.handleHeaders(init);
1628
- return _context.abrupt("return", _requester.post(req, _this.tenantsUrl, _init));
1629
- case 3:
1630
- case "end":
1631
- return _context.stop();
1632
- }
1633
- }, _callee);
1634
- }));
1635
- return function (_x, _x2) {
1636
- return _ref.apply(this, arguments);
1637
- };
1638
- }();
1639
- _this.getTenant = /*#__PURE__*/function () {
1640
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
1641
- var _requester, _init;
1642
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1643
- while (1) switch (_context2.prev = _context2.next) {
1644
- case 0:
1645
- _requester = new Requester(_this);
1646
- _init = _this.handleHeaders(init);
1647
- return _context2.abrupt("return", _requester.get(req, _this.tenantUrl, _init));
1648
- case 3:
1649
- case "end":
1650
- return _context2.stop();
1651
- }
1652
- }, _callee2);
1653
- }));
1654
- return function (_x3, _x4) {
1655
- return _ref2.apply(this, arguments);
1656
- };
1657
- }();
1658
- _this.headers = headers;
1659
- return _this;
1660
- }
1661
- _inheritsLoose(Tenants, _Config);
1662
- var _proto = Tenants.prototype;
1663
- _proto.handleHeaders = function handleHeaders(init) {
1664
- if (this.headers) {
1665
- if (init) {
1666
- var _init2;
1667
- init.headers = new Headers(_extends({}, this.headers, (_init2 = init) == null ? void 0 : _init2.headers));
1668
- return init;
1669
- } else {
1670
- init = {
1671
- headers: this.headers
1672
- };
1673
- return init;
1674
- }
1675
- }
1676
- return undefined;
1677
- };
1678
- return _createClass(Tenants, [{
1679
- key: "tenantsUrl",
1680
- get: function get() {
1681
- return '/tenants';
1682
- }
1683
- }, {
1684
- key: "tenantUrl",
1685
- get: function get() {
1686
- var _this$tenantId;
1687
- return "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}');
1688
- }
1689
- }]);
1690
- }(Config);
1691
-
1692
- function createProxyForPool(pool, config) {
1693
- var _Logger = Logger(config, '[pool]'),
1694
- info = _Logger.info,
1695
- error = _Logger.error;
1696
- return new Proxy(pool, {
1697
- get: function get(target, property) {
1698
- if (property === 'query') {
1699
- // give connection string a pass for these problems
1700
- if (!config.db.connectionString) {
1701
- if (!config.user || !config.password) {
1702
- error('Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev');
1703
- } else if (!config.db.database) {
1704
- error('Database name is missing from the config. Call `nile.init()` or set NILEDB_ID in your .env');
1705
- }
1706
- }
1707
- var caller = target[property];
1708
- return function query() {
1709
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1710
- args[_key] = arguments[_key];
1711
- }
1712
- info.apply(void 0, args);
1713
- // @ts-expect-error - not mine
1714
- var called = caller.apply(this, args);
1715
- return called;
1716
- };
1717
- }
1718
- return target[property];
1719
- }
1720
- });
1721
- }
1722
-
1723
- var _excluded$1 = ["afterCreate"];
1724
- var NileDatabase = /*#__PURE__*/function () {
1725
- function NileDatabase(config, id) {
1726
- var _this = this;
1727
- this.pool = void 0;
1728
- this.tenantId = void 0;
1729
- this.userId = void 0;
1730
- this.id = void 0;
1731
- this.config = void 0;
1732
- this.timer = void 0;
1733
- var _Logger = Logger(config, '[NileInstance]'),
1734
- warn = _Logger.warn,
1735
- info = _Logger.info;
1736
- this.id = id;
1737
- var poolConfig = _extends({
1738
- min: 0,
1739
- max: 10,
1740
- idleTimeoutMillis: 30000
1741
- }, config.db);
1742
- var afterCreate = poolConfig.afterCreate,
1743
- remaining = _objectWithoutPropertiesLoose(poolConfig, _excluded$1);
1744
- config.db = poolConfig;
1745
- this.config = config;
1746
- info(this.config.db);
1747
- this.pool = createProxyForPool(new pg.Pool(remaining), this.config);
1748
- if (typeof afterCreate === 'function') {
1749
- warn('Providing an pool configuration will stop automatic tenant context setting.');
1750
- }
1751
- // start the timer for cleanup
1752
- this.startTimeout();
1753
- this.pool.on('connect', /*#__PURE__*/function () {
1754
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(client) {
1755
- var afterCreate;
1756
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1757
- while (1) switch (_context.prev = _context.next) {
1758
- case 0:
1759
- info('pool connected');
1760
- afterCreate = makeAfterCreate(config);
1761
- afterCreate(client, function (err) {
1762
- var _Logger2 = Logger(config, '[after create callback]'),
1763
- error = _Logger2.error;
1764
- if (err) {
1765
- error('after create failed', err);
1766
- evictPool(_this.id);
1767
- }
1768
- });
1769
- _this.startTimeout();
1770
- case 4:
1771
- case "end":
1772
- return _context.stop();
1773
- }
1774
- }, _callee);
1775
- }));
1776
- return function (_x) {
1777
- return _ref.apply(this, arguments);
1778
- };
1779
- }());
1780
- this.pool.on('error', /*#__PURE__*/function () {
1781
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(e) {
1782
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1783
- while (1) switch (_context2.prev = _context2.next) {
1784
- case 0:
1785
- info('pool failed', e);
1786
- if (_this.timer) {
1787
- clearTimeout(_this.timer);
1788
- }
1789
- evictPool(_this.id);
1790
- case 3:
1791
- case "end":
1792
- return _context2.stop();
1793
- }
1794
- }, _callee2);
1795
- }));
1796
- return function (_x2) {
1797
- return _ref2.apply(this, arguments);
1798
- };
1799
- }());
1800
- }
1801
- var _proto = NileDatabase.prototype;
1802
- _proto.startTimeout = function startTimeout() {
1803
- var _this2 = this;
1804
- var _Logger3 = Logger(this.config, '[NileInstance]'),
1805
- info = _Logger3.info;
1806
- if (this.timer) {
1807
- clearTimeout(this.timer);
1808
- }
1809
- this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
1810
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1811
- while (1) switch (_context3.prev = _context3.next) {
1812
- case 0:
1813
- info('Pool reached idleTimeoutMillis.', _this2.id, 'evicted after', _this2.config.db.idleTimeoutMillis, 'ms');
1814
- _context3.next = 3;
1815
- return _this2.pool.end(function () {
1816
- info('Pool end called');
1817
- // something odd going on here. Without the callback, pool.end() is flakey
1818
- });
1819
- case 3:
1820
- evictPool(_this2.id);
1821
- case 4:
1822
- case "end":
1823
- return _context3.stop();
1824
- }
1825
- }, _callee3);
1826
- })), this.config.db.idleTimeoutMillis);
1827
- };
1828
- return NileDatabase;
1829
- }();
1830
- function makeAfterCreate(config) {
1831
- var _Logger4 = Logger(config, '[afterCreate]'),
1832
- warn = _Logger4.warn,
1833
- info = _Logger4.info;
1834
- return function (conn, done) {
1835
- conn.on('error', function errorHandler(error) {
1836
- warn('Connection was terminated by server', error);
1837
- done(error, conn);
1838
- });
1839
- if (config.tenantId) {
1840
- var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1841
- if (config.userId) {
1842
- if (!config.tenantId) {
1843
- warn('A user id cannot be set in context without a tenant id');
1844
- }
1845
- query.push("SET nile.user_id = '" + config.userId + "'");
1846
- }
1847
- // in this example we use pg driver's connection API
1848
- conn.query(query.join(';'), function (err) {
1849
- if (config.tenantId) {
1850
- info('[tenant id]', config.tenantId);
1851
- }
1852
- if (config.userId) {
1853
- info('[user id]', config.userId);
1854
- }
1855
- done(err, conn);
1856
- });
1141
+ query.push("SET nile.user_id = '" + config.userId + "'");
1142
+ }
1143
+ // in this example we use pg driver's connection API
1144
+ conn.query(query.join(';'), function (err) {
1145
+ if (query.length === 1) {
1146
+ info('[tenant id]', config.tenantId);
1147
+ }
1148
+ if (query.length === 2) {
1149
+ info('[user id]', config.userId);
1150
+ }
1151
+ done(err, conn);
1152
+ });
1857
1153
  }
1858
1154
  done(null, conn);
1859
1155
  };
@@ -1895,171 +1191,44 @@ var DBManager = /*#__PURE__*/function () {
1895
1191
  watchEvictPool(_this.poolWatcher(config));
1896
1192
  }
1897
1193
  return newOne.pool;
1898
- };
1899
- this.clear = function (config) {
1900
- var _Logger3 = Logger(config, '[DBManager]'),
1901
- info = _Logger3.info;
1902
- info('Clearing all connections', _this.connections.size);
1903
- closeEvictPool(_this.poolWatcher(config));
1904
- _this.cleared = true;
1905
- _this.connections.clear();
1906
- };
1907
- var _Logger4 = Logger(_config, '[DBManager]'),
1908
- _info = _Logger4.info;
1909
- this.cleared = false;
1910
- this.connections = new Map();
1911
- // add the base one, so you can at least query
1912
- var _id = this.makeId();
1913
- _info('constructor', _id);
1914
- this.connections.set(_id, new NileDatabase(new Config(_config), _id));
1915
- watchEvictPool(this.poolWatcher(_config));
1916
- }
1917
- var _proto = DBManager.prototype;
1918
- _proto.makeId = function makeId(tenantId, userId) {
1919
- if (tenantId && userId) {
1920
- return tenantId + ":" + userId;
1921
- }
1922
- if (tenantId) {
1923
- return "" + tenantId;
1924
- }
1925
- return 'base';
1926
- };
1927
- return DBManager;
1928
- }();
1929
-
1930
- var getServerId = function getServerId(config) {
1931
- var cfg = new Config(config);
1932
- return makeServerId(cfg);
1933
- };
1934
- var makeServerId = function makeServerId(config) {
1935
- return Buffer.from(JSON.stringify(config), 'base64').toString();
1936
- };
1937
-
1938
- /**
1939
- * a helper function to log in server side.
1940
- */
1941
- function serverAuth(config, handlers) {
1942
- return /*#__PURE__*/function () {
1943
- var _login = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
1944
- var _yield$csrfRes$json, _exec;
1945
- var email, password, sessionUrl, sessionReq, sessionRes, providers, csrf, csrfReq, csrfRes, _ref2, csrfToken, credentials, csrfCookie, signInUrl, postReq, loginRes, authCookie, _ref3, token;
1946
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1947
- while (1) switch (_context.prev = _context.next) {
1948
- case 0:
1949
- email = _ref.email, password = _ref.password;
1950
- sessionUrl = new URL(config.api.localPath + "/api/auth/providers");
1951
- sessionReq = new Request(sessionUrl, {
1952
- method: 'GET',
1953
- headers: new Headers({
1954
- host: sessionUrl.host
1955
- })
1956
- });
1957
- _context.next = 5;
1958
- return handlers.POST(sessionReq);
1959
- case 5:
1960
- sessionRes = _context.sent;
1961
- _context.next = 8;
1962
- return sessionRes == null ? void 0 : sessionRes.json();
1963
- case 8:
1964
- providers = _context.sent;
1965
- csrf = new URL(config.api.localPath + "/api/auth/csrf");
1966
- csrfReq = new Request(csrf, {
1967
- method: 'GET',
1968
- headers: new Headers({
1969
- host: sessionUrl.host
1970
- })
1971
- });
1972
- _context.next = 13;
1973
- return handlers.POST(csrfReq);
1974
- case 13:
1975
- csrfRes = _context.sent;
1976
- _context.next = 16;
1977
- return csrfRes == null ? void 0 : csrfRes.json();
1978
- case 16:
1979
- _context.t0 = _yield$csrfRes$json = _context.sent;
1980
- if (!(_context.t0 != null)) {
1981
- _context.next = 21;
1982
- break;
1983
- }
1984
- _context.t1 = _yield$csrfRes$json;
1985
- _context.next = 22;
1986
- break;
1987
- case 21:
1988
- _context.t1 = {};
1989
- case 22:
1990
- _ref2 = _context.t1;
1991
- csrfToken = _ref2.csrfToken;
1992
- credentials = providers.credentials;
1993
- csrfCookie = csrfRes == null ? void 0 : csrfRes.headers.get('set-cookie');
1994
- expect(csrfCookie).toContain('nile.csrf-token=');
1995
- signInUrl = new URL(credentials.callbackUrl);
1996
- if (csrfCookie) {
1997
- _context.next = 30;
1998
- break;
1999
- }
2000
- throw new Error('unable to authenticate REST');
2001
- case 30:
2002
- postReq = new Request(signInUrl, {
2003
- method: 'POST',
2004
- headers: new Headers({
2005
- 'content-type': 'application/json',
2006
- cookie: csrfCookie
2007
- }),
2008
- body: JSON.stringify({
2009
- email: email,
2010
- password: password,
2011
- csrfToken: csrfToken,
2012
- callbackUrl: credentials.callbackUrl
2013
- })
2014
- });
2015
- _context.next = 33;
2016
- return handlers.POST(postReq);
2017
- case 33:
2018
- loginRes = _context.sent;
2019
- authCookie = loginRes == null ? void 0 : loginRes.headers.get('set-cookie');
2020
- if (authCookie) {
2021
- _context.next = 37;
2022
- break;
2023
- }
2024
- throw new Error('authentication failed');
2025
- case 37:
2026
- expect(authCookie).toContain('nile.session-token=');
2027
- _ref3 = (_exec = /(nile\.session-token=.+?);/.exec(authCookie)) != null ? _exec : [], token = _ref3[1];
2028
- return _context.abrupt("return", new Headers({
2029
- cookie: [token, csrfCookie].join('; ')
2030
- }));
2031
- case 40:
2032
- case "end":
2033
- return _context.stop();
2034
- }
2035
- }, _callee);
2036
- }));
2037
- function login(_x) {
2038
- return _login.apply(this, arguments);
2039
- }
2040
- return login;
2041
- }();
2042
- }
2043
-
2044
- var appRoutes = function appRoutes(prefix) {
2045
- if (prefix === void 0) {
2046
- prefix = '/api';
1194
+ };
1195
+ this.clear = function (config) {
1196
+ var _Logger3 = Logger(config, '[DBManager]'),
1197
+ info = _Logger3.info;
1198
+ info('Clearing all connections', _this.connections.size);
1199
+ closeEvictPool(_this.poolWatcher(config));
1200
+ _this.cleared = true;
1201
+ _this.connections.clear();
1202
+ };
1203
+ var _Logger4 = Logger(_config, '[DBManager]'),
1204
+ _info = _Logger4.info;
1205
+ this.cleared = false;
1206
+ this.connections = new Map();
1207
+ // add the base one, so you can at least query
1208
+ var _id = this.makeId();
1209
+ _info('constructor', _id);
1210
+ this.connections.set(_id, new NileDatabase(new Config(_config), _id));
1211
+ watchEvictPool(this.poolWatcher(_config));
2047
1212
  }
2048
- return {
2049
- SIGNIN: prefix + "/auth/signin",
2050
- PROVIDERS: prefix + "/auth/providers",
2051
- SESSION: prefix + "/auth/session",
2052
- CSRF: prefix + "/auth/csrf",
2053
- CALLBACK: prefix + "/auth/callback",
2054
- SIGNOUT: prefix + "/auth/signout",
2055
- ERROR: prefix + "/auth/error",
2056
- ME: prefix + "/me",
2057
- USERS: prefix + "/users",
2058
- TENANTS: prefix + "/tenants",
2059
- TENANT_USER: prefix + "/tenants/{tenantId}/users/{userId}",
2060
- TENANT_USERS: prefix + "/tenants/{tenantId}/users",
2061
- SIGNUP: prefix + "/signup"
1213
+ var _proto = DBManager.prototype;
1214
+ _proto.makeId = function makeId(tenantId, userId) {
1215
+ if (isUUID(tenantId) && isUUID(userId)) {
1216
+ return tenantId + ":" + userId;
1217
+ }
1218
+ if (isUUID(tenantId)) {
1219
+ return "" + tenantId;
1220
+ }
1221
+ return 'base';
2062
1222
  };
1223
+ return DBManager;
1224
+ }();
1225
+
1226
+ var getServerId = function getServerId(config) {
1227
+ var cfg = new Config(config);
1228
+ return makeServerId(cfg);
1229
+ };
1230
+ var makeServerId = function makeServerId(config) {
1231
+ return Buffer.from(JSON.stringify(config), 'utf8').toString('base64');
2063
1232
  };
2064
1233
 
2065
1234
  var NILEDB_API_URL = process.env.NILEDB_API_URL;
@@ -2103,40 +1272,56 @@ function urlMatches(requestUrl, route) {
2103
1272
  }
2104
1273
 
2105
1274
  var _excluded = ["request"];
2106
- // const { log } = console;
2107
- function request(_x, _x2) {
1275
+ var _console$1 = console,
1276
+ log$1 = _console$1.log;
1277
+ function request(_x, _x2, _x3) {
2108
1278
  return _request.apply(this, arguments);
2109
1279
  }
2110
1280
  function _request() {
2111
- _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, _init) {
2112
- var request, init, requestUrl, updatedHeaders, params, _init$body, res;
1281
+ _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, _init, config) {
1282
+ var _params$method;
1283
+ var _Logger, info, error, request, init, requestUrl, updatedHeaders, params, _init$body, res, loggingRes;
2113
1284
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2114
1285
  while (1) switch (_context.prev = _context.next) {
2115
1286
  case 0:
1287
+ _Logger = Logger(_extends({}, config, {
1288
+ debug: config == null ? void 0 : config.debug
1289
+ }), '[REQUEST]', "[" + url + "]"), info = _Logger.info, error = _Logger.error;
2116
1290
  request = _init.request, init = _objectWithoutPropertiesLoose(_init, _excluded);
2117
1291
  requestUrl = new URL(request.url);
2118
- updatedHeaders = new Headers(request.headers); // updatedHeaders.delete('content-length');
2119
- // updatedHeaders.delete('transfer-encoding');
1292
+ updatedHeaders = new Headers(request.headers);
1293
+ updatedHeaders.set('host', requestUrl.host);
2120
1294
  updatedHeaders.set('niledb-origin', requestUrl.origin);
2121
1295
  updatedHeaders.set('niledb-creds', Buffer.from(process.env.NILEDB_USER + ":" + process.env.NILEDB_PASSWORD).toString('base64'));
2122
1296
  params = _extends({}, init, {
2123
1297
  headers: updatedHeaders
2124
1298
  });
2125
1299
  if (params.method === 'POST' || params.method === 'PUT') {
2126
- updatedHeaders.set('content-type', 'text/plain;charset=UTF-8');
2127
1300
  params.body = (_init$body = init.body) != null ? _init$body : request.body;
2128
1301
  // @ts-expect-error - its there
2129
1302
  params.duplex = 'half';
2130
1303
  }
2131
- // log(`[${params.method ?? 'GET'}]`, url);
2132
- _context.next = 9;
2133
- return fetch(url, _extends({}, params))["catch"](function () {
2134
- // log('An error has occurred in the fetch', e);
1304
+ log$1("[" + ((_params$method = params.method) != null ? _params$method : 'GET') + "]", url);
1305
+ _context.next = 12;
1306
+ return fetch(url, _extends({}, params))["catch"](function (e) {
1307
+ error('An error has occurred in the fetch', e);
1308
+ return new Response('An unexpected (most likely configuration) problem has occurred', {
1309
+ status: 500
1310
+ });
2135
1311
  });
2136
- case 9:
1312
+ case 12:
2137
1313
  res = _context.sent;
1314
+ loggingRes = typeof (res == null ? void 0 : res.clone) === 'function' ? res == null ? void 0 : res.clone() : null;
1315
+ _context.t0 = info;
1316
+ _context.t1 = res == null ? void 0 : res.status;
1317
+ _context.t2 = res == null ? void 0 : res.statusText;
1318
+ _context.next = 19;
1319
+ return loggingRes == null ? void 0 : loggingRes.text();
1320
+ case 19:
1321
+ _context.t3 = _context.sent;
1322
+ (0, _context.t0)('[Response]', _context.t1, _context.t2, _context.t3);
2138
1323
  return _context.abrupt("return", res);
2139
- case 11:
1324
+ case 22:
2140
1325
  case "end":
2141
1326
  return _context.stop();
2142
1327
  }
@@ -2180,15 +1365,17 @@ function GET$3(_x, _x2) {
2180
1365
  }
2181
1366
  function _GET$3() {
2182
1367
  _GET$3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(init, log) {
1368
+ var res;
2183
1369
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2184
1370
  while (1) switch (_context.prev = _context.next) {
2185
1371
  case 0:
2186
- log('[GET]');
1372
+ log('[GET]', url);
2187
1373
  _context.next = 3;
2188
1374
  return request(url, init);
2189
1375
  case 3:
2190
- return _context.abrupt("return", _context.sent);
2191
- case 4:
1376
+ res = _context.sent;
1377
+ return _context.abrupt("return", res);
1378
+ case 5:
2192
1379
  case "end":
2193
1380
  return _context.stop();
2194
1381
  }
@@ -2244,36 +1431,253 @@ function _auth() {
2244
1431
  while (1) switch (_context.prev = _context.next) {
2245
1432
  case 0:
2246
1433
  _Logger = Logger(_extends({}, config, {
2247
- debug: true
1434
+ debug: config.debug
2248
1435
  }), '[nileauth]'), info = _Logger.info;
2249
1436
  info('checking auth');
2250
1437
  sessionUrl = config.api.basePath + "/auth/session";
2251
1438
  info('using session', sessionUrl);
2252
- _context.next = 6;
1439
+ // handle the pass through with posts
1440
+ req.headers["delete"]('content-length');
1441
+ _context.next = 7;
2253
1442
  return request(sessionUrl, {
2254
1443
  request: req
2255
1444
  });
2256
- case 6:
1445
+ case 7:
2257
1446
  res = _context.sent;
2258
1447
  if (res) {
2259
- _context.next = 10;
1448
+ _context.next = 11;
1449
+ break;
1450
+ }
1451
+ info('no session found');
1452
+ return _context.abrupt("return", undefined);
1453
+ case 11:
1454
+ info('session active');
1455
+ _context.next = 14;
1456
+ return new Response(res.body).json();
1457
+ case 14:
1458
+ return _context.abrupt("return", _context.sent);
1459
+ case 15:
1460
+ case "end":
1461
+ return _context.stop();
1462
+ }
1463
+ }, _callee);
1464
+ }));
1465
+ return _auth.apply(this, arguments);
1466
+ }
1467
+
1468
+ var ResponseError = function ResponseError(body, init) {
1469
+ this.response = void 0;
1470
+ this.response = new Response(body, init);
1471
+ };
1472
+
1473
+ var X_NILE_TENANT = 'niledb-tenant-id';
1474
+ var X_NILE_USER_ID = 'niledb-user-id';
1475
+ function getTokenFromCookie(headers, cookieKey) {
1476
+ var _headers$get;
1477
+ var cookie = (_headers$get = headers.get('cookie')) == null ? void 0 : _headers$get.split('; ');
1478
+ var _cookies = {};
1479
+ if (cookie) {
1480
+ for (var _iterator = _createForOfIteratorHelperLoose(cookie), _step; !(_step = _iterator()).done;) {
1481
+ var parts = _step.value;
1482
+ var cookieParts = parts.split('=');
1483
+ var _cookie = cookieParts.slice(1).join('=');
1484
+ var name = cookieParts[0];
1485
+ _cookies[name] = _cookie;
1486
+ }
1487
+ }
1488
+ if (cookie) {
1489
+ for (var _iterator2 = _createForOfIteratorHelperLoose(cookie), _step2; !(_step2 = _iterator2()).done;) {
1490
+ var _parts = _step2.value;
1491
+ var _cookieParts = _parts.split('=');
1492
+ var _cookie2 = _cookieParts.slice(1).join('=');
1493
+ var _name = _cookieParts[0];
1494
+ _cookies[_name] = _cookie2;
1495
+ }
1496
+ }
1497
+ if (cookieKey) {
1498
+ return _cookies[cookieKey];
1499
+ }
1500
+ return null;
1501
+ }
1502
+ function getTenantFromHttp(headers, config) {
1503
+ var _ref;
1504
+ var cookieTenant = getTokenFromCookie(headers, X_NILE_TENANT);
1505
+ return (_ref = cookieTenant != null ? cookieTenant : headers == null ? void 0 : headers.get(X_NILE_TENANT)) != null ? _ref : config == null ? void 0 : config.tenantId;
1506
+ }
1507
+ function getUserFromHttp(headers, config) {
1508
+ var _headers$get2;
1509
+ var token = getTokenFromCookie(headers, config.api.cookieKey);
1510
+ if (token) {
1511
+ var jwt = jose.decodeJwt(token);
1512
+ return jwt.sub;
1513
+ }
1514
+ return (_headers$get2 = headers == null ? void 0 : headers.get(X_NILE_USER_ID)) != null ? _headers$get2 : config.userId;
1515
+ }
1516
+ function _fetch(_x, _x2, _x3) {
1517
+ return _fetch2.apply(this, arguments);
1518
+ }
1519
+ function _fetch2() {
1520
+ _fetch2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
1521
+ var _config$api, _config$api2, _response$status;
1522
+ var _Logger, info, error, url, cookieKey, headers, basicHeaders, creds, authHeader, token, tenantId, userId, useableUrl, response, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1523
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1524
+ while (1) switch (_context.prev = _context.next) {
1525
+ case 0:
1526
+ _Logger = Logger(config, '[server]'), info = _Logger.info, error = _Logger.error;
1527
+ url = "" + ((_config$api = config.api) == null ? void 0 : _config$api.basePath) + path;
1528
+ cookieKey = (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey;
1529
+ headers = new Headers(opts == null ? void 0 : opts.headers);
1530
+ basicHeaders = new Headers(opts == null ? void 0 : opts.headers);
1531
+ basicHeaders.set('content-type', 'application/json; charset=utf-8');
1532
+ creds = Buffer.from(process.env.NILEDB_USER + ":" + process.env.NILEDB_PASSWORD, 'utf8').toString('base64');
1533
+ basicHeaders.set('niledb-creds', creds);
1534
+ authHeader = headers.get('Authorization');
1535
+ if (!authHeader) {
1536
+ token = getTokenFromCookie(headers, cookieKey);
1537
+ if (token) {
1538
+ basicHeaders.set('Authorization', "Bearer " + token);
1539
+ } else if (getToken({
1540
+ config: config
1541
+ })) {
1542
+ basicHeaders.set('Authorization', "Bearer " + getToken({
1543
+ config: config
1544
+ }));
1545
+ }
1546
+ }
1547
+ tenantId = getTenantFromHttp(headers, config);
1548
+ updateTenantId(tenantId);
1549
+ userId = getUserFromHttp(headers, config);
1550
+ updateUserId(userId);
1551
+ if (!(url.includes('{tenantId}') && !tenantId)) {
1552
+ _context.next = 16;
1553
+ break;
1554
+ }
1555
+ return _context.abrupt("return", new ResponseError('tenantId is not set for request', {
1556
+ status: 400
1557
+ }));
1558
+ case 16:
1559
+ useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId))).replace('{userId}', encodeURIComponent(String(userId)));
1560
+ info('[fetch]', useableUrl);
1561
+ _context.next = 20;
1562
+ return fetch(useableUrl, _extends({}, opts, {
1563
+ headers: basicHeaders
1564
+ }))["catch"](function (e) {
1565
+ error('[fetch]', '[response]', e);
1566
+ });
1567
+ case 20:
1568
+ response = _context.sent;
1569
+ if (!(response && response.status >= 200 && response.status < 300)) {
1570
+ _context.next = 39;
1571
+ break;
1572
+ }
1573
+ if (!(typeof response.clone === 'function')) {
1574
+ _context.next = 38;
1575
+ break;
1576
+ }
1577
+ _context.prev = 23;
1578
+ _context.t0 = info;
1579
+ _context.next = 27;
1580
+ return response.clone().json();
1581
+ case 27:
1582
+ _context.t1 = _context.sent;
1583
+ (0, _context.t0)('[fetch]', '[response]', _context.t1);
1584
+ _context.next = 38;
1585
+ break;
1586
+ case 31:
1587
+ _context.prev = 31;
1588
+ _context.t2 = _context["catch"](23);
1589
+ _context.t3 = info;
1590
+ _context.next = 36;
1591
+ return response.clone().text();
1592
+ case 36:
1593
+ _context.t4 = _context.sent;
1594
+ (0, _context.t3)('[fetch]', '[response]', _context.t4);
1595
+ case 38:
1596
+ return _context.abrupt("return", response);
1597
+ case 39:
1598
+ if (!((response == null ? void 0 : response.status) === 401)) {
1599
+ _context.next = 41;
1600
+ break;
1601
+ }
1602
+ return _context.abrupt("return", new ResponseError('Unauthorized', {
1603
+ status: 401
1604
+ }));
1605
+ case 41:
1606
+ if (!((response == null ? void 0 : response.status) === 405)) {
1607
+ _context.next = 43;
1608
+ break;
1609
+ }
1610
+ return _context.abrupt("return", new ResponseError('Method not allowed', {
1611
+ status: 405
1612
+ }));
1613
+ case 43:
1614
+ errorHandler = typeof (response == null ? void 0 : response.clone) === 'function' ? response.clone() : null;
1615
+ msg = '';
1616
+ _context.prev = 45;
1617
+ _context.next = 48;
1618
+ return response == null ? void 0 : response.json();
1619
+ case 48:
1620
+ res = _context.sent;
1621
+ _context.next = 59;
1622
+ break;
1623
+ case 51:
1624
+ _context.prev = 51;
1625
+ _context.t5 = _context["catch"](45);
1626
+ if (!errorHandler) {
1627
+ _context.next = 58;
1628
+ break;
1629
+ }
1630
+ _context.next = 56;
1631
+ return errorHandler.text();
1632
+ case 56:
1633
+ msg = _context.sent;
1634
+ if (msg) {
1635
+ error('[fetch]', '[response]', "[status: " + errorHandler.status + "]", msg);
1636
+ }
1637
+ case 58:
1638
+ if (!msg) {
1639
+ error('[fetch]', '[response]', _context.t5);
1640
+ }
1641
+ case 59:
1642
+ if (!msg) {
1643
+ _context.next = 61;
2260
1644
  break;
2261
1645
  }
2262
- info('no session found');
2263
- return _context.abrupt("return", undefined);
2264
- case 10:
2265
- info('session active');
2266
- _context.next = 13;
2267
- return new Response(res.body).json();
2268
- case 13:
2269
- return _context.abrupt("return", _context.sent);
2270
- case 14:
1646
+ return _context.abrupt("return", new ResponseError(msg, {
1647
+ status: errorHandler == null ? void 0 : errorHandler.status
1648
+ }));
1649
+ case 61:
1650
+ if (!(res && 'message' in res)) {
1651
+ _context.next = 65;
1652
+ break;
1653
+ }
1654
+ _res = res, message = _res.message;
1655
+ error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", message);
1656
+ return _context.abrupt("return", new ResponseError(message, {
1657
+ status: 400
1658
+ }));
1659
+ case 65:
1660
+ if (!(res && 'errors' in res)) {
1661
+ _context.next = 69;
1662
+ break;
1663
+ }
1664
+ _res2 = res, _res2$errors = _res2.errors, _message = _res2$errors[0];
1665
+ error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", _message);
1666
+ return _context.abrupt("return", new ResponseError(_message, {
1667
+ status: 400
1668
+ }));
1669
+ case 69:
1670
+ error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", 'UNHANDLED ERROR');
1671
+ return _context.abrupt("return", new ResponseError(null, {
1672
+ status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
1673
+ }));
1674
+ case 71:
2271
1675
  case "end":
2272
1676
  return _context.stop();
2273
1677
  }
2274
- }, _callee);
1678
+ }, _callee, null, [[23, 31], [45, 51]]);
2275
1679
  }));
2276
- return _auth.apply(this, arguments);
1680
+ return _fetch2.apply(this, arguments);
2277
1681
  }
2278
1682
 
2279
1683
  /**
@@ -2383,7 +1787,7 @@ function _POST$2() {
2383
1787
  * - users
2384
1788
  * summary: lists users in the tenant
2385
1789
  * description: Returns information about the users within the tenant
2386
- * provided. You can also pass the a `niledb-tenantId` in the header or in a cookie.
1790
+ * provided. You can also pass the a `niledb-tenant-id` in the header or in a cookie.
2387
1791
  * operationId: listUsers
2388
1792
  * parameters:
2389
1793
  * - name: tenantId
@@ -2710,7 +2114,7 @@ function _POST$1() {
2710
2114
 
2711
2115
  /**
2712
2116
  * @swagger
2713
- * /api/tenants/{tenantId}/users/{userId}:
2117
+ * /api/tenants/{tenantId}/users/{email}:
2714
2118
  * delete:
2715
2119
  * tags:
2716
2120
  * - tenants
@@ -2724,7 +2128,7 @@ function _POST$1() {
2724
2128
  * required: true
2725
2129
  * schema:
2726
2130
  * type: string
2727
- * - name: userId
2131
+ * - name: email
2728
2132
  * in: path
2729
2133
  * required: true
2730
2134
  * schema:
@@ -2754,13 +2158,16 @@ function _DELETE$1() {
2754
2158
  }));
2755
2159
  case 4:
2756
2160
  init.method = 'DELETE';
2161
+ init.body = JSON.stringify({
2162
+ email: userId
2163
+ });
2757
2164
  url = "" + apiRoutes.TENANT_USER(tenantId, userId);
2758
2165
  log('[DELETE]', url);
2759
- _context.next = 9;
2166
+ _context.next = 10;
2760
2167
  return request(url, init);
2761
- case 9:
2762
- return _context.abrupt("return", _context.sent);
2763
2168
  case 10:
2169
+ return _context.abrupt("return", _context.sent);
2170
+ case 11:
2764
2171
  case "end":
2765
2172
  return _context.stop();
2766
2173
  }
@@ -2785,8 +2192,7 @@ function _DELETE$1() {
2785
2192
  * type: string
2786
2193
  * requestBody:
2787
2194
  * description: |
2788
- * The email and password combination the user will use to authenticate.
2789
- * The `name` is optional; if provided it will be recorded in the `users` table.
2195
+ * The email of the user you want to add to a tenant.
2790
2196
  * content:
2791
2197
  * application/json:
2792
2198
  * schema:
@@ -2945,17 +2351,21 @@ function GET(_x, _x2, _x3) {
2945
2351
  }
2946
2352
  function _GET() {
2947
2353
  _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(session, init, log) {
2948
- var url;
2354
+ var url, res;
2949
2355
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2950
2356
  while (1) switch (_context.prev = _context.next) {
2951
2357
  case 0:
2952
2358
  url = "" + apiRoutes.USER_TENANTS(session.id);
2359
+ if (typeof session === 'object' && 'user' in session && session.user) {
2360
+ url = "" + apiRoutes.USER_TENANTS(session.user.id);
2361
+ }
2953
2362
  log('[GET]', url);
2954
- _context.next = 4;
2363
+ _context.next = 5;
2955
2364
  return request(url, init);
2956
- case 4:
2957
- return _context.abrupt("return", _context.sent);
2958
2365
  case 5:
2366
+ res = _context.sent;
2367
+ return _context.abrupt("return", res);
2368
+ case 7:
2959
2369
  case "end":
2960
2370
  return _context.stop();
2961
2371
  }
@@ -3121,107 +2531,273 @@ function _route$7() {
3121
2531
  return _context.abrupt("return", new Response(null, {
3122
2532
  status: 401
3123
2533
  }));
3124
- case 7:
3125
- _context.t0 = request.method;
3126
- _context.next = _context.t0 === 'GET' ? 10 : _context.t0 === 'POST' ? 13 : _context.t0 === 'DELETE' ? 16 : 19;
3127
- break;
2534
+ case 7:
2535
+ _context.t0 = request.method;
2536
+ _context.next = _context.t0 === 'GET' ? 10 : _context.t0 === 'POST' ? 13 : _context.t0 === 'DELETE' ? 16 : 19;
2537
+ break;
2538
+ case 10:
2539
+ _context.next = 12;
2540
+ return GET(session, {
2541
+ request: request
2542
+ }, info);
2543
+ case 12:
2544
+ return _context.abrupt("return", _context.sent);
2545
+ case 13:
2546
+ _context.next = 15;
2547
+ return POST(session, {
2548
+ request: request
2549
+ }, info);
2550
+ case 15:
2551
+ return _context.abrupt("return", _context.sent);
2552
+ case 16:
2553
+ _context.next = 18;
2554
+ return DELETE(session, {
2555
+ request: request
2556
+ }, info);
2557
+ case 18:
2558
+ return _context.abrupt("return", _context.sent);
2559
+ case 19:
2560
+ return _context.abrupt("return", new Response('method not allowed', {
2561
+ status: 405
2562
+ }));
2563
+ case 20:
2564
+ case "end":
2565
+ return _context.stop();
2566
+ }
2567
+ }, _callee);
2568
+ }));
2569
+ return _route$7.apply(this, arguments);
2570
+ }
2571
+ function matches$7(configRoutes, request) {
2572
+ return urlMatches(request.url, configRoutes[key$3]);
2573
+ }
2574
+
2575
+ var proxyRoutes = {
2576
+ SIGNIN: /*#__PURE__*/makeRestUrl('/auth/signin'),
2577
+ PROVIDERS: /*#__PURE__*/makeRestUrl('/auth/providers'),
2578
+ SESSION: /*#__PURE__*/makeRestUrl('/auth/session'),
2579
+ CSRF: /*#__PURE__*/makeRestUrl('/auth/csrf'),
2580
+ CALLBACK: /*#__PURE__*/makeRestUrl('/auth/callback'),
2581
+ SIGNOUT: /*#__PURE__*/makeRestUrl('/auth/signout'),
2582
+ ERROR: /*#__PURE__*/makeRestUrl('/auth/error')
2583
+ };
2584
+
2585
+ var key$2 = 'SIGNIN';
2586
+ function route$6(_x) {
2587
+ return _route$6.apply(this, arguments);
2588
+ }
2589
+ function _route$6() {
2590
+ _route$6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2591
+ var url, init, _URL$pathname$split$r, provider, passThroughUrl, params, res;
2592
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2593
+ while (1) switch (_context.prev = _context.next) {
2594
+ case 0:
2595
+ url = proxyRoutes[key$2];
2596
+ init = {
2597
+ method: req.method,
2598
+ headers: req.headers
2599
+ };
2600
+ if (req.method === 'POST') {
2601
+ if (typeof req.clone === 'function') {
2602
+ init.body = req.clone().body;
2603
+ }
2604
+ _URL$pathname$split$r = new URL(req.url).pathname.split('/').reverse(), provider = _URL$pathname$split$r[0];
2605
+ url = proxyRoutes[key$2] + "/" + provider;
2606
+ }
2607
+ passThroughUrl = new URL(req.url);
2608
+ params = new URLSearchParams(passThroughUrl.search);
2609
+ url = "" + url + (params.toString() !== '' ? "?" + params.toString() : '');
2610
+ _context.next = 8;
2611
+ return request(url, _extends({}, init, {
2612
+ request: req
2613
+ }));
2614
+ case 8:
2615
+ res = _context.sent;
2616
+ return _context.abrupt("return", res);
2617
+ case 10:
2618
+ case "end":
2619
+ return _context.stop();
2620
+ }
2621
+ }, _callee);
2622
+ }));
2623
+ return _route$6.apply(this, arguments);
2624
+ }
2625
+ function matches$6(configRoutes, request) {
2626
+ return urlMatches(request.url, configRoutes[key$2]);
2627
+ }
2628
+
2629
+ function route$5(_x, _x2) {
2630
+ return _route$5.apply(this, arguments);
2631
+ }
2632
+ function _route$5() {
2633
+ _route$5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2634
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2635
+ while (1) switch (_context.prev = _context.next) {
2636
+ case 0:
2637
+ return _context.abrupt("return", request(proxyRoutes.SESSION, {
2638
+ method: req.method,
2639
+ request: req
2640
+ }, config));
2641
+ case 1:
2642
+ case "end":
2643
+ return _context.stop();
2644
+ }
2645
+ }, _callee);
2646
+ }));
2647
+ return _route$5.apply(this, arguments);
2648
+ }
2649
+ function matches$5(configRoutes, request) {
2650
+ return urlMatches(request.url, configRoutes.SESSION);
2651
+ }
2652
+
2653
+ function route$4(_x) {
2654
+ return _route$4.apply(this, arguments);
2655
+ }
2656
+ function _route$4() {
2657
+ _route$4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2658
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2659
+ while (1) switch (_context.prev = _context.next) {
2660
+ case 0:
2661
+ return _context.abrupt("return", request(proxyRoutes.PROVIDERS, {
2662
+ method: req.method,
2663
+ request: req
2664
+ }));
2665
+ case 1:
2666
+ case "end":
2667
+ return _context.stop();
2668
+ }
2669
+ }, _callee);
2670
+ }));
2671
+ return _route$4.apply(this, arguments);
2672
+ }
2673
+ function matches$4(configRoutes, request) {
2674
+ return urlMatches(request.url, configRoutes.PROVIDERS);
2675
+ }
2676
+
2677
+ function route$3(_x) {
2678
+ return _route$3.apply(this, arguments);
2679
+ }
2680
+ function _route$3() {
2681
+ _route$3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2682
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2683
+ while (1) switch (_context.prev = _context.next) {
2684
+ case 0:
2685
+ return _context.abrupt("return", request(proxyRoutes.CSRF, {
2686
+ method: req.method,
2687
+ request: req
2688
+ }));
2689
+ case 1:
2690
+ case "end":
2691
+ return _context.stop();
2692
+ }
2693
+ }, _callee);
2694
+ }));
2695
+ return _route$3.apply(this, arguments);
2696
+ }
2697
+ function matches$3(configRoutes, request) {
2698
+ return urlMatches(request.url, configRoutes.CSRF);
2699
+ }
2700
+
2701
+ var _console = console,
2702
+ log = _console.log,
2703
+ error = _console.error;
2704
+ function route$2(_x) {
2705
+ return _route$2.apply(this, arguments);
2706
+ }
2707
+ function _route$2() {
2708
+ _route$2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1) {
2709
+ var _URL$pathname$split$r, provider, passThroughUrl, params, url, res, location;
2710
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2711
+ while (1) switch (_context.prev = _context.next) {
2712
+ case 0:
2713
+ _URL$pathname$split$r = new URL(request$1.url).pathname.split('/').reverse(), provider = _URL$pathname$split$r[0];
2714
+ passThroughUrl = new URL(request$1.url);
2715
+ params = new URLSearchParams(passThroughUrl.search);
2716
+ url = proxyRoutes.CALLBACK + "/" + provider + (params.toString() !== '' ? "?" + params.toString() : '');
2717
+ _context.next = 6;
2718
+ return request(url, {
2719
+ request: request$1,
2720
+ method: request$1.method
2721
+ })["catch"](function (e) {
2722
+ error('an error has occurred in callback');
2723
+ log(e);
2724
+ });
2725
+ case 6:
2726
+ res = _context.sent;
2727
+ location = res == null ? void 0 : res.headers.get('location');
2728
+ if (!location) {
2729
+ _context.next = 10;
2730
+ break;
2731
+ }
2732
+ return _context.abrupt("return", new Response(res == null ? void 0 : res.body, {
2733
+ status: 302,
2734
+ headers: res == null ? void 0 : res.headers
2735
+ }));
3128
2736
  case 10:
3129
- _context.next = 12;
3130
- return GET(session, {
3131
- request: request
3132
- }, info);
3133
- case 12:
3134
- return _context.abrupt("return", _context.sent);
3135
- case 13:
3136
- _context.next = 15;
3137
- return POST(session, {
3138
- request: request
3139
- }, info);
3140
- case 15:
3141
- return _context.abrupt("return", _context.sent);
3142
- case 16:
3143
- _context.next = 18;
3144
- return DELETE(session, {
3145
- request: request
3146
- }, info);
3147
- case 18:
3148
- return _context.abrupt("return", _context.sent);
3149
- case 19:
3150
- return _context.abrupt("return", new Response('method not allowed', {
3151
- status: 405
2737
+ return _context.abrupt("return", new Response(res == null ? void 0 : res.body, {
2738
+ status: res == null ? void 0 : res.status,
2739
+ headers: res == null ? void 0 : res.headers
3152
2740
  }));
3153
- case 20:
2741
+ case 11:
3154
2742
  case "end":
3155
2743
  return _context.stop();
3156
2744
  }
3157
2745
  }, _callee);
3158
2746
  }));
3159
- return _route$7.apply(this, arguments);
2747
+ return _route$2.apply(this, arguments);
3160
2748
  }
3161
- function matches$7(configRoutes, request) {
3162
- return urlMatches(request.url, configRoutes[key$3]);
2749
+ function matches$2(configRoutes, request) {
2750
+ return urlMatches(request.url, configRoutes.CALLBACK);
3163
2751
  }
3164
2752
 
3165
- var proxyRoutes = {
3166
- SIGNIN: /*#__PURE__*/makeRestUrl('/auth/signin'),
3167
- PROVIDERS: /*#__PURE__*/makeRestUrl('/auth/providers'),
3168
- SESSION: /*#__PURE__*/makeRestUrl('/auth/session'),
3169
- CSRF: /*#__PURE__*/makeRestUrl('/auth/csrf'),
3170
- CALLBACK: /*#__PURE__*/makeRestUrl('/auth/callback'),
3171
- SIGNOUT: /*#__PURE__*/makeRestUrl('/auth/signout'),
3172
- ERROR: /*#__PURE__*/makeRestUrl('/auth/error')
3173
- };
3174
-
3175
- var key$2 = 'SIGNIN';
3176
- function route$6(_x) {
3177
- return _route$6.apply(this, arguments);
2753
+ var key$1 = 'SIGNOUT';
2754
+ function route$1(_x) {
2755
+ return _route$1.apply(this, arguments);
3178
2756
  }
3179
- function _route$6() {
3180
- _route$6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1) {
3181
- var url, init, _URL$pathname$split$r, provider, passThroughUrl, params, res;
2757
+ function _route$1() {
2758
+ _route$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1) {
2759
+ var url, init, _URL$pathname$split$r, provider, res;
3182
2760
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3183
2761
  while (1) switch (_context.prev = _context.next) {
3184
2762
  case 0:
3185
- url = proxyRoutes[key$2];
2763
+ url = proxyRoutes[key$1];
3186
2764
  init = {
3187
2765
  method: request$1.method
3188
2766
  };
3189
2767
  if (request$1.method === 'POST') {
3190
2768
  init.body = request$1.body;
3191
2769
  _URL$pathname$split$r = new URL(request$1.url).pathname.split('/').reverse(), provider = _URL$pathname$split$r[0];
3192
- url = proxyRoutes[key$2] + "/" + provider;
2770
+ url = proxyRoutes[key$1] + "/" + provider;
3193
2771
  }
3194
- passThroughUrl = new URL(request$1.url);
3195
- params = new URLSearchParams(passThroughUrl.search);
3196
- url = "" + url + (params.toString() !== '' ? "?" + params.toString() : '');
3197
- _context.next = 8;
2772
+ _context.next = 5;
3198
2773
  return request(url, _extends({}, init, {
3199
2774
  request: request$1
3200
2775
  }));
3201
- case 8:
2776
+ case 5:
3202
2777
  res = _context.sent;
3203
2778
  return _context.abrupt("return", res);
3204
- case 10:
2779
+ case 7:
3205
2780
  case "end":
3206
2781
  return _context.stop();
3207
2782
  }
3208
2783
  }, _callee);
3209
2784
  }));
3210
- return _route$6.apply(this, arguments);
2785
+ return _route$1.apply(this, arguments);
3211
2786
  }
3212
- function matches$6(configRoutes, request) {
3213
- return urlMatches(request.url, configRoutes[key$2]);
2787
+ function matches$1(configRoutes, request) {
2788
+ return urlMatches(request.url, configRoutes[key$1]);
3214
2789
  }
3215
2790
 
3216
- function route$5(_x) {
3217
- return _route$5.apply(this, arguments);
2791
+ var key = 'ERROR';
2792
+ function route(_x) {
2793
+ return _route.apply(this, arguments);
3218
2794
  }
3219
- function _route$5() {
3220
- _route$5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2795
+ function _route() {
2796
+ _route = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3221
2797
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3222
2798
  while (1) switch (_context.prev = _context.next) {
3223
2799
  case 0:
3224
- return _context.abrupt("return", request(proxyRoutes.SESSION, {
2800
+ return _context.abrupt("return", request(proxyRoutes[key], {
3225
2801
  method: req.method,
3226
2802
  request: req
3227
2803
  }));
@@ -3231,425 +2807,976 @@ function _route$5() {
3231
2807
  }
3232
2808
  }, _callee);
3233
2809
  }));
3234
- return _route$5.apply(this, arguments);
2810
+ return _route.apply(this, arguments);
3235
2811
  }
3236
- function matches$5(configRoutes, request) {
3237
- return urlMatches(request.url, configRoutes.SESSION);
2812
+ function matches(configRoutes, request) {
2813
+ return urlMatches(request.url, configRoutes[key]);
3238
2814
  }
3239
2815
 
3240
- function route$4(_x) {
3241
- return _route$4.apply(this, arguments);
3242
- }
3243
- function _route$4() {
3244
- _route$4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3245
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3246
- while (1) switch (_context.prev = _context.next) {
3247
- case 0:
3248
- return _context.abrupt("return", request(proxyRoutes.PROVIDERS, {
3249
- method: req.method,
3250
- request: req
3251
- }));
3252
- case 1:
3253
- case "end":
3254
- return _context.stop();
3255
- }
3256
- }, _callee);
3257
- }));
3258
- return _route$4.apply(this, arguments);
2816
+ function GETTER(configRoutes, config) {
2817
+ return /*#__PURE__*/function () {
2818
+ var _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2819
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2820
+ while (1) switch (_context.prev = _context.next) {
2821
+ case 0:
2822
+ if (!matches$a(configRoutes, req)) {
2823
+ _context.next = 2;
2824
+ break;
2825
+ }
2826
+ return _context.abrupt("return", route$a(req, config));
2827
+ case 2:
2828
+ if (!matches$9(configRoutes, req)) {
2829
+ _context.next = 4;
2830
+ break;
2831
+ }
2832
+ return _context.abrupt("return", route$9(req, config));
2833
+ case 4:
2834
+ if (!matches$8(configRoutes, req)) {
2835
+ _context.next = 6;
2836
+ break;
2837
+ }
2838
+ return _context.abrupt("return", route$8(req, config));
2839
+ case 6:
2840
+ if (!matches$7(configRoutes, req)) {
2841
+ _context.next = 8;
2842
+ break;
2843
+ }
2844
+ return _context.abrupt("return", route$7(req, config));
2845
+ case 8:
2846
+ if (!matches$5(configRoutes, req)) {
2847
+ _context.next = 10;
2848
+ break;
2849
+ }
2850
+ return _context.abrupt("return", route$5(req, config));
2851
+ case 10:
2852
+ if (!matches$6(configRoutes, req)) {
2853
+ _context.next = 12;
2854
+ break;
2855
+ }
2856
+ return _context.abrupt("return", route$6(req));
2857
+ case 12:
2858
+ if (!matches$4(configRoutes, req)) {
2859
+ _context.next = 14;
2860
+ break;
2861
+ }
2862
+ return _context.abrupt("return", route$4(req));
2863
+ case 14:
2864
+ if (!matches$3(configRoutes, req)) {
2865
+ _context.next = 16;
2866
+ break;
2867
+ }
2868
+ return _context.abrupt("return", route$3(req));
2869
+ case 16:
2870
+ if (!matches$2(configRoutes, req)) {
2871
+ _context.next = 18;
2872
+ break;
2873
+ }
2874
+ return _context.abrupt("return", route$2(req));
2875
+ case 18:
2876
+ if (!matches$1(configRoutes, req)) {
2877
+ _context.next = 20;
2878
+ break;
2879
+ }
2880
+ return _context.abrupt("return", route$1(req));
2881
+ case 20:
2882
+ if (!matches(configRoutes, req)) {
2883
+ _context.next = 22;
2884
+ break;
2885
+ }
2886
+ return _context.abrupt("return", route(req));
2887
+ case 22:
2888
+ return _context.abrupt("return", new Response(null, {
2889
+ status: 404
2890
+ }));
2891
+ case 23:
2892
+ case "end":
2893
+ return _context.stop();
2894
+ }
2895
+ }, _callee);
2896
+ }));
2897
+ function GET(_x) {
2898
+ return _GET.apply(this, arguments);
2899
+ }
2900
+ return GET;
2901
+ }();
3259
2902
  }
3260
- function matches$4(configRoutes, request) {
3261
- return urlMatches(request.url, configRoutes.PROVIDERS);
2903
+
2904
+ function POSTER(configRoutes, config) {
2905
+ return /*#__PURE__*/function () {
2906
+ var _POST = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2907
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2908
+ while (1) switch (_context.prev = _context.next) {
2909
+ case 0:
2910
+ if (!matches$8(configRoutes, req)) {
2911
+ _context.next = 2;
2912
+ break;
2913
+ }
2914
+ return _context.abrupt("return", route$8(req, config));
2915
+ case 2:
2916
+ if (!matches$9(configRoutes, req)) {
2917
+ _context.next = 4;
2918
+ break;
2919
+ }
2920
+ return _context.abrupt("return", route$9(req, config));
2921
+ case 4:
2922
+ if (!matches$7(configRoutes, req)) {
2923
+ _context.next = 6;
2924
+ break;
2925
+ }
2926
+ return _context.abrupt("return", route$7(req, config));
2927
+ case 6:
2928
+ if (!matches$5(configRoutes, req)) {
2929
+ _context.next = 8;
2930
+ break;
2931
+ }
2932
+ return _context.abrupt("return", route$5(req, config));
2933
+ case 8:
2934
+ if (!matches$6(configRoutes, req)) {
2935
+ _context.next = 10;
2936
+ break;
2937
+ }
2938
+ return _context.abrupt("return", route$6(req));
2939
+ case 10:
2940
+ if (!matches$4(configRoutes, req)) {
2941
+ _context.next = 12;
2942
+ break;
2943
+ }
2944
+ return _context.abrupt("return", route$4(req));
2945
+ case 12:
2946
+ if (!matches$3(configRoutes, req)) {
2947
+ _context.next = 14;
2948
+ break;
2949
+ }
2950
+ return _context.abrupt("return", route$3(req));
2951
+ case 14:
2952
+ if (!matches$2(configRoutes, req)) {
2953
+ _context.next = 16;
2954
+ break;
2955
+ }
2956
+ return _context.abrupt("return", route$2(req));
2957
+ case 16:
2958
+ if (!matches$1(configRoutes, req)) {
2959
+ _context.next = 18;
2960
+ break;
2961
+ }
2962
+ return _context.abrupt("return", route$1(req));
2963
+ case 18:
2964
+ return _context.abrupt("return", new Response(null, {
2965
+ status: 404
2966
+ }));
2967
+ case 19:
2968
+ case "end":
2969
+ return _context.stop();
2970
+ }
2971
+ }, _callee);
2972
+ }));
2973
+ function POST(_x) {
2974
+ return _POST.apply(this, arguments);
2975
+ }
2976
+ return POST;
2977
+ }();
3262
2978
  }
3263
2979
 
3264
- function route$3(_x) {
3265
- return _route$3.apply(this, arguments);
3266
- }
3267
- function _route$3() {
3268
- _route$3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3269
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3270
- while (1) switch (_context.prev = _context.next) {
3271
- case 0:
3272
- return _context.abrupt("return", request(proxyRoutes.CSRF, {
3273
- method: req.method,
3274
- request: req
3275
- }));
3276
- case 1:
3277
- case "end":
3278
- return _context.stop();
3279
- }
3280
- }, _callee);
3281
- }));
3282
- return _route$3.apply(this, arguments);
3283
- }
3284
- function matches$3(configRoutes, request) {
3285
- return urlMatches(request.url, configRoutes.CSRF);
2980
+ function DELETER(configRoutes, config) {
2981
+ return /*#__PURE__*/function () {
2982
+ var _DELETE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2983
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2984
+ while (1) switch (_context.prev = _context.next) {
2985
+ case 0:
2986
+ if (!matches$8(configRoutes, req)) {
2987
+ _context.next = 2;
2988
+ break;
2989
+ }
2990
+ return _context.abrupt("return", route$8(req, config));
2991
+ case 2:
2992
+ if (!matches$7(configRoutes, req)) {
2993
+ _context.next = 4;
2994
+ break;
2995
+ }
2996
+ return _context.abrupt("return", route$7(req, config));
2997
+ case 4:
2998
+ return _context.abrupt("return", new Response(null, {
2999
+ status: 404
3000
+ }));
3001
+ case 5:
3002
+ case "end":
3003
+ return _context.stop();
3004
+ }
3005
+ }, _callee);
3006
+ }));
3007
+ function DELETE(_x) {
3008
+ return _DELETE.apply(this, arguments);
3009
+ }
3010
+ return DELETE;
3011
+ }();
3286
3012
  }
3287
3013
 
3288
- var _console = console,
3289
- log = _console.log,
3290
- error = _console.error;
3291
- function route$2(_x) {
3292
- return _route$2.apply(this, arguments);
3293
- }
3294
- function _route$2() {
3295
- _route$2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1) {
3296
- var _URL$pathname$split$r, provider, passThroughUrl, params, url, res, location;
3297
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3298
- while (1) switch (_context.prev = _context.next) {
3299
- case 0:
3300
- _URL$pathname$split$r = new URL(request$1.url).pathname.split('/').reverse(), provider = _URL$pathname$split$r[0];
3301
- passThroughUrl = new URL(request$1.url);
3302
- params = new URLSearchParams(passThroughUrl.search);
3303
- url = proxyRoutes.CALLBACK + "/" + provider + (params.toString() !== '' ? "?" + params.toString() : '');
3304
- _context.next = 6;
3305
- return request(url, {
3306
- request: request$1,
3307
- method: request$1.method
3308
- })["catch"](function (e) {
3309
- error('an error has occurred in callback');
3310
- log(e);
3311
- });
3312
- case 6:
3313
- res = _context.sent;
3314
- location = res == null ? void 0 : res.headers.get('location');
3315
- if (!location) {
3316
- _context.next = 10;
3317
- break;
3318
- }
3319
- return _context.abrupt("return", new Response(res == null ? void 0 : res.body, {
3320
- status: 302,
3321
- headers: res == null ? void 0 : res.headers
3322
- }));
3323
- case 10:
3324
- return _context.abrupt("return", new Response(res == null ? void 0 : res.body, {
3325
- status: res == null ? void 0 : res.status,
3326
- headers: res == null ? void 0 : res.headers
3327
- }));
3328
- case 11:
3329
- case "end":
3330
- return _context.stop();
3331
- }
3332
- }, _callee);
3333
- }));
3334
- return _route$2.apply(this, arguments);
3335
- }
3336
- function matches$2(configRoutes, request) {
3337
- return urlMatches(request.url, configRoutes.CALLBACK);
3014
+ function PUTER(configRoutes, config) {
3015
+ return /*#__PURE__*/function () {
3016
+ var _PUT = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3017
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
3018
+ while (1) switch (_context.prev = _context.next) {
3019
+ case 0:
3020
+ if (!matches$8(configRoutes, req)) {
3021
+ _context.next = 2;
3022
+ break;
3023
+ }
3024
+ return _context.abrupt("return", route$8(req, config));
3025
+ case 2:
3026
+ if (!matches$9(configRoutes, req)) {
3027
+ _context.next = 4;
3028
+ break;
3029
+ }
3030
+ return _context.abrupt("return", route$9(req, config));
3031
+ case 4:
3032
+ if (!matches$7(configRoutes, req)) {
3033
+ _context.next = 6;
3034
+ break;
3035
+ }
3036
+ return _context.abrupt("return", route$7(req, config));
3037
+ case 6:
3038
+ return _context.abrupt("return", new Response(null, {
3039
+ status: 404
3040
+ }));
3041
+ case 7:
3042
+ case "end":
3043
+ return _context.stop();
3044
+ }
3045
+ }, _callee);
3046
+ }));
3047
+ function PUT(_x) {
3048
+ return _PUT.apply(this, arguments);
3049
+ }
3050
+ return PUT;
3051
+ }();
3338
3052
  }
3339
3053
 
3340
- var key$1 = 'SIGNOUT';
3341
- function route$1(_x) {
3342
- return _route$1.apply(this, arguments);
3343
- }
3344
- function _route$1() {
3345
- _route$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1) {
3346
- var url, init, _URL$pathname$split$r, provider, res;
3347
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3348
- while (1) switch (_context.prev = _context.next) {
3349
- case 0:
3350
- url = proxyRoutes[key$1];
3351
- init = {
3352
- method: request$1.method
3353
- };
3354
- if (request$1.method === 'POST') {
3355
- init.body = request$1.body;
3356
- _URL$pathname$split$r = new URL(request$1.url).pathname.split('/').reverse(), provider = _URL$pathname$split$r[0];
3357
- url = proxyRoutes[key$1] + "/" + provider;
3358
- }
3359
- _context.next = 5;
3360
- return request(url, _extends({}, init, {
3361
- request: request$1
3362
- }));
3363
- case 5:
3364
- res = _context.sent;
3365
- return _context.abrupt("return", res);
3366
- case 7:
3367
- case "end":
3368
- return _context.stop();
3369
- }
3370
- }, _callee);
3371
- }));
3372
- return _route$1.apply(this, arguments);
3373
- }
3374
- function matches$1(configRoutes, request) {
3375
- return urlMatches(request.url, configRoutes[key$1]);
3054
+ function Handlers(configRoutes, config) {
3055
+ var GET = GETTER(configRoutes, config);
3056
+ var POST = POSTER(configRoutes, config);
3057
+ var DELETE = DELETER(configRoutes, config);
3058
+ var PUT = PUTER(configRoutes, config);
3059
+ return {
3060
+ GET: GET,
3061
+ POST: POST,
3062
+ DELETE: DELETE,
3063
+ PUT: PUT
3064
+ };
3376
3065
  }
3377
3066
 
3378
- var key = 'ERROR';
3379
- function route(_x) {
3380
- return _route.apply(this, arguments);
3381
- }
3382
- function _route() {
3383
- _route = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3384
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3385
- while (1) switch (_context.prev = _context.next) {
3386
- case 0:
3387
- return _context.abrupt("return", request(proxyRoutes[key], {
3388
- method: req.method,
3389
- request: req
3390
- }));
3391
- case 1:
3392
- case "end":
3393
- return _context.stop();
3394
- }
3395
- }, _callee);
3396
- }));
3397
- return _route.apply(this, arguments);
3398
- }
3399
- function matches(configRoutes, request) {
3400
- return urlMatches(request.url, configRoutes[key]);
3401
- }
3067
+ var appRoutes = function appRoutes(prefix) {
3068
+ if (prefix === void 0) {
3069
+ prefix = '/api';
3070
+ }
3071
+ return {
3072
+ SIGNIN: prefix + "/auth/signin",
3073
+ PROVIDERS: prefix + "/auth/providers",
3074
+ SESSION: prefix + "/auth/session",
3075
+ CSRF: prefix + "/auth/csrf",
3076
+ CALLBACK: prefix + "/auth/callback",
3077
+ SIGNOUT: prefix + "/auth/signout",
3078
+ ERROR: prefix + "/auth/error",
3079
+ ME: prefix + "/me",
3080
+ USERS: prefix + "/users",
3081
+ TENANTS: prefix + "/tenants",
3082
+ TENANT_USER: prefix + "/tenants/{tenantId}/users/{userId}",
3083
+ TENANT_USERS: prefix + "/tenants/{tenantId}/users",
3084
+ SIGNUP: prefix + "/signup"
3085
+ };
3086
+ };
3402
3087
 
3403
- function GETTER(configRoutes, config) {
3088
+ /**
3089
+ * a helper function to log in server side.
3090
+ */
3091
+ function serverAuth(config, handlers) {
3092
+ var _Logger = Logger(config, '[serverAuth]'),
3093
+ info = _Logger.info;
3404
3094
  return /*#__PURE__*/function () {
3405
- var _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3095
+ var _login = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3096
+ var _yield$csrfRes$json, _exec;
3097
+ var email, password, sessionUrl, sessionReq, sessionRes, providers, csrf, csrfReq, csrfRes, _ref2, csrfToken, credentials, csrfCookie, signInUrl, postReq, loginRes, authCookie, _ref3, token;
3406
3098
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3407
3099
  while (1) switch (_context.prev = _context.next) {
3408
3100
  case 0:
3409
- if (!matches$a(configRoutes, req)) {
3410
- _context.next = 2;
3101
+ email = _ref.email, password = _ref.password;
3102
+ info('Obtaining session');
3103
+ sessionUrl = new URL(config.api.localPath + "/api/auth/providers");
3104
+ sessionReq = new Request(sessionUrl, {
3105
+ method: 'GET',
3106
+ headers: new Headers({
3107
+ host: sessionUrl.host
3108
+ })
3109
+ });
3110
+ _context.next = 6;
3111
+ return handlers.POST(sessionReq);
3112
+ case 6:
3113
+ sessionRes = _context.sent;
3114
+ _context.next = 9;
3115
+ return sessionRes == null ? void 0 : sessionRes.json();
3116
+ case 9:
3117
+ providers = _context.sent;
3118
+ info('Obtaining csrf');
3119
+ csrf = new URL(config.api.localPath + "/api/auth/csrf");
3120
+ csrfReq = new Request(csrf, {
3121
+ method: 'GET',
3122
+ headers: new Headers({
3123
+ host: sessionUrl.host
3124
+ })
3125
+ });
3126
+ _context.next = 15;
3127
+ return handlers.POST(csrfReq);
3128
+ case 15:
3129
+ csrfRes = _context.sent;
3130
+ _context.next = 18;
3131
+ return csrfRes == null ? void 0 : csrfRes.json();
3132
+ case 18:
3133
+ _context.t0 = _yield$csrfRes$json = _context.sent;
3134
+ if (!(_context.t0 != null)) {
3135
+ _context.next = 23;
3411
3136
  break;
3412
3137
  }
3413
- return _context.abrupt("return", route$a(req, config));
3414
- case 2:
3415
- if (!matches$9(configRoutes, req)) {
3416
- _context.next = 4;
3138
+ _context.t1 = _yield$csrfRes$json;
3139
+ _context.next = 24;
3140
+ break;
3141
+ case 23:
3142
+ _context.t1 = {};
3143
+ case 24:
3144
+ _ref2 = _context.t1;
3145
+ csrfToken = _ref2.csrfToken;
3146
+ credentials = providers.credentials;
3147
+ csrfCookie = csrfRes == null ? void 0 : csrfRes.headers.get('set-cookie');
3148
+ if (credentials) {
3149
+ _context.next = 30;
3417
3150
  break;
3418
3151
  }
3419
- return _context.abrupt("return", route$9(req, config));
3420
- case 4:
3421
- if (!matches$8(configRoutes, req)) {
3422
- _context.next = 6;
3152
+ throw new Error('Unable to obtain credential provider. Aborting server side login.');
3153
+ case 30:
3154
+ signInUrl = new URL(credentials.callbackUrl);
3155
+ if (csrfCookie) {
3156
+ _context.next = 33;
3423
3157
  break;
3424
3158
  }
3425
- return _context.abrupt("return", route$8(req, config));
3426
- case 6:
3427
- if (!matches$7(configRoutes, req)) {
3428
- _context.next = 8;
3159
+ throw new Error('Unable to authenticate REST');
3160
+ case 33:
3161
+ info('Attempting sign in to', signInUrl.href);
3162
+ postReq = new Request(signInUrl, {
3163
+ method: 'POST',
3164
+ headers: new Headers({
3165
+ 'content-type': 'application/json',
3166
+ cookie: csrfCookie
3167
+ }),
3168
+ body: JSON.stringify({
3169
+ email: email,
3170
+ password: password,
3171
+ csrfToken: csrfToken,
3172
+ callbackUrl: credentials.callbackUrl
3173
+ })
3174
+ });
3175
+ _context.next = 37;
3176
+ return handlers.POST(postReq);
3177
+ case 37:
3178
+ loginRes = _context.sent;
3179
+ authCookie = loginRes == null ? void 0 : loginRes.headers.get('set-cookie');
3180
+ if (authCookie) {
3181
+ _context.next = 41;
3429
3182
  break;
3430
3183
  }
3431
- return _context.abrupt("return", route$7(req, config));
3432
- case 8:
3433
- if (!matches$5(configRoutes, req)) {
3434
- _context.next = 10;
3184
+ throw new Error('authentication failed');
3185
+ case 41:
3186
+ info('Server login successful');
3187
+ _ref3 = (_exec = /(nile\.session-token=.+?);/.exec(authCookie)) != null ? _exec : [], token = _ref3[1];
3188
+ return _context.abrupt("return", new Headers({
3189
+ cookie: [token, csrfCookie].join('; ')
3190
+ }));
3191
+ case 44:
3192
+ case "end":
3193
+ return _context.stop();
3194
+ }
3195
+ }, _callee);
3196
+ }));
3197
+ function login(_x) {
3198
+ return _login.apply(this, arguments);
3199
+ }
3200
+ return login;
3201
+ }();
3202
+ }
3203
+
3204
+ var Requester = /*#__PURE__*/function (_Config) {
3205
+ function Requester(config) {
3206
+ return _Config.call(this, config) || this;
3207
+ }
3208
+ _inheritsLoose(Requester, _Config);
3209
+ var _proto = Requester.prototype;
3210
+ _proto.rawRequest = /*#__PURE__*/function () {
3211
+ var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(method, url, init, body) {
3212
+ var _init, res;
3213
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
3214
+ while (1) switch (_context.prev = _context.next) {
3215
+ case 0:
3216
+ _init = _extends({}, init, {
3217
+ body: body,
3218
+ method: method
3219
+ });
3220
+ _context.next = 3;
3221
+ return _fetch(this, url, _init);
3222
+ case 3:
3223
+ res = _context.sent;
3224
+ if (!(res instanceof ResponseError)) {
3225
+ _context.next = 6;
3435
3226
  break;
3436
3227
  }
3437
- return _context.abrupt("return", route$5(req));
3438
- case 10:
3439
- if (!matches$6(configRoutes, req)) {
3440
- _context.next = 12;
3228
+ return _context.abrupt("return", res.response);
3229
+ case 6:
3230
+ return _context.abrupt("return", res);
3231
+ case 7:
3232
+ case "end":
3233
+ return _context.stop();
3234
+ }
3235
+ }, _callee, this);
3236
+ }));
3237
+ function rawRequest(_x, _x2, _x3, _x4) {
3238
+ return _rawRequest.apply(this, arguments);
3239
+ }
3240
+ return rawRequest;
3241
+ }()
3242
+ /**
3243
+ * three options here
3244
+ * 1) pass in headers for a server side request
3245
+ * 2) pass in the payload that matches the api
3246
+ * 3) pass in the request object sent by a browser
3247
+ * @param method
3248
+ * @param url
3249
+ * @param req
3250
+ * @param init
3251
+ * @returns
3252
+ */
3253
+ ;
3254
+ _proto.request =
3255
+ /*#__PURE__*/
3256
+ function () {
3257
+ var _request = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(method, url, req, init) {
3258
+ var headers, tenantId, cookie, _headers, _tenantId, _cookie, body, _init;
3259
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3260
+ while (1) switch (_context2.prev = _context2.next) {
3261
+ case 0:
3262
+ // set the headers
3263
+ headers = new Headers(init ? init == null ? void 0 : init.headers : {});
3264
+ if (req instanceof Headers) {
3265
+ tenantId = req.get(X_NILE_TENANT);
3266
+ cookie = req.get('cookie');
3267
+ if (tenantId) {
3268
+ headers.set(X_NILE_TENANT, tenantId);
3269
+ }
3270
+ if (cookie) {
3271
+ headers.set('cookie', cookie);
3272
+ }
3273
+ } else if (req instanceof Request) {
3274
+ // pass back the X_NILE_TENANT
3275
+ _headers = new Headers(req == null ? void 0 : req.headers);
3276
+ _tenantId = _headers.get(X_NILE_TENANT);
3277
+ _cookie = _headers.get('cookie');
3278
+ if (_tenantId) {
3279
+ headers.set(X_NILE_TENANT, _tenantId);
3280
+ }
3281
+ if (_cookie) {
3282
+ headers.set('cookie', _cookie);
3283
+ }
3284
+ }
3285
+ // default the body - may be the actual payload for the API
3286
+ body = JSON.stringify(req); // comes from next/some server
3287
+ if (!(method === 'GET')) {
3288
+ _context2.next = 7;
3441
3289
  break;
3442
3290
  }
3443
- return _context.abrupt("return", route$6(req));
3444
- case 12:
3445
- if (!matches$4(configRoutes, req)) {
3446
- _context.next = 14;
3291
+ body = undefined;
3292
+ _context2.next = 14;
3293
+ break;
3294
+ case 7:
3295
+ if (!(req instanceof Request)) {
3296
+ _context2.next = 13;
3447
3297
  break;
3448
3298
  }
3449
- return _context.abrupt("return", route$4(req));
3299
+ _context2.next = 10;
3300
+ return new Response(req.body).text();
3301
+ case 10:
3302
+ body = _context2.sent;
3303
+ _context2.next = 14;
3304
+ break;
3305
+ case 13:
3306
+ if (
3307
+ // is just headers for a GET request
3308
+ req instanceof Headers || JSON.stringify(req) === '{}' || req && typeof req === 'object' && Object.values(req).length === 0) {
3309
+ body = undefined;
3310
+ }
3450
3311
  case 14:
3451
- if (!matches$3(configRoutes, req)) {
3452
- _context.next = 16;
3312
+ _init = _extends({}, init, {
3313
+ headers: headers
3314
+ });
3315
+ _context2.next = 17;
3316
+ return this.rawRequest(method, url, _init, body);
3317
+ case 17:
3318
+ return _context2.abrupt("return", _context2.sent);
3319
+ case 18:
3320
+ case "end":
3321
+ return _context2.stop();
3322
+ }
3323
+ }, _callee2, this);
3324
+ }));
3325
+ function request(_x5, _x6, _x7, _x8) {
3326
+ return _request.apply(this, arguments);
3327
+ }
3328
+ return request;
3329
+ }();
3330
+ _proto.post = /*#__PURE__*/function () {
3331
+ var _post = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, url, init) {
3332
+ var response;
3333
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3334
+ while (1) switch (_context3.prev = _context3.next) {
3335
+ case 0:
3336
+ _context3.next = 2;
3337
+ return this.request('POST', url, req, init);
3338
+ case 2:
3339
+ response = _context3.sent;
3340
+ if (!(response && response.status >= 200 && response.status < 300)) {
3341
+ _context3.next = 5;
3453
3342
  break;
3454
3343
  }
3455
- return _context.abrupt("return", route$3(req));
3456
- case 16:
3457
- if (!matches$2(configRoutes, req)) {
3458
- _context.next = 18;
3344
+ return _context3.abrupt("return", response.json());
3345
+ case 5:
3346
+ return _context3.abrupt("return", response);
3347
+ case 6:
3348
+ case "end":
3349
+ return _context3.stop();
3350
+ }
3351
+ }, _callee3, this);
3352
+ }));
3353
+ function post(_x9, _x10, _x11) {
3354
+ return _post.apply(this, arguments);
3355
+ }
3356
+ return post;
3357
+ }();
3358
+ _proto.get = /*#__PURE__*/function () {
3359
+ var _get = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, url, init) {
3360
+ var response;
3361
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
3362
+ while (1) switch (_context4.prev = _context4.next) {
3363
+ case 0:
3364
+ _context4.next = 2;
3365
+ return this.request('GET', url, req, init);
3366
+ case 2:
3367
+ response = _context4.sent;
3368
+ if (!(response && response.status >= 200 && response.status < 300)) {
3369
+ _context4.next = 5;
3459
3370
  break;
3460
3371
  }
3461
- return _context.abrupt("return", route$2(req));
3462
- case 18:
3463
- if (!matches$1(configRoutes, req)) {
3464
- _context.next = 20;
3372
+ return _context4.abrupt("return", response.json());
3373
+ case 5:
3374
+ return _context4.abrupt("return", response);
3375
+ case 6:
3376
+ case "end":
3377
+ return _context4.stop();
3378
+ }
3379
+ }, _callee4, this);
3380
+ }));
3381
+ function get(_x12, _x13, _x14) {
3382
+ return _get.apply(this, arguments);
3383
+ }
3384
+ return get;
3385
+ }();
3386
+ _proto.put = /*#__PURE__*/function () {
3387
+ var _put = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, url, init) {
3388
+ var response;
3389
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
3390
+ while (1) switch (_context5.prev = _context5.next) {
3391
+ case 0:
3392
+ _context5.next = 2;
3393
+ return this.request('PUT', url, req, init);
3394
+ case 2:
3395
+ response = _context5.sent;
3396
+ if (!(response && response.status >= 200 && response.status < 300)) {
3397
+ _context5.next = 5;
3465
3398
  break;
3466
3399
  }
3467
- return _context.abrupt("return", route$1(req));
3468
- case 20:
3469
- if (!matches(configRoutes, req)) {
3470
- _context.next = 22;
3400
+ return _context5.abrupt("return", response.json());
3401
+ case 5:
3402
+ return _context5.abrupt("return", response);
3403
+ case 6:
3404
+ case "end":
3405
+ return _context5.stop();
3406
+ }
3407
+ }, _callee5, this);
3408
+ }));
3409
+ function put(_x15, _x16, _x17) {
3410
+ return _put.apply(this, arguments);
3411
+ }
3412
+ return put;
3413
+ }();
3414
+ _proto["delete"] = /*#__PURE__*/function () {
3415
+ var _delete2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, url, init) {
3416
+ var response;
3417
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
3418
+ while (1) switch (_context6.prev = _context6.next) {
3419
+ case 0:
3420
+ _context6.next = 2;
3421
+ return this.request('DELETE', url, req, init);
3422
+ case 2:
3423
+ response = _context6.sent;
3424
+ if (!(response && response.status >= 200 && response.status < 300)) {
3425
+ _context6.next = 5;
3471
3426
  break;
3472
3427
  }
3473
- return _context.abrupt("return", route(req));
3474
- case 22:
3475
- return _context.abrupt("return", new Response(null, {
3476
- status: 404
3477
- }));
3478
- case 23:
3428
+ return _context6.abrupt("return", response.json());
3429
+ case 5:
3430
+ return _context6.abrupt("return", response);
3431
+ case 6:
3479
3432
  case "end":
3480
- return _context.stop();
3433
+ return _context6.stop();
3481
3434
  }
3482
- }, _callee);
3435
+ }, _callee6, this);
3483
3436
  }));
3484
- function GET(_x) {
3485
- return _GET.apply(this, arguments);
3437
+ function _delete(_x18, _x19, _x20) {
3438
+ return _delete2.apply(this, arguments);
3439
+ }
3440
+ return _delete;
3441
+ }();
3442
+ return Requester;
3443
+ }(Config);
3444
+
3445
+ var Tenants = /*#__PURE__*/function (_Config) {
3446
+ function Tenants(config, headers) {
3447
+ var _this;
3448
+ _this = _Config.call(this, config) || this;
3449
+ _this.headers = void 0;
3450
+ _this.createTenant = /*#__PURE__*/function () {
3451
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3452
+ var _req, _requester, _init;
3453
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
3454
+ while (1) switch (_context.prev = _context.next) {
3455
+ case 0:
3456
+ if (typeof req === 'string') {
3457
+ _req = new Request("" + _this.api.basePath + _this.tenantsUrl, {
3458
+ body: JSON.stringify({
3459
+ name: req
3460
+ }),
3461
+ method: 'POST'
3462
+ });
3463
+ } else {
3464
+ _req = req;
3465
+ }
3466
+ _requester = new Requester(_this);
3467
+ _init = _this.handleHeaders(init);
3468
+ return _context.abrupt("return", _requester.post(_req, _this.tenantsUrl, _init));
3469
+ case 4:
3470
+ case "end":
3471
+ return _context.stop();
3472
+ }
3473
+ }, _callee);
3474
+ }));
3475
+ return function (_x, _x2) {
3476
+ return _ref.apply(this, arguments);
3477
+ };
3478
+ }();
3479
+ _this.getTenant = /*#__PURE__*/function () {
3480
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
3481
+ var _requester, _init;
3482
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3483
+ while (1) switch (_context2.prev = _context2.next) {
3484
+ case 0:
3485
+ if (typeof req === 'string') {
3486
+ _this.tenantId = req;
3487
+ }
3488
+ _requester = new Requester(_this);
3489
+ _init = _this.handleHeaders(init);
3490
+ return _context2.abrupt("return", _requester.get(req, _this.tenantUrl, _init));
3491
+ case 4:
3492
+ case "end":
3493
+ return _context2.stop();
3494
+ }
3495
+ }, _callee2);
3496
+ }));
3497
+ return function (_x3, _x4) {
3498
+ return _ref2.apply(this, arguments);
3499
+ };
3500
+ }();
3501
+ _this.listTenants = /*#__PURE__*/function () {
3502
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3503
+ var _requester, _init;
3504
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3505
+ while (1) switch (_context3.prev = _context3.next) {
3506
+ case 0:
3507
+ _requester = new Requester(_this);
3508
+ _init = _this.handleHeaders(init);
3509
+ return _context3.abrupt("return", _requester.get(req, _this.tenantListUrl, _init));
3510
+ case 3:
3511
+ case "end":
3512
+ return _context3.stop();
3513
+ }
3514
+ }, _callee3);
3515
+ }));
3516
+ return function (_x5, _x6) {
3517
+ return _ref3.apply(this, arguments);
3518
+ };
3519
+ }();
3520
+ _this.headers = headers;
3521
+ return _this;
3522
+ }
3523
+ _inheritsLoose(Tenants, _Config);
3524
+ var _proto = Tenants.prototype;
3525
+ _proto.handleHeaders = function handleHeaders(init) {
3526
+ if (this.headers) {
3527
+ if (init) {
3528
+ var _init2;
3529
+ init.headers = new Headers(_extends({}, this.headers, (_init2 = init) == null ? void 0 : _init2.headers));
3530
+ return init;
3531
+ } else {
3532
+ init = {
3533
+ headers: this.headers
3534
+ };
3535
+ return init;
3536
+ }
3537
+ }
3538
+ return undefined;
3539
+ };
3540
+ return _createClass(Tenants, [{
3541
+ key: "tenantsUrl",
3542
+ get: function get() {
3543
+ return '/tenants';
3544
+ }
3545
+ }, {
3546
+ key: "tenantUrl",
3547
+ get: function get() {
3548
+ var _this$tenantId;
3549
+ return "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}');
3550
+ }
3551
+ }, {
3552
+ key: "tenantListUrl",
3553
+ get: function get() {
3554
+ var _this$userId;
3555
+ return "/users/" + ((_this$userId = this.userId) != null ? _this$userId : '{userId}') + "/tenants";
3556
+ }
3557
+ }]);
3558
+ }(Config);
3559
+
3560
+ var Users = /*#__PURE__*/function (_Config) {
3561
+ function Users(config, headers) {
3562
+ var _this;
3563
+ _this = _Config.call(this, config) || this;
3564
+ _this.headers = void 0;
3565
+ _this.createUser = /*#__PURE__*/function () {
3566
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3567
+ var _requester, _init;
3568
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
3569
+ while (1) switch (_context.prev = _context.next) {
3570
+ case 0:
3571
+ _requester = new Requester(_this);
3572
+ _init = _this.handleHeaders(init);
3573
+ _context.next = 4;
3574
+ return _requester.post(req, _this.usersUrl, _init);
3575
+ case 4:
3576
+ return _context.abrupt("return", _context.sent);
3577
+ case 5:
3578
+ case "end":
3579
+ return _context.stop();
3580
+ }
3581
+ }, _callee);
3582
+ }));
3583
+ return function (_x, _x2) {
3584
+ return _ref.apply(this, arguments);
3585
+ };
3586
+ }();
3587
+ _this.updateUser = /*#__PURE__*/function () {
3588
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(userId, req, init) {
3589
+ var _requester, _init;
3590
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3591
+ while (1) switch (_context2.prev = _context2.next) {
3592
+ case 0:
3593
+ _requester = new Requester(_this);
3594
+ _init = _this.handleHeaders(init);
3595
+ _context2.next = 4;
3596
+ return _requester.put(req, _this.usersUrl + "/" + userId, _init);
3597
+ case 4:
3598
+ return _context2.abrupt("return", _context2.sent);
3599
+ case 5:
3600
+ case "end":
3601
+ return _context2.stop();
3602
+ }
3603
+ }, _callee2);
3604
+ }));
3605
+ return function (_x3, _x4, _x5) {
3606
+ return _ref2.apply(this, arguments);
3607
+ };
3608
+ }();
3609
+ _this.listUsers = /*#__PURE__*/function () {
3610
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3611
+ var _requester, _init;
3612
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3613
+ while (1) switch (_context3.prev = _context3.next) {
3614
+ case 0:
3615
+ _requester = new Requester(_this);
3616
+ _init = _this.handleHeaders(init);
3617
+ _context3.next = 4;
3618
+ return _requester.get(req, _this.tenantUsersUrl, _init);
3619
+ case 4:
3620
+ return _context3.abrupt("return", _context3.sent);
3621
+ case 5:
3622
+ case "end":
3623
+ return _context3.stop();
3624
+ }
3625
+ }, _callee3);
3626
+ }));
3627
+ return function (_x6, _x7) {
3628
+ return _ref3.apply(this, arguments);
3629
+ };
3630
+ }();
3631
+ _this.linkUser = /*#__PURE__*/function () {
3632
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
3633
+ var _requester, _init;
3634
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
3635
+ while (1) switch (_context4.prev = _context4.next) {
3636
+ case 0:
3637
+ _requester = new Requester(_this);
3638
+ _init = _this.handleHeaders(init);
3639
+ _context4.next = 4;
3640
+ return _requester.put(req, _this.tenantUsersUrl, _init);
3641
+ case 4:
3642
+ return _context4.abrupt("return", _context4.sent);
3643
+ case 5:
3644
+ case "end":
3645
+ return _context4.stop();
3646
+ }
3647
+ }, _callee4);
3648
+ }));
3649
+ return function (_x8, _x9) {
3650
+ return _ref4.apply(this, arguments);
3651
+ };
3652
+ }();
3653
+ _this.tenantUsersDeleteUrl = function (userId) {
3654
+ var _this$tenantId;
3655
+ return "/tenants/" + ((_this$tenantId = _this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users/" + (userId != null ? userId : '{userId}');
3656
+ };
3657
+ _this.getUserId = /*#__PURE__*/function () {
3658
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req) {
3659
+ var body;
3660
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
3661
+ while (1) switch (_context5.prev = _context5.next) {
3662
+ case 0:
3663
+ if (!(req instanceof Request)) {
3664
+ _context5.next = 6;
3665
+ break;
3666
+ }
3667
+ _context5.next = 3;
3668
+ return new Response(req == null ? void 0 : req.body).json();
3669
+ case 3:
3670
+ body = _context5.sent;
3671
+ if (!body) {
3672
+ _context5.next = 6;
3673
+ break;
3674
+ }
3675
+ return _context5.abrupt("return", body.id);
3676
+ case 6:
3677
+ if (!('id' in req)) {
3678
+ _context5.next = 8;
3679
+ break;
3680
+ }
3681
+ return _context5.abrupt("return", req.id);
3682
+ case 8:
3683
+ return _context5.abrupt("return", null);
3684
+ case 9:
3685
+ case "end":
3686
+ return _context5.stop();
3687
+ }
3688
+ }, _callee5);
3689
+ }));
3690
+ return function (_x10) {
3691
+ return _ref5.apply(this, arguments);
3692
+ };
3693
+ }();
3694
+ _this.unlinkUser = /*#__PURE__*/function () {
3695
+ var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, init) {
3696
+ var _requester, userId, _init;
3697
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
3698
+ while (1) switch (_context6.prev = _context6.next) {
3699
+ case 0:
3700
+ _requester = new Requester(_this);
3701
+ _context6.next = 3;
3702
+ return _this.getUserId(req);
3703
+ case 3:
3704
+ userId = _context6.sent;
3705
+ _init = _this.handleHeaders(init);
3706
+ _context6.next = 7;
3707
+ return _requester["delete"](req, _this.tenantUsersDeleteUrl(userId), _init);
3708
+ case 7:
3709
+ return _context6.abrupt("return", _context6.sent);
3710
+ case 8:
3711
+ case "end":
3712
+ return _context6.stop();
3713
+ }
3714
+ }, _callee6);
3715
+ }));
3716
+ return function (_x11, _x12) {
3717
+ return _ref6.apply(this, arguments);
3718
+ };
3719
+ }();
3720
+ _this.me = /*#__PURE__*/function () {
3721
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(req, init) {
3722
+ var _requester, _init;
3723
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
3724
+ while (1) switch (_context7.prev = _context7.next) {
3725
+ case 0:
3726
+ _requester = new Requester(_this);
3727
+ _init = _this.handleHeaders(init);
3728
+ _context7.next = 4;
3729
+ return _requester.get(req, _this.meUrl, _init);
3730
+ case 4:
3731
+ return _context7.abrupt("return", _context7.sent);
3732
+ case 5:
3733
+ case "end":
3734
+ return _context7.stop();
3735
+ }
3736
+ }, _callee7);
3737
+ }));
3738
+ return function (_x13, _x14) {
3739
+ return _ref7.apply(this, arguments);
3740
+ };
3741
+ }();
3742
+ _this.headers = headers;
3743
+ return _this;
3744
+ }
3745
+ _inheritsLoose(Users, _Config);
3746
+ var _proto = Users.prototype;
3747
+ _proto.handleHeaders = function handleHeaders(init) {
3748
+ if (this.headers) {
3749
+ if (init) {
3750
+ var _init2;
3751
+ init.headers = new Headers(_extends({}, this.headers, (_init2 = init) == null ? void 0 : _init2.headers));
3752
+ return init;
3753
+ } else {
3754
+ init = {
3755
+ headers: this.headers
3756
+ };
3757
+ return init;
3758
+ }
3486
3759
  }
3487
- return GET;
3488
- }();
3489
- }
3490
-
3491
- function POSTER(configRoutes, config) {
3492
- return /*#__PURE__*/function () {
3493
- var _POST = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3494
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3495
- while (1) switch (_context.prev = _context.next) {
3496
- case 0:
3497
- if (!matches$8(configRoutes, req)) {
3498
- _context.next = 2;
3499
- break;
3500
- }
3501
- return _context.abrupt("return", route$8(req, config));
3502
- case 2:
3503
- if (!matches$9(configRoutes, req)) {
3504
- _context.next = 4;
3505
- break;
3506
- }
3507
- return _context.abrupt("return", route$9(req, config));
3508
- case 4:
3509
- if (!matches$7(configRoutes, req)) {
3510
- _context.next = 6;
3511
- break;
3512
- }
3513
- return _context.abrupt("return", route$7(req, config));
3514
- case 6:
3515
- if (!matches$5(configRoutes, req)) {
3516
- _context.next = 8;
3517
- break;
3518
- }
3519
- return _context.abrupt("return", route$5(req));
3520
- case 8:
3521
- if (!matches$6(configRoutes, req)) {
3522
- _context.next = 10;
3523
- break;
3524
- }
3525
- return _context.abrupt("return", route$6(req));
3526
- case 10:
3527
- if (!matches$4(configRoutes, req)) {
3528
- _context.next = 12;
3529
- break;
3530
- }
3531
- return _context.abrupt("return", route$4(req));
3532
- case 12:
3533
- if (!matches$3(configRoutes, req)) {
3534
- _context.next = 14;
3535
- break;
3536
- }
3537
- return _context.abrupt("return", route$3(req));
3538
- case 14:
3539
- if (!matches$2(configRoutes, req)) {
3540
- _context.next = 16;
3541
- break;
3542
- }
3543
- return _context.abrupt("return", route$2(req));
3544
- case 16:
3545
- if (!matches$1(configRoutes, req)) {
3546
- _context.next = 18;
3547
- break;
3548
- }
3549
- return _context.abrupt("return", route$1(req));
3550
- case 18:
3551
- return _context.abrupt("return", new Response(null, {
3552
- status: 404
3553
- }));
3554
- case 19:
3555
- case "end":
3556
- return _context.stop();
3557
- }
3558
- }, _callee);
3559
- }));
3560
- function POST(_x) {
3561
- return _POST.apply(this, arguments);
3760
+ return undefined;
3761
+ };
3762
+ return _createClass(Users, [{
3763
+ key: "usersUrl",
3764
+ get: function get() {
3765
+ return '/users';
3562
3766
  }
3563
- return POST;
3564
- }();
3565
- }
3566
-
3567
- function DELETER(configRoutes, config) {
3568
- return /*#__PURE__*/function () {
3569
- var _DELETE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3570
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3571
- while (1) switch (_context.prev = _context.next) {
3572
- case 0:
3573
- if (!matches$8(configRoutes, req)) {
3574
- _context.next = 2;
3575
- break;
3576
- }
3577
- return _context.abrupt("return", route$8(req, config));
3578
- case 2:
3579
- if (!matches$7(configRoutes, req)) {
3580
- _context.next = 4;
3581
- break;
3582
- }
3583
- return _context.abrupt("return", route$7(req, config));
3584
- case 4:
3585
- return _context.abrupt("return", new Response(null, {
3586
- status: 404
3587
- }));
3588
- case 5:
3589
- case "end":
3590
- return _context.stop();
3591
- }
3592
- }, _callee);
3593
- }));
3594
- function DELETE(_x) {
3595
- return _DELETE.apply(this, arguments);
3767
+ }, {
3768
+ key: "tenantUsersUrl",
3769
+ get: function get() {
3770
+ var _this$tenantId2;
3771
+ return "/tenants/" + ((_this$tenantId2 = this.tenantId) != null ? _this$tenantId2 : '{tenantId}') + "/users";
3596
3772
  }
3597
- return DELETE;
3598
- }();
3599
- }
3600
-
3601
- function PUTER(configRoutes, config) {
3602
- return /*#__PURE__*/function () {
3603
- var _PUT = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3604
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3605
- while (1) switch (_context.prev = _context.next) {
3606
- case 0:
3607
- if (!matches$8(configRoutes, req)) {
3608
- _context.next = 2;
3609
- break;
3610
- }
3611
- return _context.abrupt("return", route$8(req, config));
3612
- case 2:
3613
- if (!matches$9(configRoutes, req)) {
3614
- _context.next = 4;
3615
- break;
3616
- }
3617
- return _context.abrupt("return", route$9(req, config));
3618
- case 4:
3619
- if (!matches$7(configRoutes, req)) {
3620
- _context.next = 6;
3621
- break;
3622
- }
3623
- return _context.abrupt("return", route$7(req, config));
3624
- case 6:
3625
- return _context.abrupt("return", new Response(null, {
3626
- status: 404
3627
- }));
3628
- case 7:
3629
- case "end":
3630
- return _context.stop();
3631
- }
3632
- }, _callee);
3633
- }));
3634
- function PUT(_x) {
3635
- return _PUT.apply(this, arguments);
3773
+ }, {
3774
+ key: "meUrl",
3775
+ get: function get() {
3776
+ return '/me';
3636
3777
  }
3637
- return PUT;
3638
- }();
3639
- }
3640
-
3641
- function Handlers(configRoutes, config) {
3642
- var GET = GETTER(configRoutes, config);
3643
- var POST = POSTER(configRoutes, config);
3644
- var DELETE = DELETER(configRoutes, config);
3645
- var PUT = PUTER(configRoutes, config);
3646
- return {
3647
- GET: GET,
3648
- POST: POST,
3649
- DELETE: DELETE,
3650
- PUT: PUT
3651
- };
3652
- }
3778
+ }]);
3779
+ }(Config);
3653
3780
 
3654
3781
  var Api = /*#__PURE__*/function () {
3655
3782
  function Api(config) {
@@ -3693,6 +3820,7 @@ var Api = /*#__PURE__*/function () {
3693
3820
  }
3694
3821
  }]);
3695
3822
  }();
3823
+
3696
3824
  var Server = /*#__PURE__*/function () {
3697
3825
  function Server(config) {
3698
3826
  var _this = this;
@@ -3714,32 +3842,32 @@ var Server = /*#__PURE__*/function () {
3714
3842
  _this.token = token;
3715
3843
  });
3716
3844
  }
3717
- var _proto2 = Server.prototype;
3718
- _proto2.setConfig = function setConfig(cfg) {
3845
+ var _proto = Server.prototype;
3846
+ _proto.setConfig = function setConfig(cfg) {
3719
3847
  this.config = new Config(cfg);
3720
3848
  };
3721
- _proto2.init = /*#__PURE__*/function () {
3722
- var _init = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(cfg) {
3849
+ _proto.init = /*#__PURE__*/function () {
3850
+ var _init = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(cfg) {
3723
3851
  var updatedConfig;
3724
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3725
- while (1) switch (_context2.prev = _context2.next) {
3852
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
3853
+ while (1) switch (_context.prev = _context.next) {
3726
3854
  case 0:
3727
- _context2.next = 2;
3855
+ _context.next = 2;
3728
3856
  return this.config.configure(_extends({}, this.config, cfg));
3729
3857
  case 2:
3730
- updatedConfig = _context2.sent;
3858
+ updatedConfig = _context.sent;
3731
3859
  this.setConfig(updatedConfig);
3732
3860
  this.manager.clear(this.config);
3733
3861
  this.manager = new DBManager(this.config);
3734
3862
  this.api = new Api(this.config);
3735
- return _context2.abrupt("return", this);
3863
+ return _context.abrupt("return", this);
3736
3864
  case 8:
3737
3865
  case "end":
3738
- return _context2.stop();
3866
+ return _context.stop();
3739
3867
  }
3740
- }, _callee2, this);
3868
+ }, _callee, this);
3741
3869
  }));
3742
- function init(_x2) {
3870
+ function init(_x) {
3743
3871
  return _init.apply(this, arguments);
3744
3872
  }
3745
3873
  return init;
@@ -3749,7 +3877,7 @@ var Server = /*#__PURE__*/function () {
3749
3877
  * returns the global Server object, an existing server that's already been configured,
3750
3878
  * or a new one if the config isn't in the cache
3751
3879
  */
3752
- _proto2.getInstance = function getInstance(config) {
3880
+ _proto.getInstance = function getInstance(config) {
3753
3881
  var _config = _extends({}, this.config, config);
3754
3882
  var serverId = getServerId(_config);
3755
3883
  var currentServerId = makeServerId(this.config);
@@ -3768,8 +3896,9 @@ var Server = /*#__PURE__*/function () {
3768
3896
  existing.databaseId = updatedConfig.databaseId;
3769
3897
  return existing;
3770
3898
  }
3771
- this.servers.set(serverId, new Server(_config));
3772
- return this.servers.get(serverId);
3899
+ var newServer = new Server(_config);
3900
+ this.servers.set(serverId, newServer);
3901
+ return newServer;
3773
3902
  };
3774
3903
  return _createClass(Server, [{
3775
3904
  key: "databaseId",
@@ -3824,30 +3953,29 @@ var Server = /*#__PURE__*/function () {
3824
3953
  }, {
3825
3954
  key: "db",
3826
3955
  get: function get() {
3827
- // only need to interact with the knex object
3828
3956
  return this.manager.getConnection(this.config);
3829
3957
  }
3830
3958
  }]);
3831
3959
  }();
3832
- function create(_x3) {
3960
+ function create(_x2) {
3833
3961
  return _create.apply(this, arguments);
3834
3962
  }
3835
3963
  function _create() {
3836
- _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(config) {
3964
+ _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
3837
3965
  var server;
3838
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3839
- while (1) switch (_context3.prev = _context3.next) {
3966
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3967
+ while (1) switch (_context2.prev = _context2.next) {
3840
3968
  case 0:
3841
3969
  server = new Server(config);
3842
- _context3.next = 3;
3970
+ _context2.next = 3;
3843
3971
  return server.init();
3844
3972
  case 3:
3845
- return _context3.abrupt("return", _context3.sent);
3973
+ return _context2.abrupt("return", _context2.sent);
3846
3974
  case 4:
3847
3975
  case "end":
3848
- return _context3.stop();
3976
+ return _context2.stop();
3849
3977
  }
3850
- }, _callee3);
3978
+ }, _callee2);
3851
3979
  }));
3852
3980
  return _create.apply(this, arguments);
3853
3981
  }