Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Nov 22, 2024
1 parent ee3fa97 commit f78e621
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 41 deletions.
105 changes: 68 additions & 37 deletions types/cache-override.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@
declare module 'fastly:cache-override' {
interface ICacheOverride {
/**
* The cache key for this cache entry
*/
cacheKey?: string | undefined;
/**
* Override the caching behavior of this request/response to use the given Time to Live (TTL), in seconds.
*/
ttl?: number | undefined;
/**
* Override the caching behavior of this request/response to use the given `stale-while-revalidate` time,
* in seconds
*/
swr?: number | undefined;
/**
* Override the caching behavior of this request/response to include the given surrogate key, provided as
* a header value.
*
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
* for details.
*/
surrogateKeys?: Set<string>;
/**
* Override the caching behavior of this request/response to include the given surrogate keys, provided as
* a header value.
*
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
* for details.
*/
surrogateKey?: string | undefined;
/**
* Override the caching behavior of this request/response to enable or disable PCI/HIPAA-compliant
* non-volatile caching.
*
* By default, this is false, which means the request may not be PCI/HIPAA-compliant. Set it to
* true to enable compliant caching.
*
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
* for details.
*/
pci?: boolean;
/**
* Get or set the set of request headers for which the response may vary.
*/
vary?: Set<string>;
}

/**
* Configures the caching behavior of a {@linkcode "globals".Response}.
*
Expand Down Expand Up @@ -64,6 +111,7 @@ declare module 'fastly:cache-override' {
* ```
* </noscript>
*/

class CacheOverride {
/**
* @param mode Sets the cache override mode for a request
Expand All @@ -73,41 +121,12 @@ declare module 'fastly:cache-override' {
* - "pass": Do not cache the response to this request, regardless of the origin response’s headers.
* - "override": Override particular cache control settings using a {@linkcode CacheOverride} object.
*
* @param [init]
*
* @param {number} [init.ttl]
* Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
*
* @param {number} [init.swr]
* Override the caching behavior of this request to use the given `stale-while-revalidate` time,
* in seconds
*
* @param {string} [init.surrogateKey]
* Override the caching behavior of this request to include the given surrogate key, provided as
* a header value.
*
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
* for details.
*
* @param {boolean} [init.pci]
* Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
* non-volatile caching.
* @param [init] Init options for the cache override
*
* By default, this is false, which means the request may not be PCI/HIPAA-compliant. Set it to
* true to enable compliant caching.
*
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
* for details.
*/
constructor(
mode: 'none' | 'pass' | 'override',
init?: {
ttl?: number;
swr?: number;
surrogateKey?: string;
pci?: boolean;
},
);
constructor(mode: 'none' | 'pass' | 'override', init?: ICacheOverride);

cacheKey?: string;

/**
* Sets the cache override mode for a request
Expand All @@ -121,20 +140,28 @@ declare module 'fastly:cache-override' {
/**
* Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
*/
public ttl?: number;
public ttl: number | undefined;
/**
* Override the caching behavior of this request to use the given `stale-while-revalidate` time,
* in seconds
*/
public swr?: number;
public swr: number | undefined;
/**
* Override the caching behavior of this request to include the given surrogate key, provided as
* a header value.
*
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
* for details.
*/
public surrogateKey?: string;
public surrogateKey: string | undefined;
/**
* Override the caching behavior of this request to include the given surrogate key, provided as
* a header value.
*
* See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
* for details.
*/
public surrogateKeys: Set<string> | undefined;
/**
* Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
* non-volatile caching.
Expand All @@ -145,6 +172,10 @@ declare module 'fastly:cache-override' {
* See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
* for details.
*/
public pci?: boolean;
public pci: boolean | undefined;
/**
* Get or set the set of request headers for which the response may vary.
*/
public vary: Set<string> | undefined;
}
}
61 changes: 57 additions & 4 deletions types/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,25 @@ declare interface RequestInit {
/** The Fastly configured backend name or instance the request should be sent to. */
backend?: string | import('fastly:backend').Backend;
cacheOverride?: import('fastly:cache-override').CacheOverride;
/**
* Provide a callback to modify the request before sending to the backend.
*/
beforeSend?: (req: Request) => Promise<void>;
/**
* Provide a callback to modify the response before storing in the cache.
*
* May return another response entirely.
*
* Alternatively a void return indicates that the request will not be cached.
*/
beforeCache?: (res: Response) => Promise<Response | undefined>;
/**
* Boolean indicating if the request is cacheable under standard cache rules.
*/
isCacheable: boolean;
/**
* @deprecated use cacheOverride.cacheKey instead
*/
cacheKey?: string;
fastly?: {
decompressGzip?: boolean;
Expand Down Expand Up @@ -1272,12 +1291,24 @@ interface Request extends Body {
clone(): Request;

/**
* The request backend, null for the downstream request itself
* The request backend, undefined for the downstream request itself
*/
readonly backend: import('fastly:backend').Backend | undefined;

/**
* The cache override options for this request. May be modified up until the point that
* the request is stored in the cache.
*/
readonly cacheOverride: import('fastly:cache-override').CacheOverride;
/**
* @deprecated use {@link cacheOverride} directly.
*/
backend: import('fastly:backend').Backend | undefined;
setCacheOverride(
override: import('fastly:cache-override').CacheOverride,
): void;
/**
* @deprecated set {@link cacheOverride.cacheKey} instead.
*/
setCacheKey(key: string): void;
setManualFramingHeaders(manual: boolean): void;
}
Expand Down Expand Up @@ -1311,10 +1342,18 @@ declare interface ResponseInit {
* @group Fetch API
*/
interface Response extends Body {
/**
* The response headers.
*
* May be modified even for upstream responses prior to storage in the cache.
*/
readonly headers: Headers;
readonly ok: boolean;
// readonly redirected: boolean;
readonly status: number;
/**
* The response status. May be modified prior to storage in the cache.
*/
status: number;
readonly statusText: string;
// readonly type: ResponseType;
readonly url: string;
Expand All @@ -1334,10 +1373,24 @@ interface Response extends Body {
readonly port: number | undefined;
// clone(): Response;
setManualFramingHeaders(manual: boolean): void;
/**
* The cache override options for this response.
*
* May be modified prior to storage in the cache.
*/
readonly cacheOverride: import('fastly:cache-override').CacheOverride;
/**
* Set a custom transform stream for the origin on cache miss.
* Can only be called when the onAfterSend hook is triggered, and before the promise has resolved.
*
* @param transformStream A transform stream to transform the backend response before storing
* in the cache and returning the response to the client.
*/
setCacheTransform(transformStream: TransformStream): void;
/**
* The response backend, if an upstream response
*/
backend: import('fastly:backend').Backend | undefined;
readonly backend: import('fastly:backend').Backend | undefined;
}

/**
Expand Down

0 comments on commit f78e621

Please sign in to comment.