@rescale/nemo
Version:
A middleware composition library for Next.js applications that allows you to organize and chain middleware functions based on URL patterns.
140 lines (137 loc) • 6.03 kB
TypeScript
import { M as MiddlewareConfig, G as GlobalMiddlewareConfig, N as NemoConfig, a as NextMiddlewareResult } from './types--9jM0uBl.js';
export { C as ChainType, E as ErrorHandler, e as MiddlewareChain, f as MiddlewareConfigValue, d as MiddlewareContext, g as MiddlewareMetadata, b as NemoEvent, c as NextMiddleware, h as NextMiddlewareWithMeta, S as Storage } from './types--9jM0uBl.js';
import { NextRequest, NextFetchEvent } from 'next/server';
export { NemoMiddlewareError } from './errors.js';
export { areResponsesEqual } from './utils.js';
import 'next/dist/server/after/builtin-request-context';
import './storage/adapter.js';
type NextMiddlewareFunction = (request: NextRequest, event: NextFetchEvent) => Promise<NextMiddlewareResult>;
declare class NEMO {
private readonly config;
private readonly middlewares;
private readonly globalMiddleware?;
private readonly logger;
private readonly storage;
private readonly storageFactory;
/**
* NEMO Middleware/Proxy
* Compatible with both Next.js <16 (middleware.ts) and Next.js 16+ (proxy.ts)
* @param middlewares - Middleware configuration
* @param globalMiddleware - Global middleware configuration
* @param config - NEMO configuration
*/
constructor(middlewares: MiddlewareConfig, globalMiddleware?: GlobalMiddlewareConfig, config?: NemoConfig);
/**
* Computes if the given path matches the given pattern using path-to-regexp
* @param pattern - The pattern to match
* @param path - The path to check
* @param exact - Whether to match exactly or as a prefix
* @returns Whether the path matches the pattern
*/
private computePathMatch;
/**
* Porównuje początkowe i końcowe nagłówki i zwraca różnicę
* @param initialHeaders - Początkowe nagłówki żądania
* @param finalHeaders - Końcowe nagłówki żądania
* @returns Obiekt zawierający nowe i zmodyfikowane nagłówki
*/
private getHeadersDiff;
/**
* Attaches metadata to the given middleware function.
* @param middleware - The middleware function to attach metadata to.
* @param metadata - The metadata to attach.
* @returns The middleware function with attached metadata.
*/
private attachMetadata;
/**
* Creates a full pattern path by combining base path and key
* @param key - The route key
* @param basePath - The base path
* @returns The combined full pattern
*/
private createFullPattern;
/**
* Propagate the queue of middleware functions for the given request.
* @param request - The request to propagate the queue for.
* @returns The queue of middleware functions.
*/
private propagateQueue;
/**
* Process the queue of middleware functions.
* @param queue - The queue of middleware functions to process.
* @param request - The request to process the queue for.
* @param event - The fetch event to process the queue for.
* @returns The result of the middleware processing.
*/
private processQueue;
private initializeTimingTracking;
private shouldSkipMiddleware;
private handleChainTransition;
private processMiddlewareResult;
private executeMiddlewareChain;
private executeMiddleware;
private logMiddlewareExecution;
private isTerminatingResult;
private applyHeadersToRequest;
private updateTimingStats;
private handleMiddlewareError;
private logFinalTiming;
private createFinalResponse;
/**
* Middleware/Proxy handler
* Compatible with both Next.js <16 (middleware.ts) and Next.js 16+ (proxy.ts)
* @param request - The request to process the middleware for.
* @param event - The fetch event to process the middleware for.
* @returns The result of the middleware processing.
*/
middleware: NextMiddlewareFunction;
}
/**
* @deprecated This function is going to be deprecated as it's named just like many other packages and can cause conflicts. Use `new NEMO()` instead.
*
* Example for Next.js 16+: `export const proxy = createNEMO(middlewares, globalMiddleware, config)`
* Example for Next.js <16: `export const middleware = createNEMO(middlewares, globalMiddleware, config)`
*
* @param middlewares - Middleware configuration
* @param globalMiddleware - Global middleware configuration
* @returns NextMiddleware (compatible with both Next.js <16 NextMiddleware and Next.js 16+ NextProxy)
*
* @example
* ```ts
* import { createNEMO } from "@rescale/nemo";
*
* // Next.js 16+: export const proxy = createNEMO({...})
* // Next.js <16: export const middleware = createNEMO({...})
* const proxy = createNEMO({
* "/api/:path*": (req, event) => {
* console.log("API request:", req.nextUrl.pathname);
* },
* });
* ```
*/
declare function createMiddleware(middlewares: MiddlewareConfig, globalMiddleware?: GlobalMiddlewareConfig, config?: NemoConfig): NextMiddlewareFunction;
/**
* Creates a new NEMO instance with the given middlewares and optional configurations.
*
* Compatible with both Next.js <16 (middleware.ts) and Next.js 16+ (proxy.ts).
*
* @param middlewares - Middleware configuration
* @param globalMiddleware - Global middleware configuration
* @param config - Optional Nemo configuration
* @returns NextMiddleware (compatible with both Next.js <16 NextMiddleware and Next.js 16+ NextProxy)
*
* @example
* ```ts
* import { createNEMO } from "@rescale/nemo";
*
* // Next.js 16+: export const proxy = createNEMO({...})
* // Next.js <16: export const middleware = createNEMO({...})
* const proxy = createNEMO({
* "/api/:path*": (req, event) => {
* console.log("API request:", req.nextUrl.pathname);
* },
* });
* ```
*/
declare function createNEMO(middlewares: MiddlewareConfig, globalMiddleware?: GlobalMiddlewareConfig, config?: NemoConfig): NextMiddlewareFunction;
export { GlobalMiddlewareConfig, MiddlewareConfig, NEMO, NemoConfig, NextMiddlewareResult, createMiddleware, createNEMO };