37 lines
828 B
TypeScript
37 lines
828 B
TypeScript
|
|
const services: Record<string, any> = {};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Registra un objeto de servicio (API) bajo un nombre.
|
||
|
|
*/
|
||
|
|
export function registerService(name: string, api: any) {
|
||
|
|
if (services[name]) {
|
||
|
|
throw new Error(`❌ Servicio "${name}" ya fue registrado.`);
|
||
|
|
}
|
||
|
|
services[name] = api;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Recupera un servicio registrado, con tipado opcional.
|
||
|
|
*/
|
||
|
|
export function getService<T = any>(name: string): T {
|
||
|
|
const service = services[name];
|
||
|
|
if (!service) {
|
||
|
|
throw new Error(`❌ Servicio "${name}" no encontrado.`);
|
||
|
|
}
|
||
|
|
return service;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Permite saber si un servicio fue registrado.
|
||
|
|
*/
|
||
|
|
export function hasService(name: string): boolean {
|
||
|
|
return !!services[name];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Devuelve todos los servicios (para depuración o tests).
|
||
|
|
*/
|
||
|
|
export function listServices(): string[] {
|
||
|
|
return Object.keys(services);
|
||
|
|
}
|