mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
refactor client
This commit is contained in:
parent
d2f5ad03c3
commit
9834099f49
@ -1,80 +1,83 @@
|
||||
const axios = require("axios")
|
||||
const camalize = require("@corenode/utils/dist/camalize").default
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
const FixedMethods = {
|
||||
"del": "delete"
|
||||
}
|
||||
|
||||
class Bridge {
|
||||
constructor(params) {
|
||||
constructor(params = {}) {
|
||||
this.params = params
|
||||
|
||||
this.origin = this.params.origin
|
||||
this.headers = { ...this.params.headers }
|
||||
|
||||
this.instance = axios.create({
|
||||
baseURL: this.origin,
|
||||
headers: this.headers
|
||||
})
|
||||
|
||||
this.map = null
|
||||
this.endpoints = {}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
async connect() {
|
||||
//get map
|
||||
handleRequestContext = () => {
|
||||
if (typeof this.params.onRequestContext === "function") {
|
||||
return this.params.onRequestContext()
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
initialize = async () => {
|
||||
this.map = await this.getMap()
|
||||
|
||||
for await (let method of Object.keys(this.map)) {
|
||||
method = method.toLowerCase()
|
||||
|
||||
const fixedMethod = FixedMethods[method] ?? method
|
||||
|
||||
if (typeof this.endpoints[fixedMethod] !== "object") {
|
||||
this.endpoints[fixedMethod] = {}
|
||||
}
|
||||
|
||||
this.map[method].forEach((endpoint) => {
|
||||
const route = endpoint.route
|
||||
const tree = route.split("/")
|
||||
const hasTree = tree.length >= 1
|
||||
let nameKey = route
|
||||
|
||||
// check if has tree
|
||||
if (hasTree) {
|
||||
// remove first whitespace item in route index[0]
|
||||
if (tree[0] == "") {
|
||||
tree.shift()
|
||||
}
|
||||
|
||||
nameKey = camalize(tree.join("_"))
|
||||
}
|
||||
|
||||
// if is an blank route, set as index
|
||||
if (nameKey == "") {
|
||||
nameKey = "index"
|
||||
}
|
||||
|
||||
this.endpoints[fixedMethod][nameKey] = generateDispatcher(this.instance, fixedMethod, route, this.handleRequestContext)
|
||||
})
|
||||
}
|
||||
|
||||
return this.endpoints
|
||||
}
|
||||
|
||||
getMap = async () => {
|
||||
const req = await this.instance.get("/map")
|
||||
this.map = req.data
|
||||
return req.data
|
||||
}
|
||||
}
|
||||
|
||||
function generateDispatcher(bridge, method, route, getContext) {
|
||||
function generateDispatcher(instance, method, route, handleRequestContext) {
|
||||
return function (body, query, options) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let requestParams = {
|
||||
@ -86,8 +89,8 @@ function generateDispatcher(bridge, method, route, getContext) {
|
||||
params: query,
|
||||
}
|
||||
|
||||
if (typeof getContext === "function") {
|
||||
requestParams = { ...requestParams, ...getContext() }
|
||||
if (typeof handleRequestContext === "function") {
|
||||
requestParams = { ...requestParams, ...handleRequestContext() }
|
||||
}
|
||||
|
||||
let result = {
|
||||
@ -95,7 +98,7 @@ function generateDispatcher(bridge, method, route, getContext) {
|
||||
error: null,
|
||||
}
|
||||
|
||||
await bridge.instance(requestParams)
|
||||
await instance(requestParams)
|
||||
.then((response) => {
|
||||
result.response = response
|
||||
})
|
||||
@ -116,56 +119,6 @@ function generateDispatcher(bridge, method, route, getContext) {
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
if (typeof objects[method] !== "object") {
|
||||
objects[method] = Object()
|
||||
}
|
||||
|
||||
map[method].forEach((endpoint) => {
|
||||
const route = endpoint.route
|
||||
const tree = route.split("/")
|
||||
const hasTree = tree.length >= 1
|
||||
let nameKey = route
|
||||
|
||||
// check if has tree
|
||||
if (hasTree) {
|
||||
// remove first whitespace item in route index[0]
|
||||
if (tree[0] == "") {
|
||||
tree.shift()
|
||||
}
|
||||
|
||||
nameKey = camalize(tree.join("_"))
|
||||
}
|
||||
|
||||
// if is an blank route, set as index
|
||||
if (nameKey == "") {
|
||||
nameKey = "index"
|
||||
}
|
||||
|
||||
objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
|
||||
return objects
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
RequestAdaptor,
|
||||
Bridge,
|
||||
createInterface,
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user