@niledatabase/server 5.0.0-alpha.7 → 5.0.0-alpha.9

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)) {
@@ -2333,7 +2333,7 @@ var Auth = class {
2333
2333
  }
2334
2334
  if (urlError) {
2335
2335
  error("Unable to log user in", { error: urlError });
2336
- return void 0;
2336
+ return new Response(urlError, { status: signInRes.status });
2337
2337
  }
2338
2338
  }
2339
2339
  if (!token) {
@@ -2515,29 +2515,26 @@ 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
- this.#logger?.warn(
2531
- "Unable to verify email. The current user's email will be set to verified anyway. Be sure to configure emails for production."
2532
- );
2527
+ const message = "Unable to verify email.";
2528
+ this.#logger?.warn(message);
2529
+ res = new Response(message, { status: 400 });
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,12 +2861,22 @@ function bindHandleOnRequest(instance) {
2863
2861
  }
2864
2862
  const ext = await create2(instance);
2865
2863
  if (ext.onRequest) {
2864
+ const previousContext = instance.getContext();
2865
+ if (previousContext.preserveHeaders) {
2866
+ instance.setContext({ preserveHeaders: false });
2867
+ }
2866
2868
  await ext.onRequest(_init.request);
2867
2869
  const updatedContext = instance.getContext();
2868
2870
  if (updatedContext?.headers) {
2869
2871
  const cookie = updatedContext.headers.get("cookie");
2870
2872
  if (cookie) {
2871
- params.headers.set("cookie", cookie);
2873
+ params.headers.set(
2874
+ "cookie",
2875
+ mergeCookies(
2876
+ previousContext.preserveHeaders ? previousContext.headers?.get("cookie") : null,
2877
+ updatedContext.headers.get("cookie")
2878
+ )
2879
+ );
2872
2880
  }
2873
2881
  if (updatedContext.tenantId) {
2874
2882
  params.headers.set(
@@ -2888,6 +2896,17 @@ function buildExtensionConfig(instance) {
2888
2896
  handleOnRequest: bindHandleOnRequest(instance)
2889
2897
  };
2890
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
+ }
2891
2910
 
2892
2911
  // src/Server.ts
2893
2912
  var Server = class {
@@ -2899,6 +2918,7 @@ var Server = class {
2899
2918
  #paths;
2900
2919
  #manager;
2901
2920
  #headers;
2921
+ #preserveHeaders;
2902
2922
  constructor(config) {
2903
2923
  this.#config = new Config({
2904
2924
  ...config,
@@ -2924,6 +2944,7 @@ var Server = class {
2924
2944
  ...this.#config.handlers,
2925
2945
  withContext: handlersWithContext(this.#config)
2926
2946
  };
2947
+ this.#preserveHeaders = config?.preserveHeaders ?? false;
2927
2948
  this.#paths = this.#config.paths;
2928
2949
  this.#config.tenantId = getTenantId({ config: this.#config });
2929
2950
  this.#manager = new DBManager(this.#config);
@@ -2990,6 +3011,10 @@ var Server = class {
2990
3011
  ok = true;
2991
3012
  this.#config.userId = req.userId;
2992
3013
  }
3014
+ if (req && typeof req === "object" && "preserveHeaders" in req) {
3015
+ ok = true;
3016
+ this.#preserveHeaders = Boolean(req.preserveHeaders);
3017
+ }
2993
3018
  if (ok) {
2994
3019
  return;
2995
3020
  }
@@ -3012,7 +3037,8 @@ var Server = class {
3012
3037
  return {
3013
3038
  headers: this.#headers,
3014
3039
  userId: this.#config.userId,
3015
- tenantId: this.#config.tenantId
3040
+ tenantId: this.#config.tenantId,
3041
+ preserveHeaders: this.#preserveHeaders
3016
3042
  };
3017
3043
  }
3018
3044
  /**