@noony-serverless/core 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,12 +2,12 @@ import type { Context } from '../core/core';
|
|
|
2
2
|
/**
|
|
3
3
|
* Get a service from the dependency injection container
|
|
4
4
|
*
|
|
5
|
-
* Type-safe utility to resolve services from the TypeDI container
|
|
6
|
-
*
|
|
5
|
+
* Type-safe utility to resolve services from the TypeDI container.
|
|
6
|
+
* Supports both class constructors and string identifiers.
|
|
7
7
|
*
|
|
8
8
|
* @template T The service type to resolve
|
|
9
9
|
* @param context - Request context containing the DI container
|
|
10
|
-
* @param
|
|
10
|
+
* @param serviceIdentifier - Service class constructor OR string identifier
|
|
11
11
|
* @returns Service instance
|
|
12
12
|
* @throws Error if container is not initialized
|
|
13
13
|
*
|
|
@@ -16,13 +16,26 @@ import type { Context } from '../core/core';
|
|
|
16
16
|
* import { getService } from '@noony-serverless/core';
|
|
17
17
|
* import { UserService } from '../services/user.service';
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* // ✅ Best: Class-based access (type inferred automatically)
|
|
20
|
+
* export async function handler(context: Context) {
|
|
21
|
+
* const userService = getService(context, UserService);
|
|
22
|
+
* // userService is typed as UserService automatically
|
|
23
|
+
* const users = await userService.getUsers();
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* // ✅ Good: String-based with explicit generic (when manual instantiation required)
|
|
27
|
+
* export async function handler(context: Context) {
|
|
28
|
+
* const planRepo = getService<ActionPlanRepository>(context, 'ActionPlanRepository');
|
|
29
|
+
* // planRepo is typed as ActionPlanRepository via explicit generic
|
|
30
|
+
* const plan = await planRepo.findById(id);
|
|
31
|
+
* }
|
|
21
32
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
33
|
+
* // ⚠️ Avoid: String without generic (only for quick prototypes)
|
|
34
|
+
* export async function handler(context: Context) {
|
|
35
|
+
* const repo = getService(context, 'ActionPlanRepository');
|
|
36
|
+
* // repo has type 'unknown' - requires manual type assertion
|
|
24
37
|
* }
|
|
25
38
|
* ```
|
|
26
39
|
*/
|
|
27
|
-
export declare function getService<T>(context: Context<unknown, unknown>,
|
|
40
|
+
export declare function getService<T>(context: Context<unknown, unknown>, serviceIdentifier: (new (...args: any[]) => T) | string): T;
|
|
28
41
|
//# sourceMappingURL=container.utils.d.ts.map
|
|
@@ -4,12 +4,12 @@ exports.getService = getService;
|
|
|
4
4
|
/**
|
|
5
5
|
* Get a service from the dependency injection container
|
|
6
6
|
*
|
|
7
|
-
* Type-safe utility to resolve services from the TypeDI container
|
|
8
|
-
*
|
|
7
|
+
* Type-safe utility to resolve services from the TypeDI container.
|
|
8
|
+
* Supports both class constructors and string identifiers.
|
|
9
9
|
*
|
|
10
10
|
* @template T The service type to resolve
|
|
11
11
|
* @param context - Request context containing the DI container
|
|
12
|
-
* @param
|
|
12
|
+
* @param serviceIdentifier - Service class constructor OR string identifier
|
|
13
13
|
* @returns Service instance
|
|
14
14
|
* @throws Error if container is not initialized
|
|
15
15
|
*
|
|
@@ -18,18 +18,31 @@ exports.getService = getService;
|
|
|
18
18
|
* import { getService } from '@noony-serverless/core';
|
|
19
19
|
* import { UserService } from '../services/user.service';
|
|
20
20
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
21
|
+
* // ✅ Best: Class-based access (type inferred automatically)
|
|
22
|
+
* export async function handler(context: Context) {
|
|
23
|
+
* const userService = getService(context, UserService);
|
|
24
|
+
* // userService is typed as UserService automatically
|
|
25
|
+
* const users = await userService.getUsers();
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // ✅ Good: String-based with explicit generic (when manual instantiation required)
|
|
29
|
+
* export async function handler(context: Context) {
|
|
30
|
+
* const planRepo = getService<ActionPlanRepository>(context, 'ActionPlanRepository');
|
|
31
|
+
* // planRepo is typed as ActionPlanRepository via explicit generic
|
|
32
|
+
* const plan = await planRepo.findById(id);
|
|
33
|
+
* }
|
|
23
34
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
35
|
+
* // ⚠️ Avoid: String without generic (only for quick prototypes)
|
|
36
|
+
* export async function handler(context: Context) {
|
|
37
|
+
* const repo = getService(context, 'ActionPlanRepository');
|
|
38
|
+
* // repo has type 'unknown' - requires manual type assertion
|
|
26
39
|
* }
|
|
27
40
|
* ```
|
|
28
41
|
*/
|
|
29
|
-
function getService(context,
|
|
42
|
+
function getService(context, serviceIdentifier) {
|
|
30
43
|
if (!context.container) {
|
|
31
44
|
throw new Error('Container not initialized. Did you forget to add DependencyInjectionMiddleware?');
|
|
32
45
|
}
|
|
33
|
-
return context.container.get(
|
|
46
|
+
return context.container.get(serviceIdentifier);
|
|
34
47
|
}
|
|
35
48
|
//# sourceMappingURL=container.utils.js.map
|
package/package.json
CHANGED