-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
814775e
to
ae87558
Compare
bc53276
to
f0d3ee1
Compare
f0d3ee1
to
895b152
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
…ha to be set on res.locals
895b152
to
39327b4
Compare
There was a problem hiding this 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
good catch! missed this because i forgot that |
There was a problem hiding this 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!
newmiddleware/auth.js
Outdated
const { | ||
accessToken: userAccessToken, | ||
params: { siteName }, | ||
} = req | ||
const { | ||
locals: { userId }, | ||
locals: { userId, accessToken: userAccessToken }, | ||
} = res |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!
* 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
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 theRequestHandler
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
req.property = some_value
intores.locals.property = some_value
req.property
tores.locals.property