Skip to content

Instantly share code, notes, and snippets.

@DougAnderson444
Created May 13, 2021 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DougAnderson444/81ec7244fa6be266504771ac24dcf59b to your computer and use it in GitHub Desktop.
Save DougAnderson444/81ec7244fa6be266504771ac24dcf59b to your computer and use it in GitHub Desktop.
oak-server-post-rpc.ts
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