Skip to content

Commit

Permalink
Merge pull request #71 from kbarbounakis/finalize-request-after-format
Browse files Browse the repository at this point in the history
finalize context before format response
  • Loading branch information
kbarbounakis authored Mar 19, 2024
2 parents eddfd36 + d4e1b90 commit 853f88c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/@themost/express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion modules/@themost/express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/express",
"version": "1.6.2",
"version": "1.7.0",
"description": "MOST Data ORM Express Middleware",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
4 changes: 2 additions & 2 deletions modules/@themost/express/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ class ExpressDataApplication extends IApplication {
return context;
}
});
req.on('end', () => {
//on end
res.on('close', () => {
//on clse
if (req.context) {
//finalize data context
return req.context.finalize( () => {
Expand Down
41 changes: 26 additions & 15 deletions modules/@themost/express/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ const DefaultTopOption = 25;
* @returns string
*/

function finalizeContext(req, next) {
if (req && req.context && typeof req.context.finalize === 'function') {
return req.context.finalize(next);
}
return next();
}

/**
* Gets or sets the name of the route parameter that holds the name of an entity function
* @property
Expand All @@ -118,20 +125,22 @@ const DefaultTopOption = 25;
* @param {*} res
*/
function tryFormat(data, req, res) {
// get response formatter
const responseFormatter = req.context.getApplication().getStrategy(ResponseFormatter);
//if service exists
if (responseFormatter) {
// call Response.format with formatter
return res.format(responseFormatter.format(data).for(req, res));
}
// fallback to json
if (data == null) {
// send no content if data is empty
return res.status(204).type('application/json').send();
}
// otherwise send json data
return res.json(data);
// finalize context
return finalizeContext(req, () => {
// get response formatter
const responseFormatter = req.context.getApplication().getStrategy(ResponseFormatter);
//if service exists
if (responseFormatter) {
// call Response.format with formatter
return res.format(responseFormatter.format(data).for(req, res));
}
if (data == null) {
// send no content if data is empty
return res.status(204).type('application/json').send();
}
// otherwise send json data
return res.json(data);
});
}

/**
Expand All @@ -141,7 +150,9 @@ function tryFormat(data, req, res) {
* @param {NextFunction} next
*/
function tryFormatStream(data, req, res, next) {
return new StreamFormatter(data).execute(req, res, next);
return finalizeContext(req, () => {
return new StreamFormatter(data).execute(req, res, next);
});
}

/**
Expand Down

0 comments on commit 853f88c

Please sign in to comment.