@niledatabase/server 5.0.0-alpha.6 → 5.0.0-alpha.8

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.d.mts CHANGED
@@ -376,6 +376,7 @@ declare class Server {
376
376
  headers: Headers | undefined;
377
377
  userId: string | null | undefined;
378
378
  tenantId: string | null | undefined;
379
+ preserveHeaders: boolean;
379
380
  };
380
381
  }
381
382
  declare function create(config?: NileConfig): Server;
@@ -483,6 +484,11 @@ type NileConfig = {
483
484
  * Functions to run at various points to make life easier
484
485
  */
485
486
  extensions?: Extension[];
487
+ /**
488
+ * In some cases, like when using REST context, we want to preserve the headers from the request,
489
+ * regardless of what an extension might do
490
+ */
491
+ preserveHeaders?: boolean;
486
492
  };
487
493
  type NileDb = NilePoolConfig & {
488
494
  tenantId?: string;
package/dist/index.d.ts CHANGED
@@ -376,6 +376,7 @@ declare class Server {
376
376
  headers: Headers | undefined;
377
377
  userId: string | null | undefined;
378
378
  tenantId: string | null | undefined;
379
+ preserveHeaders: boolean;
379
380
  };
380
381
  }
381
382
  declare function create(config?: NileConfig): Server;
@@ -483,6 +484,11 @@ type NileConfig = {
483
484
  * Functions to run at various points to make life easier
484
485
  */
485
486
  extensions?: Extension[];
487
+ /**
488
+ * In some cases, like when using REST context, we want to preserve the headers from the request,
489
+ * regardless of what an extension might do
490
+ */
491
+ preserveHeaders?: boolean;
486
492
  };
