@fedify/next
Version:
Integrate Fedify with Next.js
93 lines (92 loc) • 3.79 kB
text/typescript
import { Federation, FederationFetchOptions } from "@fedify/fedify";
//#region src/index.d.ts
interface ContextDataFactory<TContextData> {
(request: Request): TContextData | Promise<TContextData>;
}
type ErrorHandlers = Omit<FederationFetchOptions<unknown>, "contextData">;
/**
* Wrapper function for Next.js middleware to integrate with the
* {@link Federation} object.
*
* @template TContextData A type of the context data for the
* {@link Federation} object.
* @param federation A {@link Federation} object to integrate with Next.js.
* @param contextDataFactory A function to create a context data for the
* {@link Federation} object.
* @param errorHandlers A set of error handlers to handle errors during
* the federation fetch.
* @returns A Next.js middleware function to integrate with the
* {@link Federation} object.
*
* @example
* ```ts ignore
* import { fedifyWith } from "@fedify/next";
* import { federation } from "./federation";
*
* export default fedifyWith(federation)(
* function (request: Request) {
* // You can add custom logic here for other requests
* // except federation requests. If there is no custom logic,
* // you can omit this function.
* }
* )
*
* // This config makes middleware process only requests with the
* // "Accept" header matching the federation accept regex.
* // More details: https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional.
* export const config = {
* runtime: "nodejs",
* matcher: [
* {
* source: "/:path*",
* has: [
* {
* type: "header",
* key: "Accept",
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
* },
* ],
* },
* {
* source: "/:path*",
* has: [
* {
* type: "header",
* key: "content-type",
* value: ".*application\\/((jrd|activity|ld)\\+json|xrd\\+xml).*",
* },
* ],
* },
* { source: "/.well-known/nodeinfo" },
* { source: "/.well-known/x-nodeinfo2" },
* ],
* };
* ```
*/
declare const fedifyWith: <TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>) => (middleware?: (request: Request) => unknown) => (request: Request) => unknown;
declare const isFederationRequest: (request: Request) => boolean;
/**
* Check if the request has the header matching the federation
* accept regex.
* @param key The header key to check.
* @param request The request to check.
* @returns `true` if the request has the header matching
* the federation accept regex, `false` otherwise.
*/
declare const hasFederationHeader: (key: string) => (request: Request) => boolean;
declare const isNodeInfoRequest: (request: Request) => boolean;
/**
* Create a Next.js handler to integrate with the {@link Federation} object.
*
* @template TContextData A type of the context data for the
* {@link Federation} object.
* @param federation A {@link Federation} object to integrate with Next.js.
* @param contextDataFactory A function to create a context data for the
* {@link Federation} object.
* @param errorHandlers A set of error handlers to handle errors during
* the federation fetch.
* @returns A Next.js handler.
*/
declare function integrateFederation<TContextData>(federation: Federation<TContextData>, contextDataFactory?: ContextDataFactory<TContextData>, errorHandlers?: Partial<ErrorHandlers>): (request: Request) => Promise<Response>;
//#endregion
export { fedifyWith, hasFederationHeader, integrateFederation, isFederationRequest, isNodeInfoRequest };