Skip to content

Commit 3f13d65

Browse files
committed
wip
1 parent ee3fa97 commit 3f13d65

File tree

2 files changed

+125
-43
lines changed

2 files changed

+125
-43
lines changed

types/cache-override.d.ts

+68-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
11
declare module 'fastly:cache-override' {
2+
interface ICacheOverride {
3+
/**
4+
* The cache key for this cache entry
5+
*/
6+
cacheKey?: string | undefined;
7+
/**
8+
* Override the caching behavior of this request/response to use the given Time to Live (TTL), in seconds.
9+
*/
10+
ttl?: number | undefined;
11+
/**
12+
* Override the caching behavior of this request/response to use the given `stale-while-revalidate` time,
13+
* in seconds
14+
*/
15+
swr?: number | undefined;
16+
/**
17+
* Override the caching behavior of this request/response to include the given surrogate key, provided as
18+
* a header value.
19+
*
20+
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
21+
* for details.
22+
*/
23+
surrogateKeys?: Set<string>;
24+
/**
25+
* Override the caching behavior of this request/response to include the given surrogate keys, provided as
26+
* a header value.
27+
*
28+
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
29+
* for details.
30+
*/
31+
surrogateKey?: string | undefined;
32+
/**
33+
* Override the caching behavior of this request/response to enable or disable PCI/HIPAA-compliant
34+
* non-volatile caching.
35+
*
36+
* By default, this is false, which means the request may not be PCI/HIPAA-compliant. Set it to
37+
* true to enable compliant caching.
38+
*
39+
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
40+
* for details.
41+
*/
42+
pci?: boolean;
43+
/**
44+
* Get or set the set of request headers for which the response may vary.
45+
*/
46+
vary?: Set<string>;
47+
}
48+
249
/**
350
* Configures the caching behavior of a {@linkcode "globals".Response}.
451
*
@@ -17,8 +64,6 @@ declare module 'fastly:cache-override' {
1764
* "origins": [
1865
* "https://http-me.glitch.me"
1966
* ],
20-
* "src": {
21-
* "deps": "{\n \"@fastly/js-compute\": \"^0.7.0\"\n}",
2267
* "main": "/// <reference types=\"@fastly/js-compute\" />\nimport { CacheOverride } from \"fastly:cache-override\";\n\n// In this example we override the cache for all the requests prefixed /static/ \n// to have a long TTL (Time To Live), and the home page to have a short TTL and \n// a long SWR (Stale While Revalidate).\nasync function app (event) {\n const path = (new URL(event.request.url)).pathname;\n let cacheOverride;\n if (path == '/') {\n cacheOverride = new CacheOverride('override', {ttl: 10, swr: 86_400});\n } else if (path.startsWith('/static/')) {\n cacheOverride = new CacheOverride('override', {ttl: 86_400});\n } else {\n cacheOverride = new CacheOverride('none')\n }\n return fetch(event.request.url, {\n cacheOverride,\n backend: 'origin_0'\n });\n}\naddEventListener(\"fetch\", event => event.respondWith(app(event)));\n"
2368
* },
2469
* "requests": [
@@ -64,6 +109,7 @@ declare module 'fastly:cache-override' {
64109
* ```
65110
* </noscript>
66111
*/
112+
67113
class CacheOverride {
68114
/**
69115
* @param mode Sets the cache override mode for a request
@@ -73,41 +119,12 @@ declare module 'fastly:cache-override' {
73119
* - "pass": Do not cache the response to this request, regardless of the origin response’s headers.
74120
* - "override": Override particular cache control settings using a {@linkcode CacheOverride} object.
75121
*
76-
* @param [init]
77-
*
78-
* @param {number} [init.ttl]
79-
* Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
80-
*
81-
* @param {number} [init.swr]
82-
* Override the caching behavior of this request to use the given `stale-while-revalidate` time,
83-
* in seconds
84-
*
85-
* @param {string} [init.surrogateKey]
86-
* Override the caching behavior of this request to include the given surrogate key, provided as
87-
* a header value.
88-
*
89-
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
90-
* for details.
91-
*
92-
* @param {boolean} [init.pci]
93-
* Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
94-
* non-volatile caching.
122+
* @param [init] Init options for the cache override
95123
*
96-
* By default, this is false, which means the request may not be PCI/HIPAA-compliant. Set it to
97-
* true to enable compliant caching.
98-
*
99-
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
100-
* for details.
101124
*/
102-
constructor(
103-
mode: 'none' | 'pass' | 'override',
104-
init?: {
105-
ttl?: number;
106-
swr?: number;
107-
surrogateKey?: string;
108-
pci?: boolean;
109-
},
110-
);
125+
constructor(mode: 'none' | 'pass' | 'override', init?: ICacheOverride);
126+
127+
cacheKey?: string;
111128

112129
/**
113130
* Sets the cache override mode for a request
@@ -121,20 +138,28 @@ declare module 'fastly:cache-override' {
121138
/**
122139
* Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
123140
*/
124-
public ttl?: number;
141+
public ttl: number | undefined;
125142
/**
126143
* Override the caching behavior of this request to use the given `stale-while-revalidate` time,
127144
* in seconds
128145
*/
129-
public swr?: number;
146+
public swr: number | undefined;
130147
/**
131148
* Override the caching behavior of this request to include the given surrogate key, provided as
132149
* a header value.
133150
*
134151
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
135152
* for details.
136153
*/
137-
public surrogateKey?: string;
154+
public surrogateKey: string | undefined;
155+
/**
156+
* Override the caching behavior of this request to include the given surrogate key, provided as
157+
* a header value.
158+
*
159+
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
160+
* for details.
161+
*/
162+
public surrogateKeys: Set<string> | undefined;
138163
/**
139164
* Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
140165
* non-volatile caching.
@@ -145,6 +170,10 @@ declare module 'fastly:cache-override' {
145170
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
146171
* for details.
147172
*/
148-
public pci?: boolean;
173+
public pci: boolean | undefined;
174+
/**
175+
* Get or set the set of request headers for which the response may vary.
176+
*/
177+
public vary: Set<string> | undefined;
149178
}
150179
}

