@geekmidas/services 1.0.0 → 1.0.2
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 +16 -0
- package/dist/{ServiceDiscovery-DLMyH_yM.mjs → ServiceDiscovery-BQ45ZFgI.mjs} +1 -3
- package/dist/ServiceDiscovery-BQ45ZFgI.mjs.map +1 -0
- package/dist/{ServiceDiscovery-CfVBrQHt.d.cts → ServiceDiscovery-Dvqa-Q1_.d.cts} +2 -4
- package/dist/ServiceDiscovery-Dvqa-Q1_.d.cts.map +1 -0
- package/dist/{ServiceDiscovery-BO2FzEGt.cjs → ServiceDiscovery-YeM2FZsl.cjs} +1 -3
- package/dist/ServiceDiscovery-YeM2FZsl.cjs.map +1 -0
- package/dist/{ServiceDiscovery-DpeEKNe2.d.mts → ServiceDiscovery-ykolgkIj.d.mts} +2 -4
- package/dist/ServiceDiscovery-ykolgkIj.d.mts.map +1 -0
- package/dist/ServiceDiscovery.cjs +1 -1
- package/dist/ServiceDiscovery.d.cts +1 -1
- package/dist/ServiceDiscovery.d.mts +1 -1
- package/dist/ServiceDiscovery.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +21 -11
- package/src/ServiceDiscovery.ts +1 -3
- package/dist/ServiceDiscovery-BO2FzEGt.cjs.map +0 -1
- package/dist/ServiceDiscovery-CfVBrQHt.d.cts.map +0 -1
- package/dist/ServiceDiscovery-DLMyH_yM.mjs.map +0 -1
- package/dist/ServiceDiscovery-DpeEKNe2.d.mts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @geekmidas/services
|
|
2
2
|
|
|
3
|
+
## 1.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 🐛 [`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix `package.json` exports so TypeScript declarations resolve correctly under NodeNext/Bundler module resolution. Each subpath export now nests `types` inside its `import`/`require` condition, pointing at the `.d.mts` and `.d.cts` files that `tsdown` actually emits (previously the exports referenced non-existent `.d.ts` files, causing type-resolution failures for consumers). Both ESM (`.mjs`) and CJS (`.cjs`) runtime entry points are preserved. Additionally, `@geekmidas/ui` had `import` paths pointing at `.js` files that were never emitted — those are corrected to `.mjs`.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651)]:
|
|
10
|
+
- @geekmidas/envkit@1.0.7
|
|
11
|
+
- @geekmidas/logger@1.0.2
|
|
12
|
+
|
|
13
|
+
## 1.0.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 🔥 [`4bed570`](https://github.com/geekmidas/toolbox/commit/4bed57049db24417ef81279bc88fa0e1255f7b9a) Thanks [@geekmidas](https://github.com/geekmidas)! - Remove singleton enforcement so people can use it how they see fit
|
|
18
|
+
|
|
3
19
|
## 1.0.0
|
|
4
20
|
|
|
5
21
|
### Major Changes
|
|
@@ -71,10 +71,8 @@ var ServiceDiscovery = class ServiceDiscovery {
|
|
|
71
71
|
ServiceDiscovery._instance = void 0;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
-
* Private constructor to enforce singleton pattern.
|
|
75
74
|
*
|
|
76
75
|
* @param envParser - Environment parser for service configuration
|
|
77
|
-
* @private
|
|
78
76
|
*/
|
|
79
77
|
constructor(envParser) {
|
|
80
78
|
this.envParser = envParser;
|
|
@@ -192,4 +190,4 @@ var ServiceDiscovery = class ServiceDiscovery {
|
|
|
192
190
|
|
|
193
191
|
//#endregion
|
|
194
192
|
export { ServiceDiscovery };
|
|
195
|
-
//# sourceMappingURL=ServiceDiscovery-
|
|
193
|
+
//# sourceMappingURL=ServiceDiscovery-BQ45ZFgI.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-BQ45ZFgI.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 *\n\t * @param envParser - Environment parser for service configuration\n\t */\n\tconstructor(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;;;;;CAMD,YAAqBA,WAAkC;EAAlC;CAAoC;;;;;;;;;;;;;;;;;;;;;;;;;CA0BzD,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"}
|
|
@@ -69,12 +69,10 @@ declare class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {
|
|
|
69
69
|
*/
|
|
70
70
|
static reset(): void;
|
|
71
71
|
/**
|
|
72
|
-
* Private constructor to enforce singleton pattern.
|
|
73
72
|
*
|
|
74
73
|
* @param envParser - Environment parser for service configuration
|
|
75
|
-
* @private
|
|
76
74
|
*/
|
|
77
|
-
|
|
75
|
+
constructor(envParser: EnvironmentParser<{}>);
|
|
78
76
|
/**
|
|
79
77
|
* Register multiple services with the service discovery.
|
|
80
78
|
* Services are instantiated lazily on first access.
|
|
@@ -186,4 +184,4 @@ type ServiceRecord<T extends Service[]> = { [K in T[number] as K['serviceName']]
|
|
|
186
184
|
//# sourceMappingURL=ServiceDiscovery.d.ts.map
|
|
187
185
|
//#endregion
|
|
188
186
|
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
|
189
|
-
//# sourceMappingURL=ServiceDiscovery-
|
|
187
|
+
//# sourceMappingURL=ServiceDiscovery-Dvqa-Q1_.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-Dvqa-Q1_.d.cts","names":[],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;;AAgL8B;AAoB9B;AAA+B,cApMlB,gBAoMkB,CAAA,kBApMiB,MAoMjB,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA;EAAA,SAAW,SAAA,EAlJT,iBAkJS,CAAA,CAAA,CAAA,CAAA;EAAO;EAAO,eAAA,SAAA;EAiB5C;EAAa,QAAA,QAAA;EAAA;EAAkB,QACpC,SAAA;EAAC;;;;;;AACG;;;;;;;+BAlMmB,uCACjB,wBACT,iBAAiB;;;;;;;;;;;;;;;;;;yBA2BY;;;;;;;;;;;;;;;;;;;;;;;;;qBA0BP,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"}
|
|
@@ -71,10 +71,8 @@ var ServiceDiscovery = class ServiceDiscovery {
|
|
|
71
71
|
ServiceDiscovery._instance = void 0;
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
-
* Private constructor to enforce singleton pattern.
|
|
75
74
|
*
|
|
76
75
|
* @param envParser - Environment parser for service configuration
|
|
77
|
-
* @private
|
|
78
76
|
*/
|
|
79
77
|
constructor(envParser) {
|
|
80
78
|
this.envParser = envParser;
|
|
@@ -197,4 +195,4 @@ Object.defineProperty(exports, 'ServiceDiscovery', {
|
|
|
197
195
|
return ServiceDiscovery;
|
|
198
196
|
}
|
|
199
197
|
});
|
|
200
|
-
//# sourceMappingURL=ServiceDiscovery-
|
|
198
|
+
//# sourceMappingURL=ServiceDiscovery-YeM2FZsl.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-YeM2FZsl.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 *\n\t * @param envParser - Environment parser for service configuration\n\t */\n\tconstructor(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;;;;;CAMD,YAAqBA,WAAkC;EAAlC;CAAoC;;;;;;;;;;;;;;;;;;;;;;;;;CA0BzD,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"}
|
|
@@ -69,12 +69,10 @@ declare class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {
|
|
|
69
69
|
*/
|
|
70
70
|
static reset(): void;
|
|
71
71
|
/**
|
|
72
|
-
* Private constructor to enforce singleton pattern.
|
|
73
72
|
*
|
|
74
73
|
* @param envParser - Environment parser for service configuration
|
|
75
|
-
* @private
|
|
76
74
|
*/
|
|
77
|
-
|
|
75
|
+
constructor(envParser: EnvironmentParser<{}>);
|
|
78
76
|
/**
|
|
79
77
|
* Register multiple services with the service discovery.
|
|
80
78
|
* Services are instantiated lazily on first access.
|
|
@@ -186,4 +184,4 @@ type ServiceRecord<T extends Service[]> = { [K in T[number] as K['serviceName']]
|
|
|
186
184
|
//# sourceMappingURL=ServiceDiscovery.d.ts.map
|
|
187
185
|
//#endregion
|
|
188
186
|
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
|
189
|
-
//# sourceMappingURL=ServiceDiscovery-
|
|
187
|
+
//# sourceMappingURL=ServiceDiscovery-ykolgkIj.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServiceDiscovery-ykolgkIj.d.mts","names":[],"sources":["../src/ServiceDiscovery.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;;AAgL8B;AAoB9B;AAA+B,cApMlB,gBAoMkB,CAAA,kBApMiB,MAoMjB,CAAA,MAAA,EAAA,OAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA;EAAA,SAAW,SAAA,EAlJT,iBAkJS,CAAA,CAAA,CAAA,CAAA;EAAO;EAAO,eAAA,SAAA;EAiB5C;EAAa,QAAA,QAAA;EAAA;EAAkB,QACpC,SAAA;EAAC;;;;;;AACG;;;;;;;+BAlMmB,uCACjB,wBACT,iBAAiB;;;;;;;;;;;;;;;;;;yBA2BY;;;;;;;;;;;;;;;;;;;;;;;;;qBA0BP,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"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./types-D7d_yeU5.cjs";
|
|
2
|
-
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-
|
|
2
|
+
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-Dvqa-Q1_.cjs";
|
|
3
3
|
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import "./types-CcHmCx_U.mjs";
|
|
2
|
-
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-
|
|
2
|
+
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-ykolgkIj.mjs";
|
|
3
3
|
export { ExtractServiceNames, ServiceDiscovery, ServiceRecord };
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_context = require('./context-DUTDtYd2.cjs');
|
|
2
|
-
const require_ServiceDiscovery = require('./ServiceDiscovery-
|
|
2
|
+
const require_ServiceDiscovery = require('./ServiceDiscovery-YeM2FZsl.cjs');
|
|
3
3
|
|
|
4
4
|
exports.ServiceDiscovery = require_ServiceDiscovery.ServiceDiscovery;
|
|
5
5
|
exports.runWithRequestContext = require_context.runWithRequestContext;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Service, ServiceContext, ServiceRegisterOptions } from "./types-D7d_yeU5.cjs";
|
|
2
|
-
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-
|
|
2
|
+
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-Dvqa-Q1_.cjs";
|
|
3
3
|
import { RequestContextData, runWithRequestContext, serviceContext } from "./context-BVeZOvOd.cjs";
|
|
4
4
|
export { ExtractServiceNames, RequestContextData, Service, ServiceContext, ServiceDiscovery, ServiceRecord, ServiceRegisterOptions, runWithRequestContext, serviceContext };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Service, ServiceContext, ServiceRegisterOptions } from "./types-CcHmCx_U.mjs";
|
|
2
|
-
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-
|
|
2
|
+
import { ExtractServiceNames, ServiceDiscovery, ServiceRecord } from "./ServiceDiscovery-ykolgkIj.mjs";
|
|
3
3
|
import { RequestContextData, runWithRequestContext, serviceContext } from "./context-DMExgNzl.mjs";
|
|
4
4
|
export { ExtractServiceNames, RequestContextData, Service, ServiceContext, ServiceDiscovery, ServiceRecord, ServiceRegisterOptions, runWithRequestContext, serviceContext };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { runWithRequestContext, serviceContext } from "./context-B5YTspJR.mjs";
|
|
2
|
-
import { ServiceDiscovery } from "./ServiceDiscovery-
|
|
2
|
+
import { ServiceDiscovery } from "./ServiceDiscovery-BQ45ZFgI.mjs";
|
|
3
3
|
|
|
4
4
|
export { ServiceDiscovery, runWithRequestContext, serviceContext };
|
package/package.json
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/services",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
"import": {
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"default": "./dist/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./dist/index.d.cts",
|
|
14
|
+
"default": "./dist/index.cjs"
|
|
15
|
+
}
|
|
11
16
|
},
|
|
12
17
|
"./context": {
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/context.d.mts",
|
|
20
|
+
"default": "./dist/context.mjs"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/context.d.cts",
|
|
24
|
+
"default": "./dist/context.cjs"
|
|
25
|
+
}
|
|
16
26
|
}
|
|
17
27
|
},
|
|
18
28
|
"repository": {
|
|
@@ -25,8 +35,8 @@
|
|
|
25
35
|
},
|
|
26
36
|
"dependencies": {},
|
|
27
37
|
"peerDependencies": {
|
|
28
|
-
"@geekmidas/envkit": "^1.0.
|
|
29
|
-
"@geekmidas/logger": "^1.0.
|
|
38
|
+
"@geekmidas/envkit": "^1.0.7",
|
|
39
|
+
"@geekmidas/logger": "^1.0.2"
|
|
30
40
|
},
|
|
31
41
|
"peerDependenciesMeta": {
|
|
32
42
|
"@geekmidas/envkit": {
|
|
@@ -37,8 +47,8 @@
|
|
|
37
47
|
}
|
|
38
48
|
},
|
|
39
49
|
"devDependencies": {
|
|
40
|
-
"@geekmidas/envkit": "^1.0.
|
|
41
|
-
"@geekmidas/logger": "^1.0.
|
|
50
|
+
"@geekmidas/envkit": "^1.0.7",
|
|
51
|
+
"@geekmidas/logger": "^1.0.2"
|
|
42
52
|
},
|
|
43
53
|
"scripts": {
|
|
44
54
|
"ts": "tsc --noEmit --skipLibCheck src/**/*.ts"
|
package/src/ServiceDiscovery.ts
CHANGED
|
@@ -79,12 +79,10 @@ export class ServiceDiscovery<TServices extends Record<string, unknown> = {}> {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
* Private constructor to enforce singleton pattern.
|
|
83
82
|
*
|
|
84
83
|
* @param envParser - Environment parser for service configuration
|
|
85
|
-
* @private
|
|
86
84
|
*/
|
|
87
|
-
|
|
85
|
+
constructor(readonly envParser: EnvironmentParser<{}>) {}
|
|
88
86
|
|
|
89
87
|
/**
|
|
90
88
|
* Register multiple services with the service discovery.
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceDiscovery-DpeEKNe2.d.mts","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"}
|