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 return false
} }
handleBeforeRequest = async (request) => {
if (typeof this.params.onBeforeRequest === "function") {
return await this.params.onBeforeRequest(request)
}
return false
}
registerHTTPDispatchers = async (map) => { registerHTTPDispatchers = async (map) => {
if (typeof map !== "object") { if (typeof map !== "object") {
console.error("[Bridge] > createHTTPDispatchers > map is not an object") console.error("[Bridge] > createHTTPDispatchers > map is not an object")
@ -136,14 +144,15 @@ module.exports = class Bridge {
nameKey = "index" nameKey = "index"
} }
this.endpoints[fixedMethod][nameKey] = generateHTTPRequestDispatcher( this.endpoints[fixedMethod][nameKey] = generateHTTPRequestDispatcher({
this.httpInterface, instance: this.httpInterface,
fixedMethod, method: fixedMethod,
route, route: route,
this.handleRequestContext, beforeRequest: this.handleBeforeRequest,
this.handleResponse, handleRequestContext: this.handleRequestContext,
this.params.requestHeaders 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 function (body, query, options) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let requestParams = { let requestParams = {
@ -10,6 +17,10 @@ export default function generateHTTPRequestDispatcher(instance, method, route, h
params: query, params: query,
} }
if (typeof beforeRequest === "function") {
await beforeRequest(requestParams)
}
if (typeof handleRequestContext === "function") { if (typeof handleRequestContext === "function") {
const context = await handleRequestContext() const context = await handleRequestContext()
requestParams = { ...requestParams, ...context } requestParams = { ...requestParams, ...context }