Skip to content

Commit

Permalink
converting get-provider-state file to typescript and extracting types…
Browse files Browse the repository at this point in the history
… to a helper file
  • Loading branch information
NiranjanaBinoy committed Mar 21, 2024
1 parent 831cd9d commit 0012b54
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 49 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type {
JsonRpcEngineNextCallback,
JsonRpcEngineEndCallback,
} from '@metamask/json-rpc-engine';
import type {
JsonRpcRequest,
JsonRpcResponse,
JsonRpcParams,
Json,
} from '@metamask/utils';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import {
HandlerWrapperType,
getProviderStateType,
ProviderStateHandlerResult,
} from './handlers-helper';

type getProviderStateImplementationType<
Params extends JsonRpcParams = JsonRpcParams,
Result extends Json = Json,
> = {
implementation: (
_req: JsonRpcRequest<Params>,
res: JsonRpcResponse<Result>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{ _getProviderState }: Record<string, getProviderStateType>,
) => Promise<void>;
} & HandlerWrapperType;

/**
* This RPC method gets background state relevant to the provider.
* The background sends RPC notifications on state changes, but the provider
* first requests state on initialization.
*/

const getProviderState: getProviderStateImplementationType = {
methodNames: [MESSAGE_TYPE.GET_PROVIDER_STATE],
implementation: getProviderStateHandler,
hookNames: {
getProviderState: true,
},
};
export default getProviderState;

/**
* @typedef {object} ProviderStateHandlerOptions
* @property {() => ProviderStateHandlerResult} getProviderState - A function that
* gets the current provider state.
*/

/**
* @param _req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The json-rpc-engine 'next' callback.
* @param end - The json-rpc-engine 'end' callback.
* @param options
* @param options.getProviderState
*/
async function getProviderStateHandler<
Params extends JsonRpcParams = JsonRpcParams,
Result extends Json = Json,
>(
_req: JsonRpcRequest<Params>,
res: JsonRpcResponse<Result>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{
getProviderState: _getProviderState,
}: Record<string, (origin: string) => Promise<ProviderStateHandlerResult>>,
): Promise<void> {
res.result = {
...(await _getProviderState((origin = _req.origin))),
};
return end();
}
22 changes: 22 additions & 0 deletions app/scripts/lib/rpc-method-middleware/handlers/handlers-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type HandlerWrapperType = {
methodNames: [string];
hookNames: Record<string, boolean>;
};

/**
* @typedef {object} ProviderStateHandlerResult
* @property {string} chainId - The current chain ID.
* @property {boolean} isUnlocked - Whether the extension is unlocked or not.
* @property {string} networkVersion - The current network ID.
* @property {string[]} accounts - List of permitted accounts for the specified origin.
*/
export type ProviderStateHandlerResult = {
chainId: string;
isUnlocked: boolean;
networkVersion: string;
accounts: string[];
};

export type getProviderStateType = (
origin: string,
) => Promise<ProviderStateHandlerResult>;

0 comments on commit 0012b54

Please sign in to comment.