-
-
Notifications
You must be signed in to change notification settings - Fork 0
Introducing performance optimizations and TypeScript Types #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9a0ed9f
add TypeScript definitions for middleware endpoint configuration and …
jkyberneees cfc6aea
refactor: optimize router handling and middleware execution logic
jkyberneees 51ab57e
chore: update package.json to include additional files and upgrade de…
jkyberneees 0d163a5
style: format code for consistency and readability
jkyberneees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Represents a route endpoint configuration for middleware matching. | ||
*/ | ||
export interface RouteEndpoint { | ||
/** HTTP methods to match. Defaults to ["GET"] if not specified. */ | ||
methods?: string[]; | ||
/** URL pattern to match against. Supports find-my-way route patterns. */ | ||
url: string; | ||
/** Optional version constraint for the route. */ | ||
version?: string; | ||
/** Whether to update req.params with matched route parameters. Defaults to false. */ | ||
updateParams?: boolean; | ||
} | ||
|
||
/** | ||
* Configuration options for middleware execution conditions. | ||
*/ | ||
export interface MiddlewareOptions { | ||
/** Array of endpoints (strings or RouteEndpoint objects) to match against. */ | ||
endpoints?: (string | RouteEndpoint)[]; | ||
/** Custom function to determine if middleware should execute. */ | ||
custom?: (req: any) => boolean; | ||
} | ||
|
||
/** | ||
* Standard Express/Connect-style middleware function signature. | ||
* @param req - The request object | ||
* @param res - The response object | ||
* @param next - Function to call the next middleware in the chain | ||
*/ | ||
export type MiddlewareFunction = (req: any, res: any, next: () => void) => void; | ||
|
||
/** | ||
* Enhanced middleware function with conditional execution capabilities. | ||
* Extends the base middleware function with iff and unless methods. | ||
*/ | ||
export interface ExtendedMiddleware extends MiddlewareFunction { | ||
/** | ||
* Execute middleware only if the specified condition is met. | ||
* @param options - Condition options: MiddlewareOptions object, custom function, or array of endpoints | ||
* @returns New ExtendedMiddleware instance with the condition applied | ||
*/ | ||
iff: (options: MiddlewareOptions | ((req: any) => boolean) | (string | RouteEndpoint)[]) => ExtendedMiddleware; | ||
|
||
/** | ||
* Execute middleware unless the specified condition is met. | ||
* @param options - Condition options: MiddlewareOptions object, custom function, or array of endpoints | ||
* @returns New ExtendedMiddleware instance with the condition applied | ||
*/ | ||
unless: (options: MiddlewareOptions | ((req: any) => boolean) | (string | RouteEndpoint)[]) => ExtendedMiddleware; | ||
} | ||
|
||
/** | ||
* Configuration options for the router instance. | ||
*/ | ||
export interface RouterOptions { | ||
/** Default route handler function. */ | ||
defaultRoute?: (req: any, res: any) => boolean; | ||
/** Additional router-specific options. */ | ||
[key: string]: any; | ||
} | ||
|
||
/** | ||
* Factory function for creating router instances. | ||
* @param options - Optional router configuration | ||
* @returns Router instance | ||
*/ | ||
export interface RouterFactory { | ||
(options?: RouterOptions): any; | ||
} | ||
|
||
/** | ||
* Main middleware enhancement function that adds iff/unless capabilities to middleware. | ||
* | ||
* @param routerOpts - Optional router configuration options | ||
* @param routerFactory - Optional router factory function (defaults to find-my-way) | ||
* @returns Function that takes a middleware and returns an ExtendedMiddleware with iff/unless methods | ||
* | ||
* @example | ||
* ```typescript | ||
* import iffUnless from 'middleware-if-unless'; | ||
* | ||
* const iu = iffUnless(); | ||
* const middleware = (req, res, next) => { | ||
* console.log('Middleware executed'); | ||
* next(); | ||
* }; | ||
* | ||
* const enhanced = iu(middleware); | ||
* | ||
* // Execute only for specific routes | ||
* app.use(enhanced.iff(['/api/*'])); | ||
* | ||
* // Execute unless specific routes match | ||
* app.use(enhanced.unless(['/public/*'])); | ||
* ``` | ||
*/ | ||
export default function iffUnless( | ||
routerOpts?: RouterOptions, | ||
routerFactory?: RouterFactory | ||
): (middleware: MiddlewareFunction) => ExtendedMiddleware; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,15 @@ | |
"if", | ||
"unless" | ||
], | ||
"files": [ | ||
"README.md" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"README.md" | ||
], | ||
"typings": "index.d.ts", | ||
"author": "Rolando Santamaria Maso <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
|
@@ -29,13 +32,13 @@ | |
"homepage": "https://github.com/jkyberneees/middleware-if-unless#readme", | ||
"devDependencies": { | ||
"chai": "^4.3.7", | ||
"express-unless": "^1.0.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"restana": "^4.9.7", | ||
"supertest": "^6.3.3" | ||
"express-unless": "^2.1.3", | ||
"mocha": "^11.7.2", | ||
"nyc": "^17.1.0", | ||
"restana": "^5.1.0", | ||
"supertest": "^7.1.4" | ||
}, | ||
"dependencies": { | ||
"find-my-way": "^9.0.1" | ||
"find-my-way": "^9.3.0" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.