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

chore(app): replace custom properties on req with res.locals #413

Merged
merged 6 commits into from
Apr 4, 2022

Conversation

seaerchin
Copy link
Contributor

@seaerchin seaerchin commented Mar 31, 2022

Problem

Application code previously used to set custom properties on the req object of express. This is acceptable in javascript but as express typings doesn't expose such properties on the request object, will prove to be troublesome to type in typescript.

Express instead has a res.locals property (and a corresponding type parameter in the RequestHandler type) that is used for such custom properties. We should instead be using this for ease of transition to typescript.

Part of isomerpages/isomercms-frontend#849
Closes #379

Solution

  1. change existing declarations of req.property = some_value into res.locals.property = some_value
  2. update call sites of req.property to res.locals.property

alexanderleegs
alexanderleegs previously approved these changes Apr 1, 2022
Copy link
Contributor

@alexanderleegs alexanderleegs left a comment

Choose a reason for hiding this comment

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

lgtm!

Base automatically changed from test/fix-ci to develop April 1, 2022 04:01
Copy link
Contributor

@alexanderleegs alexanderleegs left a comment

Choose a reason for hiding this comment

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

We missed changing a few endpoints -

  • newroutes/authenticated/sites
  • newmiddleware/auth

@seaerchin seaerchin requested a review from alexanderleegs April 1, 2022 04:57
@seaerchin
Copy link
Contributor Author

We missed changing a few endpoints -

  • newroutes/authenticated/sites
  • newmiddleware/auth

good catch! missed this because i forgot that . doesn't include line breaks, which resulted in a faulty regex used for this

Copy link
Contributor

@alexanderleegs alexanderleegs left a comment

Choose a reason for hiding this comment

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

Minor nit, but lgtm otherwise!

Comment on lines 34 to 39
const {
accessToken: userAccessToken,
params: { siteName },
} = req
const {
locals: { userId },
locals: { userId, accessToken: userAccessToken },
} = res
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit: could we swap this to

const { siteName } = req.params
const { userId, accessToken: userAccessToken } = res.locals

?

Same for newroutes/authenticated/sites!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch!! i think this helps with readability and i'd be more than happy to make this change!

@seaerchin seaerchin merged commit 4edec0e into develop Apr 4, 2022
@seaerchin seaerchin deleted the chore/use-res-locals branch April 4, 2022 03:34
This was referenced Apr 14, 2022
harishv7 pushed a commit that referenced this pull request Feb 17, 2023
* refactor(middleware): changed accessToken, currentCommitSha and treeSha to be set on res.locals

* chore(server): modify places that used req.xxx to use res.locals.xxx

* chore(authservice): refactor to explicit object

* fix(authservice.spec): changed test due to async weirdness

* chore(req): updated last few instances of usertoken being retrieved from req

* chore(auth): destructure from res.locals for readability rather than res
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.

[REF] Property setting on express request
2 participants