handleResponse

This commit is contained in:
srgooglo 2022-01-25 11:14:28 +01:00
parent 2068821230
commit a53d39b825
2 changed files with 19 additions and 3 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -76,6 +76,14 @@ class Bridge {
return false return false
} }
handleResponse = async (response) => {
if (typeof this.params.onResponse === "function") {
return await this.params.onResponse(response)
}
return false
}
updateEndpointMap = async () => { updateEndpointMap = async () => {
this.map = await this.getMap() this.map = await this.getMap()
@ -109,7 +117,7 @@ class Bridge {
nameKey = "index" 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 function (body, query, options) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let requestParams = { let requestParams = {
@ -148,14 +156,22 @@ function generateDispatcher(instance, method, route, handleRequestContext) {
error: null, error: null,
} }
await instance(requestParams) const request = await instance(requestParams)
.then((response) => { .then((response) => {
result.response = response result.response = response
return response
}) })
.catch((error) => { .catch((error) => {
result.error = error.response.data.error ?? error.response.data result.error = error.response.data.error ?? error.response.data
return error
}) })
if (typeof handleResponse === "function") {
await handleResponse(request)
}
if (requestParams.parseData) { if (requestParams.parseData) {
if (result.error) { if (result.error) {
return reject(result.error) return reject(result.error)