mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
added requestAdaptor
This commit is contained in:
parent
2726c6cf0d
commit
fa14a25a22
@ -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,
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user