-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add page for making a request with query parameters (#227)
We now have a page for making requests directly with query string parameters. For example, you can make an `eth_getLogs` request by navigation to: `[test dapp path]/request.html?method=eth_getLogs¶ms=[{}]` This can be useful for testing RPC methods not covered by the test dapp functionality, or for e2e testing (especially on mobile, where clicking elements on the page can be challenging). --------- Co-authored-by: legobeat <[email protected]>
- Loading branch information
Showing
6 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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,15 @@ | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>E2E Test Dapp - Send Individual Request</title> | ||
<link rel="icon" type="image/svg" href="metamask-fox.svg" /> | ||
</head> | ||
|
||
<body> | ||
<main> | ||
Initializing | ||
</main> | ||
|
||
<script src="request.js" defer></script> | ||
</body> | ||
</html> |
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,97 @@ | ||
/** | ||
* Get the `main` section of the page, ensuring that it is the only | ||
* one present. | ||
*/ | ||
function getMainElement() { | ||
const mainElements = document.getElementsByTagName('main'); | ||
|
||
if (mainElements.length === 0) { | ||
throw new Error('Main element not found'); | ||
} else if (mainElements.length > 1) { | ||
throw new Error('Multiple main elements found'); | ||
} | ||
return mainElements[0]; | ||
} | ||
|
||
/** | ||
* Get request data from the query string. | ||
* | ||
* @returns {object} The request data. | ||
*/ | ||
function getRequestData() { | ||
const queryString = window.location.search; | ||
|
||
if (queryString.length === 0) { | ||
throw new Error('Request invalid: query string empty'); | ||
} | ||
|
||
const searchParams = new URLSearchParams(queryString); | ||
const method = searchParams.get('method'); | ||
|
||
if (method === null) { | ||
throw new Error('Request invalid: method not provided in query string'); | ||
} | ||
|
||
const rawParams = searchParams.get('params'); | ||
|
||
let params; | ||
if (rawParams !== null) { | ||
try { | ||
params = JSON.parse(rawParams); | ||
} catch (error) { | ||
throw new Error('Request invalid: failed to parse params', { | ||
cause: error, | ||
}); | ||
} | ||
|
||
if (params === null) { | ||
throw new Error(`Request invalid: params parsed as null`); | ||
} else if (typeof params !== 'object') { | ||
throw new Error( | ||
`Request invalid: params parsed as type '${typeof params}'`, | ||
); | ||
} | ||
} | ||
|
||
const request = { method }; | ||
if (params) { | ||
request.params = params; | ||
} | ||
return request; | ||
} | ||
|
||
/** | ||
* Run the request encoded in the query parameters. | ||
*/ | ||
async function main() { | ||
const mainElement = getMainElement(); | ||
|
||
/** | ||
* Log a message, and set it on the page. | ||
* | ||
* @param {string} message - The message to log and set on the page. | ||
*/ | ||
function logAndSet(message) { | ||
console.log(message); | ||
mainElement.innerText = message; | ||
} | ||
|
||
try { | ||
if (!window.ethereum) { | ||
throw new Error('Provider not found'); | ||
} | ||
|
||
const requestData = getRequestData(); | ||
|
||
logAndSet(`Sending request: ${JSON.stringify(requestData)}`); | ||
|
||
const result = await window.ethereum.request(requestData); | ||
|
||
logAndSet(`Response: ${JSON.stringify(result)}`); | ||
} catch (error) { | ||
mainElement.innerText = error.message || 'Unknown error'; | ||
throw error; | ||
} | ||
} | ||
|
||
main().catch(console.error); |
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