@nitrostack/core 1.0.0
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.
- package/LICENSE +201 -0
- package/README.md +80 -0
- package/dist/auth/api-key.d.ts +118 -0
- package/dist/auth/api-key.d.ts.map +1 -0
- package/dist/auth/api-key.js +168 -0
- package/dist/auth/api-key.js.map +1 -0
- package/dist/auth/client.d.ts +151 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +330 -0
- package/dist/auth/client.js.map +1 -0
- package/dist/auth/index.d.ts +31 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +46 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/middleware.d.ts +95 -0
- package/dist/auth/middleware.d.ts.map +1 -0
- package/dist/auth/middleware.js +260 -0
- package/dist/auth/middleware.js.map +1 -0
- package/dist/auth/pkce.d.ts +53 -0
- package/dist/auth/pkce.d.ts.map +1 -0
- package/dist/auth/pkce.js +105 -0
- package/dist/auth/pkce.js.map +1 -0
- package/dist/auth/quick-setup.d.ts +94 -0
- package/dist/auth/quick-setup.d.ts.map +1 -0
- package/dist/auth/quick-setup.js +210 -0
- package/dist/auth/quick-setup.js.map +1 -0
- package/dist/auth/secure-secret.d.ts +136 -0
- package/dist/auth/secure-secret.d.ts.map +1 -0
- package/dist/auth/secure-secret.js +182 -0
- package/dist/auth/secure-secret.js.map +1 -0
- package/dist/auth/server-integration.d.ts +97 -0
- package/dist/auth/server-integration.d.ts.map +1 -0
- package/dist/auth/server-integration.js +182 -0
- package/dist/auth/server-integration.js.map +1 -0
- package/dist/auth/server-metadata.d.ts +51 -0
- package/dist/auth/server-metadata.d.ts.map +1 -0
- package/dist/auth/server-metadata.js +106 -0
- package/dist/auth/server-metadata.js.map +1 -0
- package/dist/auth/simple-jwt.d.ts +174 -0
- package/dist/auth/simple-jwt.d.ts.map +1 -0
- package/dist/auth/simple-jwt.js +162 -0
- package/dist/auth/simple-jwt.js.map +1 -0
- package/dist/auth/token-store.d.ts +104 -0
- package/dist/auth/token-store.d.ts.map +1 -0
- package/dist/auth/token-store.js +205 -0
- package/dist/auth/token-store.js.map +1 -0
- package/dist/auth/token-validation.d.ts +59 -0
- package/dist/auth/token-validation.d.ts.map +1 -0
- package/dist/auth/token-validation.js +241 -0
- package/dist/auth/token-validation.js.map +1 -0
- package/dist/auth/types.d.ts +215 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +6 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/core/apikey-module.d.ts +69 -0
- package/dist/core/apikey-module.d.ts.map +1 -0
- package/dist/core/apikey-module.js +114 -0
- package/dist/core/apikey-module.js.map +1 -0
- package/dist/core/app-decorator.d.ts +59 -0
- package/dist/core/app-decorator.d.ts.map +1 -0
- package/dist/core/app-decorator.js +322 -0
- package/dist/core/app-decorator.js.map +1 -0
- package/dist/core/builders.d.ts +50 -0
- package/dist/core/builders.d.ts.map +1 -0
- package/dist/core/builders.js +139 -0
- package/dist/core/builders.js.map +1 -0
- package/dist/core/component.d.ts +111 -0
- package/dist/core/component.d.ts.map +1 -0
- package/dist/core/component.js +228 -0
- package/dist/core/component.js.map +1 -0
- package/dist/core/config-module.d.ts +62 -0
- package/dist/core/config-module.d.ts.map +1 -0
- package/dist/core/config-module.js +94 -0
- package/dist/core/config-module.js.map +1 -0
- package/dist/core/decorators/cache.decorator.d.ts +61 -0
- package/dist/core/decorators/cache.decorator.d.ts.map +1 -0
- package/dist/core/decorators/cache.decorator.js +115 -0
- package/dist/core/decorators/cache.decorator.js.map +1 -0
- package/dist/core/decorators/health-check.decorator.d.ts +80 -0
- package/dist/core/decorators/health-check.decorator.d.ts.map +1 -0
- package/dist/core/decorators/health-check.decorator.js +153 -0
- package/dist/core/decorators/health-check.decorator.js.map +1 -0
- package/dist/core/decorators/rate-limit.decorator.d.ts +63 -0
- package/dist/core/decorators/rate-limit.decorator.d.ts.map +1 -0
- package/dist/core/decorators/rate-limit.decorator.js +129 -0
- package/dist/core/decorators/rate-limit.decorator.js.map +1 -0
- package/dist/core/decorators.d.ts +190 -0
- package/dist/core/decorators.d.ts.map +1 -0
- package/dist/core/decorators.js +170 -0
- package/dist/core/decorators.js.map +1 -0
- package/dist/core/di/container.d.ts +64 -0
- package/dist/core/di/container.d.ts.map +1 -0
- package/dist/core/di/container.js +105 -0
- package/dist/core/di/container.js.map +1 -0
- package/dist/core/di/injectable.decorator.d.ts +62 -0
- package/dist/core/di/injectable.decorator.d.ts.map +1 -0
- package/dist/core/di/injectable.decorator.js +66 -0
- package/dist/core/di/injectable.decorator.js.map +1 -0
- package/dist/core/errors.d.ts +54 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +87 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/events/event-emitter.d.ts +50 -0
- package/dist/core/events/event-emitter.d.ts.map +1 -0
- package/dist/core/events/event-emitter.js +94 -0
- package/dist/core/events/event-emitter.js.map +1 -0
- package/dist/core/events/event.decorator.d.ts +48 -0
- package/dist/core/events/event.decorator.d.ts.map +1 -0
- package/dist/core/events/event.decorator.js +72 -0
- package/dist/core/events/event.decorator.js.map +1 -0
- package/dist/core/events/log-emitter.d.ts +14 -0
- package/dist/core/events/log-emitter.d.ts.map +1 -0
- package/dist/core/events/log-emitter.js +20 -0
- package/dist/core/events/log-emitter.js.map +1 -0
- package/dist/core/filters/exception-filter.decorator.d.ts +40 -0
- package/dist/core/filters/exception-filter.decorator.d.ts.map +1 -0
- package/dist/core/filters/exception-filter.decorator.js +54 -0
- package/dist/core/filters/exception-filter.decorator.js.map +1 -0
- package/dist/core/filters/exception-filter.interface.d.ts +39 -0
- package/dist/core/filters/exception-filter.interface.d.ts.map +1 -0
- package/dist/core/filters/exception-filter.interface.js +2 -0
- package/dist/core/filters/exception-filter.interface.js.map +1 -0
- package/dist/core/guards/apikey.guard.d.ts +22 -0
- package/dist/core/guards/apikey.guard.d.ts.map +1 -0
- package/dist/core/guards/apikey.guard.js +11 -0
- package/dist/core/guards/apikey.guard.js.map +1 -0
- package/dist/core/guards/guard.interface.d.ts +18 -0
- package/dist/core/guards/guard.interface.d.ts.map +1 -0
- package/dist/core/guards/guard.interface.js +2 -0
- package/dist/core/guards/guard.interface.js.map +1 -0
- package/dist/core/guards/jwt.guard.d.ts +18 -0
- package/dist/core/guards/jwt.guard.d.ts.map +1 -0
- package/dist/core/guards/jwt.guard.js +2 -0
- package/dist/core/guards/jwt.guard.js.map +1 -0
- package/dist/core/guards/oauth.guard.d.ts +35 -0
- package/dist/core/guards/oauth.guard.d.ts.map +1 -0
- package/dist/core/guards/oauth.guard.js +2 -0
- package/dist/core/guards/oauth.guard.js.map +1 -0
- package/dist/core/guards/use-guards.decorator.d.ts +25 -0
- package/dist/core/guards/use-guards.decorator.d.ts.map +1 -0
- package/dist/core/guards/use-guards.decorator.js +32 -0
- package/dist/core/guards/use-guards.decorator.js.map +1 -0
- package/dist/core/health/health-checks.resource.d.ts +14 -0
- package/dist/core/health/health-checks.resource.d.ts.map +1 -0
- package/dist/core/health/health-checks.resource.js +29 -0
- package/dist/core/health/health-checks.resource.js.map +1 -0
- package/dist/core/index.d.ts +57 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +59 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/interceptors/interceptor.decorator.d.ts +37 -0
- package/dist/core/interceptors/interceptor.decorator.d.ts.map +1 -0
- package/dist/core/interceptors/interceptor.decorator.js +51 -0
- package/dist/core/interceptors/interceptor.decorator.js.map +1 -0
- package/dist/core/interceptors/interceptor.interface.d.ts +31 -0
- package/dist/core/interceptors/interceptor.interface.d.ts.map +1 -0
- package/dist/core/interceptors/interceptor.interface.js +2 -0
- package/dist/core/interceptors/interceptor.interface.js.map +1 -0
- package/dist/core/jwt-module.d.ts +51 -0
- package/dist/core/jwt-module.d.ts.map +1 -0
- package/dist/core/jwt-module.js +52 -0
- package/dist/core/jwt-module.js.map +1 -0
- package/dist/core/logger.d.ts +18 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +53 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/middleware/middleware.decorator.d.ts +39 -0
- package/dist/core/middleware/middleware.decorator.d.ts.map +1 -0
- package/dist/core/middleware/middleware.decorator.js +53 -0
- package/dist/core/middleware/middleware.decorator.js.map +1 -0
- package/dist/core/middleware/middleware.interface.d.ts +29 -0
- package/dist/core/middleware/middleware.interface.d.ts.map +1 -0
- package/dist/core/middleware/middleware.interface.js +2 -0
- package/dist/core/middleware/middleware.interface.js.map +1 -0
- package/dist/core/module.d.ts +93 -0
- package/dist/core/module.d.ts.map +1 -0
- package/dist/core/module.js +87 -0
- package/dist/core/module.js.map +1 -0
- package/dist/core/oauth-module.d.ts +123 -0
- package/dist/core/oauth-module.d.ts.map +1 -0
- package/dist/core/oauth-module.js +324 -0
- package/dist/core/oauth-module.js.map +1 -0
- package/dist/core/pipes/pipe.decorator.d.ts +64 -0
- package/dist/core/pipes/pipe.decorator.d.ts.map +1 -0
- package/dist/core/pipes/pipe.decorator.js +85 -0
- package/dist/core/pipes/pipe.decorator.js.map +1 -0
- package/dist/core/pipes/pipe.interface.d.ts +41 -0
- package/dist/core/pipes/pipe.interface.d.ts.map +1 -0
- package/dist/core/pipes/pipe.interface.js +2 -0
- package/dist/core/pipes/pipe.interface.js.map +1 -0
- package/dist/core/prompt.d.ts +46 -0
- package/dist/core/prompt.d.ts.map +1 -0
- package/dist/core/prompt.js +76 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/resource.d.ts +47 -0
- package/dist/core/resource.d.ts.map +1 -0
- package/dist/core/resource.js +90 -0
- package/dist/core/resource.js.map +1 -0
- package/dist/core/server.d.ts +129 -0
- package/dist/core/server.d.ts.map +1 -0
- package/dist/core/server.js +617 -0
- package/dist/core/server.js.map +1 -0
- package/dist/core/tool.d.ts +108 -0
- package/dist/core/tool.d.ts.map +1 -0
- package/dist/core/tool.js +241 -0
- package/dist/core/tool.js.map +1 -0
- package/dist/core/transports/discovery-http-server.d.ts +19 -0
- package/dist/core/transports/discovery-http-server.d.ts.map +1 -0
- package/dist/core/transports/discovery-http-server.js +54 -0
- package/dist/core/transports/discovery-http-server.js.map +1 -0
- package/dist/core/transports/http-server.d.ts +108 -0
- package/dist/core/transports/http-server.d.ts.map +1 -0
- package/dist/core/transports/http-server.js +293 -0
- package/dist/core/transports/http-server.js.map +1 -0
- package/dist/core/transports/streamable-http.d.ts +177 -0
- package/dist/core/transports/streamable-http.d.ts.map +1 -0
- package/dist/core/transports/streamable-http.js +1287 -0
- package/dist/core/transports/streamable-http.js.map +1 -0
- package/dist/core/types.d.ts +195 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/widgets/widget-examples.resource.d.ts +17 -0
- package/dist/core/widgets/widget-examples.resource.d.ts.map +1 -0
- package/dist/core/widgets/widget-examples.resource.js +28 -0
- package/dist/core/widgets/widget-examples.resource.js.map +1 -0
- package/dist/core/widgets/widget-registry.d.ts +56 -0
- package/dist/core/widgets/widget-registry.d.ts.map +1 -0
- package/dist/core/widgets/widget-registry.js +75 -0
- package/dist/core/widgets/widget-registry.js.map +1 -0
- package/dist/testing/index.d.ts +103 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +161 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/ui-next/index.d.ts +31 -0
- package/dist/ui-next/index.d.ts.map +1 -0
- package/dist/ui-next/index.js +687 -0
- package/dist/ui-next/index.js.map +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { JsonValue, ClassConstructor } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Metadata keys for decorators
|
|
6
|
+
*/
|
|
7
|
+
export declare const TOOL_METADATA: unique symbol;
|
|
8
|
+
export declare const WIDGET_METADATA: unique symbol;
|
|
9
|
+
export declare const RESOURCE_METADATA: unique symbol;
|
|
10
|
+
export declare const PROMPT_METADATA: unique symbol;
|
|
11
|
+
export declare const GUARDS_METADATA: unique symbol;
|
|
12
|
+
/**
|
|
13
|
+
* Example data for tools/resources
|
|
14
|
+
*/
|
|
15
|
+
export interface ExampleData {
|
|
16
|
+
request?: JsonValue;
|
|
17
|
+
response?: JsonValue;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Tool decorator options
|
|
21
|
+
*/
|
|
22
|
+
export interface ToolOptions {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
inputSchema: z.ZodSchema;
|
|
26
|
+
examples?: ExampleData;
|
|
27
|
+
metadata?: {
|
|
28
|
+
category?: string;
|
|
29
|
+
tags?: string[];
|
|
30
|
+
rateLimit?: {
|
|
31
|
+
maxCalls: number;
|
|
32
|
+
windowMs: number;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resource decorator options
|
|
38
|
+
*/
|
|
39
|
+
export interface ResourceOptions {
|
|
40
|
+
uri: string;
|
|
41
|
+
name: string;
|
|
42
|
+
description: string;
|
|
43
|
+
mimeType?: string;
|
|
44
|
+
examples?: {
|
|
45
|
+
response?: JsonValue;
|
|
46
|
+
};
|
|
47
|
+
metadata?: {
|
|
48
|
+
cacheable?: boolean;
|
|
49
|
+
cacheMaxAge?: number;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Prompt decorator options
|
|
54
|
+
*/
|
|
55
|
+
export interface PromptOptions {
|
|
56
|
+
name: string;
|
|
57
|
+
description: string;
|
|
58
|
+
arguments?: Array<{
|
|
59
|
+
name: string;
|
|
60
|
+
description: string;
|
|
61
|
+
required?: boolean;
|
|
62
|
+
}>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Tool metadata stored on class
|
|
66
|
+
*/
|
|
67
|
+
interface ToolMetadataEntry {
|
|
68
|
+
methodName: string;
|
|
69
|
+
options: ToolOptions;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Tool decorator - Marks a method as an MCP tool
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* @Tool({
|
|
77
|
+
* name: 'login',
|
|
78
|
+
* description: 'Login with email and password',
|
|
79
|
+
* inputSchema: z.object({
|
|
80
|
+
* email: z.string().email(),
|
|
81
|
+
* password: z.string(),
|
|
82
|
+
* }),
|
|
83
|
+
* })
|
|
84
|
+
* async login(input: LoginInput, context: ExecutionContext) {
|
|
85
|
+
* // Implementation
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function Tool(options: ToolOptions): MethodDecorator;
|
|
90
|
+
/**
|
|
91
|
+
* Widget decorator - Links a tool to a Next.js widget route
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* @Tool({ name: 'login', ... })
|
|
96
|
+
* @Widget('login-result')
|
|
97
|
+
* async login(input: LoginInput, context: ExecutionContext) {
|
|
98
|
+
* // Implementation
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function Widget(routePath: string): MethodDecorator;
|
|
103
|
+
/**
|
|
104
|
+
* Resource metadata stored on class
|
|
105
|
+
*/
|
|
106
|
+
interface ResourceMetadataEntry {
|
|
107
|
+
methodName: string;
|
|
108
|
+
options: ResourceOptions;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Resource decorator - Marks a method as an MCP resource
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* @Resource({
|
|
116
|
+
* uri: 'db://users/schema',
|
|
117
|
+
* name: 'User Schema',
|
|
118
|
+
* description: 'Database schema for users',
|
|
119
|
+
* })
|
|
120
|
+
* async getUserSchema(context: ExecutionContext) {
|
|
121
|
+
* // Return schema
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function Resource(options: ResourceOptions): MethodDecorator;
|
|
126
|
+
/**
|
|
127
|
+
* Prompt metadata stored on class
|
|
128
|
+
*/
|
|
129
|
+
interface PromptMetadataEntry {
|
|
130
|
+
methodName: string;
|
|
131
|
+
options: PromptOptions;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Prompt decorator - Marks a method as an MCP prompt
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* @Prompt({
|
|
139
|
+
* name: 'authentication-help',
|
|
140
|
+
* description: 'Help with authentication',
|
|
141
|
+
* })
|
|
142
|
+
* async authHelp(args: PromptArgs, context: ExecutionContext) {
|
|
143
|
+
* // Return prompt messages
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export declare function Prompt(options: PromptOptions): MethodDecorator;
|
|
148
|
+
/**
|
|
149
|
+
* Extract tool definitions from a decorated class
|
|
150
|
+
*/
|
|
151
|
+
export declare function extractTools(target: ClassConstructor): ToolMetadataEntry[];
|
|
152
|
+
/**
|
|
153
|
+
* Extract resource definitions from a decorated class
|
|
154
|
+
*/
|
|
155
|
+
export declare function extractResources(target: ClassConstructor): ResourceMetadataEntry[];
|
|
156
|
+
/**
|
|
157
|
+
* Extract prompt definitions from a decorated class
|
|
158
|
+
*/
|
|
159
|
+
export declare function extractPrompts(target: ClassConstructor): PromptMetadataEntry[];
|
|
160
|
+
/**
|
|
161
|
+
* Get widget metadata for a specific method
|
|
162
|
+
*/
|
|
163
|
+
export declare function getWidgetMetadata(target: object, methodName: string): string | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* Get guards metadata for a specific method
|
|
166
|
+
*/
|
|
167
|
+
export declare function getGuardsMetadata(target: object, methodName: string): ClassConstructor[];
|
|
168
|
+
/**
|
|
169
|
+
* Initial tool decorator metadata key
|
|
170
|
+
*/
|
|
171
|
+
export declare const INITIAL_TOOL_METADATA: unique symbol;
|
|
172
|
+
/**
|
|
173
|
+
* InitialTool decorator - Marks a tool to be automatically called when the client starts
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* @Tool({ ... })
|
|
178
|
+
* @InitialTool()
|
|
179
|
+
* async init(input: InitInput, context: ExecutionContext) {
|
|
180
|
+
* // Implementation
|
|
181
|
+
* }
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export declare function InitialTool(): MethodDecorator;
|
|
185
|
+
/**
|
|
186
|
+
* Get initial tool metadata for a specific method
|
|
187
|
+
*/
|
|
188
|
+
export declare function getInitialToolMetadata(target: object, methodName: string): boolean;
|
|
189
|
+
export {};
|
|
190
|
+
//# sourceMappingURL=decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../../src/core/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9D;;GAEG;AACH,eAAO,MAAM,aAAa,eAA0B,CAAC;AACrD,eAAO,MAAM,eAAe,eAA4B,CAAC;AACzD,eAAO,MAAM,iBAAiB,eAA8B,CAAC;AAC7D,eAAO,MAAM,eAAe,eAA4B,CAAC;AACzD,eAAO,MAAM,eAAe,eAA4B,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,SAAS,CAAC;IACpB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,CAAC,CAAC,SAA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;CAC1B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,CAalE;AAED;;GAEG;AACH,UAAU,mBAAmB;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,aAAa,CAAC;CACxB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,eAAe,CAa9D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,iBAAiB,EAAE,CAE1E;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,qBAAqB,EAAE,CAElF;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,gBAAgB,GAAG,mBAAmB,EAAE,CAE9E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAExF;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAExF;AAGD;;GAEG;AACH,eAAO,MAAM,qBAAqB,eAAkC,CAAC;AAErE;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,IAAI,eAAe,CAK7C;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAElF"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Metadata keys for decorators
|
|
4
|
+
*/
|
|
5
|
+
export const TOOL_METADATA = Symbol('tool:metadata');
|
|
6
|
+
export const WIDGET_METADATA = Symbol('widget:metadata');
|
|
7
|
+
export const RESOURCE_METADATA = Symbol('resource:metadata');
|
|
8
|
+
export const PROMPT_METADATA = Symbol('prompt:metadata');
|
|
9
|
+
export const GUARDS_METADATA = Symbol('guards:metadata');
|
|
10
|
+
/**
|
|
11
|
+
* Tool decorator - Marks a method as an MCP tool
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* @Tool({
|
|
16
|
+
* name: 'login',
|
|
17
|
+
* description: 'Login with email and password',
|
|
18
|
+
* inputSchema: z.object({
|
|
19
|
+
* email: z.string().email(),
|
|
20
|
+
* password: z.string(),
|
|
21
|
+
* }),
|
|
22
|
+
* })
|
|
23
|
+
* async login(input: LoginInput, context: ExecutionContext) {
|
|
24
|
+
* // Implementation
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function Tool(options) {
|
|
29
|
+
return function (target, propertyKey, descriptor) {
|
|
30
|
+
// Get existing tools or create new array
|
|
31
|
+
const existingTools = Reflect.getMetadata(TOOL_METADATA, target.constructor) || [];
|
|
32
|
+
// Add this tool
|
|
33
|
+
existingTools.push({
|
|
34
|
+
methodName: String(propertyKey),
|
|
35
|
+
options,
|
|
36
|
+
});
|
|
37
|
+
// Store metadata on the class constructor
|
|
38
|
+
Reflect.defineMetadata(TOOL_METADATA, existingTools, target.constructor);
|
|
39
|
+
return descriptor;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Widget decorator - Links a tool to a Next.js widget route
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* @Tool({ name: 'login', ... })
|
|
48
|
+
* @Widget('login-result')
|
|
49
|
+
* async login(input: LoginInput, context: ExecutionContext) {
|
|
50
|
+
* // Implementation
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function Widget(routePath) {
|
|
55
|
+
return function (target, propertyKey, descriptor) {
|
|
56
|
+
// Store widget metadata
|
|
57
|
+
Reflect.defineMetadata(WIDGET_METADATA, routePath, target, String(propertyKey));
|
|
58
|
+
return descriptor;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Resource decorator - Marks a method as an MCP resource
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* @Resource({
|
|
67
|
+
* uri: 'db://users/schema',
|
|
68
|
+
* name: 'User Schema',
|
|
69
|
+
* description: 'Database schema for users',
|
|
70
|
+
* })
|
|
71
|
+
* async getUserSchema(context: ExecutionContext) {
|
|
72
|
+
* // Return schema
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function Resource(options) {
|
|
77
|
+
return function (target, propertyKey, descriptor) {
|
|
78
|
+
const existingResources = Reflect.getMetadata(RESOURCE_METADATA, target.constructor) || [];
|
|
79
|
+
existingResources.push({
|
|
80
|
+
methodName: String(propertyKey),
|
|
81
|
+
options,
|
|
82
|
+
});
|
|
83
|
+
Reflect.defineMetadata(RESOURCE_METADATA, existingResources, target.constructor);
|
|
84
|
+
return descriptor;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Prompt decorator - Marks a method as an MCP prompt
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* @Prompt({
|
|
93
|
+
* name: 'authentication-help',
|
|
94
|
+
* description: 'Help with authentication',
|
|
95
|
+
* })
|
|
96
|
+
* async authHelp(args: PromptArgs, context: ExecutionContext) {
|
|
97
|
+
* // Return prompt messages
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export function Prompt(options) {
|
|
102
|
+
return function (target, propertyKey, descriptor) {
|
|
103
|
+
const existingPrompts = Reflect.getMetadata(PROMPT_METADATA, target.constructor) || [];
|
|
104
|
+
existingPrompts.push({
|
|
105
|
+
methodName: String(propertyKey),
|
|
106
|
+
options,
|
|
107
|
+
});
|
|
108
|
+
Reflect.defineMetadata(PROMPT_METADATA, existingPrompts, target.constructor);
|
|
109
|
+
return descriptor;
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Extract tool definitions from a decorated class
|
|
114
|
+
*/
|
|
115
|
+
export function extractTools(target) {
|
|
116
|
+
return Reflect.getMetadata(TOOL_METADATA, target) || [];
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Extract resource definitions from a decorated class
|
|
120
|
+
*/
|
|
121
|
+
export function extractResources(target) {
|
|
122
|
+
return Reflect.getMetadata(RESOURCE_METADATA, target) || [];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Extract prompt definitions from a decorated class
|
|
126
|
+
*/
|
|
127
|
+
export function extractPrompts(target) {
|
|
128
|
+
return Reflect.getMetadata(PROMPT_METADATA, target) || [];
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get widget metadata for a specific method
|
|
132
|
+
*/
|
|
133
|
+
export function getWidgetMetadata(target, methodName) {
|
|
134
|
+
return Reflect.getMetadata(WIDGET_METADATA, target, methodName);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get guards metadata for a specific method
|
|
138
|
+
*/
|
|
139
|
+
export function getGuardsMetadata(target, methodName) {
|
|
140
|
+
return Reflect.getMetadata(GUARDS_METADATA, target, methodName) || [];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Initial tool decorator metadata key
|
|
144
|
+
*/
|
|
145
|
+
export const INITIAL_TOOL_METADATA = Symbol('initial_tool:metadata');
|
|
146
|
+
/**
|
|
147
|
+
* InitialTool decorator - Marks a tool to be automatically called when the client starts
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* @Tool({ ... })
|
|
152
|
+
* @InitialTool()
|
|
153
|
+
* async init(input: InitInput, context: ExecutionContext) {
|
|
154
|
+
* // Implementation
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function InitialTool() {
|
|
159
|
+
return function (target, propertyKey, descriptor) {
|
|
160
|
+
Reflect.defineMetadata(INITIAL_TOOL_METADATA, true, target, String(propertyKey));
|
|
161
|
+
return descriptor;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get initial tool metadata for a specific method
|
|
166
|
+
*/
|
|
167
|
+
export function getInitialToolMetadata(target, methodName) {
|
|
168
|
+
return Reflect.getMetadata(INITIAL_TOOL_METADATA, target, methodName) === true;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/core/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAI1B;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AACrD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACzD,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC7D,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AACzD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAuEzD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,IAAI,CAAC,OAAoB;IACvC,OAAO,UAAU,MAA6B,EAAE,WAA4B,EAAE,UAA8B;QAC1G,yCAAyC;QACzC,MAAM,aAAa,GAAwB,OAAO,CAAC,WAAW,CAAC,aAAa,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;QAExG,gBAAgB;QAChB,aAAa,CAAC,IAAI,CAAC;YACjB,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC;YAC/B,OAAO;SACR,CAAC,CAAC;Q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;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAc,EAAE,UAAkB;IACvE,OAAO,OAAO,CAAC,WAAW,CAAC,qBAAqB,EAAE,MAAM,EAAE,UAAU,CAAC,KAAK,IAAI,CAAC;AACjF,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Token type for DI registration
|
|
4
|
+
* Can be a class constructor, string, or symbol
|
|
5
|
+
*/
|
|
6
|
+
export type InjectionToken<T = unknown> = (new (...args: any[]) => T) | string | symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Provider type - a class constructor (using any[] for compatibility)
|
|
9
|
+
*/
|
|
10
|
+
type Provider<T = unknown> = new (...args: any[]) => T;
|
|
11
|
+
/**
|
|
12
|
+
* Dependency Injection Container
|
|
13
|
+
*
|
|
14
|
+
* Simple DI container for managing service instances and dependencies.
|
|
15
|
+
* Supports constructor injection and singleton pattern.
|
|
16
|
+
*/
|
|
17
|
+
export declare class DIContainer {
|
|
18
|
+
private static instance;
|
|
19
|
+
private providers;
|
|
20
|
+
private instances;
|
|
21
|
+
private constructor();
|
|
22
|
+
/**
|
|
23
|
+
* Get the singleton container instance
|
|
24
|
+
*/
|
|
25
|
+
static getInstance(): DIContainer;
|
|
26
|
+
/**
|
|
27
|
+
* Register a provider (class)
|
|
28
|
+
* @param token - The token to register (usually the class itself)
|
|
29
|
+
* @param provider - Optional provider class (defaults to token if it's a class)
|
|
30
|
+
*/
|
|
31
|
+
register<T>(token: InjectionToken<T>, provider?: Provider<T>): void;
|
|
32
|
+
/**
|
|
33
|
+
* Register a value directly
|
|
34
|
+
* @param token - The token to register
|
|
35
|
+
* @param value - The value to associate with the token
|
|
36
|
+
*/
|
|
37
|
+
registerValue<T>(token: InjectionToken<T>, value: T): void;
|
|
38
|
+
/**
|
|
39
|
+
* Resolve a dependency (get or create instance)
|
|
40
|
+
* @param token - The token to resolve
|
|
41
|
+
* @returns The resolved instance
|
|
42
|
+
*/
|
|
43
|
+
resolve<T>(token: InjectionToken<T>): T;
|
|
44
|
+
/**
|
|
45
|
+
* Get constructor dependencies from metadata
|
|
46
|
+
*
|
|
47
|
+
* Priority:
|
|
48
|
+
* 1. Explicit deps in @Injectable({ deps: [...] }) - works in ESM
|
|
49
|
+
* 2. @Inject() parameter decorators
|
|
50
|
+
* 3. design:paramtypes from TypeScript (may not work in ESM)
|
|
51
|
+
*/
|
|
52
|
+
private getDependencies;
|
|
53
|
+
/**
|
|
54
|
+
* Clear all registrations (useful for testing)
|
|
55
|
+
*/
|
|
56
|
+
clear(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Check if a token is registered
|
|
59
|
+
* @param token - The token to check
|
|
60
|
+
*/
|
|
61
|
+
has(token: InjectionToken): boolean;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
64
|
+
//# sourceMappingURL=container.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../../../src/core/di/container.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B;;;GAGG;AAEH,MAAM,MAAM,cAAc,CAAC,CAAC,GAAG,OAAO,IAClC,CAAC,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,GAC3B,MAAM,GACN,MAAM,CAAC;AAEX;;GAEG;AAEH,KAAK,QAAQ,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAc;IACrC,OAAO,CAAC,SAAS,CAA4C;IAC7D,OAAO,CAAC,SAAS,CAA2C;IAE5D,OAAO;IAEP;;OAEG;IACH,MAAM,CAAC,WAAW,IAAI,WAAW;IAOjC;;;;OAIG;IACH,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI;IAInE;;;;OAIG;IACH,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAI1D;;;;OAIG;IACH,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC;IA8BvC;;;;;;;OAOG;IACH,OAAO,CAAC,eAAe;IAuBvB;;OAEG;IACH,KAAK,IAAI,IAAI;IAKb;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO;CAGpC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* Dependency Injection Container
|
|
4
|
+
*
|
|
5
|
+
* Simple DI container for managing service instances and dependencies.
|
|
6
|
+
* Supports constructor injection and singleton pattern.
|
|
7
|
+
*/
|
|
8
|
+
export class DIContainer {
|
|
9
|
+
static instance;
|
|
10
|
+
providers = new Map();
|
|
11
|
+
instances = new Map();
|
|
12
|
+
constructor() { }
|
|
13
|
+
/**
|
|
14
|
+
* Get the singleton container instance
|
|
15
|
+
*/
|
|
16
|
+
static getInstance() {
|
|
17
|
+
if (!DIContainer.instance) {
|
|
18
|
+
DIContainer.instance = new DIContainer();
|
|
19
|
+
}
|
|
20
|
+
return DIContainer.instance;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Register a provider (class)
|
|
24
|
+
* @param token - The token to register (usually the class itself)
|
|
25
|
+
* @param provider - Optional provider class (defaults to token if it's a class)
|
|
26
|
+
*/
|
|
27
|
+
register(token, provider) {
|
|
28
|
+
this.providers.set(token, (provider || token));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Register a value directly
|
|
32
|
+
* @param token - The token to register
|
|
33
|
+
* @param value - The value to associate with the token
|
|
34
|
+
*/
|
|
35
|
+
registerValue(token, value) {
|
|
36
|
+
this.instances.set(token, value);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolve a dependency (get or create instance)
|
|
40
|
+
* @param token - The token to resolve
|
|
41
|
+
* @returns The resolved instance
|
|
42
|
+
*/
|
|
43
|
+
resolve(token) {
|
|
44
|
+
// Check if already instantiated
|
|
45
|
+
if (this.instances.has(token)) {
|
|
46
|
+
return this.instances.get(token);
|
|
47
|
+
}
|
|
48
|
+
// Get the provider
|
|
49
|
+
const provider = this.providers.get(token) || token;
|
|
50
|
+
// If provider is not a constructor function, throw error
|
|
51
|
+
if (typeof provider !== 'function') {
|
|
52
|
+
throw new Error(`Cannot resolve token "${String(token)}". No value or provider registered.`);
|
|
53
|
+
}
|
|
54
|
+
// Get constructor dependencies
|
|
55
|
+
const dependencies = this.getDependencies(provider);
|
|
56
|
+
// Resolve dependencies recursively
|
|
57
|
+
const resolvedDependencies = dependencies.map((dep) => this.resolve(dep));
|
|
58
|
+
// Create instance
|
|
59
|
+
const ProviderClass = provider;
|
|
60
|
+
const instance = new ProviderClass(...resolvedDependencies);
|
|
61
|
+
// Cache instance
|
|
62
|
+
this.instances.set(token, instance);
|
|
63
|
+
return instance;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get constructor dependencies from metadata
|
|
67
|
+
*
|
|
68
|
+
* Priority:
|
|
69
|
+
* 1. Explicit deps in @Injectable({ deps: [...] }) - works in ESM
|
|
70
|
+
* 2. @Inject() parameter decorators
|
|
71
|
+
* 3. design:paramtypes from TypeScript (may not work in ESM)
|
|
72
|
+
*/
|
|
73
|
+
getDependencies(target) {
|
|
74
|
+
// FIRST: Check for explicit deps in @Injectable({ deps: [...] })
|
|
75
|
+
// This is the most reliable method for ESM compatibility
|
|
76
|
+
const explicitDeps = Reflect.getMetadata('nitrostack:deps', target);
|
|
77
|
+
if (explicitDeps && explicitDeps.length > 0) {
|
|
78
|
+
return explicitDeps;
|
|
79
|
+
}
|
|
80
|
+
// SECOND: Get design:paramtypes metadata set by TypeScript
|
|
81
|
+
// This may be empty in ESM if reflect-metadata wasn't loaded early enough
|
|
82
|
+
const params = Reflect.getMetadata('design:paramtypes', target) || [];
|
|
83
|
+
// Check for @Inject tokens which override the types
|
|
84
|
+
const injectTokens = Reflect.getMetadata('nitrostack:inject', target) || [];
|
|
85
|
+
// Merge: use inject tokens where specified, otherwise use the param type
|
|
86
|
+
return params.map((param, index) => {
|
|
87
|
+
return injectTokens[index] || param;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Clear all registrations (useful for testing)
|
|
92
|
+
*/
|
|
93
|
+
clear() {
|
|
94
|
+
this.providers.clear();
|
|
95
|
+
this.instances.clear();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if a token is registered
|
|
99
|
+
* @param token - The token to check
|
|
100
|
+
*/
|
|
101
|
+
has(token) {
|
|
102
|
+
return this.providers.has(token) || this.instances.has(token);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=container.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../../src/core/di/container.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAkB1B;;;;;GAKG;AACH,MAAM,OAAO,WAAW;IACd,MAAM,CAAC,QAAQ,CAAc;IAC7B,SAAS,GAAkC,IAAI,GAAG,EAAE,CAAC;IACrD,SAAS,GAAiC,IAAI,GAAG,EAAE,CAAC;IAE5D,gBAAuB,CAAC;IAExB;;OAEG;IACH,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;YAC1B,WAAW,CAAC,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC;QAC3C,CAAC;QACD,OAAO,WAAW,CAAC,QAAQ,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,Q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;;;;;;OAOG;IACK,eAAe,CAAC,MAAgB;QACtC,iEAAiE;QACjE,yDAAyD;QACzD,MAAM,YAAY,GAChB,OAAO,CAAC,WAAW,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;QAEjD,IAAI,YAAY,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC5C,OAAO,YAAY,CAAC;QACtB,CAAC;QAED,2DAA2D;QAC3D,0EAA0E;QAC1E,MAAM,MAAM,GAAqB,OAAO,CAAC,WAAW,CAAC,mBAAmB,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;QAExF,oDAAoD;QACpD,MAAM,YAAY,GAAqB,OAAO,CAAC,WAAW,CAAC,mBAAmB,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;QAE9F,yEAAyE;QACzE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YACjC,OAAO,YAAY,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;QACtC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,KAAqB;QACvB,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { InjectionToken } from './container.js';
|
|
3
|
+
import type { ClassConstructor } from '../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options for @Injectable decorator
|
|
6
|
+
*/
|
|
7
|
+
export interface InjectableOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Explicit dependencies for ESM compatibility.
|
|
10
|
+
*
|
|
11
|
+
* In ESM environments, TypeScript's emitDecoratorMetadata may not work
|
|
12
|
+
* reliably due to module loading order. Use this to explicitly declare
|
|
13
|
+
* constructor dependencies.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* @Injectable({ deps: [DatabaseService, LoggerService] })
|
|
18
|
+
* export class UserService {
|
|
19
|
+
* constructor(private db: DatabaseService, private logger: LoggerService) {}
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
deps?: InjectionToken[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Marks a class as injectable (can be used with DI)
|
|
27
|
+
*
|
|
28
|
+
* @example Basic usage (relies on TypeScript's emitDecoratorMetadata):
|
|
29
|
+
* ```typescript
|
|
30
|
+
* @Injectable()
|
|
31
|
+
* export class UserService {
|
|
32
|
+
* constructor(private db: DatabaseService) {}
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example With explicit deps (recommended for ESM compatibility):
|
|
37
|
+
* ```typescript
|
|
38
|
+
* @Injectable({ deps: [DatabaseService] })
|
|
39
|
+
* export class UserService {
|
|
40
|
+
* constructor(private db: DatabaseService) {}
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function Injectable(options?: InjectableOptions): ClassDecorator;
|
|
45
|
+
/**
|
|
46
|
+
* Inject a specific token (optional - for custom injection)
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* constructor(@Inject('DATABASE_CONFIG') private config: DatabaseConfig) {}
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function Inject(token: InjectionToken): ParameterDecorator;
|
|
54
|
+
/**
|
|
55
|
+
* Check if a class is injectable
|
|
56
|
+
*/
|
|
57
|
+
export declare function isInjectable(target: ClassConstructor): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Get inject tokens for constructor parameters
|
|
60
|
+
*/
|
|
61
|
+
export declare function getInjectTokens(target: ClassConstructor): InjectionToken[];
|
|
62
|
+
//# sourceMappingURL=injectable.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectable.decorator.d.ts","sourceRoot":"","sources":["../../../src/core/di/injectable.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAe,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAMpD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,EAAE,cAAc,EAAE,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,cAAc,CAetE;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,cAAc,GAAG,kBAAkB,CAMhE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAE9D;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,gBAAgB,GAAG,cAAc,EAAE,CAE1E"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { DIContainer } from './container.js';
|
|
3
|
+
const INJECTABLE_KEY = 'nitrostack:injectable';
|
|
4
|
+
const INJECT_KEY = 'nitrostack:inject';
|
|
5
|
+
const DEPS_KEY = 'nitrostack:deps';
|
|
6
|
+
/**
|
|
7
|
+
* Marks a class as injectable (can be used with DI)
|
|
8
|
+
*
|
|
9
|
+
* @example Basic usage (relies on TypeScript's emitDecoratorMetadata):
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @Injectable()
|
|
12
|
+
* export class UserService {
|
|
13
|
+
* constructor(private db: DatabaseService) {}
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @example With explicit deps (recommended for ESM compatibility):
|
|
18
|
+
* ```typescript
|
|
19
|
+
* @Injectable({ deps: [DatabaseService] })
|
|
20
|
+
* export class UserService {
|
|
21
|
+
* constructor(private db: DatabaseService) {}
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function Injectable(options) {
|
|
26
|
+
return (target) => {
|
|
27
|
+
Reflect.defineMetadata(INJECTABLE_KEY, true, target);
|
|
28
|
+
// Store explicit deps if provided (for ESM compatibility)
|
|
29
|
+
if (options?.deps && options.deps.length > 0) {
|
|
30
|
+
Reflect.defineMetadata(DEPS_KEY, options.deps, target);
|
|
31
|
+
}
|
|
32
|
+
// Auto-register in DI container
|
|
33
|
+
const container = DIContainer.getInstance();
|
|
34
|
+
if (!container.has(target)) {
|
|
35
|
+
container.register(target);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Inject a specific token (optional - for custom injection)
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* constructor(@Inject('DATABASE_CONFIG') private config: DatabaseConfig) {}
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function Inject(token) {
|
|
48
|
+
return (target, propertyKey, parameterIndex) => {
|
|
49
|
+
const existingTokens = Reflect.getMetadata(INJECT_KEY, target) || [];
|
|
50
|
+
existingTokens[parameterIndex] = token;
|
|
51
|
+
Reflect.defineMetadata(INJECT_KEY, existingTokens, target);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check if a class is injectable
|
|
56
|
+
*/
|
|
57
|
+
export function isInjectable(target) {
|
|
58
|
+
return Reflect.getMetadata(INJECTABLE_KEY, target) === true;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get inject tokens for constructor parameters
|
|
62
|
+
*/
|
|
63
|
+
export function getInjectTokens(target) {
|
|
64
|
+
return Reflect.getMetadata(INJECT_KEY, target) || [];
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=injectable.decorator.js.map
|