Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#IP-82] Add a response header containing the current app version #140

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

fabriziopapi
Copy link
Contributor

In order to ease the troubleshooting, we want to add to all the service response a new header field (X-API-Version) containing the current app version.
We created a middleware setting the response header and a common register function to call from all the service code.
Both those functions have been implemented in this project to match the DRY principle and to ease the future similar task.

@pagopa-github-bot
Copy link
Contributor

pagopa-github-bot commented Apr 15, 2021

Warnings
⚠️ Please include a Pivotal story at the beginning of the PR title (see below).

Example of PR titles that include pivotal stories:

  • single story: [#123456] my PR title
  • multiple stories: [#123456,#123457,#123458] my PR title

New dependencies added: jest-express.

jest-express

Author: James W. Lane

Description: jest mock

Homepage: https://github.com/jameswlane/jest-express#readme

Createdabout 3 years ago
Last Updatedabout 1 year ago
LicenseMIT
Maintainers1
Releases27
Direct Dependenciesjest-node-http
Keywordsjest, mock and express
This README is too long to show.

Generated by 🚫 dangerJS against 629d26f

@codecov
Copy link

codecov bot commented Apr 15, 2021

Codecov Report

Merging #140 (629d26f) into master (0e4b323) will increase coverage by 0.59%.
The diff coverage is 87.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #140      +/-   ##
==========================================
+ Coverage   84.43%   85.02%   +0.59%     
==========================================
  Files          33       34       +1     
  Lines         880      888       +8     
  Branches       86       91       +5     
==========================================
+ Hits          743      755      +12     
+ Misses        134      130       -4     
  Partials        3        3              
Impacted Files Coverage Δ
src/utils/types.ts 90.90% <ø> (ø)
src/models/message_status.ts 88.88% <33.33%> (+4.67%) ⬆️
src/models/notification_status.ts 85.71% <50.00%> (+5.06%) ⬆️
src/utils/azure_storage.ts 41.77% <52.17%> (+2.26%) ⬆️
src/utils/async.ts 62.00% <68.75%> (-1.47%) ⬇️
src/utils/response.ts 46.15% <75.00%> (-3.85%) ⬇️
src/models/notification.ts 96.29% <83.33%> (+3.43%) ⬆️
src/mailer/mailup.ts 90.47% <85.71%> (ø)
src/utils/express.ts 76.19% <86.66%> (ø)
src/mailer/transports.ts 73.07% <88.88%> (+1.07%) ⬆️
... and 22 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dc89b6e...629d26f. Read the comment docs.

res,
next
): void => {
fromNullable(process.env.npm_package_version).map(v =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find that in docs, but I'm pretty sure process.env.npm_package_version is defined only when the application is started with npm start (or yarn start).
Although I think that's the case of every app we make, it's a bold assumption. A safer approach would be to read package.json file

import { version } from "../package.json";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the npm_package_version works only for npm-started app, but using the import approach delegate the "version" extraction to the middleware caller becouse it depends of the project structure ("../package.json").
We can easly take the verison as input in this function, attributing the version rightness responsability to the caller.
Do you think it's safer in this way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I didn't realize that we were going to do that from a package.
We could pass the whole package to the factory method, or we can just read the file from

`${process.cwd()}/package.json`

Maybe the second option will provide the right level of abstraction. I don't have a strong opinion actually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've chained both the solution: the verison is extracted by package.json, if fails it will fallback to npm env version.

src/utils/express.ts Outdated Show resolved Hide resolved
@fabriziopapi fabriziopapi marked this pull request as ready for review April 28, 2021 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants