Skip to content

Commit a9cc051

Browse files
committed
docs: add property injection example with Inject decorator
Addresses feedback that docs lacked an explicit Inject property decorator example and showed no metadata consumption for DI.
1 parent da7db0e commit a9cc051

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.changeset/clever-terms-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zendrex/annotate": patch
3+
---
4+
5+
Add property injection example to docs and JSDoc

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import {
7878
const Tag = createClassDecorator<string>();
7979
const Route = createMethodDecorator<string>();
8080
const Column = createPropertyDecorator<string>();
81-
const Inject = createParameterDecorator<string>();
81+
const Param = createParameterDecorator<string>();
8282
```
8383

8484
### Compose Functions
@@ -124,6 +124,34 @@ for (const col of columns) {
124124
// age [{ type: "int", nullable: true }]
125125
```
126126

127+
### Property Injection
128+
129+
Property decorators can mark fields for dependency injection:
130+
131+
```typescript
132+
import { createPropertyDecorator } from "@zendrex/annotate";
133+
134+
const Inject = createPropertyDecorator<string>();
135+
136+
class UserService {
137+
@Inject("database")
138+
db!: Database;
139+
140+
@Inject("logger")
141+
logger!: Logger;
142+
}
143+
144+
// Reflect and resolve
145+
const deps = Inject.properties(UserService);
146+
// => [{ kind: "property", name: "db", metadata: ["database"] },
147+
// { kind: "property", name: "logger", metadata: ["logger"] }]
148+
149+
const instance = new UserService();
150+
for (const dep of deps) {
151+
(instance as any)[dep.name] = container.get(dep.metadata[0]);
152+
}
153+
```
154+
127155
### General Reflection
128156

129157
Use `reflect()` when you need to query multiple decorator types on a single class:
@@ -136,7 +164,7 @@ const reflector = reflect(UserController);
136164
// Query by decorator key
137165
const routes = reflector.methods(Route.key);
138166
const columns = reflector.properties(Column.key);
139-
const params = reflector.parameters(Inject.key);
167+
const params = reflector.parameters(Param.key);
140168
```
141169

142170
## Interceptors

src/lib/factories.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ export function createMethodDecorator<TMeta, TArgs extends unknown[] = [TMeta]>(
216216
* // Reflection - query decorated properties
217217
* const columns = Column.properties(User);
218218
* // => [{ kind: "property", name: "name", metadata: [{ type: "varchar", nullable: false }] }]
219+
*
220+
* // Dependency injection - mark properties for resolution
221+
* const Inject = createPropertyDecorator<string>();
222+
*
223+
* class Service {
224+
* \@Inject("database")
225+
* db!: Database;
226+
* }
227+
*
228+
* // Reflect and resolve
229+
* const deps = Inject.properties(Service);
230+
* for (const dep of deps) {
231+
* (instance as any)[dep.name] = container.get(dep.metadata[0]);
232+
* }
219233
* ```
220234
*/
221235
export function createPropertyDecorator<TMeta, TArgs extends unknown[] = [TMeta]>(

0 commit comments

Comments
 (0)