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,80 +1,83 @@
|
|||||||
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") {
|
||||||
|
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")
|
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 function (body, query, options) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let requestParams = {
|
let requestParams = {
|
||||||
@ -86,8 +89,8 @@ function generateDispatcher(bridge, method, route, getContext) {
|
|||||||
params: query,
|
params: query,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof getContext === "function") {
|
if (typeof handleRequestContext === "function") {
|
||||||
requestParams = { ...requestParams, ...getContext() }
|
requestParams = { ...requestParams, ...handleRequestContext() }
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = {
|
let result = {
|
||||||
@ -95,7 +98,7 @@ function generateDispatcher(bridge, method, route, getContext) {
|
|||||||
error: null,
|
error: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
await bridge.instance(requestParams)
|
await instance(requestParams)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
result.response = 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 = {
|
module.exports = {
|
||||||
RequestAdaptor,
|
|
||||||
Bridge,
|
Bridge,
|
||||||
createInterface,
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user