@@ -14,6 +14,9 @@ class FetchResponse {
14
14
get unauthenticated ( ) {
15
15
return this . statusCode === 401 ;
16
16
}
17
+ get unprocessableEntity ( ) {
18
+ return this . statusCode === 422 ;
19
+ }
17
20
get authenticationURL ( ) {
18
21
return this . response . headers . get ( "WWW-Authenticate" ) ;
19
22
}
@@ -31,7 +34,7 @@ class FetchResponse {
31
34
return Promise . reject ( new Error ( `Expected an HTML response but got "${ this . contentType } " instead` ) ) ;
32
35
}
33
36
get json ( ) {
34
- if ( this . contentType . match ( / ^ a p p l i c a t i o n \/ j s o n / ) ) {
37
+ if ( this . contentType . match ( / ^ a p p l i c a t i o n \/ . * j s o n $ / ) ) {
35
38
return this . responseJson || ( this . responseJson = this . response . json ( ) ) ;
36
39
}
37
40
return Promise . reject ( new Error ( `Expected a JSON response but got "${ this . contentType } " instead` ) ) ;
@@ -45,7 +48,7 @@ class FetchResponse {
45
48
async renderTurboStream ( ) {
46
49
if ( this . isTurboStream ) {
47
50
if ( window . Turbo ) {
48
- window . Turbo . renderStreamMessage ( await this . text ) ;
51
+ await window . Turbo . renderStreamMessage ( await this . text ) ;
49
52
} else {
50
53
console . warn ( "You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js" ) ;
51
54
}
@@ -115,7 +118,7 @@ class FetchRequest {
115
118
constructor ( method , url , options = { } ) {
116
119
this . method = method ;
117
120
this . options = options ;
118
- this . originalUrl = url ;
121
+ this . originalUrl = url . toString ( ) ;
119
122
}
120
123
async perform ( ) {
121
124
try {
@@ -131,7 +134,7 @@ class FetchRequest {
131
134
return Promise . reject ( window . location . href = response . authenticationURL ) ;
132
135
}
133
136
if ( response . ok && response . isTurboStream ) {
134
- response . renderTurboStream ( ) ;
137
+ await response . renderTurboStream ( ) ;
135
138
}
136
139
return response ;
137
140
}
@@ -180,7 +183,7 @@ class FetchRequest {
180
183
return "text/vnd.turbo-stream.html, text/html, application/xhtml+xml" ;
181
184
182
185
case "json" :
183
- return "application/json" ;
186
+ return "application/json, application/vnd.api+json " ;
184
187
185
188
default :
186
189
return "*/*" ;
@@ -205,7 +208,7 @@ class FetchRequest {
205
208
return query . length > 0 ? `?${ query } ` : "" ;
206
209
}
207
210
get url ( ) {
208
- return this . originalUrl . split ( "?" ) [ 0 ] + this . query ;
211
+ return this . originalUrl . split ( "?" ) [ 0 ] . split ( "#" ) [ 0 ] + this . query ;
209
212
}
210
213
get responseKind ( ) {
211
214
return this . options . responseKind || "html" ;
0 commit comments