@fedify/next 2.0.0-dev.1604 → 2.0.0-dev.161

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright 2024–2025 Hong Minhee
3
+ Copyright 2024–2026 Hong Minhee
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
package/dist/index.cjs ADDED
@@ -0,0 +1,147 @@
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 next_server = __toESM(require("next/server"));
25
+
26
+ //#region src/index.ts
27
+ /**
28
+ * Wrapper function for Next.js middleware to integrate with the
29
+ * {@link Federation} object.
30
+ *
31
+ * @template TContextData A type of the context data for the
32
+ * {@link Federation} object.
33
+ * @param federation A {@link Federation} object to integrate with Next.js.
34
+ * @param contextDataFactory A function to create a context data for the
35
+ * {@link Federation} object.
36
+ * @param errorHandlers A set of error handlers to handle errors during
37
+ * the federation fetch.
38
+ * @returns A Next.js middleware function to integrate with the
39
+ * {@link Federation} object.
40
+ *
41
+ * @example
42
+ * ```ts ignore
43
+ * import { fedifyWith } from "@fedify/next";
44
+ * import { federation } from "./federation";
45
+ *
46
+ * export default fedifyWith(federation)(
47
+ * function (request: Request) {
48
+ * // You can add custom logic here for other requests
49
+ * // except federation requests. If there is no custom logic,
50
+ * // you can omit this function.
51
+ * }
52
+ * )
53
+ *
54
+ * // This config makes middleware process only requests with the
55
+ * // "Accept" header matching the federation accept regex.
56
+ * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
57
+ * export const config = {
58
+ * runtime: "nodejs",
59
+ * matcher: [
60
+ * {
61
+ * source: "/:path*",
62
+ * has: [
63
+ * {
64
+ * type: "header",
65
+ * key: "Accept",
66
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
67
+ * },
68
+ * ],
69
+ * },
70
+ * {
71
+ * source: "/:path*",
72
+ * has: [
73
+ * {
74
+ * type: "header",
75
+ * key: "content-type",
76
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
77
+ * },
78
+ * ],
79
+ * },
80
+ * { source: "/.well-known/nodeinfo" },
81
+ * { source: "/.well-known/x-nodeinfo2" },
82
+ * ],
83
+ * };
84
+ * ```
85
+ */
86
+ const fedifyWith = (federation, contextDataFactory, errorHandlers) => (middleware = (_) => next_server.NextResponse.next()) => async (request) => {
87
+ if (isFederationRequest(request)) return await integrateFederation(federation, contextDataFactory, errorHandlers)(request);
88
+ return await middleware(request);
89
+ };
90
+ const isFederationRequest = (request) => [
91
+ hasFederationHeader("accept"),
92
+ hasFederationHeader("content-type"),
93
+ isNodeInfoRequest
94
+ ].some((f) => f(request));
95
+ /**
96
+ * Check if the request has the header matching the federation
97
+ * accept regex.
98
+ * @param key The header key to check.
99
+ * @param request The request to check.
100
+ * @returns `true` if the request has the header matching
101
+ * the federation accept regex, `false` otherwise.
102
+ */
103
+ const hasFederationHeader = (key) => (request) => {
104
+ const value = request.headers.get(key);
105
+ return value ? FEDERATION_ACCEPT_REGEX.test(value) : false;
106
+ };
107
+ const isNodeInfoRequest = (request) => {
108
+ const url = new URL(request.url);
109
+ return NODEINFO_PATHS.some((path) => url.pathname.startsWith(path));
110
+ };
111
+ const NODEINFO_PATHS = ["/.well-known/nodeinfo", "/.well-known/x-nodeinfo2"];
112
+ const FEDERATION_ACCEPT_REGEX = /.*application\/((jrd|activity|ld)\+json|xrd\+xml).*/;
113
+ /**
114
+ * Create a Next.js handler to integrate with the {@link Federation} object.
115
+ *
116
+ * @template TContextData A type of the context data for the
117
+ * {@link Federation} object.
118
+ * @param federation A {@link Federation} object to integrate with Next.js.
119
+ * @param contextDataFactory A function to create a context data for the
120
+ * {@link Federation} object.
121
+ * @param errorHandlers A set of error handlers to handle errors during
122
+ * the federation fetch.
123
+ * @returns A Next.js handler.
124
+ */
125
+ function integrateFederation(federation, contextDataFactory = () => void 0, errorHandlers) {
126
+ return async (request) => await federation.fetch(request, {
127
+ contextData: await contextDataFactory(request),
128
+ onNotFound,
129
+ onNotAcceptable,
130
+ ...errorHandlers
131
+ });
132
+ }
133
+ const onNotFound = () => new Response("Not found", { status: 404 });
134
+ const onNotAcceptable = () => new Response("Not acceptable", {
135
+ status: 406,
136
+ headers: {
137
+ "Content-Type": "text/plain",
138
+ Vary: "Accept"
139
+ }
140
+ });
141
+
142
+ //#endregion
143
+ exports.fedifyWith = fedifyWith;
144
+ exports.hasFederationHeader = hasFederationHeader;
145
+ exports.integrateFederation = integrateFederation;
146
+ exports.isFederationRequest = isFederationRequest;
147
+ exports.isNodeInfoRequest = isNodeInfoRequest;
@@ -0,0 +1,94 @@
1
+ import { Federation, FederationFetchOptions } from "@fedify/fedify";
2
+
3
+ //#region src/index.d.ts
4
+
5
+ interface ContextDataFactory<TContextData> {
6
+ (request: Request): TContextData | Promise<TContextData>;
7
+ }
8
+ type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
9
+ /**
10
+ * Wrapper function for Next.js middleware to integrate with the
11
+ * {@link Federation} object.
12
+ *
13
+ * @template TContextData A type of the context data for the
14
+ * {@link Federation} object.
15
+ * @param federation A {@link Federation} object to integrate with Next.js.
16
+ * @param contextDataFactory A function to create a context data for the
17
+ * {@link Federation} object.
18
+ * @param errorHandlers A set of error handlers to handle errors during
19
+ * the federation fetch.
20
+ * @returns A Next.js middleware function to integrate with the
21
+ * {@link Federation} object.
22
+ *
23
+ * @example
24
+ * ```ts ignore
25
+ * import { fedifyWith } from "@fedify/next";
26
+ * import { federation } from "./federation";
27
+ *
28
+ * export default fedifyWith(federation)(
29
+ * function (request: Request) {
30
+ * // You can add custom logic here for other requests
31
+ * // except federation requests. If there is no custom logic,
32
+ * // you can omit this function.
33
+ * }
34
+ * )
35
+ *
36
+ * // This config makes middleware process only requests with the
37
+ * // "Accept" header matching the federation accept regex.
38
+ * // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
39
+ * export const config = {
40
+ * runtime: "nodejs",
41
+ * matcher: [
42
+ * {
43
+ * source: "/:path*",
44
+ * has: [
45
+ * {
46
+ * type: "header",
47
+ * key: "Accept",
48
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
49
+ * },
50
+ * ],
51
+ * },
52
+ * {
53
+ * source: "/:path*",
54
+ * has: [
55
+ * {
56
+ * type: "header",
57
+ * key: "content-type",
58
+ * value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
59
+ * },
60
+ * ],
61
+ * },
62
+ * { source: "/.well-known/nodeinfo" },
63
+ * { source: "/.well-known/x-nodeinfo2" },
64
+ * ],
65
+ * };
66
+ * ```
67
+ */
68
+ declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
69
+ declare const isFederationRequest: (request: Request) => boolean;
70
+ /**
71
+ * Check if the request has the header matching the federation
72
+ * accept regex.
73
+ * @param key The header key to check.
74
+ * @param request The request to check.
75
+ * @returns `true` if the request has the header matching
76
+ * the federation accept regex, `false` otherwise.
77
+ */
78
+ declare const hasFederationHeader: (key: string) => (request: Request) => boolean;
79
+ declare const isNodeInfoRequest: (request: Request) => boolean;
80
+ /**
81
+ * Create a Next.js handler to integrate with the {@link Federation} object.
82
+ *
83
+ * @template TContextData A type of the context data for the
84
+ * {@link Federation} object.
85
+ * @param federation A {@link Federation} object to integrate with Next.js.
86
+ * @param contextDataFactory A function to create a context data for the
87
+ * {@link Federation} object.
88
+ * @param errorHandlers A set of error handlers to handle errors during
89
+ * the federation fetch.
90
+ * @returns A Next.js handler.
91
+ */
92
+ declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
93
+ //#endregion
94
+ export { fedifyWith, hasFederationHeader, integrateFederation, isFederationRequest, isNodeInfoRequest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/next",
3
- "version": "2.0.0-dev.1604+23a1ea67",
3
+ "version": "2.0.0-dev.161+b505ad7a",
4
4
  "description": "Integrate Fedify with Next.js",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -29,19 +29,17 @@
29
29
  "https://github.com/sponsors/dahlia"
30
30
  ],
