-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
types(remix-server-runtime): make context
mandatory in DataFunctionArgs
#3989
Conversation
🦋 Changeset detectedLatest commit: dcc1a57 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Hi @justinwaite, Welcome, and thank you for contributing to Remix! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at hello@remix.run. Thanks! - The Remix team |
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
@@ -110,7 +110,7 @@ async function handleDataRequest({ | |||
match = getRequestMatch(url, matches); | |||
|
|||
response = await callRouteAction({ | |||
loadContext, | |||
loadContext: loadContext ?? {}, |
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.
I would default this as high up as possible. So, in the args definition line 79) or even higher up. I'm assuming that the consumer of this function doesn't really or shouldn't be instantiating the empty object. In that case malang it okay to default it as an argument.
Edit: same with all the other changes. It's just a little bit more elegant. I wonder if defining it at the start of thr chain is possible or a "cleaner" solution
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 call. I moved it up to the requestHandler
to be the default value for the loadContext arg. This definitely did clean it up
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.
Looks like this is on the right track!
We'd like to make all user-facing APIs (i.e. things exported from the main index.ts
) optional (context?: AppLoadContext
) while making all the internal APIs required (context: AppLoadContext
).
The only user-facing API should be the requestHandler
function within the createRequestHandler
function. I think all that's needed is to change that to:
return async function requestHandler(request, loadContext = {}) {
For all other places, you should be able to make the load context required.
context
mandatory in DataFunctionArgs
Hey @pcattori, what exactly would you like me to change here? The interface for export type RequestHandler = (
request: Request,
loadContext?: AppLoadContext
) => Promise<Response>; The only place I'm making it required is in |
I think @marwan38 said what should change more clearly than I did 😅 was just trying to emphasize that we want that change not only because its tidier, but because it correctly models that users should be able to omit |
Waiting for #4010 to fix the Windows CI errors |
Nice! Thank you |
Closes: #3988
My strategy for addressing this issue is to always pass an empty object for the context, rather than passing nothing (or undefined). Since we've already declared the AppLoadContext interface to be
{[key: string]: unknown}
, I feel like this would be a reasonable approach.Testing Strategy:
I created an app in the playground and (manually had to) copy over the built remix-server-runtime directory into it. I then augmented the AppLoadContext interface with a property called
notes
. I then tried to access both a known and unknown property to make sure that typescript knew the difference.Here you can see the difference. Since


asdf
isn't part of the AppLoadContext augmented interface, Typescript correctly infers it asunknown
. However, sincenotes
was added to the interface, it correctly infers it as astring[]
. Note in either case that we didn't need to add an undefined check for the context, since it will always be at least an empty object ({}
).