487
493
  type NileDb = NilePoolConfig & {
488
494
  tenantId?: string;
package/dist/index.js CHANGED
@@ -439,7 +439,7 @@ async function fetchTenantUsers(config, method, payload) {
439
439
  const { body, params } = {};
440
440
  if (!config.tenantId) {
441
441
  throw new Error(
442
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
442
+ "Unable to fetch the user's tenants, the tenantId context is missing. Call nile.setContext({ tenantId })"
443
443
  );
444
444
  }
445
445
  if (!isUUID(config.tenantId)) {
@@ -530,7 +530,7 @@ function matches4(configRoutes, request2) {
530
530
  async function fetchInvite(config, method, body) {
531
531
  if (!config.tenantId) {
532
532
  throw new Error(
533
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
533
+ "Unable to fetch the invite for the tenant, the tenantId context is missing. Call nile.setContext({ tenantId })"
534
534
  );
535
535
  }
536
536
  if (!isUUID(config.tenantId)) {
@@ -585,7 +585,7 @@ function matches5(configRoutes, request2) {
585
585
  async function fetchInvites(config) {
586
586
  if (!config.tenantId) {
587
587
  throw new Error(
588
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
588
+ "Unable to fetch invites for the tenant, the tenantId context is missing. Call nile.setContext({ tenantId })"
589
589
  );
590
590
  }
591
591
  if (!isUUID(config.tenantId)) {
@@ -698,7 +698,7 @@ async function fetchTenants(config, method, body) {
698
698
  async function fetchTenant(config, method, body) {
699
699
  if (!config.tenantId) {
700
700
  throw new Error(
701
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
701
+ "Unable to fetch tenants, the tenantId context is missing. Call nile.setContext({ tenantId })"
702
702
  );
703
703
  }
704
704
  if (!isUUID(config.tenantId)) {
@@ -2515,16 +2515,13 @@ var Users = class {
2515
2515
  async verifySelf(options, rawResponse = false) {
2516
2516
  const bypassEmail = typeof options === "object" ? options.bypassEmail ?? process.env.NODE_ENV !== "production" : process.env.NODE_ENV !== "production";
2517
2517
  const callbackUrl = typeof options === "object" ? options.callbackUrl : defaultCallbackUrl2(this.#config).callbackUrl;
2518
+ let res;
2518
2519
  try {
2519
2520
  const me = await this.getSelf();
2520
2521
  if (me instanceof Response) {
2521
2522
  return me;
2522
2523
  }
2523
- const res = await verifyEmailAddress(
2524
- this.#config,
2525
- me,
2526
- String(callbackUrl)
2527
- );
2524
+ res = await verifyEmailAddress(this.#config, me, String(callbackUrl));
2528
2525
  return res;
2529
2526
  } catch {
2530
2527
  this.#logger?.warn(
@@ -2532,12 +2529,12 @@ var Users = class {
2532
2529
  );
2533
2530
  }
2534
2531
  if (bypassEmail) {
2535
- return this.updateSelf({ emailVerified: true }, rawResponse);
2532
+ res = this.updateSelf({ emailVerified: true }, rawResponse);
2536
2533
  }
2537
2534
  this.#logger.error(
2538
2535
  "Unable to verify email address. Configure your SMTP server in the console."
2539
2536
  );
2540
- return void 0;
2537
+ return res;
2541
2538
  }
2542
2539
  };
2543
2540
  async function verifyEmailAddress(config, user, callback) {
@@ -2848,7 +2845,8 @@ function updateConfig(response, config) {
2848
2845
  return {
2849
2846
  ...config,
2850
2847
  origin,
2851
- headers: headers ?? void 0
2848
+ headers: headers ?? void 0,
2849
+ preserveHeaders: true
2852
2850
  };
2853
2851
  }
2854
2852
 
@@ -2863,17 +2861,32 @@ function bindHandleOnRequest(instance) {
2863
2861
  }
2864
2862
  const ext = await create2(instance);
2865
2863
  if (ext.onRequest) {
2866
- const modified = await ext.onRequest(_init.request);
2867
- if (modified?.headers) {
2868
- const modHeaders = new Headers(modified.headers);
2869
- const cookie = modHeaders.get("cookie");
2864
+ const previousContext = instance.getContext();
2865
+ if (previousContext.preserveHeaders) {
2866
+ instance.setContext({ preserveHeaders: false });
2867
+ }
2868
+ await ext.onRequest(_init.request);
2869
+ const updatedContext = instance.getContext();
2870
+ if (updatedContext?.headers) {
2871
+ const cookie = updatedContext.headers.get("cookie");
2870
2872
  if (cookie) {
2871
- params.headers.set("cookie", cookie);
2872
- debug(`${ext.id ?? create2.name} modified cookie`);
2873
+ params.headers.set(
2874
+ "cookie",
2875
+ mergeCookies(
2876
+ previousContext.preserveHeaders ? previousContext.headers?.get("cookie") : null,
2877
+ updatedContext.headers.get("cookie")
2878
+ )
2879
+ );
2880
+ }
2881
+ if (updatedContext.tenantId) {
2882
+ params.headers.set(
2883
+ TENANT_COOKIE,
2884
+ String(updatedContext.headers.get(TENANT_COOKIE))
2885
+ );
2873
2886
  }
2874
2887
  }
2888
+ debug(`${ext.id ?? create2.name} ran onRequest`);
2875
2889
  }
2876
- debug(`${ext.id ?? create2.name} ran onRequest`);
2877
2890
  }
2878
2891
  }
2879
2892
  };
@@ -2883,6 +2896,17 @@ function buildExtensionConfig(instance) {
2883
2896
  handleOnRequest: bindHandleOnRequest(instance)
2884
2897
  };
2885
2898
  }
2899
+ function mergeCookies(...cookieStrings) {
2900
+ const cookieMap = /* @__PURE__ */ new Map();
2901
+ for (const str of cookieStrings) {
2902
+ if (!str) continue;
2903
+ for (const part of str.split(";")) {
2904
+ const [key17, value] = part.split("=").map((s) => s.trim());
2905
+ if (key17 && value) cookieMap.set(key17, value);
2906
+ }
2907
+ }
2908
+ return [...cookieMap.entries()].map(([k, v]) => `${k}=${v}`).join("; ");
2909
+ }
2886
2910
 
2887
2911
  // src/Server.ts
2888
2912
  var Server = class {
@@ -2894,6 +2918,7 @@ var Server = class {
2894
2918
  #paths;
2895
2919
  #manager;
2896
2920
  #headers;
2921
+ #preserveHeaders;
2897
2922
  constructor(config) {
2898
2923
  this.#config = new Config({
2899
2924
  ...config,
@@ -2919,6 +2944,7 @@ var Server = class {
2919
2944
  ...this.#config.handlers,
2920
2945
  withContext: handlersWithContext(this.#config)
2921
2946
  };
2947
+ this.#preserveHeaders = config?.preserveHeaders ?? false;
2922
2948
  this.#paths = this.#config.paths;
2923
2949
  this.#config.tenantId = getTenantId({ config: this.#config });
2924
2950
  this.#manager = new DBManager(this.#config);
@@ -2985,6 +3011,10 @@ var Server = class {
2985
3011
  ok = true;
2986
3012
  this.#config.userId = req.userId;
2987
3013
  }
3014
+ if (req && typeof req === "object" && "preserveHeaders" in req) {
3015
+ ok = true;
3016
+ this.#preserveHeaders = Boolean(req.preserveHeaders);
3017
+ }
2988
3018
  if (ok) {
2989
3019
  return;
2990
3020
  }
@@ -3007,7 +3037,8 @@ var Server = class {
3007
3037
  return {
3008
3038
  headers: this.#headers,
3009
3039
  userId: this.#config.userId,
3010
- tenantId: this.#config.tenantId
3040
+ tenantId: this.#config.tenantId,
3041
+ preserveHeaders: this.#preserveHeaders
3011
3042
  };
3012
3043
  }
3013
3044
  /**
@@ -3051,6 +3082,7 @@ var Server = class {
3051
3082
  for (const [key17, value] of Object.entries(merged)) {
3052
3083
  this.#headers.set(key17, value);
3053
3084
  }
3085
+ this.#config.logger("[handleHeaders]").debug(JSON.stringify(merged));
3054
3086
  this.#config.headers = this.#headers;
3055
3087
  }
3056
3088
  /**