Skip to content

Commit

Permalink
feat: support proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Jan 28, 2018
1 parent 6ffcd84 commit 0d3be17
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
53 changes: 43 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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">
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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**
Expand Down Expand Up @@ -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.

Expand All @@ -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`
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
serverMiddleware: ['~/api.js'],
axios: {
prefix: `/test_api`,
proxyMode: true,
proxy: true,
credentials: true,
debug: true
},
Expand Down
32 changes: 31 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@nuxtjs/proxy@^1.1.4":
version "1.1.4"
resolved "https://registry.yarnpkg.com/@nuxtjs/proxy/-/proxy-1.1.4.tgz#71056d402d57c825a098c1651138a878ee6c0d23"
dependencies:
http-proxy-middleware "^0.17.4"

"@nuxtjs/youch@^4.2.3":
version "4.2.3"
resolved "https://registry.yarnpkg.com/@nuxtjs/youch/-/youch-4.2.3.tgz#36f8b22df5a0efaa81373109851e1d857aca6bed"
Expand Down Expand Up @@ -2616,6 +2622,10 @@ event-emitter@~0.3.5:
d "1"
es5-ext "~0.10.14"

eventemitter3@1.x.x:
version "1.2.0"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508"

events@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
Expand Down Expand Up @@ -3438,6 +3448,22 @@ http-errors@1.6.2, http-errors@~1.6.2:
setprototypeof "1.0.3"
statuses ">= 1.3.1 < 2"

http-proxy-middleware@^0.17.4:
version "0.17.4"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-0.17.4.tgz#642e8848851d66f09d4f124912846dbaeb41b833"
dependencies:
http-proxy "^1.16.2"
is-glob "^3.1.0"
lodash "^4.17.2"
micromatch "^2.3.11"

http-proxy@^1.16.2:
version "1.16.2"
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.16.2.tgz#06dff292952bf64dbe8471fa9df73066d4f37742"
dependencies:
eventemitter3 "1.x.x"
requires-port "1.x.x"

http-signature@~1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf"
Expand Down Expand Up @@ -4477,7 +4503,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0:
lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"

Expand Down Expand Up @@ -6368,6 +6394,10 @@ require-uncached@^1.0.3:
caller-path "^0.1.0"
resolve-from "^1.0.0"

requires-port@1.x.x:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"

resolve-cwd@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-2.0.0.tgz#00a9f7387556e27038eae232caa372a6a59b665a"
Expand Down

0 comments on commit 0d3be17

Please sign in to comment.