Skip to content

Commit

Permalink
Change api routes, add userEvents to POST
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed Feb 4, 2021
1 parent 274ee93 commit e6d151b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/api/UserEventsApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const UserEventsApi = (io, api, { log }) => {
let result = res.data
let req = payload
let error = res.error
if (!msg.socketId) return
const socket = io.sockets.connected[msg.socketId]
log.trace(`Sending message to client ${module}.${action} error:${JSON.stringify(error)}`)
if (socket) socket.emit('data', formatRes({ module, action, result, req, error }))
Expand Down
54 changes: 34 additions & 20 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,43 @@ setup({ log, skipCheck: true }).then(({ db, initConfig }) => {
status.start()
txPool.start()

let userEvents

const delayedResult = (res, payload, socket) => {
const { params, result, delayed } = res
if (delayed && userEvents) {
const registry = delayed.registry || (!result.data && delayed.runIfEmpty)
if (payload.getDelayed) {
const lastBlock = api.getLastBlock()
const block = (lastBlock) ? lastBlock.number : null

userEvents.send({
action: delayed.action,
module: delayed.module,
params,
socketId: socket ? socket.id : undefined,
payload,
block,
result
})
}
res.result.delayed = { fields: delayed.fields, registry }
}
return res
}

const send = ({ res, response, payload }) => {
const { result } = delayedResult(response, payload)
res.send(result)
}

// http server
const { httpServer } = HttpServer({ api, status, log }, send)
httpServer.listen(port, address)
const io = new IO(httpServer)

// start userEvents api
const userEvents = UserEventsApi(io, api, { log })
userEvents = UserEventsApi(io, api, { log })

io.httpServer.on('listening', () => {
log.info(`Server listening on: ${address || '0.0.0.0'}:${port}`)
Expand Down Expand Up @@ -127,25 +157,9 @@ setup({ log, skipCheck: true }).then(({ db, initConfig }) => {
socket.on('data', async payload => {
try {
const res = await api.run(payload)
const { module, action, params, result, delayed } = res
if (delayed && userEvents) {
const registry = delayed.registry || (!result.data && delayed.runIfEmpty)
if (payload.getDelayed) {
const lastBlock = api.getLastBlock()
const block = (lastBlock) ? lastBlock.number : null
userEvents.send({
action: delayed.action,
module: delayed.module,
params,
socketId: socket.id,
payload,
block,
result
})
}
result.delayed = { fields: delayed.fields, registry }
}
socket.emit('data', formatRes({ module, action, result, req: payload }))
const req = payload
const { module, action, result } = delayedResult(res, payload, socket)
socket.emit('data', formatRes({ module, action, result, req }))
} catch (err) {
log.debug(`Action: ${payload.action}: ERROR: ${err}`)
log.trace(err)
Expand Down
22 changes: 11 additions & 11 deletions src/api/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ const router = express.Router()
const Routes = ({ log, api }, send) => {
const getResult = async ({ module, action, params }) => {
try {
const { result } = await api.run({ module, action, params })
if (!result) throw new Error('Missing result')
if (!result.data) throw new Error('Missing data')
return result
const response = await api.run({ module, action, params })
if (!response.result) throw new Error('Missing result')
// if (!result.data) throw new Error('Missing data')
return response
} catch (err) {
log.debug({ module, action, params })
return Promise.reject(err)
}
}
const sendResult = async ({ res, req, next }, payload) => {
const { action } = payload
let result
const { module, action } = payload
let response
try {
if (!!module !== !!action) {
res.status(400).send()
return
}
if (!module && !action) result = api.info()
else result = await getResult(payload)
if (!result) throw new Error('Empty result')
if (typeof send === 'function') send({ result, res, req, next, payload })
else res.send(result)
if (!module && !action) response = { result: api.info() }
else response = await getResult(payload)
if (typeof send === 'function') send({ response, res, req, next, payload })
else res.send(response.result)
} catch (err) {
res.status(404).send()
log.error(err)
Expand Down

0 comments on commit e6d151b

Please sign in to comment.