Skip to content

Commit 864ba8d

Browse files
authored
Merge pull request #22 from krutoo/default-headers-when
defaultHeaders - "when" option
2 parents 7834469 + a1c7fdf commit 864ba8d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/middleware/default-headers.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export interface DefaultHeadersOptions {
1010
* - "defaults-append" - `request.headers` will be added to `defaults` using "append" method
1111
*/
1212
strategy?: 'set' | 'append' | 'defaults-set' | 'defaults-append';
13+
14+
/** Determines whether or not to use default headers. */
15+
when?: (request: Request) => boolean;
16+
}
17+
18+
function getTrue() {
19+
return true;
1320
}
1421

1522
/**
@@ -19,9 +26,16 @@ export interface DefaultHeadersOptions {
1926
*/
2027
export function defaultHeaders(
2128
defaults: HeadersInit,
22-
{ strategy = 'set' }: DefaultHeadersOptions = {},
29+
{
30+
strategy = 'set',
31+
when: matches = getTrue,
32+
}: DefaultHeadersOptions = {},
2333
): Middleware {
2434
return (request, next) => {
35+
if (!matches(request)) {
36+
return next(request);
37+
}
38+
2539
/**
2640
* Previously, there was a different approach here:
2741
* headers were created based on "defaults" argument,

0 commit comments

Comments
 (0)