@fedify/next 1.9.0-pr.355.1353
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 +20 -0
- package/README.md +204 -0
- package/dist/index.d.mts +78 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.js +121 -0
- package/dist/index.mjs +96 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024–2025 Hong Minhee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
<!-- deno-fmt-ignore-file -->
|
|
2
|
+
|
|
3
|
+
@fedify/next: Integrate Fedify with Next.js
|
|
4
|
+
===========================================
|
|
5
|
+
|
|
6
|
+
[![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
|
|
7
|
+
|
|
8
|
+
This package provides a simple way to integrate [Fedify] with [Next.js].
|
|
9
|
+
|
|
10
|
+
> [!IMPORTANT]
|
|
11
|
+
> We recommend initializing your app using the `init` command of the
|
|
12
|
+
> [Fedify CLI] rather than installing this package directly.
|
|
13
|
+
|
|
14
|
+
> [!IMPORTANT]
|
|
15
|
+
> This package runs Next.js middleware on the Node.js runtime.
|
|
16
|
+
> Therefore, you must use version 15.5 or later, or at least 15.4 canary.
|
|
17
|
+
> For more details, refer to the [official documentation of `middleware`].
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Usage
|
|
22
|
+
|
|
23
|
+
~~~~ typescript
|
|
24
|
+
// --- middleware.ts ---
|
|
25
|
+
import { fedifyWith } from "@fedify/next";
|
|
26
|
+
import { federation } from "./federation";
|
|
27
|
+
|
|
28
|
+
export default fedifyWith(federation)();
|
|
29
|
+
|
|
30
|
+
// This config must be defined on `middleware.ts`.
|
|
31
|
+
export const config = {
|
|
32
|
+
runtime: "nodejs",
|
|
33
|
+
matcher: [{
|
|
34
|
+
source: "/:path*",
|
|
35
|
+
has: [
|
|
36
|
+
{
|
|
37
|
+
type: "header",
|
|
38
|
+
key: "Accept",
|
|
39
|
+
value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
}],
|
|
43
|
+
};
|
|
44
|
+
~~~~
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
The integration code looks like this:
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
~~~~ typescript
|
|
51
|
+
/**
|
|
52
|
+
* Fedify with Next.js
|
|
53
|
+
* ===================
|
|
54
|
+
*
|
|
55
|
+
* This module provides a [Next.js] middleware to integrate with the Fedify.
|
|
56
|
+
*
|
|
57
|
+
* [Next.js]: https://nextjs.org/
|
|
58
|
+
*
|
|
59
|
+
* @module
|
|
60
|
+
* @since 1.9.0
|
|
61
|
+
*/
|
|
62
|
+
import type { Federation, FederationFetchOptions } from "@fedify/fedify";
|
|
63
|
+
import { notFound } from "next/navigation";
|
|
64
|
+
import { NextResponse } from "next/server";
|
|
65
|
+
import { getXForwardedRequest } from "x-forwarded-fetch";
|
|
66
|
+
|
|
67
|
+
interface ContextDataFactory<TContextData> {
|
|
68
|
+
(request: Request):
|
|
69
|
+
| TContextData
|
|
70
|
+
| Promise<TContextData>;
|
|
71
|
+
}
|
|
72
|
+
type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Wrapper function for Next.js middleware to integrate with the
|
|
76
|
+
* {@link Federation} object.
|
|
77
|
+
*
|
|
78
|
+
* @template TContextData A type of the context data for the
|
|
79
|
+
* {@link Federation} object.
|
|
80
|
+
* @param {Federation<TContextData>} federation A {@link Federation} object
|
|
81
|
+
* to integrate with Next.js.
|
|
82
|
+
* @param {ContextDataFactory<TContextData>} contextDataFactory A function
|
|
83
|
+
* to create a context data for the
|
|
84
|
+
* {@link Federation} object.
|
|
85
|
+
* @param {Partial<ErrorHandlers>} errorHandlers A set of error handlers to
|
|
86
|
+
* handle errors during the federation fetch.
|
|
87
|
+
* @returns A Next.js middleware function to integrate with the
|
|
88
|
+
* {@link Federation} object.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { fedifyWith } from "@fedify/next";
|
|
93
|
+
* import { federation } from "./federation";
|
|
94
|
+
*
|
|
95
|
+
* export default fedifyWith(federation)(
|
|
96
|
+
* function (request: Request) {
|
|
97
|
+
* // You can add custom logic here for other requests
|
|
98
|
+
* // except federation requests. If there is no custom logic,
|
|
99
|
+
* // you can omit this function.
|
|
100
|
+
* }
|
|
101
|
+
* )
|
|
102
|
+
*
|
|
103
|
+
* // This config makes middleware process only requests with the
|
|
104
|
+
* // "Accept" header matching the federation accept regex.
|
|
105
|
+
* // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
|
|
106
|
+
* export const config = {
|
|
107
|
+
* runtime: "nodejs",
|
|
108
|
+
* matcher: [{
|
|
109
|
+
* source: "/:path*",
|
|
110
|
+
* has: [
|
|
111
|
+
* {
|
|
112
|
+
* type: "header",
|
|
113
|
+
* key: "Accept",
|
|
114
|
+
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
|
|
115
|
+
* },
|
|
116
|
+
* ],
|
|
117
|
+
* }],
|
|
118
|
+
* };
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export const fedifyWith = <TContextData>(
|
|
122
|
+
federation: Federation<TContextData>,
|
|
123
|
+
contextDataFactory?: ContextDataFactory<TContextData>,
|
|
124
|
+
errorHandlers?: Partial<ErrorHandlers>,
|
|
125
|
+
) =>
|
|
126
|
+
(
|
|
127
|
+
middleware: (request: Request) => unknown =
|
|
128
|
+
((_: Request) => NextResponse.next()),
|
|
129
|
+
): (request: Request) => unknown =>
|
|
130
|
+
async (request: Request) => {
|
|
131
|
+
if (hasFederationAcceptHeader(request)) {
|
|
132
|
+
return await integrateFederation(
|
|
133
|
+
federation,
|
|
134
|
+
contextDataFactory,
|
|
135
|
+
errorHandlers,
|
|
136
|
+
)(request);
|
|
137
|
+
}
|
|
138
|
+
return await middleware(request);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Check if the request has the "Accept" header matching the federation
|
|
143
|
+
* accept regex.
|
|
144
|
+
*
|
|
145
|
+
* @param {Request} request The request to check.
|
|
146
|
+
* @returns {boolean} `true` if the request has the "Accept" header matching
|
|
147
|
+
* the federation accept regex, `false` otherwise.
|
|
148
|
+
*/
|
|
149
|
+
export const hasFederationAcceptHeader = (request: Request): boolean => {
|
|
150
|
+
const acceptHeader = request.headers.get("Accept");
|
|
151
|
+
// Check if the Accept header matches the federation accept regex.
|
|
152
|
+
// If the header is not present, return false.
|
|
153
|
+
return acceptHeader ? FEDERATION_ACCEPT_REGEX.test(acceptHeader) : false;
|
|
154
|
+
};
|
|
155
|
+
const FEDERATION_ACCEPT_REGEX =
|
|
156
|
+
/.*application\/((jrd|activity|ld)\+json|xrd\+xml).*/;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Create a Next.js handler to integrate with the {@link Federation} object.
|
|
160
|
+
*
|
|
161
|
+
* @template TContextData A type of the context data for the
|
|
162
|
+
* {@link Federation} object.
|
|
163
|
+
* @param {Federation<TContextData>} federation A {@link Federation} object
|
|
164
|
+
* to integrate with Next.js.
|
|
165
|
+
* @param {ContextDataFactory<TContextData>} contextDataFactory A function
|
|
166
|
+
* to create a context data for the
|
|
167
|
+
* {@link Federation} object.
|
|
168
|
+
* @param {Partial<ErrorHandlers>} errorHandlers A set of error handlers to
|
|
169
|
+
* handle errors during the federation fetch.
|
|
170
|
+
* @returns A Next.js handler.
|
|
171
|
+
*/
|
|
172
|
+
export function integrateFederation<TContextData>(
|
|
173
|
+
federation: Federation<TContextData>,
|
|
174
|
+
contextDataFactory: ContextDataFactory<TContextData> = () =>
|
|
175
|
+
undefined as TContextData,
|
|
176
|
+
errorHandlers?: Partial<ErrorHandlers>,
|
|
177
|
+
) {
|
|
178
|
+
return async (request: Request) => {
|
|
179
|
+
const forwardedRequest = await getXForwardedRequest(request);
|
|
180
|
+
const contextData = await contextDataFactory(forwardedRequest);
|
|
181
|
+
return await federation.fetch(
|
|
182
|
+
forwardedRequest,
|
|
183
|
+
{
|
|
184
|
+
contextData,
|
|
185
|
+
onNotFound: notFound,
|
|
186
|
+
onNotAcceptable,
|
|
187
|
+
...errorHandlers,
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const onNotAcceptable = () =>
|
|
193
|
+
new Response("Not acceptable", {
|
|
194
|
+
status: 406,
|
|
195
|
+
headers: { "Content-Type": "text/plain", Vary: "Accept" },
|
|
196
|
+
});
|
|
197
|
+
~~~~
|
|
198
|
+
|
|
199
|
+
[@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
|
|
200
|
+
[@fedify@hollo.social]: https://hollo.social/@fedify
|
|
201
|
+
[Fedify]: https://fedify.dev/
|
|
202
|
+
[Next.js]: https://nextjs.org/
|
|
203
|
+
[official documentation of `middleware`]: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#runtime
|
|
204
|
+
[Fedify CLI]: https://www.npmjs.com/package/@fedify/cli
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
* source: "/:path*",
|
|
43
|
+
* has: [
|
|
44
|
+
* {
|
|
45
|
+
* type: "header",
|
|
46
|
+
* key: "Accept",
|
|
47
|
+
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
|
|
48
|
+
* },
|
|
49
|
+
* ],
|
|
50
|
+
* }],
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
|
|
55
|
+
/**
|
|
56
|
+
* Check if the request has the "Accept" header matching the federation
|
|
57
|
+
* accept regex.
|
|
58
|
+
*
|
|
59
|
+
* @param request The request to check.
|
|
60
|
+
* @returns `true` if the request has the "Accept" header matching
|
|
61
|
+
* the federation accept regex, `false` otherwise.
|
|
62
|
+
*/
|
|
63
|
+
declare const hasFederationAcceptHeader: (request: Request) => boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Create a Next.js handler to integrate with the {@link Federation} object.
|
|
66
|
+
*
|
|
67
|
+
* @template TContextData A type of the context data for the
|
|
68
|
+
* {@link Federation} object.
|
|
69
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
70
|
+
* @param contextDataFactory A function to create a context data for the
|
|
71
|
+
* {@link Federation} object.
|
|
72
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
73
|
+
* the federation fetch.
|
|
74
|
+
* @returns A Next.js handler.
|
|
75
|
+
*/
|
|
76
|
+
declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { fedifyWith, hasFederationAcceptHeader, integrateFederation };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
|
+
* source: "/:path*",
|
|
43
|
+
* has: [
|
|
44
|
+
* {
|
|
45
|
+
* type: "header",
|
|
46
|
+
* key: "Accept",
|
|
47
|
+
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
|
|
48
|
+
* },
|
|
49
|
+
* ],
|
|
50
|
+
* }],
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
|
|
55
|
+
/**
|
|
56
|
+
* Check if the request has the "Accept" header matching the federation
|
|
57
|
+
* accept regex.
|
|
58
|
+
*
|
|
59
|
+
* @param request The request to check.
|
|
60
|
+
* @returns `true` if the request has the "Accept" header matching
|
|
61
|
+
* the federation accept regex, `false` otherwise.
|
|
62
|
+
*/
|
|
63
|
+
declare const hasFederationAcceptHeader: (request: Request) => boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Create a Next.js handler to integrate with the {@link Federation} object.
|
|
66
|
+
*
|
|
67
|
+
* @template TContextData A type of the context data for the
|
|
68
|
+
* {@link Federation} object.
|
|
69
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
70
|
+
* @param contextDataFactory A function to create a context data for the
|
|
71
|
+
* {@link Federation} object.
|
|
72
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
73
|
+
* the federation fetch.
|
|
74
|
+
* @returns A Next.js handler.
|
|
75
|
+
*/
|
|
76
|
+
declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { fedifyWith, hasFederationAcceptHeader, integrateFederation };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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_navigation = __toESM(require("next/navigation"));
|
|
25
|
+
const next_server = __toESM(require("next/server"));
|
|
26
|
+
|
|
27
|
+
//#region src/index.ts
|
|
28
|
+
/**
|
|
29
|
+
* Wrapper function for Next.js middleware to integrate with the
|
|
30
|
+
* {@link Federation} object.
|
|
31
|
+
*
|
|
32
|
+
* @template TContextData A type of the context data for the
|
|
33
|
+
* {@link Federation} object.
|
|
34
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
35
|
+
* @param contextDataFactory A function to create a context data for the
|
|
36
|
+
* {@link Federation} object.
|
|
37
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
38
|
+
* the federation fetch.
|
|
39
|
+
* @returns A Next.js middleware function to integrate with the
|
|
40
|
+
* {@link Federation} object.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts ignore
|
|
44
|
+
* import { fedifyWith } from "@fedify/next";
|
|
45
|
+
* import { federation } from "./federation";
|
|
46
|
+
*
|
|
47
|
+
* export default fedifyWith(federation)(
|
|
48
|
+
* function (request: Request) {
|
|
49
|
+
* // You can add custom logic here for other requests
|
|
50
|
+
* // except federation requests. If there is no custom logic,
|
|
51
|
+
* // you can omit this function.
|
|
52
|
+
* }
|
|
53
|
+
* )
|
|
54
|
+
*
|
|
55
|
+
* // This config makes middleware process only requests with the
|
|
56
|
+
* // "Accept" header matching the federation accept regex.
|
|
57
|
+
* // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
|
|
58
|
+
* export const config = {
|
|
59
|
+
* runtime: "nodejs",
|
|
60
|
+
* matcher: [{
|
|
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
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const fedifyWith = (federation, contextDataFactory, errorHandlers) => (middleware = (_) => next_server.NextResponse.next()) => async (request) => {
|
|
74
|
+
if (hasFederationAcceptHeader(request)) return await integrateFederation(federation, contextDataFactory, errorHandlers)(request);
|
|
75
|
+
return await middleware(request);
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Check if the request has the "Accept" header matching the federation
|
|
79
|
+
* accept regex.
|
|
80
|
+
*
|
|
81
|
+
* @param request The request to check.
|
|
82
|
+
* @returns `true` if the request has the "Accept" header matching
|
|
83
|
+
* the federation accept regex, `false` otherwise.
|
|
84
|
+
*/
|
|
85
|
+
const hasFederationAcceptHeader = (request) => {
|
|
86
|
+
const acceptHeader = request.headers.get("Accept");
|
|
87
|
+
return acceptHeader ? FEDERATION_ACCEPT_REGEX.test(acceptHeader) : false;
|
|
88
|
+
};
|
|
89
|
+
const FEDERATION_ACCEPT_REGEX = /.*application\/((jrd|activity|ld)\+json|xrd\+xml).*/;
|
|
90
|
+
/**
|
|
91
|
+
* Create a Next.js handler to integrate with the {@link Federation} object.
|
|
92
|
+
*
|
|
93
|
+
* @template TContextData A type of the context data for the
|
|
94
|
+
* {@link Federation} object.
|
|
95
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
96
|
+
* @param contextDataFactory A function to create a context data for the
|
|
97
|
+
* {@link Federation} object.
|
|
98
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
99
|
+
* the federation fetch.
|
|
100
|
+
* @returns A Next.js handler.
|
|
101
|
+
*/
|
|
102
|
+
function integrateFederation(federation, contextDataFactory = () => void 0, errorHandlers) {
|
|
103
|
+
return async (request) => await federation.fetch(request, {
|
|
104
|
+
contextData: await contextDataFactory(request),
|
|
105
|
+
onNotFound: next_navigation.notFound,
|
|
106
|
+
onNotAcceptable,
|
|
107
|
+
...errorHandlers
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const onNotAcceptable = () => new Response("Not acceptable", {
|
|
111
|
+
status: 406,
|
|
112
|
+
headers: {
|
|
113
|
+
"Content-Type": "text/plain",
|
|
114
|
+
Vary: "Accept"
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
exports.fedifyWith = fedifyWith;
|
|
120
|
+
exports.hasFederationAcceptHeader = hasFederationAcceptHeader;
|
|
121
|
+
exports.integrateFederation = integrateFederation;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { notFound } from "next/navigation";
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Wrapper function for Next.js middleware to integrate with the
|
|
7
|
+
* {@link Federation} object.
|
|
8
|
+
*
|
|
9
|
+
* @template TContextData A type of the context data for the
|
|
10
|
+
* {@link Federation} object.
|
|
11
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
12
|
+
* @param contextDataFactory A function to create a context data for the
|
|
13
|
+
* {@link Federation} object.
|
|
14
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
15
|
+
* the federation fetch.
|
|
16
|
+
* @returns A Next.js middleware function to integrate with the
|
|
17
|
+
* {@link Federation} object.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts ignore
|
|
21
|
+
* import { fedifyWith } from "@fedify/next";
|
|
22
|
+
* import { federation } from "./federation";
|
|
23
|
+
*
|
|
24
|
+
* export default fedifyWith(federation)(
|
|
25
|
+
* function (request: Request) {
|
|
26
|
+
* // You can add custom logic here for other requests
|
|
27
|
+
* // except federation requests. If there is no custom logic,
|
|
28
|
+
* // you can omit this function.
|
|
29
|
+
* }
|
|
30
|
+
* )
|
|
31
|
+
*
|
|
32
|
+
* // This config makes middleware process only requests with the
|
|
33
|
+
* // "Accept" header matching the federation accept regex.
|
|
34
|
+
* // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
|
|
35
|
+
* export const config = {
|
|
36
|
+
* runtime: "nodejs",
|
|
37
|
+
* matcher: [{
|
|
38
|
+
* source: "/:path*",
|
|
39
|
+
* has: [
|
|
40
|
+
* {
|
|
41
|
+
* type: "header",
|
|
42
|
+
* key: "Accept",
|
|
43
|
+
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
|
|
44
|
+
* },
|
|
45
|
+
* ],
|
|
46
|
+
* }],
|
|
47
|
+
* };
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
const fedifyWith = (federation, contextDataFactory, errorHandlers) => (middleware = (_) => NextResponse.next()) => async (request) => {
|
|
51
|
+
if (hasFederationAcceptHeader(request)) return await integrateFederation(federation, contextDataFactory, errorHandlers)(request);
|
|
52
|
+
return await middleware(request);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Check if the request has the "Accept" header matching the federation
|
|
56
|
+
* accept regex.
|
|
57
|
+
*
|
|
58
|
+
* @param request The request to check.
|
|
59
|
+
* @returns `true` if the request has the "Accept" header matching
|
|
60
|
+
* the federation accept regex, `false` otherwise.
|
|
61
|
+
*/
|
|
62
|
+
const hasFederationAcceptHeader = (request) => {
|
|
63
|
+
const acceptHeader = request.headers.get("Accept");
|
|
64
|
+
return acceptHeader ? FEDERATION_ACCEPT_REGEX.test(acceptHeader) : false;
|
|
65
|
+
};
|
|
66
|
+
const FEDERATION_ACCEPT_REGEX = /.*application\/((jrd|activity|ld)\+json|xrd\+xml).*/;
|
|
67
|
+
/**
|
|
68
|
+
* Create a Next.js handler to integrate with the {@link Federation} object.
|
|
69
|
+
*
|
|
70
|
+
* @template TContextData A type of the context data for the
|
|
71
|
+
* {@link Federation} object.
|
|
72
|
+
* @param federation A {@link Federation} object to integrate with Next.js.
|
|
73
|
+
* @param contextDataFactory A function to create a context data for the
|
|
74
|
+
* {@link Federation} object.
|
|
75
|
+
* @param errorHandlers A set of error handlers to handle errors during
|
|
76
|
+
* the federation fetch.
|
|
77
|
+
* @returns A Next.js handler.
|
|
78
|
+
*/
|
|
79
|
+
function integrateFederation(federation, contextDataFactory = () => void 0, errorHandlers) {
|
|
80
|
+
return async (request) => await federation.fetch(request, {
|
|
81
|
+
contextData: await contextDataFactory(request),
|
|
82
|
+
onNotFound: notFound,
|
|
83
|
+
onNotAcceptable,
|
|
84
|
+
...errorHandlers
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const onNotAcceptable = () => new Response("Not acceptable", {
|
|
88
|
+
status: 406,
|
|
89
|
+
headers: {
|
|
90
|
+
"Content-Type": "text/plain",
|
|
91
|
+
Vary: "Accept"
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
export { fedifyWith, hasFederationAcceptHeader, integrateFederation };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedify/next",
|
|
3
|
+
"version": "1.9.0-pr.355.1353+2b6be9b6",
|
|
4
|
+
"description": "Integrate Fedify with Next.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Fedify",
|
|
7
|
+
"ActivityPub",
|
|
8
|
+
"Fediverse",
|
|
9
|
+
"Next",
|
|
10
|
+
"Next.js"
|
|
11
|
+
],
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Chanhaeng Lee",
|
|
14
|
+
"email": "2chanhaeng@gmail.com",
|
|
15
|
+
"url": "https://chomu.dev"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://fedify.dev/",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/fedify-dev/fedify.git",
|
|
21
|
+
"directory": "packages/next"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/fedify-dev/fedify/issues"
|
|
26
|
+
},
|
|
27
|
+
"funding": [
|
|
28
|
+
"https://opencollective.com/fedify",
|
|
29
|
+
"https://github.com/sponsors/dahlia"
|
|
30
|
+
],
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.mjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"default": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/index.d.mts",
|
|
42
|
+
"default": "./dist/index.mjs"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist/",
|
|
49
|
+
"package.json"
|
|
50
|
+
],
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"next": "^15.4.2-canary",
|
|
53
|
+
"@fedify/fedify": "1.9.0-pr.355.1353+2b6be9b6"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"tsdown": "^0.12.9",
|
|
57
|
+
"typescript": "^5.8.3"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsdown",
|
|
61
|
+
"prepublish": "tsdown"
|
|
62
|
+
}
|
|
63
|
+
}
|