-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: middleware resolveAllSafe (#17)
* feat: middleware resolveAllSafe * refactor: mv to separate folder * chore: update typedoc script * chore: jsdoc
- Loading branch information
Showing
5 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 @@ | ||
export {resolveAllSafe} from "./resolve-all-safe"; |
This file contains 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,22 @@ | ||
import {type Middleware, type Token, Type} from "../index"; | ||
|
||
/** | ||
* Middleware that makes `resolveAll` return an empty array for unregistered tokens instead of throwing. | ||
* | ||
* This middleware modifies the behavior of `resolveAll` to safely handle cases where tokens haven't been | ||
* registered in the container. Instead of throwing an error, it will return an empty array. | ||
* | ||
* @example | ||
* ```ts | ||
* import {resolveAllSafe} from "di-wise/middlewares"; | ||
* | ||
* const container = applyMiddleware(createContainer(), [resolveAllSafe]); | ||
* | ||
* container.resolveAll(NonRegisteredToken); // => [] | ||
* ``` | ||
*/ | ||
export const resolveAllSafe: Middleware = (composer) => { | ||
composer.use("resolveAll", (next) => <T>(...args: Token<T>[]) => { | ||
return next(...args, Type.Null); | ||
}); | ||
}; |