Skip to content

Commit

Permalink
feat: allow adding custom headers with nuxt config (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored and pi0 committed Oct 21, 2019
1 parent fdb9837 commit 3e38906
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
21 changes: 21 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,24 @@ Also helps making consistent requests in both SSR and Client Side code.
* Default `['host', 'accept', 'cf-ray', 'cf-connecting-ip', 'content-length']`

Only efficient when `proxyHeaders` is set to true. Removes unwanted request headers to the API backend in SSR.

### `headers`

Headers added to all requests. If provided, will be merged with the defaults.

```js
{
common: {
'Accept': 'application/json, text/plain, */*'
},
delete: {},
get: {},
head: {},
post: {},
put: {},
patch: {}
}
```

- **NOTE:** Do NOT include any credentials or tokens here. One can easily access them.
- **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.
22 changes: 18 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path')
const consola = require('consola')
const defu = require('defu')

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

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

// Headers
const headers = {
common: {
'Accept': 'application/json, text/plain, */*'
},
delete: {},
get: {},
head: {},
post: {},
put: {},
patch: {}
}

// Apply defaults
const options = {
const options = defu(moduleOptions, {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
browserBaseURL: null,
credentials: false,
Expand All @@ -48,8 +62,8 @@ function axiosModule(_moduleOptions) {
proxy: false,
retry: false,
https,
...moduleOptions
}
headers
})

// ENV overrides

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

// Convert http:// to https:// if https option is on
if (options.https === true) {
const https = s => s.indexOf('//localhost:') > -1 ? s : s.replace('http://', 'https://')
const https = s => s.includes('//localhost:') ? s : s.replace('http://', 'https://')
options.baseURL = https(options.baseURL)
options.browserBaseURL = https(options.browserBaseURL)
}
Expand Down
12 changes: 1 addition & 11 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,7 @@ export default (ctx, inject) => {
// Create fresh objects for all default header scopes
// Axios creates only one which is shared across SSR requests!
// https://github.com/mzabriskie/axios/blob/master/lib/defaults.js
const headers = {
common : {
'Accept': 'application/json, text/plain, */*'
},
delete: {},
get: {},
head: {},
post: {},
put: {},
patch: {}
}
const headers = <%= JSON.stringify(options.headers, null, 4) %>

const axiosOptions = {
baseURL,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@nuxtjs/proxy": "^1.3.3",
"axios": "^0.19.0",
"axios-retry": "^3.1.2",
"consola": "^2.10.1"
"consola": "^2.10.1",
"defu": "^0.0.3"
},
"devDependencies": {
"@babel/core": "latest",
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/pages/asyncData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script>
export default {
async asyncData({ app }) {
async asyncData ({ app }) {
const res = await app.$axios.$get('foo/bar')
return {
res
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/pages/mounted.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<script>
export default {
data() {
data () {
return {
res: ''
}
},
async mounted() {
async mounted () {
// Request with full url becasue we are in JSDom env
this.res = await this.$axios.$get('http://localhost:3000/test_api/foo/bar')
}
Expand Down
6 changes: 3 additions & 3 deletions test/fixture/pages/ssr.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ let reqCtr = 1
export default {
computed: {
axiosSessionId() {
axiosSessionId () {
return this.$axios.defaults.headers.common.SessionId
},
axiosEncoding() {
axiosEncoding () {
return this.$axios.defaults.headers.common['accept-encoding']
}
},
fetch({ app, route }) {
fetch ({ app, route }) {
const doLogin = route.query.login !== undefined
if (doLogin) {
app.$axios.setHeader('SessionId', reqCtr++)
Expand Down

0 comments on commit 3e38906

Please sign in to comment.