Skip to content

Commit cfeb34d

Browse files
t27duckmarcelolx
authored andcommitted
Allow additional/custom headers in request
This change allows for the user to specify additional headers in the request object. ``` const request = new Request('post', 'localhost:3000/my_endpoint', { body: { name: 'Request.JS' }, headers: { 'X-Custom-Header': '...' } }) ``` Anything in the `headers` option is merged into the preset headers. Last one in wins, so this should also allow for overriding defaults if desired.
1 parent 9f41962 commit cfeb34d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/request.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ export class Request {
2929
}
3030

3131
get headers () {
32-
return compact({
33-
'X-Requested-With': 'XMLHttpRequest',
34-
'X-CSRF-Token': this.csrfToken,
35-
'Content-Type': this.contentType,
36-
Accept: this.accept
37-
})
32+
return compact(
33+
Object.assign({
34+
'X-Requested-With': 'XMLHttpRequest',
35+
'X-CSRF-Token': this.csrfToken,
36+
'Content-Type': this.contentType,
37+
Accept: this.accept
38+
},
39+
this.additionalHeaders)
40+
)
3841
}
3942

4043
get csrfToken () {
@@ -77,6 +80,10 @@ export class Request {
7780
get signal () {
7881
return this.options.signal
7982
}
83+
84+
get additionalHeaders () {
85+
return this.options.headers || {}
86+
}
8087
}
8188

8289
function compact (object) {

0 commit comments

Comments
 (0)