Permalink
Browse files

feat: support proxy

  • Loading branch information...
pi0 committed Jan 28, 2018
1 parent 6ffcd84 commit 0d3be173f2eb98910a6826788530893e837ccd38
Showing with 83 additions and 14 deletions.
  1. +43 −10 README.md
  2. +6 −1 lib/module.js
  3. +2 −1 package.json
  4. +1 −1 test/fixture/nuxt.config.js
  5. +31 −1 yarn.lock
@@ -1,6 +1,6 @@
<p align="center">
<img src="https://user-images.githubusercontent.com/5158436/30198986-d4c5d7f8-9485-11e7-9c3e-8b5f5f061f5f.png">
</p>
<h1 align="center">Axios</h1>

<p align="center"> Secure and Easy <a href="https://github.com/mzabriskie/axios">Axios</a> integration with Nuxt.js. </p>

<p align="center">
<a href="https://david-dm.org/nuxt-community/axios-module">
@@ -24,13 +24,9 @@
</a>
</p>

<h1 align="center">Axios</h1>

<p align="center"> Secure and Easy <a href="https://github.com/mzabriskie/axios">Axios</a> integration with Nuxt.js. </p>

[📖 Release Notes](./CHANGELOG.md)

If you are coming from an older release please be sure to read [Migration Guide](https://github.com/nuxt-community/axios-module/wiki/Migration-guide)
If you are coming from an older release please be sure to read [Migration Guide](https://github.com/nuxt-community/axios-module/wiki/Migration-guide).

## Features

@@ -46,6 +42,8 @@ If you are coming from an older release please be sure to read [Migration Guide]

✓ Automatically integrate with Nuxt.js progress bar

✓ Easily integrate with [Proxy Module](https://github.com/nuxt-community/proxy-module)

# Table of Contents

* [Setup](#setup)
@@ -64,6 +62,7 @@ If you are coming from an older release please be sure to read [Migration Guide]
* [baseURL](#baseurl)
* [browserBaseURL](#browserbaseurl)
* [progress](#progress)
* [proxy](#proxy)
* [credentials](#credentials)
* [debug](#debug)
* [proxyHeaders](#proxyheaders)
@@ -151,12 +150,15 @@ If you need to customize axios by registering interceptors and changing global c
**nuxt.config.js**

```js
{
modules: [
'@nuxtjs/axios',
],
plugins: [
'~/plugins/axios'
]
}
```

**plugins/axios.js**
@@ -294,7 +296,7 @@ Environment variable `API_URL` can be used to **override** `baseURL`.

### `browserBaseURL`

* Default: `baseURL` (or `prefix` when `options.proxyMode` is `true`)
* Default: `baseURL` (or `prefix` when `options.proxy` is enabled)

Base URL which is used and prepended to make requests in client side.

@@ -306,6 +308,38 @@ Environment variable `API_URL_BROWSER` can be used to **override** `browserBaseU

Integrate with Nuxt.js progress bar to show a loading bar while making requests. (Only on browser, when loading bar is available.)

### `proxy`

* Default: `false`

You can easily integrate Axios with [Proxy Module](https://github.com/nuxt-community/proxy-module) and is much recommended to prevent CORS and deployment problems.

**nuxt.config.js**

```js
{
modules: [
'@nuxtjs/axios'
],
axios: {
proxy: true
},
proxy: {
'/api/': 'http://api.example.com',
'/api2/': 'http://api.another-website.com'
}
}
```

**Note:** It is not required to manually register `@nuxtjs/proxy` module.

**Note:** `/api/` will be added to all requests to the API end point. If you need to remove it use `pathRewrite`:
```js
{ '/api/': 'http://api.example.com', pathRewrite: { '^/api/', '' } }
```

### `credentials`

* Default: `false`
@@ -335,7 +369,6 @@ Also helps making consistent requests in both SSR and Client Side code.

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


## License

[MIT License](./LICENSE) - Copyright (c) 2017 Nuxt Community
@@ -59,7 +59,7 @@ module.exports = function nuxtAxios (_moduleOptions) {

// Default browserBaseURL
if (!options.browserBaseURL) {
options.browserBaseURL = options.proxyMode ? prefix : options.baseURL
options.browserBaseURL = options.proxy ? prefix : options.baseURL
}

// Register plugin
@@ -69,6 +69,11 @@ module.exports = function nuxtAxios (_moduleOptions) {
options
})

// Proxy integration
if (options.proxy) {
this.requireModule('@nuxtjs/proxy')
}

/* eslint-disable no-console */
debug(
`BaseURL: ${chalk.green(options.baseURL)} (Browser: ${chalk.green(
@@ -34,12 +34,12 @@
]
},
"dependencies": {
"@nuxtjs/proxy": "^1.1.4",
"axios": "^0.17.1",
"chalk": "^2.3.0",
"debug": "^3.1.0"
},
"devDependencies": {
"nuxt": "^1.1.1",
"codecov": "^3.0.0",
"eslint": "^4.14.0",
"eslint-config-standard": "^11.0.0-beta.0",
@@ -51,6 +51,7 @@
"eslint-plugin-vue": "^2.1.0",
"jest": "^22.0.4",
"jsdom": "^11.5.1",
"nuxt": "^1.1.1",
"standard-version": "^4.2.0"
}
}
@@ -11,7 +11,7 @@ module.exports = {
serverMiddleware: ['~/api.js'],
axios: {
prefix: `/test_api`,
proxyMode: true,
proxy: true,
credentials: true,
debug: true
},

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 0d3be17

Please sign in to comment.