@rescale/nemo
Version:
A middleware composition library for Next.js applications that allows you to organize and chain middleware functions based on URL patterns.
124 lines (121 loc) • 4.93 kB
TypeScript
import { NextMiddleware } from 'next/server';
import { M as MiddlewareConfig, G as GlobalMiddlewareConfig, N as NemoConfig } from './types-B-aMIH53.js';
export { E as ErrorHandler, e as MiddlewareChain, f as MiddlewareConfigValue, d as MiddlewareContext, g as MiddlewareMetadata, a as NemoEvent, c as NextMiddleware, b as NextMiddlewareResult, h as NextMiddlewareWithMeta, S as Storage } from './types-B-aMIH53.js';
export { NemoMiddlewareError } from './errors.js';
export { areResponsesEqual } from './utils.js';
import 'next/dist/server/after/builtin-request-context';
import './storage/adapter.js';
declare class NEMO {
private readonly config;
private readonly middlewares;
private readonly globalMiddleware?;
private readonly logger;
private readonly storage;
/**
* NEMO Middleware
* @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 executeMiddlewareChain;
private executeMiddleware;
private logMiddlewareExecution;
private isTerminatingResult;
private applyHeadersToRequest;
private updateTimingStats;
private handleMiddlewareError;
private logFinalTiming;
private createFinalResponse;
/**
* Middleware
* @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: NextMiddleware;
}
/**
* @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: `export const middleware = createNEMO(middlewares, globalMiddleware, config)`.
*
* @param middlewares - Middleware configuration
* @param globalMiddleware - Global middleware configuration
* @returns NextMiddleware
*
* @example
* ```ts
* import { createNEMO } from "@rescale/nemo";
*
* const middleware = createNEMO({
* "/api/:path*": (req, event) => {
* console.log("API request:", req.nextUrl.pathname);
* },
* });
* ```
*/
declare function createMiddleware(middlewares: MiddlewareConfig, globalMiddleware?: GlobalMiddlewareConfig, config?: NemoConfig): NextMiddleware;
/**
* Creates a new NEMO instance with the given middlewares and optional configurations.
*
* @param middlewares - Middleware configuration
* @param globalMiddleware - Global middleware configuration
* @param config - Optional Nemo configuration
* @returns NextMiddleware
*
* @example
* ```ts
* import { createNEMO } from "@rescale/nemo";
*
* const middleware = createNEMO({
* "/api/:path*": (req, event) => {
* console.log("API request:", req.nextUrl.pathname);
* },
* });
* ```
*/
declare function createNEMO(middlewares: MiddlewareConfig, globalMiddleware?: GlobalMiddlewareConfig, config?: NemoConfig): NextMiddleware;
export { GlobalMiddlewareConfig, MiddlewareConfig, NEMO, NemoConfig, createMiddleware, createNEMO };