Fix decorator injection from the documented subpath - #4
Open
refucktor wants to merge 1 commit into
Open
Conversation
- Build package entrypoints with shared chunks so decorators and the container use the same metadata store - Cover direct and consumer package imports with decorator injection tests - Define rootDir so declaration generation succeeds with TypeScript 5.9
Author
|
This is the documented pattern, taken directly from the website: import { Container } from "fast-injection";
import { singleton, inject } from "fast-injection/decorators";
// Define services with decorators
@singleton()
class Database {
query(sql: string) {
return "Query result for: " + sql;
}
}
@singleton()
class UserService {
constructor(@inject(Database) private db: Database) {}
getUser(id: string) {
return this.db.query(`SELECT * FROM users WHERE id = ${id}`);
}
}
// Create container and register services
const container = new Container();
container.register(Database);
container.register(UserService);
// Resolve with automatic dependency injection
const userService = container.resolve(UserService);
// This part was added by me to test the resolution and it fails
console.info("=== Example: Dependency Injection with Decorators ===");
const user = userService.getUser("123");
console.log(user); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The documented import pattern uses the container from
fast-injectionand decorators fromfast-injection/decorators. In the published package, those entrypoints were bundled independently. Each bundle created its own decorator metadata store, so@inject(...)saved dependency information in a store the container could not read.As a result, registered classes resolved without their decorated constructor dependencies. For example,
constructor(@inject(Database) private db: Database)receivedundefinedfordb, causing a runtime error when the service used it.Fix
Build the package entrypoints with Bun code splitting so the main entrypoint and decorator subpath share the same metadata module at runtime.
Also add a package-boundary regression test that builds a temporary package and runs a consumer using the documented imports. This catches future builds that accidentally isolate decorator metadata again.
Verification
bun testbun run lintbun run build