Skip to content

Commit

Permalink
feat: middleware resolveAllSafe (#17)
Browse files Browse the repository at this point in the history
* feat: middleware resolveAllSafe

* refactor: mv to separate folder

* chore: update typedoc script

* chore: jsdoc
  • Loading branch information
exuanbo authored Nov 23, 2024
1 parent 14ac1a2 commit 3ede3ee
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
5 changes: 4 additions & 1 deletion jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@exuanbo/di-wise",
"version": "0.2.6",
"exports": "./src/index.ts",
"exports": {
".": "./src/index.ts",
"./middlewares": "./src/middlewares/index.ts"
},
"publish": {
"include": ["src", "!src/__tests__", "LICENSE", "README.md", "tsconfig.json"]
}
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
},
"./middlewares": {
"import": {
"types": "./dist/es/middlewares.d.mts",
"default": "./dist/es/middlewares.mjs"
},
"require": {
"types": "./dist/cjs/middlewares.d.ts",
"default": "./dist/cjs/middlewares.js"
}
}
},
"sideEffects": false,
Expand All @@ -31,7 +41,7 @@
"lint:ci": "eslint . --max-warnings 0 --cache --cache-location ./node_modules/.cache/eslint/.eslintcache",
"format": "prettier \"**/*.{json,md,yml}\" --write --log-level warn --cache",
"format:check": "prettier \"**/*.{json,md,yml}\" --check --cache",
"typedoc": "typedoc src/index.ts --includeVersion"
"typedoc": "typedoc src/index.ts src/middlewares/index.ts --includeVersion"
},
"repository": {
"type": "git",
Expand Down
26 changes: 25 additions & 1 deletion src/__tests__/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import {beforeEach, describe, expect, it, vi} from "vitest";

import {applyMiddleware, type Container, createContainer, inject, injectAll, type Middleware, type Token} from "..";
import {
applyMiddleware,
type Container,
createContainer,
inject,
injectAll,
type Middleware,
type Token,
Type,
} from "..";
import {resolveAllSafe} from "../middlewares";

describe("Middleware", () => {
let container: Container;
Expand Down Expand Up @@ -78,4 +88,18 @@ describe("Middleware", () => {
["[B] post resolve Wizard {wand: Wand {decorations: [Decoration {}]}}"],
]);
});

describe("resolveAllOptional", () => {
it("should resolve all tokens to empty array if they are not provided", () => {
applyMiddleware(container, [resolveAllSafe]);

const NonRegistered = Type("NonRegistered");
expect(container.resolveAll(NonRegistered)).toEqual([]);

const Registered = Type<string>("Registered");
container.register(Registered, {useValue: "Success"});

expect(container.resolveAll(Registered)).toEqual(["Success"]);
});
});
});
1 change: 1 addition & 0 deletions src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {resolveAllSafe} from "./resolve-all-safe";
22 changes: 22 additions & 0 deletions src/middlewares/resolve-all-safe.ts
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);
});
};

0 comments on commit 3ede3ee

Please sign in to comment.