Skip to content

Commit 8c32b9d

Browse files
authored
🤖 Merge PR DefinitelyTyped#72265 http-cache-semantics: upgrade types to match upstream release 4.2.0 by @hexchain
1 parent 6cc7117 commit 8c32b9d

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

‎types/http-cache-semantics/http-cache-semantics-tests.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ new CachePolicy(req, res, { trustServerDate: true });
1313
policy.storable(); // $ExpectType boolean
1414
policy.satisfiesWithoutRevalidation(req); // $ExpectType boolean
1515
policy.responseHeaders(); // $ExpectType Headers
16+
policy.stale(); // $ExpectType boolean
17+
policy.age(); // $ExpectType number
18+
policy.maxAge(); // $ExpectType number
1619
policy.timeToLive(); // $ExpectType number
1720
CachePolicy.fromObject(policy.toObject()); // $ExpectType CachePolicy
1821
policy.revalidationHeaders(req); // $ExpectType Headers
1922
policy.revalidatedPolicy(req, res); // $ExpectType RevalidationPolicy
23+
policy.evaluateRequest(req); // $ExpectType EvaluateRequestResult

‎types/http-cache-semantics/index.d.ts‎

Lines changed: 97 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export = CachePolicy;
22

33
declare 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

76129
declare 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
}

‎types/http-cache-semantics/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@types/http-cache-semantics",
4-
"version": "4.0.9999",
4+
"version": "4.2.9999",
55
"projects": [
66
"https://github.com/kornelski/http-cache-semantics#readme"
77
],

0 commit comments

Comments
 (0)