Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit bdf7f4b

Browse files
committed
fix: Allow static headers
1 parent a42b37c commit bdf7f4b

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/FileSystem.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import pathLib from 'path'
1212
import RNFS from 'react-native-fs'
1313
import sha1 from 'crypto-js/sha1'
1414
import URL from 'url-parse'
15-
import { from, Observable, of, ReplaySubject } from 'rxjs'
15+
import { defer, from, Observable, of, ReplaySubject } from 'rxjs'
1616
import {
1717
switchMap,
1818
catchError,
@@ -421,14 +421,15 @@ export class FileSystem {
421421
* @param componentId {String} - Unique id of the requestor.
422422
* @param cacheStrategy {CacheStrategy} - The cache strategy to use, defaults to 'immutable'.
423423
* @param fileName {String} - defaults to a sha1 hash of the url param with extension of same filetype.
424+
* @param headers {HeaderFn} - Headers to include with requests
424425
* @returns {Observable<CacheFileInfo>} observable that resolves to an object that contains the local path of the downloaded file and the filename.
425426
*/
426427
observable(
427428
url: string,
428429
componentId: string,
429430
cacheStrategy: CacheStrategy = 'immutable',
430431
fileName: string | null = null,
431-
headers?: HeaderFn,
432+
headers: HeaderFn = {},
432433
): Observable<CacheFileInfo> {
433434
if (!url) {
434435
return of({
@@ -470,11 +471,7 @@ export class FileSystem {
470471
path: 'file://' + this.baseFilePath + fileName,
471472
fileName,
472473
} as CacheFileInfo),
473-
from(
474-
new Promise<RNFS.Headers>((resolve) =>
475-
resolve(headers === undefined ? {} : headers()),
476-
),
477-
).pipe(
474+
defer(async () => (headers instanceof Function ? await headers() : headers)).pipe(
478475
mergeMap((headers) =>
479476
this.fetchFile(url, fileName, {
480477
'if-modified-since': new Date(stat.mtime).toUTCString(),
@@ -488,9 +485,9 @@ export class FileSystem {
488485
}
489486

490487
// Download
491-
return from(
492-
new Promise<RNFS.Headers>((resolve) => resolve(headers === undefined ? {} : headers())),
493-
).pipe(mergeMap((headers) => this.fetchFile(url, fileName, headers)))
488+
return defer(async () => (headers instanceof Function ? await headers() : headers)).pipe(
489+
mergeMap((headers) => this.fetchFile(url, fileName, headers)),
490+
)
494491
}),
495492
publishReplay(1),
496493
refCount(),

src/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ const imageCacheHoc = <P extends object>(
8080
if (options.defaultPlaceholder && typeof options.defaultPlaceholder !== 'object') {
8181
throw new Error('defaultPlaceholder option must be a ReactNode')
8282
}
83+
if (
84+
options.headers &&
85+
!(typeof options.headers === 'function' || typeof options.headers === 'object')
86+
) {
87+
throw new Error('headers option must be a function or object')
88+
}
8389

8490
return class extends React.PureComponent<
8591
P & ReactNativeImageCacheHocProps,
@@ -176,7 +182,7 @@ const imageCacheHoc = <P extends object>(
176182
cachePruneTriggerLimit: options.cachePruneTriggerLimit || 1024 * 1024 * 15, // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
177183
fileDirName: options.fileDirName || null, // Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
178184
defaultPlaceholder: options.defaultPlaceholder || null, // Default placeholder component to render while remote image file is downloading. Can be overridden with placeholder prop. Defaults to <Image> component with style prop passed through.
179-
headers: options.headers ?? (() => ({})),
185+
headers: options.headers ?? {},
180186
}
181187

182188
// Init file system lib

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import RNFS from 'react-native-fs'
22

3-
export type HeaderFn = () => RNFS.Headers | Promise<RNFS.Headers>
3+
export type HeaderFn = RNFS.Headers | (() => RNFS.Headers | Promise<RNFS.Headers>)

tests/CacheableImage.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ describe('CacheableImage', function () {
5252
}),
5353
).toThrow('defaultPlaceholder option must be a ReactNode')
5454

55+
expect(() =>
56+
imageCacheHoc(Image, {
57+
headers: 1 as any,
58+
}),
59+
).toThrow('headers option must be a function or object')
60+
5561
const validOptions: ReactNativeImageCacheHocOptions = {
5662
validProtocols: ['http', 'https'],
5763
fileHostWhitelist: ['i.redd.it', 'localhost'],
5864
cachePruneTriggerLimit: 1024 * 1024 * 10,
5965
fileDirName: 'test-dir',
6066
defaultPlaceholder: <Text>Default Placeholder</Text>,
67+
headers: { foo: 'bar' },
6168
}
6269

6370
// Valid options shouldn't throw an error

0 commit comments

Comments
 (0)