Type Alias MiddlewareFn<TParameters, TReturn>

Middleware implementation as a function.

A function-based middleware that receives MiddlewareArgs and returns the result of processing. This is the most common and flexible way to define middleware.

Type Parameters

  • TParameters extends unknown[] = unknown[]

    Type of arguments passed to the function

  • TReturn = unknown

    Type of value returned from the function

const cacheToken = contextToken<Map<string, unknown>>("cache");

const cacheMiddleware: MiddlewareFn = async ({ args, next, context }) => {
const cacheKey = JSON.stringify(args);
const cache = context.getOr(cacheToken, () => new Map());
if (cache.has(cacheKey)) return cache.get(cacheKey);

const result = await next(args);
cache.set(cacheKey, result);
context.put(cacheToken, cache);
return result;
};

IMPORT_PATH: @daiso-tech/core/middleware