Skip to content

Type Alias: ClientMiddleware()

ts
type ClientMiddleware = (action, args, next) => Promise<ToolResponse>;

Defined in: packages/core/src/client/FusionClient.ts:89

Client-side middleware for request/response interception.

Follows the same onion model as server-side middleware: each middleware wraps the next, forming a pipeline.

Use cases: authentication injection, request logging, retry logic, timeout enforcement, response transformation.

Parameters

ParameterType
actionstring
argsRecord<string, unknown>
next(action, args) => Promise<ToolResponse>

Returns

Promise<ToolResponse>

Example

typescript
const authMiddleware: ClientMiddleware = async (action, args, next) => {
    const enrichedArgs = { ...args, _token: getToken() };
    return next(action, enrichedArgs);
};

const retryMiddleware: ClientMiddleware = async (action, args, next) => {
    for (let i = 0; i < 3; i++) {
        const result = await next(action, args);
        if (!result.isError) return result;
    }
    return next(action, args);
};