-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
converting get-provider-state file to typescript and extracting types…
… to a helper file
- Loading branch information
1 parent
831cd9d
commit 0012b54
Showing
3 changed files
with
98 additions
and
49 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
app/scripts/lib/rpc-method-middleware/handlers/get-provider-state.js
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
app/scripts/lib/rpc-method-middleware/handlers/get-provider-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
app/scripts/lib/rpc-method-middleware/handlers/handlers-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |