Skip to content

Commit 5229818

Browse files
committed
feat: add RPC proxy for sending requests to Nimiq RPC node
1 parent 458c6c1 commit 5229818

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* RPC proxy for sending requests to a specified URL.
3+
* This is useful for sending requests to a Nimiq RPC node from the new
4+
* Developer Center without the need for CORS.
5+
*/
6+
7+
export default defineEventHandler(async (event) => {
8+
const body = await readBody(event)
9+
10+
if (!body.payload)
11+
throw createError({ statusCode: 400, message: 'Missing payload in request body' })
12+
if (!body.url)
13+
throw createError({ statusCode: 400, message: 'Missing url in request body' })
14+
15+
try {
16+
const response = await fetch(body.url, {
17+
method: 'POST',
18+
headers: { 'Content-Type': 'application/json' },
19+
body: JSON.stringify(body.payload),
20+
})
21+
22+
const data = await response.json()
23+
return data
24+
}
25+
catch (error) {
26+
console.error('RPC proxy error:', error)
27+
throw createError({
28+
statusCode: 502,
29+
message: 'Failed to connect to RPC node',
30+
cause: error,
31+
})
32+
}
33+
})

0 commit comments

Comments
 (0)