@geekmidas/services 0.1.0 → 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/CHANGELOG.md +13 -0
- package/dist/ServiceDiscovery-BO2FzEGt.cjs +200 -0
- package/dist/ServiceDiscovery-BO2FzEGt.cjs.map +1 -0
- package/dist/ServiceDiscovery-CfVBrQHt.d.cts +189 -0
- package/dist/ServiceDiscovery-CfVBrQHt.d.cts.map +1 -0
- package/dist/ServiceDiscovery-DLMyH_yM.mjs +195 -0
- package/dist/ServiceDiscovery-DLMyH_yM.mjs.map +1 -0
- package/dist/ServiceDiscovery-DpeEKNe2.d.mts +189 -0
- package/dist/ServiceDiscovery-DpeEKNe2.d.mts.map +1 -0
- package/dist/ServiceDiscovery.cjs +4 -0
- package/dist/ServiceDiscovery.d.cts +3 -0
- package/dist/ServiceDiscovery.d.mts +3 -0
- package/dist/ServiceDiscovery.mjs +4 -0
- package/dist/context-B5YTspJR.mjs +61 -0
- package/dist/context-B5YTspJR.mjs.map +1 -0
- package/dist/context-BVeZOvOd.d.cts +45 -0
- package/dist/context-BVeZOvOd.d.cts.map +1 -0
- package/dist/context-DMExgNzl.d.mts +45 -0
- package/dist/context-DMExgNzl.d.mts.map +1 -0
- package/dist/context-DUTDtYd2.cjs +95 -0
- package/dist/context-DUTDtYd2.cjs.map +1 -0
- package/dist/context.cjs +4 -0
- package/dist/context.d.cts +3 -0
- package/dist/context.d.mts +3 -0
- package/dist/context.mjs +3 -0
- package/dist/index.cjs +5 -193
- package/dist/index.d.cts +4 -227
- package/dist/index.d.mts +4 -227
- package/dist/index.mjs +3 -192
- package/dist/types-CcHmCx_U.d.mts +86 -0
- package/dist/types-CcHmCx_U.d.mts.map +1 -0
- package/dist/types-D7d_yeU5.d.cts +86 -0
- package/dist/types-D7d_yeU5.d.cts.map +1 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +2 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +0 -0
- package/package.json +10 -5
- package/src/ServiceDiscovery.ts +254 -0
- package/src/__tests__/context.spec.ts +276 -0
- package/src/__tests__/index.spec.ts +556 -558
- package/src/context.ts +91 -0
- package/src/index.ts +15 -295
- package/src/types.ts +85 -0
- package/tsconfig.json +9 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @geekmidas/services
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [`ff7b115`](https://github.com/geekmidas/toolbox/commit/ff7b11599f60f84ac6cdc73714c853ecf786b2e8) Thanks [@geekmidas](https://github.com/geekmidas)! - Version 1 Stable release
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`ff7b115`](https://github.com/geekmidas/toolbox/commit/ff7b11599f60f84ac6cdc73714c853ecf786b2e8)]:
|
|
12
|
+
- @geekmidas/envkit@1.0.0
|
|
13
|
+
- @geekmidas/logger@1.0.0
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
const require_context = require('./context-DUTDtYd2.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/ServiceDiscovery.ts
|
|
4
|
+
/**
|
|
5
|
+
* Service discovery container that manages service registration and retrieval.
|
|
6
|
+
* Implements a singleton pattern with lazy initialization of services.
|
|
7
|
+
*
|
|
8
|
+
* @template TServices - Record type mapping service names to their instance types
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Define service types
|
|
13
|
+
* interface MyServices {
|
|
14
|
+
* database: Database;
|
|
15
|
+
* cache: CacheService;
|
|
16
|
+
* auth: AuthService;
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Get service discovery instance
|
|
20
|
+
* const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
21
|
+
*
|
|
22
|
+
* // Register services
|
|
23
|
+
* await discovery.register([
|
|
24
|
+
* databaseService,
|
|
25
|
+
* cacheService,
|
|
26
|
+
* authService
|
|
27
|
+
* ]);
|
|
28
|
+
*
|
|
29
|
+
* // Retrieve services
|
|
30
|
+
* const db = await discovery.get('database');
|
|
31
|
+
* const { cache, auth } = await discovery.getMany(['cache', 'auth']);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var ServiceDiscovery = class ServiceDiscovery {
|
|
35
|
+
/** Singleton instance of ServiceDiscovery */
|
|
36
|
+
static _instance;
|
|
37
|
+
/** Map of registered service definitions */
|
|
38
|
+
services = /* @__PURE__ */ new Map();
|
|
39
|
+
/** Map of instantiated service instances */
|
|
40
|
+
instances = /* @__PURE__ */ new Map();
|
|
41
|
+
/**
|
|
42
|
+
* Gets the singleton instance of ServiceDiscovery.
|
|
43
|
+
* Creates a new instance if one doesn't exist.
|
|
44
|
+
*
|
|
45
|
+
* @template T - Record type mapping service names to their instance types
|
|
46
|
+
* @param envParser - Environment parser for service configuration
|
|
47
|
+
* @returns The ServiceDiscovery singleton instance
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const services = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
static getInstance(envParser) {
|
|
55
|
+
if (!ServiceDiscovery._instance) ServiceDiscovery._instance = new ServiceDiscovery(envParser);
|
|
56
|
+
return ServiceDiscovery._instance;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resets the singleton instance. Use only for testing purposes.
|
|
60
|
+
* This clears all cached services and allows a fresh instance to be created.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // In test teardown
|
|
65
|
+
* afterEach(() => {
|
|
66
|
+
* ServiceDiscovery.reset();
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
static reset() {
|
|
71
|
+
ServiceDiscovery._instance = void 0;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Private constructor to enforce singleton pattern.
|
|
75
|
+
*
|
|
76
|
+
* @param envParser - Environment parser for service configuration
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
constructor(envParser) {
|
|
80
|
+
this.envParser = envParser;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Register multiple services with the service discovery.
|
|
84
|
+
* Services are instantiated lazily on first access.
|
|
85
|
+
* Already instantiated services are returned from cache.
|
|
86
|
+
*
|
|
87
|
+
* @template T - Array type of services to register
|
|
88
|
+
* @param services - Array of services to register
|
|
89
|
+
* @returns Promise resolving to a record of service names to instances
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const services = await discovery.register([
|
|
94
|
+
* databaseService,
|
|
95
|
+
* cacheService,
|
|
96
|
+
* authService
|
|
97
|
+
* ]);
|
|
98
|
+
*
|
|
99
|
+
* // services = {
|
|
100
|
+
* // database: Database instance,
|
|
101
|
+
* // cache: CacheService instance,
|
|
102
|
+
* // auth: AuthService instance
|
|
103
|
+
* // }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
async register(services) {
|
|
107
|
+
const registeredServices = {};
|
|
108
|
+
for (const service of services) {
|
|
109
|
+
const name = service.serviceName;
|
|
110
|
+
if (this.instances.has(name)) {
|
|
111
|
+
registeredServices[name] = this.instances.get(name);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const instance = await service.register({
|
|
115
|
+
envParser: this.envParser,
|
|
116
|
+
context: require_context.serviceContext
|
|
117
|
+
});
|
|
118
|
+
this.instances.set(name, instance);
|
|
119
|
+
registeredServices[name] = instance;
|
|
120
|
+
}
|
|
121
|
+
return registeredServices;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get a service from the service discovery.
|
|
125
|
+
* Services are instantiated on first access if not already cached.
|
|
126
|
+
*
|
|
127
|
+
* @template K - The service name key
|
|
128
|
+
* @param name - The name of the service to get
|
|
129
|
+
* @returns Promise resolving to the service instance
|
|
130
|
+
* @throws {Error} If the service is not registered
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const database = await discovery.get('database');
|
|
135
|
+
* const users = await database.query('SELECT * FROM users');
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
get(name) {
|
|
139
|
+
const service = this.services.get(name);
|
|
140
|
+
if (!service) throw new Error(`Service '${name}' not found in service discovery`);
|
|
141
|
+
return service.register({
|
|
142
|
+
envParser: this.envParser,
|
|
143
|
+
context: require_context.serviceContext
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get multiple services from the service discovery.
|
|
148
|
+
* Useful for retrieving multiple dependencies at once.
|
|
149
|
+
*
|
|
150
|
+
* @template K - Array of service name keys
|
|
151
|
+
* @param names - Array of service names to retrieve
|
|
152
|
+
* @returns Promise resolving to an object containing the service instances
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const { database, cache, auth } = await discovery.getMany([
|
|
157
|
+
* 'database',
|
|
158
|
+
* 'cache',
|
|
159
|
+
* 'auth'
|
|
160
|
+
* ]);
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
async getMany(names) {
|
|
164
|
+
const result = {};
|
|
165
|
+
for (const name of names) result[name] = await this.get(name);
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Check if a service exists in the service discovery.
|
|
170
|
+
* Can check by service name or service instance.
|
|
171
|
+
*
|
|
172
|
+
* @param service - The service name or service instance to check
|
|
173
|
+
* @returns True if the service exists, false otherwise
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* if (discovery.has('database')) {
|
|
178
|
+
* const db = await discovery.get('database');
|
|
179
|
+
* }
|
|
180
|
+
*
|
|
181
|
+
* // Or check with service instance
|
|
182
|
+
* if (!discovery.has(databaseService)) {
|
|
183
|
+
* await discovery.register([databaseService]);
|
|
184
|
+
* }
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
has(service) {
|
|
188
|
+
if (typeof service === "string") return this.services.has(service);
|
|
189
|
+
return this.services.has(service.serviceName);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
Object.defineProperty(exports, 'ServiceDiscovery', {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
get: function () {
|
|
197
|
+
return ServiceDiscovery;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
//# sourceMappingURL=ServiceDiscovery-BO2FzEGt.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-BO2FzEGt.cjs","names":["envParser: EnvironmentParser<{}>","services: T","serviceContext","name: K","names: [...K]","service: string | Service"],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":["import type { EnvironmentParser } from '@geekmidas/envkit';\nimport { serviceContext } from './context';\nimport type { Service } from './types';\n\n/**\n * Service discovery container that manages service registration and retrieval.\n * Implements a singleton pattern with lazy initialization of services.\n *\n * @template TServices - Record type mapping service names to their instance types\n *\n * @example\n * ```typescript\n * // Define service types\n * interface MyServices {\n * database: Database;\n * cache: CacheService;\n * auth: AuthService;\n * }\n *\n * // Get service discovery instance\n * const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);\n *\n * // Register services\n * await discovery.register([\n * databaseService,\n * cacheService,\n * authService\n * ]);\n *\n * // Retrieve services\n * const db = await discovery.get('database');\n * const { cache, auth } = await discovery.getMany(['cache', 'auth']);\n * ```\n */\nexport class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {\n\t/** Singleton instance of ServiceDiscovery */\n\tprivate static _instance: ServiceDiscovery<any>;\n\t/** Map of registered service definitions */\n\tprivate services = new Map<string, Service>();\n\t/** Map of instantiated service instances */\n\tprivate instances = new Map<keyof TServices, TServices[keyof TServices]>();\n\n\t/**\n\t * Gets the singleton instance of ServiceDiscovery.\n\t * Creates a new instance if one doesn't exist.\n\t *\n\t * @template T - Record type mapping service names to their instance types\n\t * @param envParser - Environment parser for service configuration\n\t * @returns The ServiceDiscovery singleton instance\n\t *\n\t * @example\n\t * ```typescript\n\t * const services = ServiceDiscovery.getInstance<MyServices>(envParser);\n\t * ```\n\t */\n\tstatic getInstance<T extends Record<any, unknown> = any>(\n\t\tenvParser: EnvironmentParser<{}>,\n\t): ServiceDiscovery<T> {\n\t\tif (!ServiceDiscovery._instance) {\n\t\t\tServiceDiscovery._instance = new ServiceDiscovery<T>(envParser);\n\t\t}\n\t\treturn ServiceDiscovery._instance as ServiceDiscovery<T>;\n\t}\n\n\t/**\n\t * Resets the singleton instance. Use only for testing purposes.\n\t * This clears all cached services and allows a fresh instance to be created.\n\t *\n\t * @example\n\t * ```typescript\n\t * // In test teardown\n\t * afterEach(() => {\n\t * ServiceDiscovery.reset();\n\t * });\n\t * ```\n\t */\n\tstatic reset(): void {\n\t\tServiceDiscovery._instance = undefined as any;\n\t}\n\n\t/**\n\t * Private constructor to enforce singleton pattern.\n\t *\n\t * @param envParser - Environment parser for service configuration\n\t * @private\n\t */\n\tprivate constructor(readonly envParser: EnvironmentParser<{}>) {}\n\n\t/**\n\t * Register multiple services with the service discovery.\n\t * Services are instantiated lazily on first access.\n\t * Already instantiated services are returned from cache.\n\t *\n\t * @template T - Array type of services to register\n\t * @param services - Array of services to register\n\t * @returns Promise resolving to a record of service names to instances\n\t *\n\t * @example\n\t * ```typescript\n\t * const services = await discovery.register([\n\t * databaseService,\n\t * cacheService,\n\t * authService\n\t * ]);\n\t *\n\t * // services = {\n\t * // database: Database instance,\n\t * // cache: CacheService instance,\n\t * // auth: AuthService instance\n\t * // }\n\t * ```\n\t */\n\tasync register<T extends Service[]>(services: T): Promise<ServiceRecord<T>> {\n\t\tconst registeredServices = {} as ServiceRecord<T>;\n\t\tfor (const service of services) {\n\t\t\tconst name = service.serviceName as T[number]['serviceName'];\n\t\t\tif (this.instances.has(name)) {\n\t\t\t\t(registeredServices as any)[name] = this.instances.get(\n\t\t\t\t\tname,\n\t\t\t\t) as TServices[keyof TServices];\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Pass both envParser and context to service\n\t\t\tconst instance = await service.register({\n\t\t\t\tenvParser: this.envParser,\n\t\t\t\tcontext: serviceContext,\n\t\t\t});\n\n\t\t\tthis.instances.set(name, instance as TServices[keyof TServices]);\n\t\t\t(registeredServices as any)[name] =\n\t\t\t\tinstance as TServices[keyof TServices];\n\t\t}\n\n\t\treturn registeredServices;\n\t}\n\n\t/**\n\t * Get a service from the service discovery.\n\t * Services are instantiated on first access if not already cached.\n\t *\n\t * @template K - The service name key\n\t * @param name - The name of the service to get\n\t * @returns Promise resolving to the service instance\n\t * @throws {Error} If the service is not registered\n\t *\n\t * @example\n\t * ```typescript\n\t * const database = await discovery.get('database');\n\t * const users = await database.query('SELECT * FROM users');\n\t * ```\n\t */\n\tget<K extends keyof TServices & string>(name: K): Promise<TServices[K]> {\n\t\tconst service = this.services.get(name);\n\n\t\tif (!service) {\n\t\t\tthrow new Error(`Service '${name}' not found in service discovery`);\n\t\t}\n\n\t\treturn service.register({\n\t\t\tenvParser: this.envParser,\n\t\t\tcontext: serviceContext,\n\t\t}) as Promise<TServices[K]>;\n\t}\n\t/**\n\t * Get multiple services from the service discovery.\n\t * Useful for retrieving multiple dependencies at once.\n\t *\n\t * @template K - Array of service name keys\n\t * @param names - Array of service names to retrieve\n\t * @returns Promise resolving to an object containing the service instances\n\t *\n\t * @example\n\t * ```typescript\n\t * const { database, cache, auth } = await discovery.getMany([\n\t * 'database',\n\t * 'cache',\n\t * 'auth'\n\t * ]);\n\t * ```\n\t */\n\tasync getMany<K extends (keyof TServices & string)[]>(\n\t\tnames: [...K],\n\t): Promise<{ [P in K[number]]: TServices[P] }> {\n\t\tconst result = {} as { [P in K[number]]: TServices[P] };\n\n\t\tfor (const name of names) {\n\t\t\tresult[name] = await this.get(name);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Check if a service exists in the service discovery.\n\t * Can check by service name or service instance.\n\t *\n\t * @param service - The service name or service instance to check\n\t * @returns True if the service exists, false otherwise\n\t *\n\t * @example\n\t * ```typescript\n\t * if (discovery.has('database')) {\n\t * const db = await discovery.get('database');\n\t * }\n\t *\n\t * // Or check with service instance\n\t * if (!discovery.has(databaseService)) {\n\t * await discovery.register([databaseService]);\n\t * }\n\t * ```\n\t */\n\thas(service: string | Service): boolean {\n\t\tif (typeof service === 'string') {\n\t\t\treturn this.services.has(service);\n\t\t}\n\n\t\treturn this.services.has(service.serviceName);\n\t}\n}\n\n/**\n * Utility type to extract service names from an array of services.\n *\n * @template T - Array of Service types\n *\n * @example\n * ```typescript\n * type Names = ExtractServiceNames<[typeof databaseService, typeof cacheService]>;\n * // type Names = 'database' | 'cache'\n * ```\n */\nexport type ExtractServiceNames<T extends Service[]> = T[number]['serviceName'];\n\n/**\n * Utility type to create a record type from an array of services.\n * Maps service names to their registered instance types.\n *\n * @template T - Array of Service types\n *\n * @example\n * ```typescript\n * type MyServiceRecord = ServiceRecord<[typeof databaseService, typeof cacheService]>;\n * // type MyServiceRecord = {\n * // database: DatabaseInstance;\n * // cache: CacheInstance;\n * // }\n * ```\n */\nexport type ServiceRecord<T extends Service[]> = {\n\t[K in T[number] as K['serviceName']]: K extends Service\n\t\t? Awaited<ReturnType<K['register']>>\n\t\t: never;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,mBAAb,MAAa,iBAAiE;;CAE7E,OAAe;;CAEf,AAAQ,2BAAW,IAAI;;CAEvB,AAAQ,4BAAY,IAAI;;;;;;;;;;;;;;CAexB,OAAO,YACNA,WACsB;AACtB,OAAK,iBAAiB,UACrB,kBAAiB,YAAY,IAAI,iBAAoB;AAEtD,SAAO,iBAAiB;CACxB;;;;;;;;;;;;;CAcD,OAAO,QAAc;AACpB,mBAAiB;CACjB;;;;;;;CAQD,AAAQ,YAAqBA,WAAkC;EAAlC;CAAoC;;;;;;;;;;;;;;;;;;;;;;;;;CA0BjE,MAAM,SAA8BC,UAAwC;EAC3E,MAAM,qBAAqB,CAAE;AAC7B,OAAK,MAAM,WAAW,UAAU;GAC/B,MAAM,OAAO,QAAQ;AACrB,OAAI,KAAK,UAAU,IAAI,KAAK,EAAE;AAC7B,IAAC,mBAA2B,QAAQ,KAAK,UAAU,IAClD,KACA;AACD;GACA;GAGD,MAAM,WAAW,MAAM,QAAQ,SAAS;IACvC,WAAW,KAAK;IAChB,SAASC;GACT,EAAC;AAEF,QAAK,UAAU,IAAI,MAAM,SAAuC;AAChE,GAAC,mBAA2B,QAC3B;EACD;AAED,SAAO;CACP;;;;;;;;;;;;;;;;CAiBD,IAAwCC,MAAgC;EACvE,MAAM,UAAU,KAAK,SAAS,IAAI,KAAK;AAEvC,OAAK,QACJ,OAAM,IAAI,OAAO,WAAW,KAAK;AAGlC,SAAO,QAAQ,SAAS;GACvB,WAAW,KAAK;GAChB,SAASD;EACT,EAAC;CACF;;;;;;;;;;;;;;;;;;CAkBD,MAAM,QACLE,OAC8C;EAC9C,MAAM,SAAS,CAAE;AAEjB,OAAK,MAAM,QAAQ,MAClB,QAAO,QAAQ,MAAM,KAAK,IAAI,KAAK;AAGpC,SAAO;CACP;;;;;;;;;;;;;;;;;;;;CAqBD,IAAIC,SAAoC;AACvC,aAAW,YAAY,SACtB,QAAO,KAAK,SAAS,IAAI,QAAQ;AAGlC,SAAO,KAAK,SAAS,IAAI,QAAQ,YAAY;CAC7C;AACD"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Service } from "./types-D7d_yeU5.cjs";
|
|
2
|
+
import { EnvironmentParser } from "@geekmidas/envkit";
|
|
3
|
+
|
|
4
|
+
//#region src/ServiceDiscovery.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Service discovery container that manages service registration and retrieval.
|
|
8
|
+
* Implements a singleton pattern with lazy initialization of services.
|
|
9
|
+
*
|
|
10
|
+
* @template TServices - Record type mapping service names to their instance types
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Define service types
|
|
15
|
+
* interface MyServices {
|
|
16
|
+
* database: Database;
|
|
17
|
+
* cache: CacheService;
|
|
18
|
+
* auth: AuthService;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // Get service discovery instance
|
|
22
|
+
* const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
23
|
+
*
|
|
24
|
+
* // Register services
|
|
25
|
+
* await discovery.register([
|
|
26
|
+
* databaseService,
|
|
27
|
+
* cacheService,
|
|
28
|
+
* authService
|
|
29
|
+
* ]);
|
|
30
|
+
*
|
|
31
|
+
* // Retrieve services
|
|
32
|
+
* const db = await discovery.get('database');
|
|
33
|
+
* const { cache, auth } = await discovery.getMany(['cache', 'auth']);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {
|
|
37
|
+
readonly envParser: EnvironmentParser<{}>;
|
|
38
|
+
/** Singleton instance of ServiceDiscovery */
|
|
39
|
+
private static _instance;
|
|
40
|
+
/** Map of registered service definitions */
|
|
41
|
+
private services;
|
|
42
|
+
/** Map of instantiated service instances */
|
|
43
|
+
private instances;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the singleton instance of ServiceDiscovery.
|
|
46
|
+
* Creates a new instance if one doesn't exist.
|
|
47
|
+
*
|
|
48
|
+
* @template T - Record type mapping service names to their instance types
|
|
49
|
+
* @param envParser - Environment parser for service configuration
|
|
50
|
+
* @returns The ServiceDiscovery singleton instance
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const services = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
static getInstance<T extends Record<any, unknown> = any>(envParser: EnvironmentParser<{}>): ServiceDiscovery<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Resets the singleton instance. Use only for testing purposes.
|
|
60
|
+
* This clears all cached services and allows a fresh instance to be created.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // In test teardown
|
|
65
|
+
* afterEach(() => {
|
|
66
|
+
* ServiceDiscovery.reset();
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
static reset(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Private constructor to enforce singleton pattern.
|
|
73
|
+
*
|
|
74
|
+
* @param envParser - Environment parser for service configuration
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
private constructor();
|
|
78
|
+
/**
|
|
79
|
+
* Register multiple services with the service discovery.
|
|
80
|
+
* Services are instantiated lazily on first access.
|
|
81
|
+
* Already instantiated services are returned from cache.
|
|
82
|
+
*
|
|
83
|
+
* @template T - Array type of services to register
|
|
84
|
+
* @param services - Array of services to register
|
|
85
|
+
* @returns Promise resolving to a record of service names to instances
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const services = await discovery.register([
|
|
90
|
+
* databaseService,
|
|
91
|
+
* cacheService,
|
|
92
|
+
* authService
|
|
93
|
+
* ]);
|
|
94
|
+
*
|
|
95
|
+
* // services = {
|
|
96
|
+
* // database: Database instance,
|
|
97
|
+
* // cache: CacheService instance,
|
|
98
|
+
* // auth: AuthService instance
|
|
99
|
+
* // }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
register<T extends Service[]>(services: T): Promise<ServiceRecord<T>>;
|
|
103
|
+
/**
|
|
104
|
+
* Get a service from the service discovery.
|
|
105
|
+
* Services are instantiated on first access if not already cached.
|
|
106
|
+
*
|
|
107
|
+
* @template K - The service name key
|
|
108
|
+
* @param name - The name of the service to get
|
|
109
|
+
* @returns Promise resolving to the service instance
|
|
110
|
+
* @throws {Error} If the service is not registered
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const database = await discovery.get('database');
|
|
115
|
+
* const users = await database.query('SELECT * FROM users');
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
get<K extends keyof TServices & string>(name: K): Promise<TServices[K]>;
|
|
119
|
+
/**
|
|
120
|
+
* Get multiple services from the service discovery.
|
|
121
|
+
* Useful for retrieving multiple dependencies at once.
|
|
122
|
+
*
|
|
123
|
+
* @template K - Array of service name keys
|
|
124
|
+
* @param names - Array of service names to retrieve
|
|
125
|
+
* @returns Promise resolving to an object containing the service instances
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const { database, cache, auth } = await discovery.getMany([
|
|
130
|
+
* 'database',
|
|
131
|
+
* 'cache',
|
|
132
|
+
* 'auth'
|
|
133
|
+
* ]);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
getMany<K extends (keyof TServices & string)[]>(names: [...K]): Promise<{ [P in K[number]]: TServices[P] }>;
|
|
137
|
+
/**
|
|
138
|
+
* Check if a service exists in the service discovery.
|
|
139
|
+
* Can check by service name or service instance.
|
|
140
|
+
*
|
|
141
|
+
* @param service - The service name or service instance to check
|
|
142
|
+
* @returns True if the service exists, false otherwise
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* if (discovery.has('database')) {
|
|
147
|
+
* const db = await discovery.get('database');
|
|
148
|
+
* }
|
|
149
|
+
*
|
|
150
|
+
* // Or check with service instance
|
|
151
|
+
* if (!discovery.has(databaseService)) {
|
|
152
|
+
* await discovery.register([databaseService]);
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
has(service: string | Service): boolean;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Utility type to extract service names from an array of services.
|
|
160
|
+
*
|
|
161
|
+
* @template T - Array of Service types
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* type Names = ExtractServiceNames<[typeof databaseService, typeof cacheService]>;
|
|
166
|
+
* // type Names = 'database' | 'cache'
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
type ExtractServiceNames<T extends Service[]> = T[number]['serviceName'];
|
|
170
|
+
/**
|
|
171
|
+
* Utility type to create a record type from an array of services.
|
|
172
|
+
* Maps service names to their registered instance types.
|
|
173
|
+
*
|
|
174
|
+
* @template T - Array of Service types
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* type MyServiceRecord = ServiceRecord<[typeof databaseService, typeof cacheService]>;
|
|
179
|
+
* // type MyServiceRecord = {
|
|
180
|
+
* // database: DatabaseInstance;
|
|
181
|
+
* // cache: CacheInstance;
|
|
182
|
+
* // }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
type ServiceRecord<T extends Service[]> = { [K in T[number] as K['serviceName']]: K extends Service ? Awaited<ReturnType<K['register']>> : never };
|
|
186
|
+
//# sourceMappingURL=ServiceDiscovery.d.ts.map
|
|
187
|
+
//#endregion
|
|
188
|
+
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
|
189
|
+
//# sourceMappingURL=ServiceDiscovery-CfVBrQHt.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-CfVBrQHt.d.cts","names":[],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;AAkL8B;AAoB9B;;AAA0C,cAtM7B,gBAsM6B,CAAA,kBAtMM,MAsMN,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA;EAAO,SAAM,SAAA,EAlJd,iBAkJc,CAAA,CAAA,CAAA,CAAA;EAAC;EAiB5C,eAAA,SAAa;EAAA;EAAA,QAAW,QAAA;EAAO;EACnC,QAAY,SAAA;EAAC;;;;;AACV;;;;;;;;+BApMmB,uCACjB,wBACT,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuDK,qBAAqB,IAAI,QAAQ,cAAc;;;;;;;;;;;;;;;;sBAwCpD,0BAA0B,IAAI,QAAQ,UAAU;;;;;;;;;;;;;;;;;;2BA6BrC,kCACnB,KACT,gBAAgB,YAAY,UAAU;;;;;;;;;;;;;;;;;;;;wBA6BnB;;;;;;;;;;;;;KAoBX,8BAA8B,aAAa;;;;;;;;;;;;;;;;KAiB3C,wBAAwB,qBAC7B,aAAa,mBAAmB,UAAU,UAC7C,QAAQ,WAAW"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { serviceContext } from "./context-B5YTspJR.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/ServiceDiscovery.ts
|
|
4
|
+
/**
|
|
5
|
+
* Service discovery container that manages service registration and retrieval.
|
|
6
|
+
* Implements a singleton pattern with lazy initialization of services.
|
|
7
|
+
*
|
|
8
|
+
* @template TServices - Record type mapping service names to their instance types
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Define service types
|
|
13
|
+
* interface MyServices {
|
|
14
|
+
* database: Database;
|
|
15
|
+
* cache: CacheService;
|
|
16
|
+
* auth: AuthService;
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // Get service discovery instance
|
|
20
|
+
* const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
21
|
+
*
|
|
22
|
+
* // Register services
|
|
23
|
+
* await discovery.register([
|
|
24
|
+
* databaseService,
|
|
25
|
+
* cacheService,
|
|
26
|
+
* authService
|
|
27
|
+
* ]);
|
|
28
|
+
*
|
|
29
|
+
* // Retrieve services
|
|
30
|
+
* const db = await discovery.get('database');
|
|
31
|
+
* const { cache, auth } = await discovery.getMany(['cache', 'auth']);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var ServiceDiscovery = class ServiceDiscovery {
|
|
35
|
+
/** Singleton instance of ServiceDiscovery */
|
|
36
|
+
static _instance;
|
|
37
|
+
/** Map of registered service definitions */
|
|
38
|
+
services = /* @__PURE__ */ new Map();
|
|
39
|
+
/** Map of instantiated service instances */
|
|
40
|
+
instances = /* @__PURE__ */ new Map();
|
|
41
|
+
/**
|
|
42
|
+
* Gets the singleton instance of ServiceDiscovery.
|
|
43
|
+
* Creates a new instance if one doesn't exist.
|
|
44
|
+
*
|
|
45
|
+
* @template T - Record type mapping service names to their instance types
|
|
46
|
+
* @param envParser - Environment parser for service configuration
|
|
47
|
+
* @returns The ServiceDiscovery singleton instance
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const services = ServiceDiscovery.getInstance<MyServices>(envParser);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
static getInstance(envParser) {
|
|
55
|
+
if (!ServiceDiscovery._instance) ServiceDiscovery._instance = new ServiceDiscovery(envParser);
|
|
56
|
+
return ServiceDiscovery._instance;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resets the singleton instance. Use only for testing purposes.
|
|
60
|
+
* This clears all cached services and allows a fresh instance to be created.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* // In test teardown
|
|
65
|
+
* afterEach(() => {
|
|
66
|
+
* ServiceDiscovery.reset();
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
static reset() {
|
|
71
|
+
ServiceDiscovery._instance = void 0;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Private constructor to enforce singleton pattern.
|
|
75
|
+
*
|
|
76
|
+
* @param envParser - Environment parser for service configuration
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
constructor(envParser) {
|
|
80
|
+
this.envParser = envParser;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Register multiple services with the service discovery.
|
|
84
|
+
* Services are instantiated lazily on first access.
|
|
85
|
+
* Already instantiated services are returned from cache.
|
|
86
|
+
*
|
|
87
|
+
* @template T - Array type of services to register
|
|
88
|
+
* @param services - Array of services to register
|
|
89
|
+
* @returns Promise resolving to a record of service names to instances
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const services = await discovery.register([
|
|
94
|
+
* databaseService,
|
|
95
|
+
* cacheService,
|
|
96
|
+
* authService
|
|
97
|
+
* ]);
|
|
98
|
+
*
|
|
99
|
+
* // services = {
|
|
100
|
+
* // database: Database instance,
|
|
101
|
+
* // cache: CacheService instance,
|
|
102
|
+
* // auth: AuthService instance
|
|
103
|
+
* // }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
async register(services) {
|
|
107
|
+
const registeredServices = {};
|
|
108
|
+
for (const service of services) {
|
|
109
|
+
const name = service.serviceName;
|
|
110
|
+
if (this.instances.has(name)) {
|
|
111
|
+
registeredServices[name] = this.instances.get(name);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const instance = await service.register({
|
|
115
|
+
envParser: this.envParser,
|
|
116
|
+
context: serviceContext
|
|
117
|
+
});
|
|
118
|
+
this.instances.set(name, instance);
|
|
119
|
+
registeredServices[name] = instance;
|
|
120
|
+
}
|
|
121
|
+
return registeredServices;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get a service from the service discovery.
|
|
125
|
+
* Services are instantiated on first access if not already cached.
|
|
126
|
+
*
|
|
127
|
+
* @template K - The service name key
|
|
128
|
+
* @param name - The name of the service to get
|
|
129
|
+
* @returns Promise resolving to the service instance
|
|
130
|
+
* @throws {Error} If the service is not registered
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const database = await discovery.get('database');
|
|
135
|
+
* const users = await database.query('SELECT * FROM users');
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
get(name) {
|
|
139
|
+
const service = this.services.get(name);
|
|
140
|
+
if (!service) throw new Error(`Service '${name}' not found in service discovery`);
|
|
141
|
+
return service.register({
|
|
142
|
+
envParser: this.envParser,
|
|
143
|
+
context: serviceContext
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get multiple services from the service discovery.
|
|
148
|
+
* Useful for retrieving multiple dependencies at once.
|
|
149
|
+
*
|
|
150
|
+
* @template K - Array of service name keys
|
|
151
|
+
* @param names - Array of service names to retrieve
|
|
152
|
+
* @returns Promise resolving to an object containing the service instances
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const { database, cache, auth } = await discovery.getMany([
|
|
157
|
+
* 'database',
|
|
158
|
+
* 'cache',
|
|
159
|
+
* 'auth'
|
|
160
|
+
* ]);
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
async getMany(names) {
|
|
164
|
+
const result = {};
|
|
165
|
+
for (const name of names) result[name] = await this.get(name);
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Check if a service exists in the service discovery.
|
|
170
|
+
* Can check by service name or service instance.
|
|
171
|
+
*
|
|
172
|
+
* @param service - The service name or service instance to check
|
|
173
|
+
* @returns True if the service exists, false otherwise
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* if (discovery.has('database')) {
|
|
178
|
+
* const db = await discovery.get('database');
|
|
179
|
+
* }
|
|
180
|
+
*
|
|
181
|
+
* // Or check with service instance
|
|
182
|
+
* if (!discovery.has(databaseService)) {
|
|
183
|
+
* await discovery.register([databaseService]);
|
|
184
|
+
* }
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
has(service) {
|
|
188
|
+
if (typeof service === "string") return this.services.has(service);
|
|
189
|
+
return this.services.has(service.serviceName);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
export { ServiceDiscovery };
|
|
195
|
+
//# sourceMappingURL=ServiceDiscovery-DLMyH_yM.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-DLMyH_yM.mjs","names":["envParser: EnvironmentParser<{}>","services: T","name: K","names: [...K]","service: string | Service"],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":["import type { EnvironmentParser } from '@geekmidas/envkit';\nimport { serviceContext } from './context';\nimport type { Service } from './types';\n\n/**\n * Service discovery container that manages service registration and retrieval.\n * Implements a singleton pattern with lazy initialization of services.\n *\n * @template TServices - Record type mapping service names to their instance types\n *\n * @example\n * ```typescript\n * // Define service types\n * interface MyServices {\n * database: Database;\n * cache: CacheService;\n * auth: AuthService;\n * }\n *\n * // Get service discovery instance\n * const discovery = ServiceDiscovery.getInstance<MyServices>(envParser);\n *\n * // Register services\n * await discovery.register([\n * databaseService,\n * cacheService,\n * authService\n * ]);\n *\n * // Retrieve services\n * const db = await discovery.get('database');\n * const { cache, auth } = await discovery.getMany(['cache', 'auth']);\n * ```\n */\nexport class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {\n\t/** Singleton instance of ServiceDiscovery */\n\tprivate static _instance: ServiceDiscovery<any>;\n\t/** Map of registered service definitions */\n\tprivate services = new Map<string, Service>();\n\t/** Map of instantiated service instances */\n\tprivate instances = new Map<keyof TServices, TServices[keyof TServices]>();\n\n\t/**\n\t * Gets the singleton instance of ServiceDiscovery.\n\t * Creates a new instance if one doesn't exist.\n\t *\n\t * @template T - Record type mapping service names to their instance types\n\t * @param envParser - Environment parser for service configuration\n\t * @returns The ServiceDiscovery singleton instance\n\t *\n\t * @example\n\t * ```typescript\n\t * const services = ServiceDiscovery.getInstance<MyServices>(envParser);\n\t * ```\n\t */\n\tstatic getInstance<T extends Record<any, unknown> = any>(\n\t\tenvParser: EnvironmentParser<{}>,\n\t): ServiceDiscovery<T> {\n\t\tif (!ServiceDiscovery._instance) {\n\t\t\tServiceDiscovery._instance = new ServiceDiscovery<T>(envParser);\n\t\t}\n\t\treturn ServiceDiscovery._instance as ServiceDiscovery<T>;\n\t}\n\n\t/**\n\t * Resets the singleton instance. Use only for testing purposes.\n\t * This clears all cached services and allows a fresh instance to be created.\n\t *\n\t * @example\n\t * ```typescript\n\t * // In test teardown\n\t * afterEach(() => {\n\t * ServiceDiscovery.reset();\n\t * });\n\t * ```\n\t */\n\tstatic reset(): void {\n\t\tServiceDiscovery._instance = undefined as any;\n\t}\n\n\t/**\n\t * Private constructor to enforce singleton pattern.\n\t *\n\t * @param envParser - Environment parser for service configuration\n\t * @private\n\t */\n\tprivate constructor(readonly envParser: EnvironmentParser<{}>) {}\n\n\t/**\n\t * Register multiple services with the service discovery.\n\t * Services are instantiated lazily on first access.\n\t * Already instantiated services are returned from cache.\n\t *\n\t * @template T - Array type of services to register\n\t * @param services - Array of services to register\n\t * @returns Promise resolving to a record of service names to instances\n\t *\n\t * @example\n\t * ```typescript\n\t * const services = await discovery.register([\n\t * databaseService,\n\t * cacheService,\n\t * authService\n\t * ]);\n\t *\n\t * // services = {\n\t * // database: Database instance,\n\t * // cache: CacheService instance,\n\t * // auth: AuthService instance\n\t * // }\n\t * ```\n\t */\n\tasync register<T extends Service[]>(services: T): Promise<ServiceRecord<T>> {\n\t\tconst registeredServices = {} as ServiceRecord<T>;\n\t\tfor (const service of services) {\n\t\t\tconst name = service.serviceName as T[number]['serviceName'];\n\t\t\tif (this.instances.has(name)) {\n\t\t\t\t(registeredServices as any)[name] = this.instances.get(\n\t\t\t\t\tname,\n\t\t\t\t) as TServices[keyof TServices];\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Pass both envParser and context to service\n\t\t\tconst instance = await service.register({\n\t\t\t\tenvParser: this.envParser,\n\t\t\t\tcontext: serviceContext,\n\t\t\t});\n\n\t\t\tthis.instances.set(name, instance as TServices[keyof TServices]);\n\t\t\t(registeredServices as any)[name] =\n\t\t\t\tinstance as TServices[keyof TServices];\n\t\t}\n\n\t\treturn registeredServices;\n\t}\n\n\t/**\n\t * Get a service from the service discovery.\n\t * Services are instantiated on first access if not already cached.\n\t *\n\t * @template K - The service name key\n\t * @param name - The name of the service to get\n\t * @returns Promise resolving to the service instance\n\t * @throws {Error} If the service is not registered\n\t *\n\t * @example\n\t * ```typescript\n\t * const database = await discovery.get('database');\n\t * const users = await database.query('SELECT * FROM users');\n\t * ```\n\t */\n\tget<K extends keyof TServices & string>(name: K): Promise<TServices[K]> {\n\t\tconst service = this.services.get(name);\n\n\t\tif (!service) {\n\t\t\tthrow new Error(`Service '${name}' not found in service discovery`);\n\t\t}\n\n\t\treturn service.register({\n\t\t\tenvParser: this.envParser,\n\t\t\tcontext: serviceContext,\n\t\t}) as Promise<TServices[K]>;\n\t}\n\t/**\n\t * Get multiple services from the service discovery.\n\t * Useful for retrieving multiple dependencies at once.\n\t *\n\t * @template K - Array of service name keys\n\t * @param names - Array of service names to retrieve\n\t * @returns Promise resolving to an object containing the service instances\n\t *\n\t * @example\n\t * ```typescript\n\t * const { database, cache, auth } = await discovery.getMany([\n\t * 'database',\n\t * 'cache',\n\t * 'auth'\n\t * ]);\n\t * ```\n\t */\n\tasync getMany<K extends (keyof TServices & string)[]>(\n\t\tnames: [...K],\n\t): Promise<{ [P in K[number]]: TServices[P] }> {\n\t\tconst result = {} as { [P in K[number]]: TServices[P] };\n\n\t\tfor (const name of names) {\n\t\t\tresult[name] = await this.get(name);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Check if a service exists in the service discovery.\n\t * Can check by service name or service instance.\n\t *\n\t * @param service - The service name or service instance to check\n\t * @returns True if the service exists, false otherwise\n\t *\n\t * @example\n\t * ```typescript\n\t * if (discovery.has('database')) {\n\t * const db = await discovery.get('database');\n\t * }\n\t *\n\t * // Or check with service instance\n\t * if (!discovery.has(databaseService)) {\n\t * await discovery.register([databaseService]);\n\t * }\n\t * ```\n\t */\n\thas(service: string | Service): boolean {\n\t\tif (typeof service === 'string') {\n\t\t\treturn this.services.has(service);\n\t\t}\n\n\t\treturn this.services.has(service.serviceName);\n\t}\n}\n\n/**\n * Utility type to extract service names from an array of services.\n *\n * @template T - Array of Service types\n *\n * @example\n * ```typescript\n * type Names = ExtractServiceNames<[typeof databaseService, typeof cacheService]>;\n * // type Names = 'database' | 'cache'\n * ```\n */\nexport type ExtractServiceNames<T extends Service[]> = T[number]['serviceName'];\n\n/**\n * Utility type to create a record type from an array of services.\n * Maps service names to their registered instance types.\n *\n * @template T - Array of Service types\n *\n * @example\n * ```typescript\n * type MyServiceRecord = ServiceRecord<[typeof databaseService, typeof cacheService]>;\n * // type MyServiceRecord = {\n * // database: DatabaseInstance;\n * // cache: CacheInstance;\n * // }\n * ```\n */\nexport type ServiceRecord<T extends Service[]> = {\n\t[K in T[number] as K['serviceName']]: K extends Service\n\t\t? Awaited<ReturnType<K['register']>>\n\t\t: never;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,mBAAb,MAAa,iBAAiE;;CAE7E,OAAe;;CAEf,AAAQ,2BAAW,IAAI;;CAEvB,AAAQ,4BAAY,IAAI;;;;;;;;;;;;;;CAexB,OAAO,YACNA,WACsB;AACtB,OAAK,iBAAiB,UACrB,kBAAiB,YAAY,IAAI,iBAAoB;AAEtD,SAAO,iBAAiB;CACxB;;;;;;;;;;;;;CAcD,OAAO,QAAc;AACpB,mBAAiB;CACjB;;;;;;;CAQD,AAAQ,YAAqBA,WAAkC;EAAlC;CAAoC;;;;;;;;;;;;;;;;;;;;;;;;;CA0BjE,MAAM,SAA8BC,UAAwC;EAC3E,MAAM,qBAAqB,CAAE;AAC7B,OAAK,MAAM,WAAW,UAAU;GAC/B,MAAM,OAAO,QAAQ;AACrB,OAAI,KAAK,UAAU,IAAI,KAAK,EAAE;AAC7B,IAAC,mBAA2B,QAAQ,KAAK,UAAU,IAClD,KACA;AACD;GACA;GAGD,MAAM,WAAW,MAAM,QAAQ,SAAS;IACvC,WAAW,KAAK;IAChB,SAAS;GACT,EAAC;AAEF,QAAK,UAAU,IAAI,MAAM,SAAuC;AAChE,GAAC,mBAA2B,QAC3B;EACD;AAED,SAAO;CACP;;;;;;;;;;;;;;;;CAiBD,IAAwCC,MAAgC;EACvE,MAAM,UAAU,KAAK,SAAS,IAAI,KAAK;AAEvC,OAAK,QACJ,OAAM,IAAI,OAAO,WAAW,KAAK;AAGlC,SAAO,QAAQ,SAAS;GACvB,WAAW,KAAK;GAChB,SAAS;EACT,EAAC;CACF;;;;;;;;;;;;;;;;;;CAkBD,MAAM,QACLC,OAC8C;EAC9C,MAAM,SAAS,CAAE;AAEjB,OAAK,MAAM,QAAQ,MAClB,QAAO,QAAQ,MAAM,KAAK,IAAI,KAAK;AAGpC,SAAO;CACP;;;;;;;;;;;;;;;;;;;;CAqBD,IAAIC,SAAoC;AACvC,aAAW,YAAY,SACtB,QAAO,KAAK,SAAS,IAAI,QAAQ;AAGlC,SAAO,KAAK,SAAS,IAAI,QAAQ,YAAY;CAC7C;AACD"}
|