@niledatabase/server 4.0.0-alpha.0 → 4.0.0-alpha.2

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.
package/dist/index.mjs CHANGED
@@ -86,8 +86,8 @@ async function request(url, _init, config) {
86
86
  String(request2.headers.get(X_NILE_TENANT))
87
87
  );
88
88
  }
89
- if ("secureCookies" in config && config.secureCookies != null) {
90
- updatedHeaders.set(X_NILE_SECURECOOKIES, String(config.secureCookies));
89
+ if (config.api.secureCookies != null) {
90
+ updatedHeaders.set(X_NILE_SECURECOOKIES, String(config.api.secureCookies));
91
91
  }
92
92
  updatedHeaders.set("host", requestUrl.host);
93
93
  if (config.api.callbackUrl) {
@@ -178,7 +178,7 @@ var getSecureCookies = (cfg) => {
178
178
  if (stringCheck(process.env.NILEDB_SECURECOOKIES)) {
179
179
  return Boolean(process.env.NILEDB_SECURECOOKIES);
180
180
  }
181
- return config?.secureCookies;
181
+ return config?.api?.secureCookies;
182
182
  };
183
183
  var getDatabaseId = (cfg) => {
184
184
  const { config, logger } = cfg;
@@ -302,6 +302,19 @@ var getTenantId = (cfg) => {
302
302
  }
303
303
  return null;
304
304
  };
305
+ var getCookieKey = (cfg) => {
306
+ const { config, logger } = cfg;
307
+ const { info } = Logger(config, "[cookieKey]");
308
+ if (stringCheck(config?.api?.cookieKey)) {
309
+ logger && info(`${logger}[config] ${config?.api?.cookieKey}`);
310
+ return String(config?.api?.cookieKey);
311
+ }
312
+ if (stringCheck(process.env.NILEDB_COOKIE_KEY)) {
313
+ logger && info(`${logger}[NILEDB_COOKIE_KEY] ${process.env.NILEDB_COOKIE_KEY}`);
314
+ return String(process.env.NILEDB_COOKIE_KEY);
315
+ }
316
+ return "token";
317
+ };
305
318
  var getBasePath = (cfg) => {
306
319
  const { config, logger } = cfg;
307
320
  const { warn, info, error } = Logger(config, "[basePath]");
@@ -404,17 +417,24 @@ var stringCheck = (str) => {
404
417
  var ApiConfig = class {
405
418
  cookieKey;
406
419
  basePath;
420
+ routes;
421
+ routePrefix;
422
+ secureCookies;
407
423
  /**
408
424
  * The client side callback url. Defaults to nothing (so nile.origin will be it), but in the cases of x-origin, you want to set this explicitly to be sure nile-auth does the right thing
409
425
  * If this is set, any `callbackUrl` from the client will be ignored.
410
426
  */
411
427
  callbackUrl;
412
428
  _token;
413
- constructor({ basePath, cookieKey, token, callbackUrl }) {
414
- this.basePath = basePath;
415
- this.cookieKey = cookieKey;
416
- this.callbackUrl = callbackUrl;
417
- this._token = token;
429
+ constructor(config, logger) {
430
+ const envVarConfig = { config, logger };
431
+ this.cookieKey = getCookieKey(envVarConfig);
432
+ this._token = getToken(envVarConfig);
433
+ this.callbackUrl = getCallbackUrl(envVarConfig);
434
+ this.secureCookies = getSecureCookies(envVarConfig);
435
+ this.basePath = getBasePath(envVarConfig);
436
+ this.routes = config?.api?.routes;
437
+ this.routePrefix = config?.api?.routePrefix;
418
438
  }
419
439
  get token() {
420
440
  return this._token;
@@ -428,10 +448,7 @@ var Config = class {
428
448
  password;
429
449
  databaseId;
430
450
  databaseName;
431
- routePrefix;
432
- routes;
433
451
  logger;
434
- secureCookies;
435
452
  debug;
436
453
  db;
437
454
  api;
@@ -466,23 +483,15 @@ var Config = class {
466
483
  );
467
484
  }
468
485
  }
469
- this.secureCookies = getSecureCookies(envVarConfig);
470
486
  this.databaseId = getDatabaseId(envVarConfig);
471
487
  this.databaseName = getDatabaseName(envVarConfig);
472
488
  this._tenantId = getTenantId(envVarConfig);
473
489
  this.debug = Boolean(config?.debug);
474
490
  this._userId = config?.userId;
475
- const callbackUrl = config?.api?.callbackUrl ?? getCallbackUrl(envVarConfig);
476
- const basePath = getBasePath(envVarConfig);
477
491
  const { host, port, ...dbConfig } = config?.db ?? {};
478
492
  const configuredHost = host ?? getDbHost(envVarConfig);
479
493
  const configuredPort = port ?? getDbPort(envVarConfig);
480
- this.api = new ApiConfig({
481
- basePath,
482
- cookieKey: config?.api?.cookieKey ?? "token",
483
- token: getToken({ config }),
484
- callbackUrl
485
- });
494
+ this.api = new ApiConfig(config, logger);
486
495
  this.db = {
487
496
  user: this.user,
488
497
  password: this.password,
@@ -500,18 +509,12 @@ var Config = class {
500
509
  config
501
510
  };
502
511
  const { host, port, ...dbConfig } = config.db ?? {};
503
- const callbackUrl = config?.api?.callbackUrl ?? getCallbackUrl(envVarConfig);
504
512
  let configuredHost = host ?? getDbHost(envVarConfig);
505
513
  const configuredPort = port ?? getDbPort(envVarConfig);
506
514
  let basePath = getBasePath(envVarConfig);
507
515
  if (configuredHost && this.databaseName && this.databaseId && basePath) {
508
516
  info("Already configured, aborting fetch");
509
- this.api = new ApiConfig({
510
- basePath,
511
- cookieKey: config?.api?.cookieKey ?? "token",
512
- token: getToken({ config }),
513
- callbackUrl
514
- });
517
+ this.api = new ApiConfig(config);
515
518
  this.db = {
516
519
  user: this.user,
517
520
  password: this.password,
@@ -596,12 +599,7 @@ var Config = class {
596
599
  configuredHost = dburl.hostname;
597
600
  }
598
601
  }
599
- this.api = new ApiConfig({
600
- basePath,
601
- cookieKey: config?.api?.cookieKey ?? "token",
602
- token: getToken({ config }),
603
- callbackUrl
604
- });
602
+ this.api = new ApiConfig(config);
605
603
  this.db = {
606
604
  user: this.user,
607
605
  password: this.password,
@@ -1002,8 +1000,8 @@ function makeBasicHeaders(config, opts) {
1002
1000
  headers.set("Authorization", `Bearer ${getToken({ config })}`);
1003
1001
  }
1004
1002
  }
1005
- if ("secureCookies" in config && config.secureCookies != null) {
1006
- headers.set(X_NILE_SECURECOOKIES, String(config.secureCookies));
1003
+ if (config && config.api.secureCookies != null) {
1004
+ headers.set(X_NILE_SECURECOOKIES, String(config.api.secureCookies));
1007
1005
  }
1008
1006
  return headers;
1009
1007
  }
@@ -1886,7 +1884,7 @@ var Requester = class extends Config {
1886
1884
  var ORIGIN = "https://us-west-2.api.dev.thenile.dev";
1887
1885
  function serverLogin(config, handlers) {
1888
1886
  const { info, error, debug } = Logger(config, "[server side login]");
1889
- const routes = appRoutes(config.routePrefix);
1887
+ const routes = appRoutes(config.api.routePrefix);
1890
1888
  return async function login({
1891
1889
  email,
1892
1890
  password
@@ -2220,8 +2218,8 @@ var Api = class {
2220
2218
  this.users = new Users(config);
2221
2219
  this.tenants = new Tenants(config);
2222
2220
  this.routes = {
2223
- ...appRoutes(config?.routePrefix),
2224
- ...config?.routes
2221
+ ...appRoutes(config?.api.routePrefix),
2222
+ ...config?.api.routes
2225
2223
  };
2226
2224
  this.handlers = Handlers(this.routes, config);
2227
2225
  this.paths = {