Skip to content

Commit de12dc4

Browse files
committed
Define TypeScript types for API surface
1 parent 616e898 commit de12dc4

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

types/cache-override.d.ts

+123-2
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,31 @@ declare module "fastly:cache-override" {
9999
*
100100
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
101101
* for details.
102+
*
103+
* @param {void} [init.onBeforeSend]
104+
* Set a [callback function](https://developer.mozilla.org/en-US/docs/Glossary/Callback_function) to be invoked if a
105+
* request is going all the way to a backend, allowing the request to be modified beforehand.
106+
*
107+
* See [Modifying a request as it is forwarded to a backend](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/#modifying-a-request-as-it-is-forwarded-to-a-backend)
108+
* in the Fastly cache interfaces documentation for details.
109+
*
110+
* @param {void} [init.onAfterSend]
111+
* Set a [callback function](https://developer.mozilla.org/en-US/docs/Glossary/Callback_function) to be invoked after
112+
* a response has been sent, but before it is stored into the cache.
113+
*
114+
* See [Controlling cache behavior based on backend response](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/#controlling-cache-behavior-based-on-backend-response)
115+
* in the Fastly cache interfaces documentation for details.
102116
*/
103117
constructor(
104118
mode: "none" | "pass" | "override",
105119
init?: {
106120
ttl?: number,
107121
swr?: number,
108122
surrogateKey?: string,
109-
pci?: boolean
110-
}
123+
pci?: boolean,
124+
onBeforeSend(request: Request): void,
125+
onAfterSend(candidateResponse: CandidateResponse): void,
126+
},
111127
);
112128

113129
/**
@@ -148,4 +164,109 @@ declare module "fastly:cache-override" {
148164
*/
149165
public pci?: boolean;
150166
}
167+
168+
/**
169+
* Represents a response from the backend fetched through the readthrough cache interface before it is potentially
170+
* stored into the cache. It contains interfaces to read and manipulate headers and cache policy, and a
171+
* `bodyTransform` may be set allowing the body to be modified before it is potentially stored.
172+
*
173+
* If the backend response had a 304 Not Modified status, the `CandidateResponse`
174+
* will have status matching the original response, headers corresponding to the
175+
* original response updated with the 304 response headers, and will be marked to
176+
* revalidate the cache only (i.e., not alter the stored body).
177+
*/
178+
interface CandidateResponse {
179+
/**
180+
* Get the HTTP headers of the response. Use this object to manipulate the response's
181+
* headers.
182+
*/
183+
readonly headers: Headers;
184+
185+
/* version: string; // HTTP Version */
186+
187+
/**
188+
* Get or set the HTTP status code of the response.
189+
*/
190+
status: number;
191+
192+
/**
193+
* Get or set the Time to Live (TTL) in the cache for this response.
194+
*/
195+
ttl: number;
196+
197+
/**
198+
* The current age of the cached item, relative to the originating backend.
199+
*/
200+
readonly age: number;
201+
202+
/**
203+
* Get or set the time for which a cached item can safely be used after being considered stale.
204+
*/
205+
swr: number;
206+
207+
/**
208+
* Determines whether the cached response is stale.
209+
*
210+
* A cached response is stale if it has been in the cache beyond its TTL period.
211+
*/
212+
readonly isStale: boolean;
213+
214+
/**
215+
* Get or set the set of request headers for which the response may vary.
216+
*/
217+
vary: Set<string>;
218+
219+
/**
220+
* Get or set the surrogate keys for the cached response.
221+
*/
222+
surrogateKeys: Set<string>;
223+
224+
/**
225+
* Get or set whether this response should only be stored via PCI/HIPAA-compliant non-volatile caching.
226+
*
227+
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
228+
* for details.
229+
*/
230+
pci: boolean;
231+
232+
/**
233+
* Determines whether this response should be cached.
234+
*/
235+
readonly isCacheable: boolean;
236+
237+
/**
238+
* Force this response to be stored in the cache, even if its headers or status would normally prevent that.
239+
*/
240+
setCacheable(): void;
241+
242+
/**
243+
* Set the response to not be stored in the cache.
244+
*
245+
* @param {boolean} recordUncacheable (optional) If true, the cache will record that the originating request
246+
* led to an uncacheable response, so that future cache lookups will result in immediately going to the
247+
* backend, rather than attempting to coordinate concurrent requests to reduce backend traffic.
248+
*
249+
* See the [Fastly request collapsing guide](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/request-collapsing/)
250+
* for more details on the mechanism that `recordUncacheable` disables.
251+
*/
252+
setUncacheable(recordUncacheable?: boolean): void;
253+
254+
/**
255+
* Get or set a [TransformStream](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) to be used for
256+
* transforming the response body prior to caching.
257+
*
258+
* Body transformations are performed by specifying a transform, rather than by directly working with the body
259+
* during the onAfterSend callback function, because not every response contains a fresh body:
260+
* 304 Not Modified responses, which are used to revalidate a stale cached response, are valuable precisely because
261+
* they do not retransmit the body.
262+
*
263+
* For any other response status, the backend response will contain a relevant body, and the `bodyTransform` will
264+
* be applied to it. The original backend body is piped to the [`writeable`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/writable)
265+
* end of the transform, and transform is responsible for writing the new body, which will be read out from the
266+
* [`readable`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/readable) end of the transform.
267+
* This setup allows the transform to work with streamed chunks of the backend body, rather
268+
* than necessarily reading it entirely into memory.
269+
*/
270+
bodyTransform?: TransformStream<Uint8Array, Uint8Array>;
271+
}
151272
}

0 commit comments

Comments
 (0)