11export = CachePolicy ;
22
33declare class CachePolicy {
4- constructor ( req : CachePolicy . Request , res : CachePolicy . Response , options ?: CachePolicy . Options ) ;
4+ constructor ( req : CachePolicy . HttpRequest , res : CachePolicy . HttpResponse , options ?: CachePolicy . Options ) ;
55
66 /**
77 * Returns `true` if the response can be stored in a cache.
@@ -20,7 +20,28 @@ declare class CachePolicy {
2020 * If it returns `false`, then the response may not be matching at all (e.g. it's for a different URL or method),
2121 * or may require to be refreshed first (see `revalidationHeaders()`).
2222 */
23- satisfiesWithoutRevalidation ( newRequest : CachePolicy . Request ) : boolean ;
23+ satisfiesWithoutRevalidation ( newRequest : CachePolicy . HttpRequest ) : boolean ;
24+
25+ /**
26+ * Checks if the given request matches this cache entry, and how the cache can be used to satisfy it. Returns an object with:
27+ *
28+ * ```
29+ * {
30+ * // If defined, you must send a request to the server.
31+ * revalidation: {
32+ * headers: {}, // HTTP headers to use when sending the revalidation response
33+ * // If true, you MUST wait for a response from the server before using the cache
34+ * // If false, this is stale-while-revalidate. The cache is stale, but you can use it while you update it asynchronously.
35+ * synchronous: bool,
36+ * },
37+ * // If defined, you can use this cached response.
38+ * response: {
39+ * headers: {}, // Updated cached HTTP headers you must use when responding to the client
40+ * },
41+ * }
42+ * ```
43+ */
44+ evaluateRequest ( newRequest : CachePolicy . HttpRequest ) : CachePolicy . EvaluateRequestResult ;
2445
2546 /**
2647 * Returns updated, filtered set of response headers to return to clients receiving the cached response.
@@ -32,6 +53,28 @@ declare class CachePolicy {
3253 */
3354 responseHeaders ( ) : CachePolicy . Headers ;
3455
56+ /**
57+ * Returns the Date header value from the response or the current time if invalid.
58+ */
59+ date ( ) : number ;
60+
61+ /**
62+ * Value of the Age header, in seconds, updated for the current time.
63+ * May be fractional.
64+ */
65+ age ( ) : number ;
66+
67+ /**
68+ * Possibly outdated value of applicable max-age (or heuristic equivalent) in seconds.
69+ * This counts since response's `Date`.
70+ *
71+ * For an up-to-date value, see `timeToLive()`.
72+ *
73+ * Returns the maximum age (freshness lifetime) of the response in seconds.
74+ * @returns {number } The max-age value in seconds.
75+ */
76+ maxAge ( ) : number ;
77+
3578 /**
3679 * Returns approximate time in milliseconds until the response becomes stale (i.e. not fresh).
3780 *
@@ -42,16 +85,22 @@ declare class CachePolicy {
4285 timeToLive ( ) : number ;
4386
4487 /**
45- * Chances are you'll want to store the `CachePolicy` object along with the cached response .
46- * `obj = policy.toObject()` gives a plain JSON-serializable object .
88+ * If true, this cache entry is past its expiration date .
89+ * Note that stale cache may be useful sometimes, see `evaluateRequest()` .
4790 */
48- toObject ( ) : CachePolicy . CachePolicyObject ;
91+ stale ( ) : boolean ;
4992
5093 /**
5194 * `policy = CachePolicy.fromObject(obj)` creates an instance from object created by `toObject()`.
5295 */
5396 static fromObject ( obj : CachePolicy . CachePolicyObject ) : CachePolicy ;
5497
98+ /**
99+ * Chances are you'll want to store the `CachePolicy` object along with the cached response.
100+ * `obj = policy.toObject()` gives a plain JSON-serializable object.
101+ */
102+ toObject ( ) : CachePolicy . CachePolicyObject ;
103+
55104 /**
56105 * Returns updated, filtered set of request headers to send to the origin server to check if the cached
57106 * response can be reused. These headers allow the origin server to return status 304 indicating the
@@ -62,29 +111,37 @@ declare class CachePolicy {
62111 * @example
63112 * updateRequest.headers = cachePolicy.revalidationHeaders(updateRequest);
64113 */
65- revalidationHeaders ( newRequest : CachePolicy . Request ) : CachePolicy . Headers ;
114+ revalidationHeaders ( newRequest : CachePolicy . HttpRequest ) : CachePolicy . Headers ;
66115
67116 /**
68- * Use this method to update the cache after receiving a new response from the origin server.
117+ * Creates new CachePolicy with information combined from the previews response,
118+ * and the new revalidation response.
119+ *
120+ * Returns {policy, modified} where modified is a boolean indicating
121+ * whether the response body has been modified, and old cached body can't be used.
69122 */
70123 revalidatedPolicy (
71- revalidationRequest : CachePolicy . Request ,
72- revalidationResponse : CachePolicy . Response ,
124+ revalidationRequest : CachePolicy . HttpRequest ,
125+ revalidationResponse ? : CachePolicy . HttpResponse ,
73126 ) : CachePolicy . RevalidationPolicy ;
74127}
75128
76129declare namespace CachePolicy {
77- interface Request {
130+ interface HttpRequest {
78131 url ?: string | undefined ;
79132 method ?: string | undefined ;
80133 headers : Headers ;
81134 }
82135
83- interface Response {
136+ type Request = HttpRequest ;
137+
138+ interface HttpResponse {
84139 status ?: number | undefined ;
85140 headers : Headers ;
86141 }
87142
143+ type Response = HttpResponse ;
144+
88145 interface Options {
89146 /**
90147 * If `true`, then the response is evaluated from a perspective of a shared cache (i.e. `private` is not
@@ -162,4 +219,33 @@ declare namespace CachePolicy {
162219 modified : boolean ;
163220 matches : boolean ;
164221 }
222+
223+ interface EvaluateRequestRevalidation {
224+ synchronous : boolean ;
225+ headers : CachePolicy . Headers ;
226+ }
227+
228+ interface EvaluateRequestHitWithoutRevalidationResult {
229+ response : {
230+ headers : CachePolicy . Headers ;
231+ } ;
232+ revalidation : undefined ;
233+ }
234+
235+ interface EvaluateRequestHitWithRevalidationResult {
236+ response : {
237+ headers : CachePolicy . Headers ;
238+ } ;
239+ revalidation : EvaluateRequestRevalidation ;
240+ }
241+
242+ interface EvaluateRequestMissResult {
243+ response : undefined ;
244+ revalidation : EvaluateRequestRevalidation ;
245+ }
246+
247+ type EvaluateRequestResult =
248+ | EvaluateRequestHitWithRevalidationResult
249+ | EvaluateRequestHitWithoutRevalidationResult
250+ | EvaluateRequestMissResult ;
165251}
0 commit comments