@@ -135,13 +135,15 @@ class ImageProxy {
135135 private cacheVersion ;
136136 private key ;
137137 private baseUrl ;
138+ private headers : HeadersInit | null = null ;
138139
139140 constructor (
140141 key : string ,
141142 baseUrl : string ,
142143 options : {
143144 cacheVersion ?: number ;
144145 rateLimitOptions ?: RateLimitOptions ;
146+ headers ?: HeadersInit ;
145147 } = { }
146148 ) {
147149 this . cacheVersion = options . cacheVersion ?? 1 ;
@@ -155,9 +157,13 @@ class ImageProxy {
155157 } else {
156158 this . fetch = fetch ;
157159 }
160+ this . headers = options . headers || null ;
158161 }
159162
160- public async getImage ( path : string ) : Promise < ImageResponse > {
163+ public async getImage (
164+ path : string ,
165+ fallbackPath ?: string
166+ ) : Promise < ImageResponse > {
161167 const cacheKey = this . getCacheKey ( path ) ;
162168
163169 const imageResponse = await this . get ( cacheKey ) ;
@@ -166,7 +172,11 @@ class ImageProxy {
166172 const newImage = await this . set ( path , cacheKey ) ;
167173
168174 if ( ! newImage ) {
169- throw new Error ( 'Failed to load image' ) ;
175+ if ( fallbackPath ) {
176+ return await this . getImage ( fallbackPath ) ;
177+ } else {
178+ throw new Error ( 'Failed to load image' ) ;
179+ }
170180 }
171181
172182 return newImage ;
@@ -247,7 +257,12 @@ class ImageProxy {
247257 : '/'
248258 : '' ) +
249259 ( path . startsWith ( '/' ) ? path . slice ( 1 ) : path ) ;
250- const response = await this . fetch ( href ) ;
260+ const response = await this . fetch ( href , {
261+ headers : this . headers || undefined ,
262+ } ) ;
263+ if ( ! response . ok ) {
264+ return null ;
265+ }
251266 const arrayBuffer = await response . arrayBuffer ( ) ;
252267 const buffer = Buffer . from ( arrayBuffer ) ;
253268
0 commit comments