@niledatabase/server 4.2.0-alpha.2 → 4.2.0-alpha.4

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
@@ -2432,24 +2432,38 @@ function handlersWithContext(configRoutes, config) {
2432
2432
  const updatedConfig = updateConfig(response, config);
2433
2433
  return { response, nile: new Server(updatedConfig) };
2434
2434
  },
2435
- POST: POST5,
2436
- DELETE: DELETE3,
2437
- PUT: PUT4
2435
+ POST: async (req) => {
2436
+ const response = await POST5(req);
2437
+ const updatedConfig = updateConfig(response, config);
2438
+ return { response, nile: new Server(updatedConfig) };
2439
+ },
2440
+ DELETE: async (req) => {
2441
+ const response = await DELETE3(req);
2442
+ const updatedConfig = updateConfig(response, config);
2443
+ return { response, nile: new Server(updatedConfig) };
2444
+ },
2445
+ PUT: async (req) => {
2446
+ const response = await PUT4(req);
2447
+ const updatedConfig = updateConfig(response, config);
2448
+ return { response, nile: new Server(updatedConfig) };
2449
+ }
2438
2450
  };
2439
2451
  }
2440
2452
  function updateConfig(response, config) {
2441
2453
  let origin = "http://localhost:3000";
2442
2454
  let headers = null;
2443
- if (response.status === 302) {
2455
+ if (response?.status === 302) {
2444
2456
  const location = response.headers.get("location");
2445
2457
  if (location) {
2446
2458
  origin = location;
2447
2459
  }
2448
2460
  }
2449
2461
  const setCookies = [];
2450
- for (const [key12, value] of response.headers) {
2451
- if (key12.toLowerCase() === "set-cookie") {
2452
- setCookies.push(value);
2462
+ if (response?.headers) {
2463
+ for (const [key12, value] of response.headers) {
2464
+ if (key12.toLowerCase() === "set-cookie") {
2465
+ setCookies.push(value);
2466
+ }
2453
2467
  }
2454
2468
  }
2455
2469
  if (setCookies.length > 0) {
@@ -2546,6 +2560,9 @@ var Api = class {
2546
2560
  this.#headers = new Headers(headers ?? {});
2547
2561
  this.reset();
2548
2562
  };
2563
+ /**
2564
+ * Merge headers together
2565
+ */
2549
2566
  set headers(headers) {
2550
2567
  const updates = [];
2551
2568
  if (headers instanceof Headers) {
@@ -2559,7 +2576,9 @@ var Api = class {
2559
2576
  }
2560
2577
  const merged = {};
2561
2578
  this.#headers?.forEach((value, key12) => {
2562
- merged[key12.toLowerCase()] = value;
2579
+ if (key12.toLowerCase() !== "cookie") {
2580
+ merged[key12.toLowerCase()] = value;
2581
+ }
2563
2582
  });
2564
2583
  for (const [key12, value] of updates) {
2565
2584
  merged[key12] = value;
@@ -2586,8 +2605,18 @@ var Api = class {
2586
2605
  this.config,
2587
2606
  this.handlers
2588
2607
  )(payload);
2589
- this.headers = headers;
2590
2608
  this.setContext(headers);
2609
+ try {
2610
+ const res = await loginRes.json();
2611
+ if (res.id) {
2612
+ this.config.userId = res.id;
2613
+ }
2614
+ } catch (e) {
2615
+ const { warn } = Logger(this.config, "[API][login]");
2616
+ if (warn) {
2617
+ warn("Unable to set user id from login attempt.");
2618
+ }
2619
+ }
2591
2620
  if (config?.returnResponse) {
2592
2621
  return loginRes;
2593
2622
  }
@@ -2602,23 +2631,26 @@ var Api = class {
2602
2631
  return this.auth.getSession(this.#headers);
2603
2632
  };
2604
2633
  setContext = (req) => {
2605
- if (req instanceof Headers) {
2606
- this.headers = req;
2607
- return;
2608
- } else if (req instanceof Request) {
2609
- this.headers = new Headers(req.headers);
2610
- return;
2611
- }
2612
- const headers = new Headers(req);
2613
- if (headers) {
2614
- this.headers = headers;
2615
- } else {
2616
- const { warn } = Logger(this.config, "[API]");
2617
- if (warn) {
2618
- warn(
2619
- "Set context expects a Request, Header instance or an object of Record<string, string>"
2620
- );
2634
+ try {
2635
+ if (req instanceof Headers) {
2636
+ this.headers = req;
2637
+ return;
2638
+ } else if (req instanceof Request) {
2639
+ this.headers = new Headers(req.headers);
2640
+ return;
2621
2641
  }
2642
+ const headers = new Headers(req);
2643
+ if (headers) {
2644
+ this.headers = headers;
2645
+ return;
2646
+ }
2647
+ } catch {
2648
+ }
2649
+ const { warn } = Logger(this.config, "[API]");
2650
+ if (warn) {
2651
+ warn(
2652
+ "Set context expects a Request, Header instance or an object of Record<string, string>"
2653
+ );
2622
2654
  }
2623
2655
  };
2624
2656
  };