UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

210 lines 5.17 kB
/** * MSW handler management system */ /** * Handler manager for organizing and managing MSW request handlers */ export class HandlerManager { handlers = new Map(); groups = new Map(); /** * Register a handler with optional ID and group */ register(handler, id, group) { const handlerId = id || this.generateHandlerId(handler); this.handlers.set(handlerId, handler); if (group) { if (!this.groups.has(group)) { this.groups.set(group, new Set()); } this.groups.get(group).add(handlerId); } } /** * Unregister a handler by ID or instance */ unregister(handlerOrId) { if (typeof handlerOrId === 'string') { this.handlers.delete(handlerOrId); // Remove from all groups for (const [, handlerIds] of this.groups) { handlerIds.delete(handlerOrId); } } else { // Find handler by instance for (const [id, handler] of this.handlers) { if (handler === handlerOrId) { this.unregister(id); break; } } } } /** * Get all handlers */ getHandlers() { return Array.from(this.handlers.values()); } /** * Get handlers by group */ getHandlersByGroup(group) { const handlerIds = this.groups.get(group); if (!handlerIds) { return []; } return Array.from(handlerIds) .map(id => this.handlers.get(id)) .filter((handler) => handler !== undefined); } /** * Get handler by ID */ getHandler(id) { return this.handlers.get(id); } /** * Check if handler exists */ hasHandler(handlerOrId) { if (typeof handlerOrId === 'string') { return this.handlers.has(handlerOrId); } return Array.from(this.handlers.values()).includes(handlerOrId); } /** * Clear all handlers */ clear() { this.handlers.clear(); this.groups.clear(); } /** * Clear handlers by group */ clearGroup(group) { const handlerIds = this.groups.get(group); if (handlerIds) { for (const id of handlerIds) { this.handlers.delete(id); } this.groups.delete(group); } } /** * Get all groups */ getGroups() { return Array.from(this.groups.keys()); } /** * Get handler count */ getHandlerCount() { return this.handlers.size; } /** * Get group handler count */ getGroupHandlerCount(group) { return this.groups.get(group)?.size || 0; } /** * Register multiple handlers */ registerMany(handlers, group) { for (const handler of handlers) { this.register(handler, undefined, group); } } /** * Create a scoped handler manager for a specific group */ createScope(group) { return new ScopedHandlerManager(this, group); } /** * Generate a unique handler ID */ generateHandlerId(_handler) { // Extract method and path from handler for ID generation const timestamp = Date.now(); const random = Math.random().toString(36).substring(2, 11); return `handler_${timestamp}_${random}`; } /** * Export handlers for MSW setup */ export() { return this.getHandlers(); } /** * Import handlers from another manager */ import(other, group) { const handlers = other.getHandlers(); this.registerMany(handlers, group); } /** * Clone the handler manager */ clone() { const cloned = new HandlerManager(); cloned.import(this); return cloned; } } /** * Scoped handler manager for working with specific groups */ export class ScopedHandlerManager { parent; group; constructor(parent, group) { this.parent = parent; this.group = group; } /** * Register handler in this scope */ register(handler, id) { this.parent.register(handler, id, this.group); } /** * Unregister handler from this scope */ unregister(handlerOrId) { this.parent.unregister(handlerOrId); } /** * Get handlers in this scope */ getHandlers() { return this.parent.getHandlersByGroup(this.group); } /** * Clear all handlers in this scope */ clear() { this.parent.clearGroup(this.group); } /** * Get handler count in this scope */ getHandlerCount() { return this.parent.getGroupHandlerCount(this.group); } /** * Register multiple handlers in this scope */ registerMany(handlers) { this.parent.registerMany(handlers, this.group); } } /** * Global handler manager instance */ export const globalHandlerManager = new HandlerManager(); //# sourceMappingURL=handler-manager.js.map