@fedify/h3 2.0.0-pr.412.1559 → 2.0.0-pr.412.1692

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.cjs ADDED
@@ -0,0 +1,58 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ const h3 = __toESM(require("h3"));
25
+
26
+ //#region src/index.ts
27
+ /**
28
+ * Integrates a `Federation` instance with an H3 handler.
29
+ * @param federation
30
+ * @param contextDataFactory
31
+ * @returns
32
+ */
33
+ function integrateFederation(federation, contextDataFactory) {
34
+ return (0, h3.defineEventHandler)({ async handler(event) {
35
+ const request = (0, h3.toWebRequest)(event);
36
+ const response = await federation.fetch(request, { contextData: await contextDataFactory(event, request) });
37
+ if (response.status === 404) return;
38
+ if (response.status === 406) {
39
+ event.context["__fedify_response__"] = response;
40
+ return;
41
+ }
42
+ await event.respondWith(response);
43
+ } });
44
+ }
45
+ /**
46
+ * An error handler that responds with a 406 Not Acceptable if Fedify
47
+ * responded with a 406 Not Acceptable and the actual handler responded with
48
+ * a 404 Not Found.
49
+ * @param error The error that occurred.
50
+ * @param event The event that triggered the handler.
51
+ */
52
+ async function onError(error, event) {
53
+ if ("__fedify_response__" in event.context && event.context["__fedify_response__"].status === 406 && error.statusCode === 404) await event.respondWith(event.context["__fedify_response__"]);
54
+ }
55
+
56
+ //#endregion
57
+ exports.integrateFederation = integrateFederation;
58
+ exports.onError = onError;
@@ -0,0 +1,33 @@
1
+ import { Federation } from "@fedify/fedify";
2
+ import { EventHandler, EventHandlerRequest, EventHandlerResponse, H3Error, H3Event } from "h3";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * A factory function that creates the context data that will be passed to the
8
+ * `Federation` instance.
9
+ * @template TContextData The type of the context data that will be passed to
10
+ * the `Federation` instance.
11
+ * @param event The event that triggered the handler.
12
+ * @param request The request that triggered the handler.
13
+ * @returns The context data that will be passed to the `Federation` instance.
14
+ * This can be a promise that resolves to the context data.
15
+ */
16
+ type ContextDataFactory<TContextData> = (event: H3Event<EventHandlerRequest>, request: Request) => Promise<TContextData> | TContextData;
17
+ /**
18
+ * Integrates a `Federation` instance with an H3 handler.
19
+ * @param federation
20
+ * @param contextDataFactory
21
+ * @returns
22
+ */
23
+ declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory: ContextDataFactory<TContextData>): EventHandler<EventHandlerRequest, EventHandlerResponse>;
24
+ /**
25
+ * An error handler that responds with a 406 Not Acceptable if Fedify
26
+ * responded with a 406 Not Acceptable and the actual handler responded with
27
+ * a 404 Not Found.
28
+ * @param error The error that occurred.
29
+ * @param event The event that triggered the handler.
30
+ */
31
+ declare function onError(error: H3Error<unknown>, event: H3Event<EventHandlerResponse>): Promise<void>;
32
+ //#endregion
33
+ export { ContextDataFactory, integrateFederation, onError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/h3",
3
- "version": "2.0.0-pr.412.1559+4a4b1861",
3
+ "version": "2.0.0-pr.412.1692+4c27045a",
4
4
  "description": "Integrate Fedify with h3",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -27,14 +27,18 @@
27
27
  ],
28
28
  "type": "module",
29
29
  "module": "dist/index.js",
30
+ "main": "dist/index.cjs",
30
31
  "types": "dist/index.d.ts",
31
32
  "exports": {
32
33
  ".": {
33
- "import": {
34
- "types": "./dist/index.d.ts",
35
- "import": "./dist/index.js",
36
- "default": "./dist/index.js"
37
- }
34
+ "types": {
35
+ "import": "./dist/index.d.ts",
36
+ "require": "./dist/index.d.cts",
37
+ "default": "./dist/index.d.ts"
38
+ },
39
+ "import": "./dist/index.js",
40
+ "require": "./dist/index.cjs",
41
+ "default": "./dist/index.js"
38
42
  },
39
43
  "./package.json": "./package.json"
40
44
  },
@@ -48,7 +52,7 @@
48
52
  },
49
53
  "peerDependencies": {
50
54
  "h3": "^1.15.0",
51
- "@fedify/fedify": "2.0.0-pr.412.1559+4a4b1861"
55
+ "@fedify/fedify": "^2.0.0-pr.412.1692+4c27045a"
52
56
  },
53
57
  "scripts": {
54
58
  "build": "tsdown",