|
| 1 | +import deepEqual = require('deep-equal'); |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +type CreateUseFetch = (fetch: GlobalFetch['fetch']) => UseFetch; |
| 6 | + |
| 7 | +interface Export extends UseFetch { |
| 8 | + createUseFetch: CreateUseFetch; |
| 9 | + default: UseFetch; |
| 10 | +} |
| 11 | + |
| 12 | +interface FetchCache { |
| 13 | + fetch?: Promise<void>; |
| 14 | + error?: any; |
| 15 | + init: RequestInit | undefined; |
| 16 | + input: RequestInfo; |
| 17 | + response?: any; |
| 18 | +} |
| 19 | + |
| 20 | +type UseFetch = ( |
| 21 | + input: RequestInfo, |
| 22 | + init?: RequestInit | undefined, |
| 23 | + lifespan?: number, |
| 24 | +) => Object | string; |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +const createUseFetch: CreateUseFetch = ( |
| 29 | + fetch: GlobalFetch['fetch'], |
| 30 | +): UseFetch => { |
| 31 | + |
| 32 | + // Create a set of caches for this hook. |
| 33 | + const caches: FetchCache[] = []; |
| 34 | + |
| 35 | + return ( |
| 36 | + input: RequestInfo, |
| 37 | + init?: RequestInit | undefined, |
| 38 | + lifespan: number = 0, |
| 39 | + ): Object | string => { |
| 40 | + |
| 41 | + // Check each cache by this useFetch hook. |
| 42 | + for (const cache of caches) { |
| 43 | + |
| 44 | + // If this cache matches the request, |
| 45 | + if ( |
| 46 | + deepEqual(input, cache.input) && |
| 47 | + deepEqual(init, cache.init) |
| 48 | + ) { |
| 49 | + |
| 50 | + // If an error occurred, throw it so that componentDidCatch can handle |
| 51 | + // it. |
| 52 | + if (Object.prototype.hasOwnProperty.call(cache, 'error')) { |
| 53 | + throw cache.error; |
| 54 | + } |
| 55 | + |
| 56 | + // If a response was successful, return it. |
| 57 | + if (Object.prototype.hasOwnProperty.call(cache, 'response')) { |
| 58 | + return cache.response; |
| 59 | + } |
| 60 | + |
| 61 | + // If we are still waiting, throw the Promise so that Suspense can |
| 62 | + // fallback. |
| 63 | + throw cache.fetch; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // If no request in the cache matched this one, create a new cache entry. |
| 68 | + const cache: FetchCache = { |
| 69 | + |
| 70 | + // Make the fetch request. |
| 71 | + fetch: fetch(input, init) |
| 72 | + |
| 73 | + // Parse the response. |
| 74 | + .then((response: Response): Promise<Object | string> => { |
| 75 | + const contentType: null | string = |
| 76 | + response.headers.get('Content-Type'); |
| 77 | + if ( |
| 78 | + contentType && |
| 79 | + contentType.indexOf('application/json') !== -1 |
| 80 | + ) { |
| 81 | + return response.json(); |
| 82 | + } |
| 83 | + return response.text(); |
| 84 | + }) |
| 85 | + |
| 86 | + // Cache the response. |
| 87 | + .then((response: Object | string): void => { |
| 88 | + cache.response = response; |
| 89 | + }) |
| 90 | + |
| 91 | + // Handle an error. |
| 92 | + .catch((e: Error): void => { |
| 93 | + cache.error = e; |
| 94 | + }) |
| 95 | + |
| 96 | + // Invalidate the cache. |
| 97 | + .then((): void => { |
| 98 | + if (lifespan > 0) { |
| 99 | + setTimeout( |
| 100 | + (): void => { |
| 101 | + const index: number = caches.indexOf(cache); |
| 102 | + if (index !== -1) { |
| 103 | + caches.splice(index, 1); |
| 104 | + } |
| 105 | + }, |
| 106 | + lifespan, |
| 107 | + ); |
| 108 | + } |
| 109 | + }), |
| 110 | + init, |
| 111 | + input, |
| 112 | + }; |
| 113 | + caches.push(cache); |
| 114 | + throw cache.fetch; |
| 115 | + }; |
| 116 | +}; |
| 117 | + |
| 118 | +const _export: Export = Object.assign( |
| 119 | + createUseFetch(window.fetch), { |
| 120 | + createUseFetch, |
| 121 | + default: createUseFetch(window.fetch) |
| 122 | +}); |
| 123 | + |
| 124 | +export = _export; |
0 commit comments