1 | 'use strict';
|
2 |
|
3 | const isObject = require('isobject/index.cjs.js');
|
4 | const { useCallback } = require('react');
|
5 | const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
|
6 | const LoadingCacheValue = require('./LoadingCacheValue');
|
7 | const fetchGraphQL = require('./fetchGraphQL');
|
8 | const useCache = require('./useCache');
|
9 | const useLoading = require('./useLoading');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | module.exports = function useLoadGraphQL() {
|
34 | const cache = useCache();
|
35 | const loading = useLoading();
|
36 |
|
37 | return useCallback(
|
38 | (cacheKey, fetchUri, fetchOptions) => {
|
39 | if (typeof cacheKey !== 'string')
|
40 | throw new TypeError(
|
41 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
42 | ? 'Argument 1 `cacheKey` must be a string.'
|
43 | : createArgErrorMessageProd(1)
|
44 | );
|
45 |
|
46 | if (typeof fetchUri !== 'string')
|
47 | throw new TypeError(
|
48 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
49 | ? 'Argument 2 `fetchUri` must be a string.'
|
50 | : createArgErrorMessageProd(2)
|
51 | );
|
52 |
|
53 | if (!isObject(fetchOptions))
|
54 | throw new TypeError(
|
55 | typeof process === 'object' && process.env.NODE_ENV !== 'production'
|
56 | ? 'Argument 3 `fetchOptions` must be an object.'
|
57 | : createArgErrorMessageProd(3)
|
58 | );
|
59 |
|
60 |
|
61 | const { signal, ...modifiedFetchOptions } = fetchOptions;
|
62 | const abortController = new AbortController();
|
63 |
|
64 |
|
65 | if (signal)
|
66 | signal.aborted
|
67 | ?
|
68 | abortController.abort()
|
69 | :
|
70 |
|
71 | signal.addEventListener(
|
72 | 'abort',
|
73 | () => {
|
74 | abortController.abort();
|
75 | },
|
76 | {
|
77 |
|
78 |
|
79 | once: true,
|
80 | }
|
81 | );
|
82 |
|
83 | modifiedFetchOptions.signal = abortController.signal;
|
84 |
|
85 | return new LoadingCacheValue(
|
86 | loading,
|
87 | cache,
|
88 | cacheKey,
|
89 | fetchGraphQL(fetchUri, modifiedFetchOptions),
|
90 | abortController
|
91 | );
|
92 | },
|
93 | [cache, loading]
|
94 | );
|
95 | };
|