Skip to content

Commit 5d937fe

Browse files
committed
feat: allow headers to accept fn
1 parent 594dddc commit 5d937fe

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,34 @@ This property allows a user to pass the list of HTTP request methods accepted by
7070

7171
### headers
7272

73-
Type: `Object`
73+
Type: `Object|Function`
7474
Default: `undefined`
7575

7676
This property allows a user to pass custom HTTP headers on each request.
7777
eg. `{ "X-Custom-Header": "yes" }`
7878

79+
or
80+
81+
```js
82+
webpackDevMiddleware(compiler, {
83+
headers: () => {
84+
return {
85+
'Last-Modified': new Date(),
86+
};
87+
},
88+
});
89+
```
90+
91+
or
92+
93+
```js
94+
webpackDevMiddleware(compiler, {
95+
headers: (req, res, context) => {
96+
res.setHeader('Last-Modified', new Date());
97+
},
98+
});
99+
```
100+
79101
### index
80102

81103
Type: `Boolean|String`

src/middleware.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export default function wrapper(context) {
4242

4343
async function processRequest() {
4444
const filename = getFilenameFromUrl(context, req.url);
45-
const { headers } = context.options;
45+
let { headers } = context.options;
46+
47+
if (typeof headers === 'function') {
48+
headers = headers(req, res, context);
49+
}
50+
4651
let content;
4752

4853
if (!filename) {

src/options.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
}
2626
},
2727
"headers": {
28-
"type": "object"
28+
"anyOf": [
29+
{
30+
"type": "object"
31+
},
32+
{
33+
"instanceof": "Function"
34+
}
35+
]
2936
},
3037
"publicPath": {
3138
"description": "The `publicPath` specifies the public URL address of the output files when referenced in a browser.",

test/__snapshots__/validation-options.test.js.snap.webpack4

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`validation should throw an error on the "headers" option with "1" value 1`] = `
4+
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
5+
- options.headers should be one of these:
6+
object { … } | function
7+
Details:
8+
* options.headers should be an object:
9+
object { … }
10+
* options.headers should be an instance of function."
11+
`;
12+
313
exports[`validation should throw an error on the "headers" option with "true" value 1`] = `
414
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
5-
- options.headers should be an object:
6-
object { … }"
15+
- options.headers should be one of these:
16+
object { … } | function
17+
Details:
18+
* options.headers should be an object:
19+
object { … }
20+
* options.headers should be an instance of function."
721
`;
822

923
exports[`validation should throw an error on the "index" option with "{}" value 1`] = `

test/__snapshots__/validation-options.test.js.snap.webpack5

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`validation should throw an error on the "headers" option with "1" value 1`] = `
4+
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
5+
- options.headers should be one of these:
6+
object { … } | function
7+
Details:
8+
* options.headers should be an object:
9+
object { … }
10+
* options.headers should be an instance of function."
11+
`;
12+
313
exports[`validation should throw an error on the "headers" option with "true" value 1`] = `
414
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
5-
- options.headers should be an object:
6-
object { … }"
15+
- options.headers should be one of these:
16+
object { … } | function
17+
Details:
18+
* options.headers should be an object:
19+
object { … }
20+
* options.headers should be an instance of function."
721
`;
822

923
exports[`validation should throw an error on the "index" option with "{}" value 1`] = `

test/validation-options.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('validation', () => {
2828
failure: [{}, true],
2929
},
3030
headers: {
31-
success: [{ 'X-Custom-Header': 'yes' }],
32-
failure: [true],
31+
success: [{ 'X-Custom-Header': 'yes' }, () => {}],
32+
failure: [true, 1],
3333
},
3434
publicPath: {
3535
success: ['/foo', '', 'auto', () => '/public/path'],

0 commit comments

Comments
 (0)