express-endpoint-counter
Version:
A lightweight Express.js middleware for counting and monitoring API endpoint usage with TypeScript support
92 lines • 2.89 kB
TypeScript
import { Request, Response, NextFunction } from 'express';
export interface EndpointStats {
count: number;
totalDuration: number;
averageDuration: number;
minDuration: number;
maxDuration: number;
lastAccessedAt: Date;
method: string;
path: string;
}
export interface CounterOptions {
/** Include query parameters in endpoint identification */
includeQueryParams?: boolean;
/** Group endpoints by HTTP method */
groupByMethod?: boolean;
/** Maximum number of endpoints to track (for memory management) */
maxEndpoints?: number;
/** Normalize dynamic route parameters (e.g., /users/:id -> /users/:id) */
normalizePaths?: boolean;
/** Callback function called when stats are updated */
onUpdate?: (endpoint: string, stats: EndpointStats) => void;
/** Enable console logging */
enableLogging?: boolean;
/** Custom logger function */
logger?: (message: string) => void;
}
export declare class EndpointCounter {
private stats;
private options;
constructor(options?: CounterOptions);
private decodeURL;
private cleanupIfNeeded;
/**
* Increment the counter for a specific endpoint
*/
incrementCount(method: string, pathname: string, duration: number): number;
/**
* Get statistics for a specific endpoint or all endpoints
*/
getStats(): Map<string, EndpointStats>;
getStats(endpoint: string): EndpointStats | undefined;
/**
* Get the top N most accessed endpoints
*/
getTopEndpoints(limit?: number): Array<{
endpoint: string;
stats: EndpointStats;
}>;
/**
* Get the slowest N endpoints by average duration
*/
getSlowestEndpoints(limit?: number): Array<{
endpoint: string;
stats: EndpointStats;
}>;
/**
* Get summary statistics
*/
getSummary(): {
totalEndpoints: number;
totalRequests: number;
totalDuration: number;
averageDuration: number;
topEndpoint: {
endpoint: string;
stats: EndpointStats;
} | undefined;
slowestEndpoint: {
endpoint: string;
stats: EndpointStats;
} | undefined;
};
/**
* Reset statistics for a specific endpoint or all endpoints
*/
reset(endpoint?: string): void;
/**
* Express middleware
*/
middleware(): (req: Request, res: Response, next: NextFunction) => void;
}
/**
* Factory function to create a new EndpointCounter instance
*/
export declare function createEndpointCounter(options?: CounterOptions): EndpointCounter;
/**
* Standalone middleware for simple usage
*/
export declare function endpointCounterMiddleware(options?: CounterOptions): (req: Request, res: Response, next: NextFunction) => void;
export default EndpointCounter;
//# sourceMappingURL=index.d.ts.map