-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.ts
More file actions
69 lines (65 loc) · 2.02 KB
/
message.ts
File metadata and controls
69 lines (65 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Return an instance with the provided `Request` replacing the specified header.
* There are no side effects on the original `Request`.
*
* @example
* ```ts
* import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* declare const init: Request;
* declare const header: string;
* declare const value: string;
*
* const request = withHeader(init, header, value);
*
* assert(request.headers.get(header), value);
* assert(init !== request);
* ```
*
* @deprecated Move to [request-utils](https://github.com/httpland/request-utils).
*/
export function withHeader(
request: Request,
fieldName: string,
fieldValue: string,
): Request;
/** Return an instance with the provided `Response` replacing the specified header.
* There are no side effects on the original `Response`.
*
* @example
* ```ts
* import { withHeader } from "https://deno.land/x/http_utils@$VERSION/message.ts";
* import { assert } from "https://deno.land/std@$VERSION/testing/asserts.ts";
*
* declare const init: Response;
* declare const header: string;
* declare const value: string;
*
* const response = withHeader(init, header, value);
*
* assert(response.headers.get(header), value);
* assert(init !== response);
* ```
*
* @deprecated Move to [response-utils](https://github.com/httpland/response-utils).
*/
export function withHeader(
response: Response,
fieldName: string,
fieldValue: string,
): Response;
export function withHeader(
message: Request | Response,
fieldName: string,
fieldValue: string,
): Request | Response {
const headers = new Headers([...message.headers.entries(), [
fieldName,
fieldValue,
]]);
if (message instanceof Request) return new Request(message, { headers });
const { status, statusText } = message;
return new Response(message.body, { headers, status, statusText });
}