Documents injecute 1.x · Migrating from 0.x
Skip to content

Type Alias: Middleware ​

Middleware = (key, next, context) => unknown

Defined in: src/types.ts:310

Wraps resolution. Call next() to continue (or next(otherKey) to resolve another key) and return the value. Returning without calling next replaces the resolution.

Parameters ​

key ​

ServiceKey

next ​

(key?) => unknown

context ​

MiddlewareContext

Returns ​

unknown

Example ​

ts
const timing: Middleware = (key, next, { depth }) => {
  const start = performance.now();
  const value = next();
  console.debug(`${'  '.repeat(depth)}${String(key)} ${performance.now() - start}ms`);
  return value;
};
container.use(timing);

Released under the MIT License.