Skip to content

Commit 9a0ed9f

Browse files
committed
add TypeScript definitions for middleware endpoint configuration and execution options
1 parent 458b397 commit 9a0ed9f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

index.d.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Represents a route endpoint configuration for middleware matching.
3+
*/
4+
export interface RouteEndpoint {
5+
/** HTTP methods to match. Defaults to ["GET"] if not specified. */
6+
methods?: string[];
7+
/** URL pattern to match against. Supports find-my-way route patterns. */
8+
url: string;
9+
/** Optional version constraint for the route. */
10+
version?: string;
11+
/** Whether to update req.params with matched route parameters. Defaults to false. */
12+
updateParams?: boolean;
13+
}
14+
15+
/**
16+
* Configuration options for middleware execution conditions.
17+
*/
18+
export interface MiddlewareOptions {
19+
/** Array of endpoints (strings or RouteEndpoint objects) to match against. */
20+
endpoints?: (string | RouteEndpoint)[];
21+
/** Custom function to determine if middleware should execute. */
22+
custom?: (req: any) => boolean;
23+
}
24+
25+
/**
26+
* Standard Express/Connect-style middleware function signature.
27+
* @param req - The request object
28+
* @param res - The response object
29+
* @param next - Function to call the next middleware in the chain
30+
*/
31+
export type MiddlewareFunction = (req: any, res: any, next: () => void) => void;
32+
33+
/**
34+
* Enhanced middleware function with conditional execution capabilities.
35+
* Extends the base middleware function with iff and unless methods.
36+
*/
37+
export interface ExtendedMiddleware extends MiddlewareFunction {
38+
/**
39+
* Execute middleware only if the specified condition is met.
40+
* @param options - Condition options: MiddlewareOptions object, custom function, or array of endpoints
41+
* @returns New ExtendedMiddleware instance with the condition applied
42+
*/
43+
iff: (options: MiddlewareOptions | ((req: any) => boolean) | (string | RouteEndpoint)[]) => ExtendedMiddleware;
44+
45+
/**
46+
* Execute middleware unless the specified condition is met.
47+
* @param options - Condition options: MiddlewareOptions object, custom function, or array of endpoints
48+
* @returns New ExtendedMiddleware instance with the condition applied
49+
*/
50+
unless: (options: MiddlewareOptions | ((req: any) => boolean) | (string | RouteEndpoint)[]) => ExtendedMiddleware;
51+
}
52+
53+
/**
54+
* Configuration options for the router instance.
55+
*/
56+
export interface RouterOptions {
57+
/** Default route handler function. */
58+
defaultRoute?: (req: any, res: any) => boolean;
59+
/** Additional router-specific options. */
60+
[key: string]: any;
61+
}
62+
63+
/**
64+
* Factory function for creating router instances.
65+
* @param options - Optional router configuration
66+
* @returns Router instance
67+
*/
68+
export interface RouterFactory {
69+
(options?: RouterOptions): any;
70+
}
71+
72+
/**
73+
* Main middleware enhancement function that adds iff/unless capabilities to middleware.
74+
*
75+
* @param routerOpts - Optional router configuration options
76+
* @param routerFactory - Optional router factory function (defaults to find-my-way)
77+
* @returns Function that takes a middleware and returns an ExtendedMiddleware with iff/unless methods
78+
*
79+
* @example
80+
* ```typescript
81+
* import iffUnless from 'middleware-if-unless';
82+
*
83+
* const iu = iffUnless();
84+
* const middleware = (req, res, next) => {
85+
* console.log('Middleware executed');
86+
* next();
87+
* };
88+
*
89+
* const enhanced = iu(middleware);
90+
*
91+
* // Execute only for specific routes
92+
* app.use(enhanced.iff(['/api/*']));
93+
*
94+
* // Execute unless specific routes match
95+
* app.use(enhanced.unless(['/public/*']));
96+
* ```
97+
*/
98+
export default function iffUnless(
99+
routerOpts?: RouterOptions,
100+
routerFactory?: RouterFactory
101+
): (middleware: MiddlewareFunction) => ExtendedMiddleware;

0 commit comments

Comments
 (0)