@chidchanun/bcp 0.1.6 → 0.1.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.
@@ -49,6 +49,11 @@ import {
49
49
  type MetadataSource,
50
50
  } from "./metadata.js";
51
51
 
52
+ import {
53
+ applyResponseCookies,
54
+ runWithRequestContext,
55
+ } from "./request-context.ts";
56
+
52
57
  import {
53
58
  getContentType,
54
59
  readPublicAsset,
@@ -1405,7 +1410,8 @@ async function handleApiRoute(
1405
1410
 
1406
1411
  if (
1407
1412
  !handler &&
1408
- method === "HEAD"
1413
+ method ===
1414
+ "HEAD"
1409
1415
  ) {
1410
1416
  handler =
1411
1417
  getApiHandler(
@@ -1434,23 +1440,48 @@ async function handleApiRoute(
1434
1440
  return;
1435
1441
  }
1436
1442
 
1443
+ const request =
1444
+ await createWebRequest(
1445
+ req,
1446
+ requestUrl
1447
+ );
1448
+
1437
1449
  const response =
1438
- await handler(
1439
- await createWebRequest(
1440
- req,
1441
- requestUrl
1442
- ),
1450
+ await runWithRequestContext(
1451
+ request,
1452
+
1453
+ async () => {
1454
+ const result =
1455
+ await handler(
1456
+ request,
1457
+ {
1458
+ params,
1459
+ }
1460
+ );
1461
+
1462
+ if (
1463
+ !(
1464
+ result instanceof
1465
+ Response
1466
+ )
1467
+ ) {
1468
+ throw new Error(
1469
+ `API handler "${method} ${definition.pathname}" must return a Response object.`
1470
+ );
1471
+ }
1472
+
1473
+ return applyResponseCookies(
1474
+ result
1475
+ );
1476
+ },
1443
1477
  {
1444
- params,
1478
+ remoteAddress:
1479
+ req.socket
1480
+ .remoteAddress ??
1481
+ null,
1445
1482
  }
1446
1483
  );
1447
1484
 
1448
- if (!(response instanceof Response)) {
1449
- throw new Error(
1450
- `API handler "${method} ${definition.pathname}" must return a Response object.`
1451
- );
1452
- }
1453
-
1454
1485
  await sendWebResponse(
1455
1486
  req,
1456
1487
  res,