Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit e5efe49

Browse files
author
Dan Forbes
authored
feat/docs/plugin-middleware (#7158)
* Add Plugin Middleware Documentation Closes #6963 * Clarify Request Middleware Response Processing
1 parent 5f6deeb commit e5efe49

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

docs/docs/guides/web3_plugin_guide/plugin_authors.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,73 @@ public link(parentContext: Web3Context) {
233233
}
234234
```
235235

236+
## Plugin Middleware
237+
238+
Middleware allows plugins to intercept network interactions and inject custom logic. There are two types of plugin middleware: [request middleware](#request-middleware) and [transaction middleware](#transaction-middleware). In both cases, the middleware is implemented as a new class and registered with the plugin in the plugin's `link` method. Keep reading to learn how to add middleware to a plugin.
239+
240+
### Request Middleware
241+
242+
Request middleware allows plugins to modify RPC requests before they are sent to the network and modify RPC responses before they are returned to Web3.js for further internal processing. Request middleware must implement the [`RequestManagerMiddleware`](/api/web3-core/interface/RequestManagerMiddleware) interface, which specifies two functions: [`processRequest`](/api/web3-core/interface/RequestManagerMiddleware#processRequest) and [`processResponse`](/api/web3-core/interface/RequestManagerMiddleware#processResponse). Here is a simple example of request middleware that prints RPC requests and responses to the console:
243+
244+
```ts
245+
export class RequestMiddleware<API> implements RequestManagerMiddleware<API> {
246+
public async processRequest<ParamType = unknown[]>(
247+
request: JsonRpcPayload<ParamType>
248+
): Promise<JsonRpcPayload<ParamType>> {
249+
const reqObj = { ...request } as JsonRpcPayload;
250+
console.log("Request:", reqObj);
251+
return Promise.resolve(reqObj as JsonRpcPayload<ParamType>);
252+
}
253+
254+
public async processResponse<
255+
Method extends Web3APIMethod<API>,
256+
ResponseType = Web3APIReturnType<API, Method>
257+
>(
258+
response: JsonRpcResponse<ResponseType>
259+
): Promise<JsonRpcResponse<ResponseType>> {
260+
const resObj = { ...response };
261+
console.log("Response:", resObj);
262+
return Promise.resolve(resObj);
263+
}
264+
}
265+
```
266+
267+
To add request middleware to a plugin, use the [`Web3RequestManager.setMiddleware`](/api/web3-core/class/Web3RequestManager#setMiddleware) method in the plugin's `link` method as demonstrated below:
268+
269+
```ts
270+
public link(parentContext: Web3Context): void {
271+
parentContext.requestManager.setMiddleware(new RequestMiddleware());
272+
super.link(parentContext);
273+
}
274+
```
275+
276+
### Transaction Middleware
277+
278+
Transaction middleware allows plugins to modify transaction data before it is sent to the network. Transaction middleware must implement the [`TransactionMiddleware`](/api/web3-eth/interface/TransactionMiddleware) interface, which specifies one function: [`processTransaction`](/api/web3-eth/interface/TransactionMiddleware#processTransaction). Here is a simple example of transaction middleware that prints transaction data to the console:
279+
280+
```ts
281+
export class TxnMiddleware implements TransactionMiddleware {
282+
public async processTransaction(
283+
transaction: TransactionMiddlewareData
284+
): Promise<TransactionMiddlewareData> {
285+
const txObj = { ...transaction };
286+
console.log("Transaction data:", txObj);
287+
return Promise.resolve(txObj);
288+
}
289+
}
290+
```
291+
292+
To add transaction middleware to a plugin, use the [`Web3Eth.setTransactionMiddleware`](/api/web3-eth/class/Web3Eth#setTransactionMiddleware) method in the plugin's `link` method as demonstrated below:
293+
294+
```ts
295+
public link(parentContext: Web3Context): void {
296+
(parentContext as any).Web3Eth.setTransactionMiddleware(
297+
new TxnMiddleware()
298+
);
299+
super.link(parentContext);
300+
}
301+
```
302+
236303
## Setting Up Module Augmentation
237304

238305
In order to provide type safety and IntelliSense for your plugin when it's registered by the user, you must [augment](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) the `Web3Context` module. In simpler terms, you will be making TypeScript aware that you are modifying the interface of the class `Web3Context`, and any class that extends it, to include the interface of your plugin (i.e. your plugin's added methods, properties, etc.). As a result, your plugin object will be accessible within a namespace of your choice, which will be available within any `Web3Context` object.

0 commit comments

Comments
 (0)