@@ -17,6 +17,16 @@ import type { Plugin as SerovalPlugin } from 'seroval'
1717
1818let serovalPlugins : Array < SerovalPlugin < any , any > > | null = null
1919
20+ // caller =>
21+ // serverFnFetcher =>
22+ // client =>
23+ // server =>
24+ // fn =>
25+ // seroval =>
26+ // client middleware =>
27+ // serverFnFetcher =>
28+ // caller
29+
2030export async function serverFnFetcher (
2131 url : string ,
2232 args : Array < any > ,
@@ -37,7 +47,8 @@ export async function serverFnFetcher(
3747
3848 // Arrange the headers
3949 const headers = new Headers ( {
40- 'x-tsr-redirect' : 'manual' ,
50+ 'x-tsr-serverFn' : 'true' ,
51+ 'x-tsr-createServerFn' : 'true' ,
4152 ...( first . headers instanceof Headers
4253 ? Object . fromEntries ( first . headers . entries ( ) )
4354 : first . headers ) ,
@@ -65,12 +76,6 @@ export async function serverFnFetcher(
6576 }
6677 }
6778
68- if ( url . includes ( '?' ) ) {
69- url += `&createServerFn`
70- } else {
71- url += `?createServerFn`
72- }
73-
7479 let body = undefined
7580 if ( first . method === 'POST' ) {
7681 const fetchBody = await getFetchBody ( first )
@@ -97,6 +102,7 @@ export async function serverFnFetcher(
97102 handler ( url , {
98103 method : 'POST' ,
99104 headers : {
105+ 'x-tsr-serverFn' : 'true' ,
100106 Accept : 'application/json' ,
101107 'Content-Type' : 'application/json' ,
102108 } ,
@@ -165,7 +171,7 @@ async function getFetchBody(
165171async function getResponse ( fn : ( ) => Promise < Response > ) {
166172 const response = await ( async ( ) => {
167173 try {
168- return await fn ( )
174+ return await fn ( ) // client => server => fn => server => client
169175 } catch ( error ) {
170176 if ( error instanceof Response ) {
171177 return error
@@ -178,22 +184,16 @@ async function getResponse(fn: () => Promise<Response>) {
178184 if ( response . headers . get ( X_TSS_RAW_RESPONSE ) === 'true' ) {
179185 return response
180186 }
187+
181188 const contentType = response . headers . get ( 'content-type' )
182189 invariant ( contentType , 'expected content-type header to be set' )
183190 const serializedByStart = ! ! response . headers . get ( X_TSS_SERIALIZED )
184- // If the response is not ok, throw an error
185- if ( ! response . ok ) {
186- if ( serializedByStart && contentType . includes ( 'application/json' ) ) {
187- const jsonPayload = await response . json ( )
188- const result = fromCrossJSON ( jsonPayload , { plugins : serovalPlugins ! } )
189- throw result
190- }
191-
192- throw new Error ( await response . text ( ) )
193- }
194191
192+ // If the response is serialized by the start server, we need to process it
193+ // differently than a normal response.
195194 if ( serializedByStart ) {
196195 let result
196+ // If it's a stream from the start serializer, process it as such
197197 if ( contentType . includes ( 'application/x-ndjson' ) ) {
198198 const refs = new Map ( )
199199 result = await processServerFnResponse ( {
@@ -206,17 +206,22 @@ async function getResponse(fn: () => Promise<Response>) {
206206 } ,
207207 } )
208208 }
209+ // If it's a JSON response, it can be simpler
209210 if ( contentType . includes ( 'application/json' ) ) {
210211 const jsonPayload = await response . json ( )
211212 result = fromCrossJSON ( jsonPayload , { plugins : serovalPlugins ! } )
212213 }
214+
213215 invariant ( result , 'expected result to be resolved' )
214216 if ( result instanceof Error ) {
215217 throw result
216218 }
219+
217220 return result
218221 }
219222
223+ // If it wasn't processed by the start serializer, check
224+ // if it's JSON
220225 if ( contentType . includes ( 'application/json' ) ) {
221226 const jsonPayload = await response . json ( )
222227 const redirect = parseRedirect ( jsonPayload )
@@ -229,6 +234,12 @@ async function getResponse(fn: () => Promise<Response>) {
229234 return jsonPayload
230235 }
231236
237+ // Othwerwise, if it's not OK, throw the content
238+ if ( ! response . ok ) {
239+ throw new Error ( await response . text ( ) )
240+ }
241+
242+ // Or return the response itself
232243 return response
233244}
234245
0 commit comments