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

Commit 5cc89af

Browse files
authored
feat: allow headers
Custom headers can be passed to the hoc
2 parents 80a0ea9 + 00b90e5 commit 5cc89af

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/FileSystem.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from 'rxjs/operators'
2929
import uuid from 'react-native-uuid'
3030
import { CacheStrategy } from '.'
31+
import { HeaderFn } from './types'
3132

3233
export interface CacheFileInfo {
3334
path: string | null
@@ -289,7 +290,7 @@ export class FileSystem {
289290
fetchFile(
290291
url: string,
291292
fileName: string | null = null,
292-
headers?: { [key: string]: string },
293+
headers?: RNFS.Headers,
293294
): Observable<CacheFileInfo> {
294295
fileName = fileName || this.getFileNameFromUrl(url)
295296
const path = this.baseFilePath + fileName
@@ -427,6 +428,7 @@ export class FileSystem {
427428
componentId: string,
428429
cacheStrategy: CacheStrategy = 'immutable',
429430
fileName: string | null = null,
431+
headers?: HeaderFn,
430432
): Observable<CacheFileInfo> {
431433
if (!url) {
432434
return of({
@@ -468,16 +470,27 @@ export class FileSystem {
468470
path: 'file://' + this.baseFilePath + fileName,
469471
fileName,
470472
} as CacheFileInfo),
471-
this.fetchFile(url, fileName, {
472-
'if-modified-since': new Date(stat.mtime).toUTCString(),
473-
}),
473+
from(
474+
new Promise<RNFS.Headers>((resolve) =>
475+
resolve(headers === undefined ? {} : headers()),
476+
),
477+
).pipe(
478+
mergeMap((headers) =>
479+
this.fetchFile(url, fileName, {
480+
'if-modified-since': new Date(stat.mtime).toUTCString(),
481+
...headers,
482+
}),
483+
),
484+
),
474485
]).pipe(concatAll())
475486
}
476487
}
477488
}
478489

479490
// Download
480-
return this.fetchFile(url, fileName)
491+
return from(
492+
new Promise<RNFS.Headers>((resolve) => resolve(headers === undefined ? {} : headers())),
493+
).pipe(mergeMap((headers) => this.fetchFile(url, fileName, headers)))
481494
}),
482495
publishReplay(1),
483496
refCount(),

src/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Image, ImageStyle, Platform, StyleProp } from 'react-native'
2323
import { BehaviorSubject, Subscription } from 'rxjs'
2424
import { skip, takeUntil } from 'rxjs/operators'
2525
import URL from 'url-parse'
26+
import { HeaderFn } from './types'
2627

2728
export type CacheStrategy = 'immutable' | 'mutable'
2829

@@ -56,6 +57,7 @@ export interface ReactNativeImageCacheHocOptions {
5657
cachePruneTriggerLimit?: number // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
5758
fileDirName?: string | null // Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
5859
defaultPlaceholder?: ReactNode | null
60+
headers?: HeaderFn
5961
}
6062

6163
const imageCacheHoc = <P extends object>(
@@ -174,6 +176,7 @@ const imageCacheHoc = <P extends object>(
174176
cachePruneTriggerLimit: options.cachePruneTriggerLimit || 1024 * 1024 * 15, // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
175177
fileDirName: options.fileDirName || null, // Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
176178
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 ?? (() => ({})),
177180
}
178181

179182
// Init file system lib
@@ -236,7 +239,7 @@ const imageCacheHoc = <P extends object>(
236239

237240
// Init the image cache logic
238241
this.subscription = this.fileSystem
239-
.observable(url, this.componentId, cacheStrategy)
242+
.observable(url, this.componentId, cacheStrategy, null, this.options.headers)
240243
.pipe(takeUntil(this.unmounted$.pipe(skip(1))))
241244
.subscribe((info) => this.onSourceLoaded(info))
242245
}
@@ -281,7 +284,7 @@ const imageCacheHoc = <P extends object>(
281284
FileSystem.lockCacheFile(nextFileName, this.componentId)
282285

283286
this.subscription = this.fileSystem
284-
.observable(nextUrl, this.componentId, cacheStrategy)
287+
.observable(nextUrl, this.componentId, cacheStrategy, null, this.options.headers)
285288
.pipe(takeUntil(this.unmounted$.pipe(skip(1))))
286289
.subscribe((info) => this.onSourceLoaded(info))
287290
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import RNFS from 'react-native-fs'
2+
3+
export type HeaderFn = () => RNFS.Headers | Promise<RNFS.Headers>

0 commit comments

Comments
 (0)