removeDependency - greydragon888/real-router GitHub Wiki
getDependenciesApi().remove
1. Overview
What it does: Removes a dependency by name. When attempting to remove a non-existent dependency, logs a warning but does not throw (idempotent behavior).
import{createRouter}from"@real-router/core";import{getDependenciesApi}from"@real-router/core/api";interfaceAppDependencies{apiClient: ApiClient;tempService: TempService;cache: CacheService;}constrouter=createRouter<AppDependencies>(routes,{},{apiClient: newApiClient(),tempService: newTempService(),cache: newCacheService(),},);constdepsApi=getDependenciesApi(router);// Remove dependencydepsApi.remove("tempService");// Safe cleanup (without existence check)constcleanupDeps=["cache","tempService"]asconst;cleanupDeps.forEach((dep)=>depsApi.remove(dep));// After removaldepsApi.has("tempService");// falsedepsApi.get("tempService");// ReferenceError
3. Parameters
name (required)
Type: keyof Dependencies
Purpose: Name of dependency to remove
Constraint: Must be a string -- TypeError if not
Behavior for Different Values
Value
Behavior
Existing key
Removed via delete
Non-existing key
console.warn, no error
Non-string
TypeError thrown
4. Return Value
Type: void
5. Side Effects
Dependency removal: Dependency is removed from the router's dependency store
Warning: console.warn when attempting to remove non-existent dependency
Format: [router.removeDependency] Attempted to remove non-existent dependency: "{type}"
6. Possible Errors
Condition
Error Type
Message
Router disposed
RouterError
DISPOSED
name not string
TypeError
[router.removeDependency]: dependency name must be a string, got {type}
Non-existent dependency does not throw -- it logs a warning instead. This is intentional for idempotent cleanup.
Error Examples
constdepsApi=getDependenciesApi(router);// TypeError: not a string// @ts-expect-errordepsApi.remove(123);// TypeError: [router.removeDependency]: dependency name must be a string, got number// RouterError: disposed routerrouter.dispose();constdepsAfterDispose=getDependenciesApi(router);depsAfterDispose.remove("foo");// RouterError: DISPOSED
constdepsApi=getDependenciesApi(router);// remove -- removes one dependency at a timedepsApi.remove("service1");depsApi.remove("service2");// reset -- removes all at oncedepsApi.reset();
Idempotency vs has
constdepsApi=getDependenciesApi(router);// Option 1: Safe removal (idempotent)// Warning if doesn't exist, but no errordepsApi.remove("maybeNotExist");// Option 2: Check before removal (no warning)if(depsApi.has("maybeNotExist")){depsApi.remove("maybeNotExist");}
8. Behavior
Main Scenarios
Removes existing dependency: has() returns false after removal
Idempotent: Safe to call multiple times; subsequent calls log a warning
Disposed router: Throws RouterError(DISPOSED) on any mutating call
constdepsApi=getDependenciesApi(router);constwarnSpy=vi.spyOn(console,"warn");depsApi.remove("nonexistent");expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("[router.removeDependency]"),expect.stringContaining('Attempted to remove non-existent dependency: "string"',),);
Idempotent Removal
constdepsApi=getDependenciesApi(router);constwarnSpy=vi.spyOn(console,"warn");// First removal -- successdepsApi.remove("foo");expect(depsApi.has("foo")).toBe(false);// Second removal -- warning, but no errordepsApi.remove("foo");expect(warnSpy).toHaveBeenCalled();
constdepsApi=getDependenciesApi(router);constwarnSpy=vi.spyOn(console,"warn");// Cleanup without existence checkconstcleanupDeps=["dep1","dep2","dep3"];depsApi.set("dep1",1);// dep2 and dep3 don't exist// Doesn't throw errorexpect(()=>{cleanupDeps.forEach((dep)=>depsApi.remove(dep));}).not.toThrowError();// Warnings for non-existent (dep2 and dep3)expect(warnSpy).toHaveBeenCalledTimes(2);
Edge Cases
Repeated removal: Idempotent (warning, not error)
Integration with getAll: Removed keys do not appear in result
After removal: get() throws ReferenceError for the removed key
Guarantees
Returns void (no chaining)
Throws RouterError(DISPOSED) if router is disposed
Throws TypeError if name is not a string
Idempotent -- safe to call multiple times
Warning for non-existent (informative without breaking)
Uses Object.hasOwn() for prototype pollution protection
8. Migration from router5
Before (router5)
After (real-router)
router.removeDependency(name)
getDependenciesApi(router).remove(name)
// Before (router5)router.removeDependency("tempService");// After (real-router)import{getDependenciesApi}from"@real-router/core/api";constdepsApi=getDependenciesApi(router);depsApi.remove("tempService");