@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.mjs CHANGED
@@ -433,7 +433,7 @@ async function fetchTenantUsers(config, method, payload) {
433
433
  const { body, params } = {};
434
434
  if (!config.tenantId) {
435
435
  throw new Error(
436
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
436
+ "Unable to fetch the user's tenants, the tenantId context is missing. Call nile.setContext({ tenantId })"
437
437
  );
438
438
  }
439
439
  if (!isUUID(config.tenantId)) {
@@ -524,7 +524,7 @@ function matches4(configRoutes, request2) {
524
524
  async function fetchInvite(config, method, body) {
525
525
  if (!config.tenantId) {
526
526
  throw new Error(
527
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
527
+ "Unable to fetch the invite for the tenant, the tenantId context is missing. Call nile.setContext({ tenantId })"
528
528
  );
529
529
  }
530
530
  if (!isUUID(config.tenantId)) {
@@ -579,7 +579,7 @@ function matches5(configRoutes, request2) {
579
579
  async function fetchInvites(config) {
580
580
  if (!config.tenantId) {
581
581
  throw new Error(
582
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
582
+ "Unable to fetch invites for the tenant, the tenantId context is missing. Call nile.setContext({ tenantId })"
583
583
  );
584
584
  }
585
585
  if (!isUUID(config.tenantId)) {
@@ -692,7 +692,7 @@ async function fetchTenants(config, method, body) {
692
692
  async function fetchTenant(config, method, body) {
693
693
  if (!config.tenantId) {
694
694
  throw new Error(
695
- 'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
695
+ "Unable to fetch tenants, the tenantId context is missing. Call nile.setContext({ tenantId })"
696
696
  );
697
697
  }
698
698
  if (!isUUID(config.tenantId)) {
@@ -2509,16 +2509,13 @@ var Users = class {
2509
2509
  async verifySelf(options, rawResponse = false) {
2510
2510
  const bypassEmail = typeof options === "object" ? options.bypassEmail ?? process.env.NODE_ENV !== "production" : process.env.NODE_ENV !== "production";
2511
2511
  const callbackUrl = typeof options === "object" ? options.callbackUrl : defaultCallbackUrl2(this.#config).callbackUrl;
2512
+ let res;
2512
2513
  try {
2513
2514
  const me = await this.getSelf();
2514
2515
  if (me instanceof Response) {
2515
2516
  return me;
2516
2517
  }
2517
- const res = await verifyEmailAddress(
2518
- this.#config,
2519
- me,
2520
- String(callbackUrl)
2521
- );
2518
+ res = await verifyEmailAddress(this.#config, me, String(callbackUrl));
2522
2519
  return res;
2523
2520
  } catch {
2524
2521
  this.#logger?.warn(
@@ -2526,12 +2523,12 @@ var Users = class {
2526
2523
  );
2527
2524
  }
2528
2525
  if (bypassEmail) {
2529
- return this.updateSelf({ emailVerified: true }, rawResponse);
2526
+ res = this.updateSelf({ emailVerified: true }, rawResponse);
2530
2527
  }
2531
2528
  this.#logger.error(
2532
2529
  "Unable to verify email address. Configure your SMTP server in the console."
2533
2530
  );
2534
- return void 0;
2531
+ return res;
2535
2532
  }
2536
2533
  };
2537
2534
  async function verifyEmailAddress(config, user, callback) {
@@ -2842,7 +2839,8 @@ function updateConfig(response, config) {
2842
2839
  return {
2843
2840
  ...config,
2844
2841
  origin,
2845
- headers: headers ?? void 0
2842
+ headers: headers ?? void 0,
2843
+ preserveHeaders: true
2846
2844
  };
2847
2845
  }
2848
2846
 
@@ -2857,17 +2855,32 @@ function bindHandleOnRequest(instance) {
2857
2855
  }
2858
2856
  const ext = await create2(instance);
2859
2857
  if (ext.onRequest) {
2860
- const modified = await ext.onRequest(_init.request);
2861
- if (modified?.headers) {
2862
- const modHeaders = new Headers(modified.headers);
2863
- const cookie = modHeaders.get("cookie");
2858
+ const previousContext = instance.getContext();
2859
+ if (previousContext.preserveHeaders) {
2860
+ instance.setContext({ preserveHeaders: false });
2861
+ }
2862
+ await ext.onRequest(_init.request);
2863
+ const updatedContext = instance.getContext();
2864
+ if (updatedContext?.headers) {
2865
+ const cookie = updatedContext.headers.get("cookie");
2864
2866
  if (cookie) {
2865
- params.headers.set("cookie", cookie);
2866
- debug(`${ext.id ?? create2.name} modified cookie`);
2867
+ params.headers.set(
2868
+ "cookie",
2869
+ mergeCookies(
2870
+ previousContext.preserveHeaders ? previousContext.headers?.get("cookie") : null,
2871
+ updatedContext.headers.get("cookie")
2872
+ )
2873
+ );
2874
+ }
2875
+ if (updatedContext.tenantId) {
2876
+ params.headers.set(
2877
+ TENANT_COOKIE,
2878
+ String(updatedContext.headers.get(TENANT_COOKIE))
2879
+ );
2867
2880
  }
2868
2881
  }
2882
+ debug(`${ext.id ?? create2.name} ran onRequest`);
2869
2883
  }
2870
- debug(`${ext.id ?? create2.name} ran onRequest`);
2871
2884
  }
2872
2885
  }
2873
2886
  };
@@ -2877,6 +2890,17 @@ function buildExtensionConfig(instance) {
2877
2890
  handleOnRequest: bindHandleOnRequest(instance)
2878
2891
  };
2879
2892
  }
2893
+ function mergeCookies(...cookieStrings) {
2894
+ const cookieMap = /* @__PURE__ */ new Map();
2895
+ for (const str of cookieStrings) {
2896
+ if (!str) continue;
2897
+ for (const part of str.split(";")) {
2898
+ const [key17, value] = part.split("=").map((s) => s.trim());
2899
+ if (key17 && value) cookieMap.set(key17, value);
2900
+ }
2901
+ }
2902
+ return [...cookieMap.entries()].map(([k, v]) => `${k}=${v}`).join("; ");
2903
+ }
2880
2904
 
2881
2905
  // src/Server.ts
2882
2906
  var Server = class {
@@ -2888,6 +2912,7 @@ var Server = class {
2888
2912
  #paths;
2889
2913
  #manager;
2890
2914
  #headers;
2915
+ #preserveHeaders;
2891
2916
  constructor(config) {
2892
2917
  this.#config = new Config({
2893
2918
  ...config,
@@ -2913,6 +2938,7 @@ var Server = class {
2913
2938
  ...this.#config.handlers,
2914
2939
  withContext: handlersWithContext(this.#config)
2915
2940
  };
2941
+ this.#preserveHeaders = config?.preserveHeaders ?? false;
2916
2942
  this.#paths = this.#config.paths;
2917
2943
  this.#config.tenantId = getTenantId({ config: this.#config });
2918
2944
  this.#manager = new DBManager(this.#config);
@@ -2979,6 +3005,10 @@ var Server = class {
2979
3005
  ok = true;
2980
3006
  this.#config.userId = req.userId;
2981
3007
  }
3008
+ if (req && typeof req === "object" && "preserveHeaders" in req) {
3009
+ ok = true;
3010
+ this.#preserveHeaders = Boolean(req.preserveHeaders);
3011
+ }
2982
3012
  if (ok) {
2983
3013
  return;
2984
3014
  }
@@ -3001,7 +3031,8 @@ var Server = class {
3001
3031
  return {
3002
3032
  headers: this.#headers,
3003
3033
  userId: this.#config.userId,
3004
- tenantId: this.#config.tenantId
3034
+ tenantId: this.#config.tenantId,
3035
+ preserveHeaders: this.#preserveHeaders
3005
3036
  };
3006
3037
  }
3007
3038
  /**
@@ -3045,6 +3076,7 @@ var Server = class {
3045
3076
  for (const [key17, value] of Object.entries(merged)) {
3046
3077
  this.#headers.set(key17, value);
3047
3078
  }
3079
+ this.#config.logger("[handleHeaders]").debug(JSON.stringify(merged));
3048
3080
  this.#config.headers = this.#headers;
3049
3081
  }
3050
3082
  /**