11
11
* @module @ember -data/request/fetch
12
12
* @main @ember -data/request/fetch
13
13
*/
14
+ import { getOwnConfig , macroCondition } from '@embroider/macros' ;
15
+
14
16
import { cloneResponseProperties , type Context } from './-private/context' ;
15
17
import type { HttpErrorProps } from './-private/utils' ;
16
18
19
+ // Lazily close over fetch to avoid breaking Mirage
17
20
const _fetch : typeof fetch =
18
21
typeof fetch !== 'undefined'
19
- ? fetch
22
+ ? ( ... args ) => fetch ( ... args )
20
23
: typeof FastBoot !== 'undefined'
21
- ? ( FastBoot . require ( 'node-fetch' ) as typeof fetch )
24
+ ? ( ... args ) => ( FastBoot . require ( 'node-fetch' ) as typeof fetch ) ( ... args )
22
25
: ( ( ( ) => {
23
26
throw new Error ( 'No Fetch Implementation Found' ) ;
24
27
} ) as typeof fetch ) ;
@@ -30,6 +33,12 @@ function cloneResponse(response: Response, overrides: Partial<Response>) {
30
33
return new Response ( response . body , Object . assign ( props , overrides ) ) ;
31
34
}
32
35
36
+ let IS_MAYBE_MIRAGE = ( ) => false ;
37
+ if ( macroCondition ( getOwnConfig < { env : { TESTING : boolean } } > ( ) . env . TESTING ) ) {
38
+ IS_MAYBE_MIRAGE = ( ) =>
39
+ Boolean ( typeof window !== 'undefined' && ( window as { server ?: { pretender : unknown } } ) . server ?. pretender ) ;
40
+ }
41
+
33
42
const MUTATION_OPS = new Set ( [ 'updateRecord' , 'createRecord' , 'deleteRecord' ] ) ;
34
43
const ERROR_STATUS_CODE_FOR = new Map ( [
35
44
[ 400 , 'Bad Request' ] ,
@@ -115,9 +124,15 @@ const Fetch = {
115
124
const isMutationOp = Boolean ( op && MUTATION_OPS . has ( op ) ) ;
116
125
117
126
if ( ! isError && ! isMutationOp && response . status !== 204 && ! response . headers . has ( 'date' ) ) {
118
- const headers = new Headers ( response . headers ) ;
119
- headers . set ( 'date' , new Date ( ) . toUTCString ( ) ) ;
120
- response = cloneResponse ( response , { headers } ) ;
127
+ if ( IS_MAYBE_MIRAGE ( ) ) {
128
+ response . headers . set ( 'date' , new Date ( ) . toUTCString ( ) ) ;
129
+ } else {
130
+ const headers = new Headers ( response . headers ) ;
131
+ headers . set ( 'date' , new Date ( ) . toUTCString ( ) ) ;
132
+ response = cloneResponse ( response , {
133
+ headers,
134
+ } ) ;
135
+ }
121
136
}
122
137
123
138
context . setResponse ( response ) ;
0 commit comments