web3-core
Advanced tools
Comparing version 4.3.3-dev.fa4c72b.0 to 4.4.1-dev.fd2982d.0
@@ -1,2 +0,2 @@ | ||
import { HexString, Transaction } from 'web3-types'; | ||
import { HexString, JsonRpcPayload, JsonRpcResponse, Transaction, Web3APIMethod, Web3APIReturnType } from 'web3-types'; | ||
export declare type TransactionTypeParser = (transaction: Transaction) => HexString | undefined; | ||
@@ -11,1 +11,9 @@ export interface Method { | ||
} | ||
export interface RequestManagerMiddleware<API> { | ||
processRequest<ParamType = unknown[]>(request: JsonRpcPayload<ParamType>, options?: { | ||
[key: string]: unknown; | ||
}): Promise<JsonRpcPayload<ParamType>>; | ||
processResponse<AnotherMethod extends Web3APIMethod<API>, ResponseType = Web3APIReturnType<API, AnotherMethod>>(response: JsonRpcResponse<ResponseType>, options?: { | ||
[key: string]: unknown; | ||
}): Promise<JsonRpcResponse<ResponseType>>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Numbers, HexString, BlockNumberOrTag, Common } from 'web3-types'; | ||
import { Numbers, HexString, BlockNumberOrTag, Common, DataFormat } from 'web3-types'; | ||
import { TransactionTypeParser } from './types.js'; | ||
@@ -31,2 +31,3 @@ import { TransactionBuilder } from './web3_context.js'; | ||
transactionTypeParser?: TransactionTypeParser; | ||
defaultReturnFormat: DataFormat; | ||
} | ||
@@ -189,2 +190,4 @@ declare type ConfigEvent<T, P extends keyof T = keyof T> = P extends unknown ? { | ||
set maxListenersWarningThreshold(val: number); | ||
get defaultReturnFormat(): DataFormat; | ||
set defaultReturnFormat(val: DataFormat); | ||
get defaultNetworkId(): Numbers | undefined; | ||
@@ -191,0 +194,0 @@ set defaultNetworkId(val: Numbers | undefined); |
@@ -20,2 +20,3 @@ "use strict"; | ||
exports.Web3Config = exports.Web3ConfigEvent = void 0; | ||
const web3_types_1 = require("web3-types"); | ||
const web3_errors_1 = require("web3-errors"); | ||
@@ -58,2 +59,3 @@ const web3_utils_1 = require("web3-utils"); | ||
transactionTypeParser: undefined, | ||
defaultReturnFormat: web3_types_1.DEFAULT_RETURN_FORMAT, | ||
}; | ||
@@ -279,2 +281,9 @@ this.setConfig(options !== null && options !== void 0 ? options : {}); | ||
} | ||
get defaultReturnFormat() { | ||
return this.config.defaultReturnFormat; | ||
} | ||
set defaultReturnFormat(val) { | ||
this._triggerConfigChange('defaultReturnFormat', val); | ||
this.config.defaultReturnFormat = val; | ||
} | ||
get defaultNetworkId() { | ||
@@ -281,0 +290,0 @@ return this.config.defaultNetworkId; |
import { EthExecutionAPI, HexString, Numbers, SupportedProviders, Transaction, Web3AccountProvider, Web3APISpec, Web3BaseProvider, Web3BaseWallet, Web3BaseWalletAccount } from 'web3-types'; | ||
import { BaseTransaction } from 'web3-eth-accounts'; | ||
import { ExtensionObject } from './types.js'; | ||
import { ExtensionObject, RequestManagerMiddleware } from './types.js'; | ||
import { Web3BatchRequest } from './web3_batch_request.js'; | ||
@@ -31,2 +31,3 @@ import { Web3Config, Web3ConfigOptions } from './web3_config.js'; | ||
wallet?: Web3BaseWallet<Web3BaseWalletAccount>; | ||
requestManagerMiddleware?: RequestManagerMiddleware<API>; | ||
}; | ||
@@ -156,2 +157,3 @@ export declare type Web3ContextConstructor<T extends Web3Context, T2 extends unknown[]> = new (...args: [...extras: T2, context: Web3ContextObject]) => T; | ||
setProvider(provider?: SupportedProviders<API> | string): boolean; | ||
setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>): void; | ||
/** | ||
@@ -158,0 +160,0 @@ * Will return the {@link Web3BatchRequest} constructor. |
@@ -52,6 +52,6 @@ "use strict"; | ||
} | ||
const { config, provider, requestManager, subscriptionManager, registeredSubscriptions, accountProvider, wallet, } = providerOrContext; | ||
const { config, provider, requestManager, subscriptionManager, registeredSubscriptions, accountProvider, wallet, requestManagerMiddleware } = providerOrContext; | ||
this.setConfig(config !== null && config !== void 0 ? config : {}); | ||
this._requestManager = | ||
requestManager !== null && requestManager !== void 0 ? requestManager : new web3_request_manager_js_1.Web3RequestManager(provider, (_a = config === null || config === void 0 ? void 0 : config.enableExperimentalFeatures) === null || _a === void 0 ? void 0 : _a.useSubscriptionWhenCheckingBlockTimeout); | ||
requestManager !== null && requestManager !== void 0 ? requestManager : new web3_request_manager_js_1.Web3RequestManager(provider, (_a = config === null || config === void 0 ? void 0 : config.enableExperimentalFeatures) === null || _a === void 0 ? void 0 : _a.useSubscriptionWhenCheckingBlockTimeout, requestManagerMiddleware); | ||
if (subscriptionManager) { | ||
@@ -238,2 +238,5 @@ this._subscriptionManager = subscriptionManager; | ||
} | ||
setRequestManagerMiddleware(requestManagerMiddleware) { | ||
this.requestManager.setMiddleware(requestManagerMiddleware); | ||
} | ||
/** | ||
@@ -240,0 +243,0 @@ * Will return the {@link Web3BatchRequest} constructor. |
@@ -18,3 +18,3 @@ import { EventEmitter } from 'web3-utils'; | ||
listenerCount<K extends Web3EventKey<T>>(eventName: K): number; | ||
listeners<K extends Web3EventKey<T>>(eventName: K): Function[]; | ||
listeners<K extends Web3EventKey<T>>(eventName: K): ((...args: any[]) => void)[]; | ||
eventNames(): (string | symbol)[]; | ||
@@ -21,0 +21,0 @@ removeAllListeners(): EventEmitter; |
import { EthExecutionAPI, JsonRpcBatchRequest, JsonRpcBatchResponse, SupportedProviders, Web3APIMethod, Web3APIRequest, Web3APIReturnType, Web3APISpec, Web3BaseProviderConstructor } from 'web3-types'; | ||
import { Web3EventEmitter } from './web3_event_emitter.js'; | ||
import { RequestManagerMiddleware } from './types.js'; | ||
export declare enum Web3RequestManagerEvent { | ||
@@ -12,3 +13,4 @@ PROVIDER_CHANGED = "PROVIDER_CHANGED", | ||
private readonly useRpcCallSpecification?; | ||
constructor(provider?: SupportedProviders<API> | string, useRpcCallSpecification?: boolean); | ||
middleware?: RequestManagerMiddleware<API>; | ||
constructor(provider?: SupportedProviders<API> | string, useRpcCallSpecification?: boolean, requestManagerMiddleware?: RequestManagerMiddleware<API>); | ||
/** | ||
@@ -40,2 +42,3 @@ * Will return all available providers | ||
setProvider(provider?: SupportedProviders<API> | string): boolean; | ||
setMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>): void; | ||
/** | ||
@@ -42,0 +45,0 @@ * |
@@ -48,3 +48,3 @@ "use strict"; | ||
class Web3RequestManager extends web3_event_emitter_js_1.Web3EventEmitter { | ||
constructor(provider, useRpcCallSpecification) { | ||
constructor(provider, useRpcCallSpecification, requestManagerMiddleware) { | ||
super(); | ||
@@ -55,2 +55,4 @@ if (!(0, web3_utils_1.isNullish)(provider)) { | ||
this.useRpcCallSpecification = useRpcCallSpecification; | ||
if (!(0, web3_utils_1.isNullish)(requestManagerMiddleware)) | ||
this.middleware = requestManagerMiddleware; | ||
} | ||
@@ -111,2 +113,5 @@ /** | ||
} | ||
setMiddleware(requestManagerMiddleware) { | ||
this.middleware = requestManagerMiddleware; | ||
} | ||
/** | ||
@@ -123,3 +128,6 @@ * | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield this._sendRequest(request); | ||
const requestObj = Object.assign({}, request); | ||
let response = yield this._sendRequest(requestObj); | ||
if (!(0, web3_utils_1.isNullish)(this.middleware)) | ||
response = yield this.middleware.processResponse(response); | ||
if (web3_utils_1.jsonRpc.isResponseWithResult(response)) { | ||
@@ -148,5 +156,8 @@ return response.result; | ||
} | ||
const payload = web3_utils_1.jsonRpc.isBatchRequest(request) | ||
let payload = (web3_utils_1.jsonRpc.isBatchRequest(request) | ||
? web3_utils_1.jsonRpc.toBatchPayload(request) | ||
: web3_utils_1.jsonRpc.toPayload(request); | ||
: web3_utils_1.jsonRpc.toPayload(request)); | ||
if (!(0, web3_utils_1.isNullish)(this.middleware)) { | ||
payload = yield this.middleware.processRequest(payload); | ||
} | ||
if ((0, utils_js_1.isWeb3Provider)(provider)) { | ||
@@ -175,6 +186,8 @@ let response; | ||
return new Promise((resolve, reject) => { | ||
const rejectWithError = (err) => reject(this._processJsonRpcResponse(payload, err, { | ||
legacy: true, | ||
error: true, | ||
})); | ||
const rejectWithError = (err) => { | ||
reject(this._processJsonRpcResponse(payload, err, { | ||
legacy: true, | ||
error: true, | ||
})); | ||
}; | ||
const resolveWithResponse = (response) => resolve(this._processJsonRpcResponse(payload, response, { | ||
@@ -201,3 +214,13 @@ legacy: true, | ||
const responsePromise = result; | ||
responsePromise.then(resolveWithResponse).catch(rejectWithError); | ||
responsePromise.then(resolveWithResponse).catch(error => { | ||
try { | ||
// Attempt to process the error response | ||
const processedError = this._processJsonRpcResponse(payload, error, { legacy: true, error: true }); | ||
reject(processedError); | ||
} | ||
catch (processingError) { | ||
// Catch any errors that occur during the error processing | ||
reject(processingError); | ||
} | ||
}); | ||
} | ||
@@ -204,0 +227,0 @@ }); |
@@ -17,2 +17,3 @@ /* | ||
*/ | ||
import { DEFAULT_RETURN_FORMAT, } from 'web3-types'; | ||
import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; | ||
@@ -55,2 +56,3 @@ import { isNullish, toHex } from 'web3-utils'; | ||
transactionTypeParser: undefined, | ||
defaultReturnFormat: DEFAULT_RETURN_FORMAT, | ||
}; | ||
@@ -276,2 +278,9 @@ this.setConfig(options !== null && options !== void 0 ? options : {}); | ||
} | ||
get defaultReturnFormat() { | ||
return this.config.defaultReturnFormat; | ||
} | ||
set defaultReturnFormat(val) { | ||
this._triggerConfigChange('defaultReturnFormat', val); | ||
this.config.defaultReturnFormat = val; | ||
} | ||
get defaultNetworkId() { | ||
@@ -278,0 +287,0 @@ return this.config.defaultNetworkId; |
@@ -49,6 +49,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
const { config, provider, requestManager, subscriptionManager, registeredSubscriptions, accountProvider, wallet, } = providerOrContext; | ||
const { config, provider, requestManager, subscriptionManager, registeredSubscriptions, accountProvider, wallet, requestManagerMiddleware } = providerOrContext; | ||
this.setConfig(config !== null && config !== void 0 ? config : {}); | ||
this._requestManager = | ||
requestManager !== null && requestManager !== void 0 ? requestManager : new Web3RequestManager(provider, (_a = config === null || config === void 0 ? void 0 : config.enableExperimentalFeatures) === null || _a === void 0 ? void 0 : _a.useSubscriptionWhenCheckingBlockTimeout); | ||
requestManager !== null && requestManager !== void 0 ? requestManager : new Web3RequestManager(provider, (_a = config === null || config === void 0 ? void 0 : config.enableExperimentalFeatures) === null || _a === void 0 ? void 0 : _a.useSubscriptionWhenCheckingBlockTimeout, requestManagerMiddleware); | ||
if (subscriptionManager) { | ||
@@ -235,2 +235,5 @@ this._subscriptionManager = subscriptionManager; | ||
} | ||
setRequestManagerMiddleware(requestManagerMiddleware) { | ||
this.requestManager.setMiddleware(requestManagerMiddleware); | ||
} | ||
/** | ||
@@ -237,0 +240,0 @@ * Will return the {@link Web3BatchRequest} constructor. |
@@ -42,3 +42,3 @@ /* | ||
export class Web3RequestManager extends Web3EventEmitter { | ||
constructor(provider, useRpcCallSpecification) { | ||
constructor(provider, useRpcCallSpecification, requestManagerMiddleware) { | ||
super(); | ||
@@ -49,2 +49,4 @@ if (!isNullish(provider)) { | ||
this.useRpcCallSpecification = useRpcCallSpecification; | ||
if (!isNullish(requestManagerMiddleware)) | ||
this.middleware = requestManagerMiddleware; | ||
} | ||
@@ -105,2 +107,5 @@ /** | ||
} | ||
setMiddleware(requestManagerMiddleware) { | ||
this.middleware = requestManagerMiddleware; | ||
} | ||
/** | ||
@@ -117,3 +122,6 @@ * | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield this._sendRequest(request); | ||
const requestObj = Object.assign({}, request); | ||
let response = yield this._sendRequest(requestObj); | ||
if (!isNullish(this.middleware)) | ||
response = yield this.middleware.processResponse(response); | ||
if (jsonRpc.isResponseWithResult(response)) { | ||
@@ -142,5 +150,8 @@ return response.result; | ||
} | ||
const payload = jsonRpc.isBatchRequest(request) | ||
let payload = (jsonRpc.isBatchRequest(request) | ||
? jsonRpc.toBatchPayload(request) | ||
: jsonRpc.toPayload(request); | ||
: jsonRpc.toPayload(request)); | ||
if (!isNullish(this.middleware)) { | ||
payload = yield this.middleware.processRequest(payload); | ||
} | ||
if (isWeb3Provider(provider)) { | ||
@@ -169,6 +180,8 @@ let response; | ||
return new Promise((resolve, reject) => { | ||
const rejectWithError = (err) => reject(this._processJsonRpcResponse(payload, err, { | ||
legacy: true, | ||
error: true, | ||
})); | ||
const rejectWithError = (err) => { | ||
reject(this._processJsonRpcResponse(payload, err, { | ||
legacy: true, | ||
error: true, | ||
})); | ||
}; | ||
const resolveWithResponse = (response) => resolve(this._processJsonRpcResponse(payload, response, { | ||
@@ -195,3 +208,13 @@ legacy: true, | ||
const responsePromise = result; | ||
responsePromise.then(resolveWithResponse).catch(rejectWithError); | ||
responsePromise.then(resolveWithResponse).catch(error => { | ||
try { | ||
// Attempt to process the error response | ||
const processedError = this._processJsonRpcResponse(payload, error, { legacy: true, error: true }); | ||
reject(processedError); | ||
} | ||
catch (processingError) { | ||
// Catch any errors that occur during the error processing | ||
reject(processingError); | ||
} | ||
}); | ||
} | ||
@@ -198,0 +221,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import { HexString, Transaction } from 'web3-types'; | ||
import { HexString, JsonRpcPayload, JsonRpcResponse, Transaction, Web3APIMethod, Web3APIReturnType } from 'web3-types'; | ||
export declare type TransactionTypeParser = (transaction: Transaction) => HexString | undefined; | ||
@@ -11,2 +11,10 @@ export interface Method { | ||
} | ||
export interface RequestManagerMiddleware<API> { | ||
processRequest<ParamType = unknown[]>(request: JsonRpcPayload<ParamType>, options?: { | ||
[key: string]: unknown; | ||
}): Promise<JsonRpcPayload<ParamType>>; | ||
processResponse<AnotherMethod extends Web3APIMethod<API>, ResponseType = Web3APIReturnType<API, AnotherMethod>>(response: JsonRpcResponse<ResponseType>, options?: { | ||
[key: string]: unknown; | ||
}): Promise<JsonRpcResponse<ResponseType>>; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import { Numbers, HexString, BlockNumberOrTag, Common } from 'web3-types'; | ||
import { Numbers, HexString, BlockNumberOrTag, Common, DataFormat } from 'web3-types'; | ||
import { TransactionTypeParser } from './types.js'; | ||
@@ -31,2 +31,3 @@ import { TransactionBuilder } from './web3_context.js'; | ||
transactionTypeParser?: TransactionTypeParser; | ||
defaultReturnFormat: DataFormat; | ||
} | ||
@@ -189,2 +190,4 @@ declare type ConfigEvent<T, P extends keyof T = keyof T> = P extends unknown ? { | ||
set maxListenersWarningThreshold(val: number); | ||
get defaultReturnFormat(): DataFormat; | ||
set defaultReturnFormat(val: DataFormat); | ||
get defaultNetworkId(): Numbers | undefined; | ||
@@ -191,0 +194,0 @@ set defaultNetworkId(val: Numbers | undefined); |
import { EthExecutionAPI, HexString, Numbers, SupportedProviders, Transaction, Web3AccountProvider, Web3APISpec, Web3BaseProvider, Web3BaseWallet, Web3BaseWalletAccount } from 'web3-types'; | ||
import { BaseTransaction } from 'web3-eth-accounts'; | ||
import { ExtensionObject } from './types.js'; | ||
import { ExtensionObject, RequestManagerMiddleware } from './types.js'; | ||
import { Web3BatchRequest } from './web3_batch_request.js'; | ||
@@ -31,2 +31,3 @@ import { Web3Config, Web3ConfigOptions } from './web3_config.js'; | ||
wallet?: Web3BaseWallet<Web3BaseWalletAccount>; | ||
requestManagerMiddleware?: RequestManagerMiddleware<API>; | ||
}; | ||
@@ -156,2 +157,3 @@ export declare type Web3ContextConstructor<T extends Web3Context, T2 extends unknown[]> = new (...args: [...extras: T2, context: Web3ContextObject]) => T; | ||
setProvider(provider?: SupportedProviders<API> | string): boolean; | ||
setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>): void; | ||
/** | ||
@@ -158,0 +160,0 @@ * Will return the {@link Web3BatchRequest} constructor. |
@@ -18,3 +18,3 @@ import { EventEmitter } from 'web3-utils'; | ||
listenerCount<K extends Web3EventKey<T>>(eventName: K): number; | ||
listeners<K extends Web3EventKey<T>>(eventName: K): Function[]; | ||
listeners<K extends Web3EventKey<T>>(eventName: K): ((...args: any[]) => void)[]; | ||
eventNames(): (string | symbol)[]; | ||
@@ -21,0 +21,0 @@ removeAllListeners(): EventEmitter; |
import { EthExecutionAPI, JsonRpcBatchRequest, JsonRpcBatchResponse, SupportedProviders, Web3APIMethod, Web3APIRequest, Web3APIReturnType, Web3APISpec, Web3BaseProviderConstructor } from 'web3-types'; | ||
import { Web3EventEmitter } from './web3_event_emitter.js'; | ||
import { RequestManagerMiddleware } from './types.js'; | ||
export declare enum Web3RequestManagerEvent { | ||
@@ -12,3 +13,4 @@ PROVIDER_CHANGED = "PROVIDER_CHANGED", | ||
private readonly useRpcCallSpecification?; | ||
constructor(provider?: SupportedProviders<API> | string, useRpcCallSpecification?: boolean); | ||
middleware?: RequestManagerMiddleware<API>; | ||
constructor(provider?: SupportedProviders<API> | string, useRpcCallSpecification?: boolean, requestManagerMiddleware?: RequestManagerMiddleware<API>); | ||
/** | ||
@@ -40,2 +42,3 @@ * Will return all available providers | ||
setProvider(provider?: SupportedProviders<API> | string): boolean; | ||
setMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>): void; | ||
/** | ||
@@ -42,0 +45,0 @@ * |
{ | ||
"name": "web3-core", | ||
"version": "4.3.3-dev.fa4c72b.0+fa4c72b", | ||
"version": "4.4.1-dev.fd2982d.0+fd2982d", | ||
"description": "Web3 core tools for sub-packages. This is an internal package.", | ||
@@ -33,3 +33,3 @@ "main": "./lib/commonjs/index.js", | ||
"build:check": "node -e \"require('./lib')\"", | ||
"lint": "eslint --ext .js,.ts .", | ||
"lint": "eslint --cache --cache-strategy content --ext .ts .", | ||
"lint:fix": "eslint --fix --ext .js,.ts .", | ||
@@ -46,13 +46,13 @@ "format": "prettier --write '**/*'", | ||
"dependencies": { | ||
"web3-errors": "1.1.5-dev.fa4c72b.0+fa4c72b", | ||
"web3-eth-accounts": "4.1.1-dev.fa4c72b.0+fa4c72b", | ||
"web3-eth-iban": "4.0.8-dev.fa4c72b.0+fa4c72b", | ||
"web3-providers-http": "4.1.1-dev.fa4c72b.0+fa4c72b", | ||
"web3-providers-ws": "4.0.8-dev.fa4c72b.0+fa4c72b", | ||
"web3-types": "1.3.2-dev.fa4c72b.0+fa4c72b", | ||
"web3-utils": "4.1.1-dev.fa4c72b.0+fa4c72b", | ||
"web3-validator": "2.0.4-dev.fa4c72b.0+fa4c72b" | ||
"web3-errors": "1.2.1-dev.fd2982d.0+fd2982d", | ||
"web3-eth-accounts": "4.1.3-dev.fd2982d.0+fd2982d", | ||
"web3-eth-iban": "4.0.8-dev.fd2982d.0+fd2982d", | ||
"web3-providers-http": "4.1.1-dev.fd2982d.0+fd2982d", | ||
"web3-providers-ws": "4.0.8-dev.fd2982d.0+fd2982d", | ||
"web3-types": "1.6.1-dev.fd2982d.0+fd2982d", | ||
"web3-utils": "4.3.1-dev.fd2982d.0+fd2982d", | ||
"web3-validator": "2.0.7-dev.fd2982d.0+fd2982d" | ||
}, | ||
"optionalDependencies": { | ||
"web3-providers-ipc": "4.0.8-dev.fa4c72b.0+fa4c72b" | ||
"web3-providers-ipc": "4.0.8-dev.fd2982d.0+fd2982d" | ||
}, | ||
@@ -68,10 +68,10 @@ "devDependencies": { | ||
"eslint-plugin-import": "^2.26.0", | ||
"jest": "^28.1.3", | ||
"jest": "^29.7.0", | ||
"jest-extended": "^3.0.1", | ||
"jest-when": "^3.5.1", | ||
"prettier": "^2.7.1", | ||
"ts-jest": "^28.0.7", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^4.7.4" | ||
}, | ||
"gitHead": "fa4c72b33f5ec7a0ca8f79f74e788a1a79b9a5d7" | ||
"gitHead": "fd2982d1afacd6bf89084324b478ec04af18052e" | ||
} |
@@ -18,7 +18,12 @@ /* | ||
import { HexString, Transaction } from 'web3-types'; | ||
import { | ||
HexString, | ||
JsonRpcPayload, | ||
JsonRpcResponse, | ||
Transaction, | ||
Web3APIMethod, | ||
Web3APIReturnType, | ||
} from 'web3-types'; | ||
export type TransactionTypeParser = ( | ||
transaction: Transaction, | ||
) => HexString | undefined; | ||
export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined; | ||
@@ -34,1 +39,16 @@ export interface Method { | ||
} | ||
export interface RequestManagerMiddleware<API> { | ||
processRequest<ParamType = unknown[]>( | ||
request: JsonRpcPayload<ParamType>, | ||
options?: { [key: string]: unknown }, | ||
): Promise<JsonRpcPayload<ParamType>>; | ||
processResponse< | ||
AnotherMethod extends Web3APIMethod<API>, | ||
ResponseType = Web3APIReturnType<API, AnotherMethod>, | ||
>( | ||
response: JsonRpcResponse<ResponseType>, | ||
options?: { [key: string]: unknown }, | ||
): Promise<JsonRpcResponse<ResponseType>>; | ||
} |
@@ -18,3 +18,10 @@ /* | ||
import { Numbers, HexString, BlockNumberOrTag, Common } from 'web3-types'; | ||
import { | ||
Numbers, | ||
HexString, | ||
BlockNumberOrTag, | ||
Common, | ||
DEFAULT_RETURN_FORMAT, | ||
DataFormat, | ||
} from 'web3-types'; | ||
import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors'; | ||
@@ -56,2 +63,3 @@ import { isNullish, toHex } from 'web3-utils'; | ||
transactionTypeParser?: TransactionTypeParser; | ||
defaultReturnFormat: DataFormat; | ||
} | ||
@@ -98,2 +106,3 @@ | ||
transactionTypeParser: undefined, | ||
defaultReturnFormat: DEFAULT_RETURN_FORMAT, | ||
}; | ||
@@ -354,2 +363,11 @@ | ||
public get defaultReturnFormat() { | ||
return this.config.defaultReturnFormat; | ||
} | ||
public set defaultReturnFormat(val) { | ||
this._triggerConfigChange('defaultReturnFormat', val); | ||
this.config.defaultReturnFormat = val; | ||
} | ||
public get defaultNetworkId() { | ||
@@ -356,0 +374,0 @@ return this.config.defaultNetworkId; |
@@ -28,3 +28,3 @@ /* | ||
// eslint-disable-next-line import/no-cycle | ||
import { ExtensionObject } from './types.js'; | ||
import { ExtensionObject, RequestManagerMiddleware } from './types.js'; | ||
import { Web3BatchRequest } from './web3_batch_request.js'; | ||
@@ -69,2 +69,3 @@ // eslint-disable-next-line import/no-cycle | ||
wallet?: Web3BaseWallet<Web3BaseWalletAccount>; | ||
requestManagerMiddleware?: RequestManagerMiddleware<API>; | ||
}; | ||
@@ -134,2 +135,3 @@ | ||
wallet, | ||
requestManagerMiddleware | ||
} = providerOrContext as Web3ContextInitOptions<API, RegisteredSubs>; | ||
@@ -144,2 +146,3 @@ | ||
config?.enableExperimentalFeatures?.useSubscriptionWhenCheckingBlockTimeout, | ||
requestManagerMiddleware | ||
); | ||
@@ -359,2 +362,7 @@ | ||
} | ||
public setRequestManagerMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>){ | ||
this.requestManager.setMiddleware(requestManagerMiddleware); | ||
} | ||
/** | ||
@@ -361,0 +369,0 @@ * Will return the {@link Web3BatchRequest} constructor. |
@@ -55,2 +55,3 @@ /* | ||
import { Web3EventEmitter } from './web3_event_emitter.js'; | ||
import { RequestManagerMiddleware } from './types.js'; | ||
@@ -77,5 +78,8 @@ export enum Web3RequestManagerEvent { | ||
private readonly useRpcCallSpecification?: boolean; | ||
public middleware?: RequestManagerMiddleware<API>; | ||
public constructor( | ||
provider?: SupportedProviders<API> | string, | ||
useRpcCallSpecification?: boolean, | ||
requestManagerMiddleware?: RequestManagerMiddleware<API>, | ||
) { | ||
@@ -88,2 +92,4 @@ super(); | ||
this.useRpcCallSpecification = useRpcCallSpecification; | ||
if (!isNullish(requestManagerMiddleware)) this.middleware = requestManagerMiddleware; | ||
} | ||
@@ -148,2 +154,6 @@ | ||
public setMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>) { | ||
this.middleware = requestManagerMiddleware; | ||
} | ||
/** | ||
@@ -162,3 +172,8 @@ * | ||
>(request: Web3APIRequest<API, Method>): Promise<ResponseType> { | ||
const response = await this._sendRequest<Method, ResponseType>(request); | ||
const requestObj = { ...request }; | ||
let response = await this._sendRequest<Method, ResponseType>(requestObj); | ||
if (!isNullish(this.middleware)) response = await this.middleware.processResponse(response); | ||
if (jsonRpc.isResponseWithResult(response)) { | ||
@@ -196,6 +211,11 @@ return response.result; | ||
const payload = jsonRpc.isBatchRequest(request) | ||
? jsonRpc.toBatchPayload(request) | ||
: jsonRpc.toPayload(request); | ||
let payload = ( | ||
jsonRpc.isBatchRequest(request) | ||
? jsonRpc.toBatchPayload(request) | ||
: jsonRpc.toPayload(request) | ||
) as JsonRpcPayload; | ||
if (!isNullish(this.middleware)) { | ||
payload = await this.middleware.processRequest(payload); | ||
} | ||
if (isWeb3Provider(provider)) { | ||
@@ -237,3 +257,3 @@ let response; | ||
return new Promise<JsonRpcResponse<ResponseType>>((resolve, reject) => { | ||
const rejectWithError = (err: unknown) => | ||
const rejectWithError = (err: unknown) => { | ||
reject( | ||
@@ -249,2 +269,4 @@ this._processJsonRpcResponse( | ||
); | ||
}; | ||
const resolveWithResponse = (response: JsonRpcResponse<ResponseType>) => | ||
@@ -279,3 +301,16 @@ resolve( | ||
>; | ||
responsePromise.then(resolveWithResponse).catch(rejectWithError); | ||
responsePromise.then(resolveWithResponse).catch(error => { | ||
try { | ||
// Attempt to process the error response | ||
const processedError = this._processJsonRpcResponse( | ||
payload, | ||
error as JsonRpcResponse<ResponseType, unknown>, | ||
{ legacy: true, error: true }, | ||
); | ||
reject(processedError); | ||
} catch (processingError) { | ||
// Catch any errors that occur during the error processing | ||
reject(processingError); | ||
} | ||
}); | ||
} | ||
@@ -282,0 +317,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
482042
8489