Skip to content

Commit f78e621

Browse files
author
Guy Bedford
committed
wip
1 parent ee3fa97 commit f78e621

File tree

2 files changed

+125
-41
lines changed

2 files changed

+125
-41
lines changed

types/cache-override.d.ts

Lines changed: 68 additions & 37 deletions
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
*
@@ -64,6 +111,7 @@ declare module 'fastly:cache-override' {
64111
* ```
65112
* </noscript>
66113
*/
114+
67115
class CacheOverride {
68116
/**
69117
* @param mode Sets the cache override mode for a request
@@ -73,41 +121,12 @@ declare module 'fastly:cache-override' {
73121
* - "pass": Do not cache the response to this request, regardless of the origin response’s headers.
74122
* - "override": Override particular cache control settings using a {@linkcode CacheOverride} object.
75123
*
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.
124+
* @param [init] Init options for the cache override
95125
*
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.
101126
*/
102-
constructor(
103-
mode: 'none' | 'pass' | 'override',
104-
init?: {
105-
ttl?: number;
106-
swr?: number;
107-
surrogateKey?: string;
108-
pci?: boolean;
109-
},
110-
);
127+
constructor(mode: 'none' | 'pass' | 'override', init?: ICacheOverride);
128+
129+
cacheKey?: string;
111130

112131
/**
113132
* Sets the cache override mode for a request
@@ -121,20 +140,28 @@ declare module 'fastly:cache-override' {
121140
/**
122141
* Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
123142
*/
124-
public ttl?: number;
143+
public ttl: number | undefined;
125144
/**
126145
* Override the caching behavior of this request to use the given `stale-while-revalidate` time,
127146
* in seconds
128147
*/
129-
public swr?: number;
148+
public swr: number | undefined;
130149
/**
131150
* Override the caching behavior of this request to include the given surrogate key, provided as
132151
* a header value.
133152
*
134153
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
135154
* for details.
136155
*/
137-
public surrogateKey?: string;
156+
public surrogateKey: string | undefined;
157+
/**
158+
* Override the caching behavior of this request to include the given surrogate key, provided as
159+
* a header value.
160+
*
161+
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
162+
* for details.
163+
*/
164+
public surrogateKeys: Set<string> | undefined;
138165
/**
139166
* Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
140167
* non-volatile caching.
@@ -145,6 +172,10 @@ declare module 'fastly:cache-override' {
145172
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
146173
* for details.
147174
*/
148-
public pci?: boolean;
175+
public pci: boolean | undefined;
176+
/**
177+
* Get or set the set of request headers for which the response may vary.
178+
*/
179+
public vary: Set<string> | undefined;
149180
}
150181
}

types/globals.d.ts

Lines changed: 57 additions & 4 deletions
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)