diff --git a/README.md b/README.md index 066b4c44..baf8f73f 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -
-
-
Secure and Easy Axios integration with Nuxt.js.
-Secure and Easy Axios integration with Nuxt.js.
- [📖 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 diff --git a/lib/module.js b/lib/module.js index 3092949f..a369c533 100755 --- a/lib/module.js +++ b/lib/module.js @@ -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( diff --git a/package.json b/package.json index 9911dd2d..87130788 100755 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/fixture/nuxt.config.js b/test/fixture/nuxt.config.js index fc8da1c0..db7df93a 100644 --- a/test/fixture/nuxt.config.js +++ b/test/fixture/nuxt.config.js @@ -11,7 +11,7 @@ module.exports = { serverMiddleware: ['~/api.js'], axios: { prefix: `/test_api`, - proxyMode: true, + proxy: true, credentials: true, debug: true }, diff --git a/yarn.lock b/yarn.lock index b4fac39e..7e826a64 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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"