-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement modifyHeaders function (#179)
- Loading branch information
Showing
10 changed files
with
11,495 additions
and
5,930 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { it, expect } from "vitest"; | ||
import { modifyHeaders } from "./modify-headers"; | ||
|
||
it("modifies mutable headers", () => { | ||
const response = new Response("body", { | ||
headers: { "Some-Header": "value" }, | ||
}); | ||
|
||
const modified = modifyHeaders(response, (headers) => { | ||
headers.set("Some-Header", "modified"); | ||
headers.set("New-Header", "new"); | ||
}); | ||
|
||
expect(modified).toBe(response); // Should be in-place | ||
expect(modified.headers.get("Some-Header")).toBe("modified"); | ||
expect(modified.headers.get("New-Header")).toBe("new"); | ||
}); | ||
|
||
it("modifies immutable headers", () => { | ||
const response = Response.redirect("http://example.com"); | ||
|
||
const modified = modifyHeaders(response, (headers) => { | ||
headers.set("Some-Header", "modified"); | ||
}); | ||
|
||
expect(modified).not.toBe(response); // Should not be in-place | ||
expect(modified.headers.get("Some-Header")).toBe("modified"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Tries to modify the headers of a response, and, if it fails, | ||
* creates a mutable copy of the response and tries again. | ||
* | ||
* The problem that this function solves is that some responses | ||
* have immutable headers and there is no way to know this without | ||
* trying to modify them. This function solves this problem by | ||
* trying to modify the headers and, if it fails, creating a copy | ||
* of the response with the same body and headers and trying again. | ||
* | ||
* Usage: | ||
* | ||
* ```ts | ||
* app.use(async (ctx) => { | ||
* let response = await ctx.next(); | ||
* response = modifyHeaders(response, (headers) => { | ||
* headers.set("X-Powered-By", "Hattip"); | ||
* }); | ||
* | ||
* return response; | ||
* }); | ||
* ``` | ||
* | ||
* @param response Response object to be modified | ||
* @param modify Callback to modify the headers | ||
* | ||
* @returns a Response object with modified headers. It will be | ||
* the same object as the argument if its headers are mutable, | ||
* otherwise it will be a copy of the original. | ||
*/ | ||
export function modifyHeaders( | ||
response: Response, | ||
modify: (headers: Headers) => void, | ||
): Response { | ||
try { | ||
modify(response.headers); | ||
return response; | ||
} catch { | ||
const clone = new Response(response.body, response); | ||
modify(clone.headers); | ||
return clone; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.