Created
May 13, 2021 12:21
-
-
Save DougAnderson444/81ec7244fa6be266504771ac24dcf59b to your computer and use it in GitHub Desktop.
oak-server-post-rpc.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
router.post(`/${config.rpcpath}`, async (context: Context) => { | |
const body = context.request.body() | |
let responseBody: any | |
if (body.type === 'json') { | |
let value = await body.value | |
let id = value?.id | |
let method = value?.method | |
if (!(method in handlers)) { | |
const message = `Method not found` | |
responseBody = { error: { message, code: -32601 }, jsonrpc: '2.0', id } | |
} else { | |
try { | |
let fn = handlers[method] | |
let params = value?.params ? value.params : [] | |
const paramStr = params ? params.join(',') : '' | |
console.log(`rpc-run await ${method}(${paramStr})`) | |
const result = await fn(...params) | |
responseBody = { result, jsonrpc: '2.0', id } | |
} catch (e) { | |
responseBody = { | |
error: { message: `${e}`, code: -32603 }, | |
jsonrpc: '2.0', | |
id | |
} | |
} | |
} | |
} else { | |
responseBody = { | |
error: { message: 'Parse error', code: -32700 }, | |
jsonrpc: '2.0' | |
} | |
} | |
context.response.body = responseBody | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment