mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
handleResponse
This commit is contained in:
parent
2068821230
commit
a53d39b825
@ -76,6 +76,14 @@ class Bridge {
|
||||
return false
|
||||
}
|
||||
|
||||
handleResponse = async (response) => {
|
||||
if (typeof this.params.onResponse === "function") {
|
||||
return await this.params.onResponse(response)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
updateEndpointMap = async () => {
|
||||
this.map = await this.getMap()
|
||||
|
||||
@ -109,7 +117,7 @@ class Bridge {
|
||||
nameKey = "index"
|
||||
}
|
||||
|
||||
this.endpoints[fixedMethod][nameKey] = generateDispatcher(this.instance, fixedMethod, route, this.handleRequestContext)
|
||||
this.endpoints[fixedMethod][nameKey] = generateDispatcher(this.instance, fixedMethod, route, this.handleRequestContext, this.handleResponse)
|
||||
})
|
||||
}
|
||||
|
||||
@ -126,7 +134,7 @@ class Bridge {
|
||||
}
|
||||
}
|
||||
|
||||
function generateDispatcher(instance, method, route, handleRequestContext) {
|
||||
function generateDispatcher(instance, method, route, handleRequestContext, handleResponse) {
|
||||
return function (body, query, options) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let requestParams = {
|
||||
@ -148,14 +156,22 @@ function generateDispatcher(instance, method, route, handleRequestContext) {
|
||||
error: null,
|
||||
}
|
||||
|
||||
await instance(requestParams)
|
||||
const request = await instance(requestParams)
|
||||
.then((response) => {
|
||||
result.response = response
|
||||
|
||||
return response
|
||||
})
|
||||
.catch((error) => {
|
||||
result.error = error.response.data.error ?? error.response.data
|
||||
|
||||
return error
|
||||
})
|
||||
|
||||
if (typeof handleResponse === "function") {
|
||||
await handleResponse(request)
|
||||
}
|
||||
|
||||
if (requestParams.parseData) {
|
||||
if (result.error) {
|
||||
return reject(result.error)
|
||||
|
Loading…
x
Reference in New Issue
Block a user