Skip to content

Commit 0d3be17

Browse files
author
Pooya Parsa
committed
feat: support proxy
1 parent 6ffcd84 commit 0d3be17

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

README.md

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<p align="center">
2-
<img src="https://user-images.githubusercontent.com/5158436/30198986-d4c5d7f8-9485-11e7-9c3e-8b5f5f061f5f.png">
3-
</p>
1+
<h1 align="center">Axios</h1>
2+
3+
<p align="center"> Secure and Easy <a href="https://github.com/mzabriskie/axios">Axios</a> integration with Nuxt.js. </p>
44

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

27-
<h1 align="center">Axios</h1>
28-
29-
<p align="center"> Secure and Easy <a href="https://github.com/mzabriskie/axios">Axios</a> integration with Nuxt.js. </p>
30-
3127
[📖 Release Notes](./CHANGELOG.md)
3228

33-
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)
29+
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).
3430

3531
## Features
3632

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

4743
✓ Automatically integrate with Nuxt.js progress bar
4844

45+
✓ Easily integrate with [Proxy Module](https://github.com/nuxt-community/proxy-module)
46+
4947
# Table of Contents
5048

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

153152
```js
153+
{
154154
modules: [
155155
'@nuxtjs/axios',
156156
],
157+
157158
plugins: [
158159
'~/plugins/axios'
159160
]
161+
}
160162
```
161163

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

295297
### `browserBaseURL`
296298

297-
* Default: `baseURL` (or `prefix` when `options.proxyMode` is `true`)
299+
* Default: `baseURL` (or `prefix` when `options.proxy` is enabled)
298300

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

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

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

311+
### `proxy`
312+
313+
* Default: `false`
314+
315+
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.
316+
317+
**nuxt.config.js**
318+
319+
```js
320+
{
321+
modules: [
322+
'@nuxtjs/axios'
323+
],
324+
325+
axios: {
326+
proxy: true
327+
},
328+
329+
proxy: {
330+
'/api/': 'http://api.example.com',
331+
'/api2/': 'http://api.another-website.com'
332+
}
333+
}
334+
```
335+
336+
**Note:** It is not required to manually register `@nuxtjs/proxy` module.
337+
338+
**Note:** `/api/` will be added to all requests to the API end point. If you need to remove it use `pathRewrite`:
339+
```js
340+
{ '/api/': 'http://api.example.com', pathRewrite: { '^/api/', '' } }
341+
```
342+
309343
### `credentials`
310344

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

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

338-
339372
## License
340373

341374
[MIT License](./LICENSE) - Copyright (c) 2017 Nuxt Community

lib/module.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = function nuxtAxios (_moduleOptions) {
5959

6060
// Default browserBaseURL
6161
if (!options.browserBaseURL) {
62-
options.browserBaseURL = options.proxyMode ? prefix : options.baseURL
62+
options.browserBaseURL = options.proxy ? prefix : options.baseURL
6363
}
6464

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

72+
// Proxy integration
73+
if (options.proxy) {
74+
this.requireModule('@nuxtjs/proxy')
75+
}
76+
7277
/* eslint-disable no-console */
7378
debug(
7479
`BaseURL: ${chalk.green(options.baseURL)} (Browser: ${chalk.green(

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
]
3535
},
3636
"dependencies": {
37+
"@nuxtjs/proxy": "^1.1.4",
3738
"axios": "^0.17.1",
3839
"chalk": "^2.3.0",
3940
"debug": "^3.1.0"
4041
},
4142
"devDependencies": {
42-
"nuxt": "^1.1.1",
4343
"codecov": "^3.0.0",
4444
"eslint": "^4.14.0",
4545
"eslint-config-standard": "^11.0.0-beta.0",
@@ -51,6 +51,7 @@
5151
"eslint-plugin-vue": "^2.1.0",
5252
"jest": "^22.0.4",
5353
"jsdom": "^11.5.1",
54+
"nuxt": "^1.1.1",
5455
"standard-version": "^4.2.0"
5556
}
5657
}

test/fixture/nuxt.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
serverMiddleware: ['~/api.js'],
1212
axios: {
1313
prefix: `/test_api`,
14-
proxyMode: true,
14+
proxy: true,
1515
credentials: true,
1616
debug: true
1717
},

yarn.lock

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
esutils "^2.0.2"
1111
js-tokens "^3.0.0"
1212

13+
"@nuxtjs/proxy@^1.1.4":
14+
version "1.1.4"
15+
resolved "https://registry.yarnpkg.com/@nuxtjs/proxy/-/proxy-1.1.4.tgz#71056d402d57c825a098c1651138a878ee6c0d23"
16+
dependencies:
17+
http-proxy-middleware "^0.17.4"
18+
1319
"@nuxtjs/youch@^4.2.3":
1420
version "4.2.3"
1521
resolved "https://registry.yarnpkg.com/@nuxtjs/youch/-/youch-4.2.3.tgz#36f8b22df5a0efaa81373109851e1d857aca6bed"
@@ -2616,6 +2622,10 @@ event-emitter@~0.3.5:
26162622
d "1"
26172623
es5-ext "~0.10.14"
26182624

2625+
eventemitter3@1.x.x:
2626+
version "1.2.0"
2627+
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-1.2.0.tgz#1c86991d816ad1e504750e73874224ecf3bec508"
2628+
26192629
events@^1.0.0:
26202630
version "1.1.1"
26212631
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924"
@@ -3438,6 +3448,22 @@ http-errors@1.6.2, http-errors@~1.6.2:
34383448
setprototypeof "1.0.3"
34393449
statuses ">= 1.3.1 < 2"
34403450

3451+
http-proxy-middleware@^0.17.4:
3452+
version "0.17.4"
3453+
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-0.17.4.tgz#642e8848851d66f09d4f124912846dbaeb41b833"
3454+
dependencies:
3455+
http-proxy "^1.16.2"
3456+
is-glob "^3.1.0"
3457+
lodash "^4.17.2"
3458+
micromatch "^2.3.11"
3459+
3460+
http-proxy@^1.16.2:
3461+
version "1.16.2"
3462+
resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.16.2.tgz#06dff292952bf64dbe8471fa9df73066d4f37742"
3463+
dependencies:
3464+
eventemitter3 "1.x.x"
3465+
requires-port "1.x.x"
3466+
34413467
http-signature@~1.1.0:
34423468
version "1.1.1"
34433469
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf"
@@ -4477,7 +4503,7 @@ lodash.uniq@^4.5.0:
44774503
version "4.5.0"
44784504
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
44794505

4480-
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:
4506+
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:
44814507
version "4.17.4"
44824508
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
44834509

@@ -6368,6 +6394,10 @@ require-uncached@^1.0.3:
63686394
caller-path "^0.1.0"
63696395
resolve-from "^1.0.0"
63706396

6397+
requires-port@1.x.x:
6398+
version "1.0.0"
6399+
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
6400+
63716401
resolve-cwd@^2.0.0:
63726402
version "2.0.0"
63736403
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-2.0.0.tgz#00a9f7387556e27038eae232caa372a6a59b665a"

0 commit comments

Comments
 (0)