Skip to content

Commit fd061fa

Browse files
authored
Add copyTo (#5)
1 parent 53e7d9b commit fd061fa

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ export default async function handler(request: Request): Promise<Response> {
107107
}
108108
```
109109

110+
Some frameworks use a readonly `Response` object, so you need to use an existing `headers` object. In this case you can use the `copyTo` method to copy the headers to the response:
111+
112+
```astro
113+
---
114+
import { CacheHeaders, ONE_HOUR } from "cdn-cache-control";
115+
116+
new CacheHeaders().swr(ONE_HOUR).copyTo(Astro.response.headers);
117+
---
118+
119+
```
120+
110121
## API
111122

112123
<!-- TSDOC_START -->
@@ -168,6 +179,7 @@ Number of seconds in one year
168179
- [immutable](#gear-immutable)
169180
- [ttl](#gear-ttl)
170181
- [toObject](#gear-toobject)
182+
- [copyTo](#gear-copyto)
171183
- [getCdnCacheControl](#gear-getcdncachecontrol)
172184
- [setCdnCacheControl](#gear-setcdncachecontrol)
173185
- [getCacheControl](#gear-getcachecontrol)
@@ -233,6 +245,14 @@ Returns the headers as a plain object.
233245
| ---------- | ------------------------------ |
234246
| `toObject` | `() => Record<string, string>` |
235247

248+
#### :gear: copyTo
249+
250+
Copy the headers from this instance to another Headers instance.
251+
252+
| Method | Type |
253+
| -------- | -------------------------------------- |
254+
| `copyTo` | `<T extends Headers>(headers: T) => T` |
255+
236256
#### :gear: getCdnCacheControl
237257

238258
The parsed cache-control header for the CDN cache.
@@ -284,3 +304,7 @@ The parsed content of the cache tags header.
284304
```
285305
286306
```
307+
308+
```
309+
310+
```

index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,19 @@ describe("CacheHeaders", () => {
105105
"cache-tag": "tag1,tag2,tag3",
106106
});
107107
});
108+
109+
it("copies headers to an existing object", () => {
110+
const existing = new Headers([["x-foo", "bar"]]);
111+
const headers = new CacheHeaders().swr().tag("tag1").tag("tag2", "tag3");
112+
113+
const copied = headers.copyTo(existing);
114+
115+
assert.strictEqual(existing.get("x-foo"), "bar");
116+
assert.strictEqual(
117+
existing.get("CDN-Cache-Control"),
118+
"public,s-maxage=0,stale-while-revalidate=604800",
119+
);
120+
assert.strictEqual(existing.get("Cache-Tag"), "tag1,tag2,tag3");
121+
assert.strictEqual(existing, copied);
122+
});
108123
});

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ export class CacheHeaders extends Headers {
173173
return Object.fromEntries(this.entries());
174174
}
175175

176+
/**
177+
* Copy the headers from this instance to another Headers instance.
178+
*/
179+
180+
copyTo<T extends Headers>(headers: T): T {
181+
this.forEach((value, key) => {
182+
headers.set(key, value);
183+
});
184+
return headers;
185+
}
186+
176187
private get cacheTagHeaderName(): string {
177188
switch (this.#cdn) {
178189
case "netlify":

0 commit comments

Comments
 (0)