@@ -14,6 +14,9 @@ class FetchResponse {
1414 get unauthenticated ( ) {
1515 return this . statusCode === 401 ;
1616 }
17+ get unprocessableEntity ( ) {
18+ return this . statusCode === 422 ;
19+ }
1720 get authenticationURL ( ) {
1821 return this . response . headers . get ( "WWW-Authenticate" ) ;
1922 }
@@ -31,7 +34,7 @@ class FetchResponse {
3134 return Promise . reject ( new Error ( `Expected an HTML response but got "${ this . contentType } " instead` ) ) ;
3235 }
3336 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 $ / ) ) {
3538 return this . responseJson || ( this . responseJson = this . response . json ( ) ) ;
3639 }
3740 return Promise . reject ( new Error ( `Expected a JSON response but got "${ this . contentType } " instead` ) ) ;
@@ -45,7 +48,7 @@ class FetchResponse {
4548 async renderTurboStream ( ) {
4649 if ( this . isTurboStream ) {
4750 if ( window . Turbo ) {
48- window . Turbo . renderStreamMessage ( await this . text ) ;
51+ await window . Turbo . renderStreamMessage ( await this . text ) ;
4952 } else {
5053 console . warn ( "You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js" ) ;
5154 }
@@ -115,7 +118,7 @@ class FetchRequest {
115118 constructor ( method , url , options = { } ) {
116119 this . method = method ;
117120 this . options = options ;
118- this . originalUrl = url ;
121+ this . originalUrl = url . toString ( ) ;
119122 }
120123 async perform ( ) {
121124 try {
@@ -131,7 +134,7 @@ class FetchRequest {
131134 return Promise . reject ( window . location . href = response . authenticationURL ) ;
132135 }
133136 if ( response . ok && response . isTurboStream ) {
134- response . renderTurboStream ( ) ;
137+ await response . renderTurboStream ( ) ;
135138 }
136139 return response ;
137140 }
@@ -180,7 +183,7 @@ class FetchRequest {
180183 return "text/vnd.turbo-stream.html, text/html, application/xhtml+xml" ;
181184
182185 case "json" :
183- return "application/json" ;
186+ return "application/json, application/vnd.api+json " ;
184187
185188 default :
186189 return "*/*" ;
@@ -205,7 +208,7 @@ class FetchRequest {
205208 return query . length > 0 ? `?${ query } ` : "" ;
206209 }
207210 get url ( ) {
208- return this . originalUrl . split ( "?" ) [ 0 ] + this . query ;
211+ return this . originalUrl . split ( "?" ) [ 0 ] . split ( "#" ) [ 0 ] + this . query ;
209212 }
210213 get responseKind ( ) {
211214 return this . options . responseKind || "html" ;
0 commit comments