mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 18:44:17 +00:00
refactor client
This commit is contained in:
parent
d2f5ad03c3
commit
9834099f49
@ -1,140 +1,49 @@
|
|||||||
const axios = require("axios")
|
const axios = require("axios")
|
||||||
const camalize = require("@corenode/utils/dist/camalize").default
|
const camalize = require("@corenode/utils/dist/camalize").default
|
||||||
|
|
||||||
export class RequestAdaptor {
|
const FixedMethods = {
|
||||||
constructor(req, payload, callback) {
|
"del": "delete"
|
||||||
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
|
||||||
|
|
||||||
this.origin = this.params.origin
|
this.origin = this.params.origin
|
||||||
this.headers = { ...this.params.headers }
|
this.headers = { ...this.params.headers }
|
||||||
|
|
||||||
this.instance = axios.create({
|
this.instance = axios.create({
|
||||||
baseURL: this.origin,
|
baseURL: this.origin,
|
||||||
headers: this.headers
|
headers: this.headers
|
||||||
})
|
})
|
||||||
|
|
||||||
this.map = null
|
this.map = null
|
||||||
|
this.endpoints = {}
|
||||||
|
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect() {
|
handleRequestContext = () => {
|
||||||
//get map
|
if (typeof this.params.onRequestContext === "function") {
|
||||||
const req = await this.instance.get("/map")
|
return this.params.onRequestContext()
|
||||||
this.map = req.data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateDispatcher(bridge, method, route, getContext) {
|
|
||||||
return function (body, query, options) {
|
|
||||||
return new Promise(async (resolve, reject) => {
|
|
||||||
let requestParams = {
|
|
||||||
parseData: true,
|
|
||||||
...options,
|
|
||||||
method: method,
|
|
||||||
url: route,
|
|
||||||
data: body,
|
|
||||||
params: query,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof getContext === "function") {
|
return false
|
||||||
requestParams = { ...requestParams, ...getContext() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = {
|
initialize = async () => {
|
||||||
response: null,
|
this.map = await this.getMap()
|
||||||
error: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
await bridge.instance(requestParams)
|
for await (let method of Object.keys(this.map)) {
|
||||||
.then((response) => {
|
|
||||||
result.response = response
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
result.error = error.response.data.error ?? error.response.data
|
|
||||||
})
|
|
||||||
|
|
||||||
if (requestParams.parseData) {
|
|
||||||
if (result.error) {
|
|
||||||
return reject(result.error)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(result.response.data)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(result)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createInterface(address, getContext) {
|
|
||||||
let objects = {}
|
|
||||||
|
|
||||||
const bridge = new Bridge({
|
|
||||||
origin: address
|
|
||||||
})
|
|
||||||
|
|
||||||
await bridge.connect()
|
|
||||||
|
|
||||||
const map = bridge.map
|
|
||||||
|
|
||||||
Object.keys(map).forEach((method) => {
|
|
||||||
method = method.toLowerCase()
|
method = method.toLowerCase()
|
||||||
|
|
||||||
if (typeof objects[method] !== "object") {
|
const fixedMethod = FixedMethods[method] ?? method
|
||||||
objects[method] = Object()
|
|
||||||
|
if (typeof this.endpoints[fixedMethod] !== "object") {
|
||||||
|
this.endpoints[fixedMethod] = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
map[method].forEach((endpoint) => {
|
this.map[method].forEach((endpoint) => {
|
||||||
const route = endpoint.route
|
const route = endpoint.route
|
||||||
const tree = route.split("/")
|
const tree = route.split("/")
|
||||||
const hasTree = tree.length >= 1
|
const hasTree = tree.length >= 1
|
||||||
@ -155,17 +64,61 @@ async function createInterface(address, getContext) {
|
|||||||
nameKey = "index"
|
nameKey = "index"
|
||||||
}
|
}
|
||||||
|
|
||||||
objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext)
|
this.endpoints[fixedMethod][nameKey] = generateDispatcher(this.instance, fixedMethod, route, this.handleRequestContext)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.endpoints
|
||||||
|
}
|
||||||
|
|
||||||
|
getMap = async () => {
|
||||||
|
const req = await this.instance.get("/map")
|
||||||
|
return req.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateDispatcher(instance, method, route, handleRequestContext) {
|
||||||
|
return function (body, query, options) {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
let requestParams = {
|
||||||
|
parseData: true,
|
||||||
|
...options,
|
||||||
|
method: method,
|
||||||
|
url: route,
|
||||||
|
data: body,
|
||||||
|
params: query,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof handleRequestContext === "function") {
|
||||||
|
requestParams = { ...requestParams, ...handleRequestContext() }
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = {
|
||||||
|
response: null,
|
||||||
|
error: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
await instance(requestParams)
|
||||||
|
.then((response) => {
|
||||||
|
result.response = response
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
result.error = error.response.data.error ?? error.response.data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (requestParams.parseData) {
|
||||||
|
if (result.error) {
|
||||||
|
return reject(result.error)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(result.response.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(result)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return objects
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
RequestAdaptor,
|
|
||||||
Bridge,
|
Bridge,
|
||||||
createInterface,
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user