Skip to main content

Interceptors & Middleware ๐Ÿ”ง

Note: Interceptors and middleware support is planned for future releases. This article provides a basic workaround until official support is added.

Interceptors allow you to add cross-cutting concerns to your dependency injection system. They let you transform and enhance your services as they're being resolved - perfect for when you need to add some middleware magic to your DI setup.

Basic Workaroundโ€‹

Until official interceptor support is added, you can achieve similar functionality using factory functions:

// Simple logging wrapper
function withLogging<T>(token: TokenType<T>, factory: () => T): T {
console.log(`Creating ${token.toString()}`);
const instance = factory();
console.log(`Created ${token.toString()}`);
return instance;
}

// Usage
container.set(USER_SERVICE, {
useFactory: () => withLogging(USER_SERVICE, () => new UserService()),
});

Advanced Workaroundsโ€‹

Caching Patternโ€‹

const cache = new Map<string, any>();

function withCaching<T>(token: TokenType<T>, factory: () => T): T {
const key = token.toString();

if (cache.has(key)) {
return cache.get(key);
}

const instance = factory();
cache.set(key, instance);
return instance;
}

// Usage
container.set(DATABASE, {
useFactory: () => withCaching(DATABASE, () => new ExpensiveDatabase()),
});

Validation Patternโ€‹

function withValidation<T>(token: TokenType<T>, factory: () => T): T {
const instance = factory();

if (!instance) {
throw new Error(`Service ${token.toString()} resolved to null/undefined`);
}

return instance;
}

// Usage
container.set(USER_SERVICE, {
useFactory: () => withValidation(USER_SERVICE, () => new UserService()),
});

Roadmapโ€‹

Official interceptor and middleware support is planned to include:

  • Decorator-based interceptors
  • Middleware chains
  • Performance monitoring
  • Error handling

See the Roadmap for more details on upcoming features.

Next Stepsโ€‹

These workarounds will keep you productive until the full interceptor system arrives! ๐Ÿ”งโœจ