11import type { FormMethod as FormMethodRR } from "react-router-dom" ;
22
3- import invariant from "./invariant" ;
4- import type { Submission } from "./transition" ;
5-
63export type AppData = any ;
74
85export type FormMethod = FormMethodRR ;
@@ -33,17 +30,28 @@ export function isRedirectResponse(response: any): boolean {
3330}
3431
3532export async function fetchData (
36- url : URL ,
33+ request : Request ,
3734 routeId : string ,
38- signal : AbortSignal ,
39- submission ?: Submission
35+ isAction : boolean
4036) : Promise < Response | Error > {
37+ let url = new URL ( request . url ) ;
4138 url . searchParams . set ( "_data" , routeId ) ;
4239
43- let init : RequestInit = submission
44- ? getActionInit ( submission , signal )
45- : { credentials : "same-origin" , signal } ;
40+ let init : RequestInit | undefined ;
41+
42+ // TODO: There's a bug in @remix-run/router here at the moment where the
43+ // loader Request keeps method POST after a submission. Matt has a local
44+ // fix but this does the trick for now. Once the fix is merged to the
45+ // router, we can remove the isAction param and use the method here
46+ // if (request.method !== "GET") {
47+ if ( isAction ) {
48+ init = {
49+ method : request . method ,
50+ body : await request . formData ( ) ,
51+ } ;
52+ }
4653
54+ // TODO: Dropped credentials:"same-origin" since it's the default
4755 let response = await fetch ( url . href , init ) ;
4856
4957 if ( isErrorResponse ( response ) ) {
@@ -53,47 +61,7 @@ export async function fetchData(
5361 return error ;
5462 }
5563
64+ // TODO: Confirm difference between regex extractData JSON detection versus
65+ // @remix -run/router detection
5666 return response ;
5767}
58-
59- export async function extractData ( response : Response ) : Promise < AppData > {
60- // This same algorithm is used on the server to interpret load
61- // results when we render the HTML page.
62- let contentType = response . headers . get ( "Content-Type" ) ;
63-
64- if ( contentType && / \b a p p l i c a t i o n \/ j s o n \b / . test ( contentType ) ) {
65- return response . json ( ) ;
66- }
67-
68- return response . text ( ) ;
69- }
70-
71- function getActionInit (
72- submission : Submission ,
73- signal : AbortSignal
74- ) : RequestInit {
75- let { encType, method, formData } = submission ;
76-
77- let headers = undefined ;
78- let body = formData ;
79-
80- if ( encType === "application/x-www-form-urlencoded" ) {
81- body = new URLSearchParams ( ) ;
82- for ( let [ key , value ] of formData ) {
83- invariant (
84- typeof value === "string" ,
85- `File inputs are not supported with encType "application/x-www-form-urlencoded", please use "multipart/form-data" instead.`
86- ) ;
87- body . append ( key , value ) ;
88- }
89- headers = { "Content-Type" : encType } ;
90- }
91-
92- return {
93- method,
94- body,
95- signal,
96- credentials : "same-origin" ,
97- headers,
98- } ;
99- }
0 commit comments