mirror of
https://github.com/Permissionless-Software-Foundation/x402-base-facilitator.git
synced 2026-09-21 16:52:01 -07:00
Working towards a JSON RPC
This commit is contained in:
+15
-1
@@ -2,23 +2,29 @@
|
||||
This is the parent class library for the RPC controller.
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
|
||||
// Local support libraries
|
||||
const UserController = require('./users')
|
||||
|
||||
let _this
|
||||
|
||||
class JSONRPC {
|
||||
constructor (localConfig) {
|
||||
// Encapsulate dependencies
|
||||
this.jsonrpc = jsonrpc
|
||||
this.userController = new UserController()
|
||||
|
||||
_this = this
|
||||
}
|
||||
|
||||
// This method takes a raw string of data from IPFS, parses it, and determins
|
||||
// which controller to route the instruction to.
|
||||
router (str) {
|
||||
router (str, from) {
|
||||
try {
|
||||
console.log('router str: ', str)
|
||||
console.log('router from: ', from)
|
||||
|
||||
const parsedData = _this.jsonrpc.parse(str)
|
||||
console.log('parsedData: ', parsedData)
|
||||
@@ -28,6 +34,14 @@ class JSONRPC {
|
||||
if (parsedData.type === 'invalid') {
|
||||
return
|
||||
}
|
||||
|
||||
switch (parsedData.payload.id) {
|
||||
case 'users':
|
||||
_this.userController.userRouter(parsedData)
|
||||
break
|
||||
// case default:
|
||||
// TODO: Return an error
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error in rpc router(): ', err)
|
||||
// Do not throw error. This is a top-level function.
|
||||
|
||||
@@ -6,6 +6,26 @@ class UsersRPC {
|
||||
constructor (localConfig) {
|
||||
console.log('instantiating UsersRPC')
|
||||
}
|
||||
|
||||
userRouter (rpcData) {
|
||||
try {
|
||||
console.log('userRouter rpcData: ', rpcData)
|
||||
|
||||
if (rpcData.payload.method === 'getAll') this.getAll()
|
||||
} catch (err) {
|
||||
console.error('Error in UsersRPC/rpcRouter()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async getAll () {
|
||||
try {
|
||||
console.log('Executing get all')
|
||||
} catch (err) {
|
||||
console.error('Error in getAll()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UsersRPC
|
||||
|
||||
Reference in New Issue
Block a user