added requestAdaptor

This commit is contained in:
srgooglo 2021-07-23 10:29:52 +02:00
parent 2726c6cf0d
commit fa14a25a22

View File

@ -1,5 +1,58 @@
const axios = require("axios") const axios = require("axios")
const { camalize } = require("@corenode/utils") const { camalize } = require("@corenode/utils")
export class RequestAdaptor {
constructor(req, payload, callback) {
this.callback = callback
this.payload = payload
this.req = req
if (typeof this.req !== "function") {
return this.cb("Invalid api request")
}
if (typeof this.payload === "undefined") {
return this.cb("Payload not provided")
}
}
send = async () => {
let payloads = {
body: undefined,
query: undefined,
}
if (Array.isArray(this.payload)) {
if (typeof this.payload[0] === "object") {
payloads.body = this.payload[0]
}
if (typeof this.payload[1] === "object") {
payloads.query = this.payload[1]
}
}else if (typeof this.payload === "object"){
payloads = {
...payloads,
...this.payload
}
}
return await this.req(payloads.body, payloads.query, { parseData: false })
.then((res) => {
this.cb(false, res)
return res.data
})
.catch((err) => {
this.cb(err.response.data, err.response)
return err
})
}
cb = (...context) => {
if (typeof this.callback === "function") {
this.callback(...context)
}
}
}
class Bridge { class Bridge {
constructor(params) { constructor(params) {
this.params = params this.params = params
@ -96,6 +149,7 @@ async function createInterface(address, getContext) {
} }
module.exports = { module.exports = {
RequestAdaptor,
Bridge, Bridge,
createInterface, createInterface,
} }