Skip to content

Commit

Permalink
Add copyTo (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jun 7, 2024
1 parent 53e7d9b commit fd061fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ export default async function handler(request: Request): Promise<Response> {
}
```

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:

```astro
---
import { CacheHeaders, ONE_HOUR } from "cdn-cache-control";
new CacheHeaders().swr(ONE_HOUR).copyTo(Astro.response.headers);
---
```

## API

<!-- TSDOC_START -->
Expand Down Expand Up @@ -168,6 +179,7 @@ Number of seconds in one year
- [immutable](#gear-immutable)
- [ttl](#gear-ttl)
- [toObject](#gear-toobject)
- [copyTo](#gear-copyto)
- [getCdnCacheControl](#gear-getcdncachecontrol)
- [setCdnCacheControl](#gear-setcdncachecontrol)
- [getCacheControl](#gear-getcachecontrol)
Expand Down Expand Up @@ -233,6 +245,14 @@ Returns the headers as a plain object.
| ---------- | ------------------------------ |
| `toObject` | `() => Record<string, string>` |

#### :gear: copyTo

Copy the headers from this instance to another Headers instance.

| Method | Type |
| -------- | -------------------------------------- |
| `copyTo` | `<T extends Headers>(headers: T) => T` |

#### :gear: getCdnCacheControl

The parsed cache-control header for the CDN cache.
Expand Down Expand Up @@ -284,3 +304,7 @@ The parsed content of the cache tags header.
```
```

```
```
15 changes: 15 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,19 @@ describe("CacheHeaders", () => {
"cache-tag": "tag1,tag2,tag3",
});
});

it("copies headers to an existing object", () => {
const existing = new Headers([["x-foo", "bar"]]);
const headers = new CacheHeaders().swr().tag("tag1").tag("tag2", "tag3");

const copied = headers.copyTo(existing);

assert.strictEqual(existing.get("x-foo"), "bar");
assert.strictEqual(
existing.get("CDN-Cache-Control"),
"public,s-maxage=0,stale-while-revalidate=604800",
);
assert.strictEqual(existing.get("Cache-Tag"), "tag1,tag2,tag3");
assert.strictEqual(existing, copied);
});
});
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ export class CacheHeaders extends Headers {
return Object.fromEntries(this.entries());
}

/**
* Copy the headers from this instance to another Headers instance.
*/

copyTo<T extends Headers>(headers: T): T {
this.forEach((value, key) => {
headers.set(key, value);
});
return headers;
}

private get cacheTagHeaderName(): string {
switch (this.#cdn) {
case "netlify":
Expand Down

0 comments on commit fd061fa

Please sign in to comment.