Skip to content

Commit

Permalink
fix(hermes): cumulative middleware context (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
kon14 authored Oct 28, 2023
1 parent 766e49a commit 65dbfac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
3 changes: 1 addition & 2 deletions libraries/hermes/src/Rest/Rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ export class RestController extends ConduitRouter {
};
self
.checkMiddlewares(context, route.input.middlewares)
.then(r => {
Object.assign(context.context, r);
.then(() => {
if (route.input.action !== ConduitRouteActions.GET) {
return route.executeRequest(context);
}
Expand Down
14 changes: 7 additions & 7 deletions libraries/hermes/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ export abstract class ConduitRouter {
this._middlewareOwners.set(middleware.name, moduleUrl);
}

checkMiddlewares(params: ConduitRouteParameters, middlewares?: string[]) {
let primaryPromise = new Promise(resolve => {
resolve({});
});
async checkMiddlewares(params: ConduitRouteParameters, middlewares?: string[]) {
let primaryPromise = Promise.resolve();
middlewares?.forEach(m => {
const middleware = m.split('?')[0];
if (!this._middlewares?.hasOwnProperty(middleware)) {
primaryPromise = Promise.reject('Middleware does not exist');
} else {
primaryPromise = primaryPromise.then(r => {
primaryPromise = primaryPromise.then(() => {
return this._middlewares![middleware].executeRequest.bind(
this._middlewares![middleware],
)(params)
.then(p => {
if (p.result) {
Object.assign(r as Record<string, unknown>, JSON.parse(p.result));
Object.assign(
params.context as Record<string, unknown>,
JSON.parse(p.result),
);
}
return r;
})
.catch((err: Error) => {
if (!m.includes('?')) throw err;
Expand Down

0 comments on commit 65dbfac

Please sign in to comment.