Skip to content

Documentation / @agentick/kernel / Middleware

Type Alias: Middleware()<TArgs> ​

Middleware<TArgs> = (args, envelope, next) => Promise<any>

Defined in: kernel/src/procedure.ts:174

Middleware function that can intercept and transform procedure execution.

Middleware can:

  • Transform input arguments before passing to the next middleware/handler
  • Modify the result after next() returns
  • Short-circuit execution by not calling next()
  • Handle or transform errors

Type Parameters ​

TArgs ​

TArgs extends any[] = any[]

The argument types of the procedure

Parameters ​

args ​

TArgs

envelope ​

ProcedureEnvelope<TArgs>

next ​

(transformedArgs?) => Promise<any>

Returns ​

Promise<any>

Examples ​

typescript
const loggingMiddleware: Middleware<[string]> = async (args, envelope, next) => {
  console.log(`${envelope.operationName} called with:`, args);
  const start = Date.now();
  try {
    const result = await next();
    console.log(`Completed in ${Date.now() - start}ms`);
    return result;
  } catch (error) {
    console.error(`Failed:`, error);
    throw error;
  }
};
typescript
const upperMiddleware: Middleware<[string]> = async (args, envelope, next) => {
  return next([args[0].toUpperCase()]);
};

See ​

Released under the ISC License.