-
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
Add type deprecations for types now in React Router #5679
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2eb289
Add type deprecations for types now in React Router
brophdawg11 94f9bcf
Fix indentation
brophdawg11 f6d4092
Revert changes that should go to v2
brophdawg11 10eec8f
Merge branch 'dev' into brophdawg11/v1-rr-type-deprecations
brophdawg11 4d4a84f
Merge branch 'dev' into brophdawg11/v1-rr-type-deprecations
brophdawg11 6b184f2
Updates
brophdawg11 ac94a7a
Updates
brophdawg11 503cc2d
Add back removed comment
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@remix-run/react": patch | ||
"@remix-run/server-runtime": patch | ||
--- | ||
|
||
Add type deprecations for types now in React Router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { | ||
defer as routerDefer, | ||
json as routerJson, | ||
redirect as routerRedirect, | ||
type UNSAFE_DeferredData as DeferredData, | ||
type TrackedPromise, | ||
} from "@remix-run/router"; | ||
|
@@ -13,11 +15,15 @@ export type TypedDeferredData<Data extends Record<string, unknown>> = Pick< | |
data: Data; | ||
}; | ||
|
||
// TODO: The RR version of this is the same minus the generic. Should we add | ||
// that over there and remove this? | ||
export type DeferFunction = <Data extends Record<string, unknown>>( | ||
data: Data, | ||
init?: number | ResponseInit | ||
) => TypedDeferredData<Data>; | ||
|
||
// TODO: The RR version of this is identical except it's generic doesn't | ||
// `extends unknown`. Should we add that over there and remove this? | ||
export type JsonFunction = <Data extends unknown>( | ||
brophdawg11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: Data, | ||
init?: number | ResponseInit | ||
|
@@ -39,28 +45,18 @@ export type TypedResponse<T extends unknown = unknown> = Omit< | |
* @see https://remix.run/utils/json | ||
*/ | ||
export const json: JsonFunction = (data, init = {}) => { | ||
let responseInit = typeof init === "number" ? { status: init } : init; | ||
|
||
let headers = new Headers(responseInit.headers); | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/json; charset=utf-8"); | ||
} | ||
|
||
return new Response(JSON.stringify(data), { | ||
...responseInit, | ||
headers, | ||
}); | ||
return routerJson(data, init); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use router methods directly, but preserve typecasting on the returned values |
||
}; | ||
|
||
/** | ||
* This is a shortcut for creating `application/json` responses. Converts `data` | ||
* to JSON and sets the `Content-Type` header. | ||
* This is a shortcut for creating Remix deferred responses | ||
* | ||
* @see https://remix.run/api/remix#json | ||
* @see https://remix.run/docs/utils/defer | ||
*/ | ||
export const defer: DeferFunction = (data, init = {}) => { | ||
let responseInit = typeof init === "number" ? { status: init } : init; | ||
|
||
// TODO: Do we need this or is this copy/paste from json()? | ||
brophdawg11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let headers = new Headers(responseInit.headers); | ||
if (!headers.has("Content-Type")) { | ||
headers.set("Content-Type", "application/json; charset=utf-8"); | ||
|
@@ -72,6 +68,8 @@ export const defer: DeferFunction = (data, init = {}) => { | |
}) as TypedDeferredData<typeof data>; | ||
}; | ||
|
||
// TODO: The RR version is identical minus `TypedResponse` and it just returns | ||
// a `Response`. Should we add that over there? | ||
export type RedirectFunction = ( | ||
url: string, | ||
init?: number | ResponseInit | ||
|
@@ -84,20 +82,7 @@ export type RedirectFunction = ( | |
* @see https://remix.run/utils/redirect | ||
*/ | ||
export const redirect: RedirectFunction = (url, init = 302) => { | ||
let responseInit = init; | ||
if (typeof responseInit === "number") { | ||
responseInit = { status: responseInit }; | ||
} else if (typeof responseInit.status === "undefined") { | ||
responseInit.status = 302; | ||
} | ||
|
||
let headers = new Headers(responseInit.headers); | ||
headers.set("Location", url); | ||
|
||
return new Response(null, { | ||
...responseInit, | ||
headers, | ||
}) as TypedResponse<never>; | ||
return routerRedirect(url, init) as TypedResponse<never>; | ||
}; | ||
|
||
export function isDeferredData(value: any): value is DeferredData { | ||
|
@@ -122,6 +107,7 @@ export function isResponse(value: any): value is Response { | |
); | ||
} | ||
|
||
//TODO(v2): Can we leverage this stuff from RR? | ||
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]); | ||
export function isRedirectStatusCode(statusCode: number): boolean { | ||
return redirectStatusCodes.has(statusCode); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No longer used