UNPKG

next-wayfinder

Version:

Apply multiple next.js middlewares with ease

70 lines (67 loc) 3.22 kB
import { NextRequest, NextResponse, NextFetchEvent, NextMiddleware } from 'next/server'; import { Path } from 'path-to-regexp'; type MaybePromise<T> = T | Promise<T>; type UrlParams = Record<string, string | string[] | undefined>; interface NextRequestWithParams<T> extends NextRequest { params: UrlParams; ctx?: T; } type BeforeAllMiddleware = (req: NextRequest, res: NextResponse) => MaybePromise<NextResponse>; type NextMiddlewareWithParams<T> = (request: NextRequestWithParams<T>, response: NextResponse, event: NextFetchEvent) => ReturnType<NextMiddleware>; type PathMatcher = Path; type ResponseFactory = NextResponse | ((request: NextRequest, ev: NextFetchEvent) => NextResponse); /** * * A function to extract `hostname` and `pathname` from `NextRequest` */ interface RequestParser { (req: NextRequest): { hostname: string; pathname: string; }; } interface RequestInjector<T> { (request: NextRequestWithParams<T>): MaybePromise<T>; } type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; type HostnameCheck = string | RegExp | ((hostname: string) => boolean); interface HostnameMiddleware<T> { handler: NextMiddlewareWithParams<T> | Middleware<T>[]; hostname: HostnameCheck | HostnameCheck[]; beforeAll?: BeforeAllMiddleware; pre?: (request: NextRequestWithParams<T>) => MaybePromise<boolean | { redirectTo: string | URL; statusCode?: number; }>; } interface PathMiddleware<T> { method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; guard?: (params: UrlParams) => boolean; handler: NextMiddlewareWithParams<T> | Middleware<T>[]; path: PathMatcher; pre?: (request: NextRequestWithParams<T>) => MaybePromise<boolean | { redirectTo: string | URL; statusCode?: number; }>; } interface RedirectMiddleware<T> { method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; guard?: (params: UrlParams) => boolean; path: PathMatcher; redirectTo: string | ((req: NextRequestWithParams<T>) => string); includeOrigin?: string | boolean; } interface RewriteMiddleware<T> { method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS'; guard?: (params: UrlParams) => boolean; path: PathMatcher; rewriteTo: string | ((req: NextRequestWithParams<T>) => string); } type Middleware<T> = PathMiddleware<T> | RedirectMiddleware<T> | RewriteMiddleware<T> | HostnameMiddleware<T>; declare const Middleware: { isRewrite: <T>(middleware: Middleware<T>) => middleware is RewriteMiddleware<T>; isRedirect: <T_1>(middleware: Middleware<T_1>) => middleware is RedirectMiddleware<T_1>; isPath: <T_2>(middleware: Middleware<T_2>) => middleware is PathMiddleware<T_2>; isHostname: <T_3>(middleware: Middleware<T_3>) => middleware is HostnameMiddleware<T_3>; }; export { type BeforeAllMiddleware, type HTTPMethod, type HostnameCheck, type HostnameMiddleware, Middleware, type NextMiddlewareWithParams, type NextRequestWithParams, type PathMatcher, type PathMiddleware, type RedirectMiddleware, type RequestInjector, type RequestParser, type ResponseFactory, type RewriteMiddleware, type UrlParams };