File tree 3 files changed +40
-7
lines changed
3 files changed +40
-7
lines changed Original file line number Diff line number Diff line change @@ -33,12 +33,14 @@ export const fetcher = async <T extends object>(
33
33
return camelizeKeys ( json ) as T ;
34
34
}
35
35
36
- if ( typeof fetch === 'undefined' ) {
37
- throw new Error ( 'fetch is not defined' ) ;
36
+ if ( typeof globalThis . fetch !== 'function' ) {
37
+ throw new Error (
38
+ 'Looks like there is no global fetch function. Take a look at https://quranjs.com/techniques#custom-fetcher for more info.'
39
+ ) ;
38
40
}
39
41
40
42
// if there is no fetchFn, we use the global fetch
41
- const res = await fetch ( makeUrl ( url , params ) ) ;
43
+ const res = await globalThis . fetch ( makeUrl ( url , params ) ) ;
42
44
43
45
if ( ! res . ok || res . status >= 400 ) {
44
46
throw new Error ( `${ res . status } ${ res . statusText } ` ) ;
@@ -57,12 +59,14 @@ export const mergeApiOptions = (
57
59
options : MergeApiOptionsObject = { } ,
58
60
defaultOptions : Record < string , unknown > = { }
59
61
) => {
62
+ const clonedOptions = { ...options } ;
63
+
60
64
// we can set it to undefined because `makeUrl` will filter it out
61
- if ( options . fetchFn ) options . fetchFn = undefined ;
65
+ if ( clonedOptions . fetchFn ) clonedOptions . fetchFn = undefined ;
62
66
63
67
const final : Record < string , unknown > = {
64
68
...defaultOptions ,
65
- ...options ,
69
+ ...clonedOptions ,
66
70
} ;
67
71
68
72
if ( final . fields ) {
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest' ;
2
+ import fetch from 'cross-fetch' ;
3
+ import { quran } from '../src' ;
4
+
5
+ describe ( 'Custom fetcher' , ( ) => {
6
+ it ( 'should fail with no fetch' , ( ) => {
7
+ // @ts -expect-error - we are testing this
8
+ globalThis . fetch = undefined ;
9
+
10
+ expect ( ( ) => quran . v4 . chapters . findAll ( ) ) . rejects . toThrowError (
11
+ / g l o b a l f e t c h /
12
+ ) ;
13
+ } ) ;
14
+
15
+ it ( 'should not fail if you pass a fetchFn' , ( ) => {
16
+ // @ts -expect-error - we are testing this
17
+ globalThis . fetch = undefined ;
18
+
19
+ expect (
20
+ quran . v4 . chapters . findById ( '1' , {
21
+ fetchFn : ( url ) => {
22
+ return fetch ( url ) . then ( ( res ) => res . json ( ) ) ;
23
+ } ,
24
+ } )
25
+ ) . resolves . not . toThrow ( ) ;
26
+ } ) ;
27
+ } ) ;
Original file line number Diff line number Diff line change 1
1
import fetch from 'cross-fetch' ;
2
- import { beforeAll , afterEach , afterAll } from 'vitest' ;
2
+ import { beforeAll , afterEach , afterAll , beforeEach } from 'vitest' ;
3
3
import { server } from '../mocks/server' ;
4
4
5
5
// Establish API mocking before all tests.
6
6
beforeAll ( async ( ) => {
7
7
server . listen ( ) ;
8
+ } ) ;
8
9
10
+ beforeEach ( async ( ) => {
9
11
// we do this instead of passing a fetchFn every time
10
- global . fetch = fetch ;
12
+ globalThis . fetch = fetch ;
11
13
} ) ;
12
14
13
15
// Reset any request handlers that we may add during the tests,
You can’t perform that action at this time.
0 commit comments