added beforeRequest handler

This commit is contained in:
srgooglo 2022-09-09 15:12:26 +02:00
parent 10216dd05d
commit 1c4a378fd7
2 changed files with 29 additions and 9 deletions

View File

@ -100,6 +100,14 @@ module.exports = class Bridge {
return false
}
handleBeforeRequest = async (request) => {
if (typeof this.params.onBeforeRequest === "function") {
return await this.params.onBeforeRequest(request)
}
return false
}
registerHTTPDispatchers = async (map) => {
if (typeof map !== "object") {
console.error("[Bridge] > createHTTPDispatchers > map is not an object")
@ -136,14 +144,15 @@ module.exports = class Bridge {
nameKey = "index"
}
this.endpoints[fixedMethod][nameKey] = generateHTTPRequestDispatcher(
this.httpInterface,
fixedMethod,
route,
this.handleRequestContext,
this.handleResponse,
this.params.requestHeaders
)
this.endpoints[fixedMethod][nameKey] = generateHTTPRequestDispatcher({
instance: this.httpInterface,
method: fixedMethod,
route: route,
beforeRequest: this.handleBeforeRequest,
handleRequestContext: this.handleRequestContext,
handleResponse: this.handleResponse,
requestHeaders: this.params.requestHeaders,
})
})
}

View File

@ -1,4 +1,11 @@
export default function generateHTTPRequestDispatcher(instance, method, route, handleRequestContext, handleResponse) {
export default function generateHTTPRequestDispatcher({
instance,
method,
route,
beforeRequest,
handleRequestContext,
handleResponse,
}) {
return function (body, query, options) {
return new Promise(async (resolve, reject) => {
let requestParams = {
@ -10,6 +17,10 @@ export default function generateHTTPRequestDispatcher(instance, method, route, h
params: query,
}
if (typeof beforeRequest === "function") {
await beforeRequest(requestParams)
}
if (typeof handleRequestContext === "function") {
const context = await handleRequestContext()
requestParams = { ...requestParams, ...context }