31
31
  "type": "module",
32
- "main": "./dist/index.js",
32
+ "main": "./dist/index.cjs",
33
33
  "module": "./dist/index.js",
34
34
  "types": "./dist/index.d.ts",
35
35
  "exports": {
36
36
  ".": {
37
37
  "require": {
38
- "types": "./dist/index.d.ts",
39
- "import": "./dist/index.js",
40
- "default": "./dist/index.js"
38
+ "types": "./dist/index.d.cts",
39
+ "default": "./dist/index.cjs"
41
40
  },
42
41
  "import": {
43
42
  "types": "./dist/index.d.ts",
44
- "import": "./dist/index.js",
45
43
  "default": "./dist/index.js"
46
44
  }
47
45
  },
@@ -53,11 +51,11 @@
53
51
  ],
54
52
  "peerDependencies": {
55
53
  "next": "^15.4.6",
56
- "@fedify/fedify": "^2.0.0-dev.1604+23a1ea67"
54
+ "@fedify/fedify": "^2.0.0-dev.161+b505ad7a"
57
55
  },
58
56
  "devDependencies": {
59
57
  "tsdown": "^0.12.9",
60
- "typescript": "^5.9.2"
58
+ "typescript": "^5.9.3"
61
59
  },
62
60
  "scripts": {
63
61
  "build": "tsdown",