Skip to content

feat: allow headers to accept fn #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,34 @@ This property allows a user to pass the list of HTTP request methods accepted by

### headers

Type: `Object`
Type: `Object|Function`
Default: `undefined`

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

or

```js
webpackDevMiddleware(compiler, {
headers: () => {
return {
'Last-Modified': new Date(),
};
},
});
```

or

```js
webpackDevMiddleware(compiler, {
headers: (req, res, context) => {
res.setHeader('Last-Modified', new Date());
},
});
```

### index

Type: `Boolean|String`
Expand Down
7 changes: 6 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export default function wrapper(context) {

async function processRequest() {
const filename = getFilenameFromUrl(context, req.url);
const { headers } = context.options;
let { headers } = context.options;

if (typeof headers === 'function') {
headers = headers(req, res, context);
}

let content;

if (!filename) {
Expand Down
9 changes: 8 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
}
},
"headers": {
"type": "object"
"anyOf": [
{
"type": "object"
},
{
"instanceof": "Function"
}
]
},
"publicPath": {
"description": "The `publicPath` specifies the public URL address of the output files when referenced in a browser.",
Expand Down
18 changes: 16 additions & 2 deletions test/__snapshots__/validation-options.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validation should throw an error on the "headers" option with "1" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.headers should be one of these:
object { … } | function
Details:
* options.headers should be an object:
object { … }
* options.headers should be an instance of function."
`;

exports[`validation should throw an error on the "headers" option with "true" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.headers should be an object:
object { … }"
- options.headers should be one of these:
object { … } | function
Details:
* options.headers should be an object:
object { … }
* options.headers should be an instance of function."
`;

exports[`validation should throw an error on the "index" option with "{}" value 1`] = `
Expand Down
18 changes: 16 additions & 2 deletions test/__snapshots__/validation-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validation should throw an error on the "headers" option with "1" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.headers should be one of these:
object { … } | function
Details:
* options.headers should be an object:
object { … }
* options.headers should be an instance of function."
`;

exports[`validation should throw an error on the "headers" option with "true" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.headers should be an object:
object { … }"
- options.headers should be one of these:
object { … } | function
Details:
* options.headers should be an object:
object { … }
* options.headers should be an instance of function."
`;

exports[`validation should throw an error on the "index" option with "{}" value 1`] = `
Expand Down
4 changes: 2 additions & 2 deletions test/validation-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('validation', () => {
failure: [{}, true],
},
headers: {
success: [{ 'X-Custom-Header': 'yes' }],
failure: [true],
success: [{ 'X-Custom-Header': 'yes' }, () => {}],
failure: [true, 1],
},
publicPath: {
success: ['/foo', '', 'auto', () => '/public/path'],
Expand Down