Skip to content

Commit

Permalink
Merge pull request #122 from emr/middleware-support
Browse files Browse the repository at this point in the history
Add middleware support
  • Loading branch information
dphilipson authored Apr 21, 2022
2 parents 51f0417 + 3ee0298 commit 75da446
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,18 @@ export function createAlchemyWeb3(

function fillInConfigDefaults({
writeProvider = getWindowProvider(),
jsonRpcSenderMiddlewares = [],
maxRetries = DEFAULT_MAX_RETRIES,
retryInterval = DEFAULT_RETRY_INTERVAL,
retryJitter = DEFAULT_RETRY_JITTER,
}: AlchemyWeb3Config = {}): FullConfig {
return { writeProvider, maxRetries, retryInterval, retryJitter };
return {
writeProvider,
jsonRpcSenderMiddlewares,
maxRetries,
retryInterval,
retryJitter,
};
}

function getWindowProvider(): Provider | null {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function isSubscriptionEvent(

export interface AlchemyWeb3Config {
writeProvider?: Provider | null;
jsonRpcSenderMiddlewares?: JsonRpcSenderMiddleware[];
maxRetries?: number;
retryInterval?: number;
retryJitter?: number;
Expand Down Expand Up @@ -86,3 +87,8 @@ export type SendJsonRpcFunction = (
export interface TransactionsOptions {
address?: string;
}

export type JsonRpcSenderMiddleware = (
req: SingleOrBatchRequest,
next: () => Promise<SingleOrBatchResponse>,
) => Promise<SingleOrBatchResponse>;
20 changes: 16 additions & 4 deletions src/web3-adapter/sendJsonRpcPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
FullConfig,
JsonRpcRequest,
JsonRpcResponse,
JsonRpcSenderMiddleware,
Provider,
SingleOrBatchRequest,
SingleOrBatchResponse,
Expand Down Expand Up @@ -34,11 +35,12 @@ export function makeJsonRpcPayloadSender(
alchemySendJsonRpc: AlchemySendJsonRpcFunction,
config: FullConfig,
): JsonRpcPayloadSender {
let currentWriteProvider = config.writeProvider;
// Copy middlewares from config.
const middlewares: JsonRpcSenderMiddleware[] = [];
config.jsonRpcSenderMiddlewares.forEach((m) => middlewares.push(m));

const sendJsonRpcPayload = (
payload: SingleOrBatchRequest,
): Promise<SingleOrBatchResponse> => {
let currentWriteProvider = config.writeProvider;
middlewares.push((payload) => {
const disallowedMethod = getDisallowedMethod(payload);
if (!disallowedMethod) {
try {
Expand All @@ -63,6 +65,16 @@ export function makeJsonRpcPayloadSender(
}
return sendJsonRpcWithProvider(currentWriteProvider, payload);
}
});

const sendJsonRpcPayload = (
payload: SingleOrBatchRequest,
): Promise<SingleOrBatchResponse> => {
const getNext = (i: number) => {
const middleware = middlewares[i];
return () => middleware(payload, getNext(i + 1));
};
return getNext(0)();
};

function setWriteProvider(writeProvider: Provider | null | undefined) {
Expand Down

0 comments on commit 75da446

Please sign in to comment.