Skip to content

Commit af1e86d

Browse files
ricardogobbosouzapi0
authored andcommitted
feat: allow adding custom headers with nuxt config (#294)
1 parent fba95fa commit af1e86d

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed

Diff for: docs/options.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Environment variable `API_URL` can be used to **override** `baseURL`.
4343

4444
### `browserBaseURL`
4545

46-
* Default: `baseURL`
46+
* Default: `baseURL`
4747
**WARNING:** when the `proxy` option is enabled the default for browserBaseURL becomes `prefix` instead of `baseURL`
4848

4949
Defines the base URL which is used and prepended to make client side requests.
@@ -148,3 +148,24 @@ This also helps making consistent requests in both SSR and Client Side code.
148148
* Default `['host', 'accept', 'cf-ray', 'cf-connecting-ip', 'content-length']`
149149

150150
This is useful and efficient only when `proxyHeaders` is set to true. Removes unwanted requests headers to the API backend in SSR.
151+
152+
### `headers`
153+
154+
Headers added to all requests. If provided, will be merged with the defaults.
155+
156+
```js
157+
{
158+
common: {
159+
'Accept': 'application/json, text/plain, */*'
160+
},
161+
delete: {},
162+
get: {},
163+
head: {},
164+
post: {},
165+
put: {},
166+
patch: {}
167+
}
168+
```
169+
170+
- **NOTE:** Do NOT include any credentials or tokens here. One can easily access them.
171+
- **NOTE:** This headers are effective to ALL requests. Please take care and consider providing special headers on each call that needs this unless you are pretty sure you always need to add headers.

Diff for: lib/module.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const consola = require('consola')
3+
const defu = require('defu')
34

45
const logger = consola.withScope('nuxt:axios')
56

@@ -36,8 +37,21 @@ function axiosModule(_moduleOptions) {
3637
// HTTPS
3738
const https = Boolean(this.options.server && this.options.server.https)
3839

40+
// Headers
41+
const headers = {
42+
common: {
43+
'Accept': 'application/json, text/plain, */*'
44+
},
45+
delete: {},
46+
get: {},
47+
head: {},
48+
post: {},
49+
put: {},
50+
patch: {}
51+
}
52+
3953
// Apply defaults
40-
const options = {
54+
const options = defu(moduleOptions, {
4155
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
4256
browserBaseURL: null,
4357
credentials: false,
@@ -48,8 +62,8 @@ function axiosModule(_moduleOptions) {
4862
proxy: false,
4963
retry: false,
5064
https,
51-
...moduleOptions
52-
}
65+
headers
66+
})
5367

5468
// ENV overrides
5569

@@ -75,7 +89,7 @@ function axiosModule(_moduleOptions) {
7589

7690
// Convert http:// to https:// if https option is on
7791
if (options.https === true) {
78-
const https = s => s.indexOf('//localhost:') > -1 ? s : s.replace('http://', 'https://')
92+
const https = s => s.includes('//localhost:') ? s : s.replace('http://', 'https://')
7993
options.baseURL = https(options.baseURL)
8094
options.browserBaseURL = https(options.browserBaseURL)
8195
}

Diff for: lib/plugin.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,7 @@ export default (ctx, inject) => {
156156
// Create fresh objects for all default header scopes
157157
// Axios creates only one which is shared across SSR requests!
158158
// https://github.com/mzabriskie/axios/blob/master/lib/defaults.js
159-
const headers = {
160-
common : {
161-
'Accept': 'application/json, text/plain, */*'
162-
},
163-
delete: {},
164-
get: {},
165-
head: {},
166-
post: {},
167-
put: {},
168-
patch: {}
169-
}
159+
const headers = <%= JSON.stringify(options.headers, null, 4) %>
170160

171161
const axiosOptions = {
172162
baseURL,

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"@nuxtjs/proxy": "^1.3.3",
2727
"axios": "^0.19.0",
2828
"axios-retry": "^3.1.2",
29-
"consola": "^2.10.1"
29+
"consola": "^2.10.1",
30+
"defu": "^0.0.3"
3031
},
3132
"devDependencies": {
3233
"@babel/core": "latest",

Diff for: test/fixture/pages/asyncData.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<script>
88
export default {
9-
async asyncData({ app }) {
9+
async asyncData ({ app }) {
1010
const res = await app.$axios.$get('foo/bar')
1111
return {
1212
res

Diff for: test/fixture/pages/mounted.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<script>
88
export default {
9-
data() {
9+
data () {
1010
return {
1111
res: ''
1212
}
1313
},
1414
15-
async mounted() {
15+
async mounted () {
1616
// Request with full url becasue we are in JSDom env
1717
this.res = await this.$axios.$get('http://localhost:3000/test_api/foo/bar')
1818
}

Diff for: test/fixture/pages/ssr.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ let reqCtr = 1
1111
1212
export default {
1313
computed: {
14-
axiosSessionId() {
14+
axiosSessionId () {
1515
return this.$axios.defaults.headers.common.SessionId
1616
},
1717
18-
axiosEncoding() {
18+
axiosEncoding () {
1919
return this.$axios.defaults.headers.common['accept-encoding']
2020
}
2121
},
22-
fetch({ app, route }) {
22+
fetch ({ app, route }) {
2323
const doLogin = route.query.login !== undefined
2424
if (doLogin) {
2525
app.$axios.setHeader('SessionId', reqCtr++)

0 commit comments

Comments
 (0)