Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware support #122

Merged
merged 4 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
18 changes: 14 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,14 @@ export function makeJsonRpcPayloadSender(
}
return sendJsonRpcWithProvider(currentWriteProvider, payload);
}
});

const sendJsonRpcPayload = (
payload: SingleOrBatchRequest,
): Promise<SingleOrBatchResponse> => {
let i = 0;
const next = () => middlewares[i++](payload, next);
return next();
};

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