types/globals.d.ts

+57-4
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,25 @@ declare interface RequestInit {
12251225
/** The Fastly configured backend name or instance the request should be sent to. */
12261226
backend?: string | import('fastly:backend').Backend;
12271227
cacheOverride?: import('fastly:cache-override').CacheOverride;
1228+
/**
1229+
* Provide a callback to modify the request before sending to the backend.
1230+
*/
1231+
beforeSend?: (req: Request) => Promise<void>;
1232+
/**
1233+
* Provide a callback to modify the response before storing in the cache.
1234+
*
1235+
* May return another response entirely.
1236+
*
1237+
* Alternatively a void return indicates that the request will not be cached.
1238+
*/
1239+
beforeCache?: (res: Response) => Promise<Response | undefined>;
1240+
/**
1241+
* Boolean indicating if the request is cacheable under standard cache rules.
1242+
*/
1243+
isCacheable: boolean;
1244+
/**
1245+
* @deprecated use cacheOverride.cacheKey instead
1246+
*/
12281247
cacheKey?: string;
12291248
fastly?: {
12301249
decompressGzip?: boolean;
@@ -1272,12 +1291,24 @@ interface Request extends Body {
12721291
clone(): Request;
12731292

12741293
/**
1275-
* The request backend, null for the downstream request itself
1294+
* The request backend, undefined for the downstream request itself
1295+
*/
1296+
readonly backend: import('fastly:backend').Backend | undefined;
1297+
1298+
/**
1299+
* The cache override options for this request. May be modified up until the point that
1300+
* the request is stored in the cache.
1301+
*/
1302+
readonly cacheOverride: import('fastly:cache-override').CacheOverride;
1303+
/**
1304+
* @deprecated use {@link cacheOverride} directly.
12761305
*/
1277-
backend: import('fastly:backend').Backend | undefined;
12781306
setCacheOverride(
12791307
override: import('fastly:cache-override').CacheOverride,
12801308
): void;
1309+
/**
1310+
* @deprecated set {@link cacheOverride.cacheKey} instead.
1311+
*/
12811312
setCacheKey(key: string): void;
12821313
setManualFramingHeaders(manual: boolean): void;
12831314
}
@@ -1311,10 +1342,18 @@ declare interface ResponseInit {
13111342
* @group Fetch API
13121343
*/
13131344
interface Response extends Body {
1345+
/**
1346+
* The response headers.
1347+
*
1348+
* May be modified even for upstream responses prior to storage in the cache.
1349+
*/
13141350
readonly headers: Headers;
13151351
readonly ok: boolean;
13161352
// readonly redirected: boolean;
1317-
readonly status: number;
1353+
/**
1354+
* The response status. May be modified prior to storage in the cache.
1355+
*/
1356+
status: number;
13181357
readonly statusText: string;
13191358
// readonly type: ResponseType;
13201359
readonly url: string;
@@ -1334,10 +1373,24 @@ interface Response extends Body {
13341373
readonly port: number | undefined;
13351374
// clone(): Response;
13361375
setManualFramingHeaders(manual: boolean): void;
1376+
/**
1377+
* The cache override options for this response.
1378+
*
1379+
* May be modified prior to storage in the cache.
1380+
*/
1381+
readonly cacheOverride: import('fastly:cache-override').CacheOverride;
1382+
/**
1383+
* Set a custom transform stream for the origin on cache miss.
1384+
* Can only be called when the onAfterSend hook is triggered, and before the promise has resolved.
1385+
*
1386+
* @param transformStream A transform stream to transform the backend response before storing
1387+
* in the cache and returning the response to the client.
1388+
*/
1389+
setCacheTransform(transformStream: TransformStream): void;
13371390
/**
13381391
* The response backend, if an upstream response
13391392
*/
1340-
backend: import('fastly:backend').Backend | undefined;
1393+
readonly backend: import('fastly:backend').Backend | undefined;
13411394
}
13421395

13431396
/**

0 commit comments

Comments
 (0)