1- import type { Base64Encoder , EnvironmentOptions , Factory } from './environment.js'
1+ import type { Base64Encoder , EnvironmentOptions , RequestContext , RequestContextFactory } from './environment.js'
22
33import * as HEADERS from '../headers.js'
44
@@ -17,28 +17,24 @@ const serializeResourceHeaders = Symbol('serializeResourceHeaders')
1717
1818export class NetlifyCache implements Cache {
1919 #base64Encode: Base64Encoder
20- #getHost?: Factory < string >
21- #getToken: Factory < string >
22- #getURL: Factory < string >
20+ #getContext: RequestContextFactory
2321 #name: string
2422 #userAgent?: string
2523
26- constructor ( { base64Encode, getHost , getToken , getURL , name, userAgent } : NetlifyCacheOptions ) {
24+ constructor ( { base64Encode, getContext , name, userAgent } : NetlifyCacheOptions ) {
2725 this . #base64Encode = base64Encode
28- this . #getHost = getHost
29- this . #getToken = getToken
30- this . #getURL = getURL
26+ this . #getContext = getContext
3127 this . #name = name
3228 this . #userAgent = userAgent
3329 }
3430
35- private [ getInternalHeaders ] ( ) {
31+ private [ getInternalHeaders ] ( requestContext : RequestContext ) {
32+ const { host, token } = requestContext
3633 const headers : Record < string , string > = {
37- Authorization : `Bearer ${ this . #getToken ( ) } ` ,
34+ Authorization : `Bearer ${ token } ` ,
3835 [ HEADERS . ResourceStore ] : this . #name,
3936 }
4037
41- const host = this . #getHost?.( )
4238 if ( host ) {
4339 headers [ HEADERS . NetlifyForwardedHost ] = host
4440 }
@@ -82,10 +78,11 @@ export class NetlifyCache implements Cache {
8278
8379 // eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
8480 async delete ( request : RequestInfo ) {
81+ const context = this . #getContext( )
8582 const resourceURL = extractAndValidateURL ( request )
8683
87- await fetch ( `${ this . #getURL ( ) } /${ toCacheKey ( resourceURL ) } ` , {
88- headers : this [ getInternalHeaders ] ( ) ,
84+ await fetch ( `${ context . url } /${ toCacheKey ( resourceURL ) } ` , {
85+ headers : this [ getInternalHeaders ] ( context ) ,
8986 method : 'DELETE' ,
9087 } )
9188
@@ -100,10 +97,11 @@ export class NetlifyCache implements Cache {
10097
10198 async match ( request : RequestInfo ) {
10299 try {
100+ const context = this . #getContext( )
103101 const resourceURL = extractAndValidateURL ( request )
104- const cacheURL = `${ this . #getURL ( ) } /${ toCacheKey ( resourceURL ) } `
102+ const cacheURL = `${ context . url } /${ toCacheKey ( resourceURL ) } `
105103 const response = await fetch ( cacheURL , {
106- headers : this [ getInternalHeaders ] ( ) ,
104+ headers : this [ getInternalHeaders ] ( context ) ,
107105 method : 'GET' ,
108106 } )
109107
@@ -145,12 +143,13 @@ export class NetlifyCache implements Cache {
145143 throw new TypeError ( "Cannot cache response with 'Vary: *' header." )
146144 }
147145
146+ const context = this . #getContext( )
148147 const resourceURL = extractAndValidateURL ( request )
149148
150- await fetch ( `${ this . #getURL ( ) } /${ toCacheKey ( resourceURL ) } ` , {
149+ await fetch ( `${ context . url } /${ toCacheKey ( resourceURL ) } ` , {
151150 body : response . body ,
152151 headers : {
153- ...this [ getInternalHeaders ] ( ) ,
152+ ...this [ getInternalHeaders ] ( context ) ,
154153 [ HEADERS . ResourceHeaders ] : this [ serializeResourceHeaders ] ( response . headers ) ,
155154 [ HEADERS . ResourceStatus ] : response . status . toString ( ) ,
156155 } ,
0 commit comments