Skip to content

Commit 647b58f

Browse files
author
Pooya Parsa
committed
feat: rewrite plugin
1 parent 672dbb9 commit 647b58f

File tree

5 files changed

+92
-280
lines changed

5 files changed

+92
-280
lines changed

Diff for: README.md

+8-111
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,14 @@
3939
* [Store](#store-nuxtserverinit)
4040
* [Store Actions](#store-actions)
4141
* [Options](#options)
42-
* [Prefix, Host and port](#prefix-host-and-port)
42+
* [Prefix, Host and Port](#prefix-host-and-port)
4343
* [baseURL](#baseurl)
4444
* [browserBaseURL](#browserbaseurl)
4545
* [credentials](#credentials)
4646
* [debug](#debug)
4747
* [proxyHeaders](#proxyheaders)
4848
* [proxyHeadersIgnore](#proxyheadersignore)
49-
* [redirectError](#redirecterror)
50-
* [requestInterceptor](#requestinterceptor)
51-
* [responseInterceptor](#responseinterceptor)
52-
* [init](#init)
5349
* [disableDefaultErrorHandler](#disabledefaulterrorhandler)
54-
* [errorHandler](#errorhandler)
5550
* [Helpers](#helpers)
5651
* [Fetch Style Requests](#fetch-style-requests)
5752
* [Set Header](#setheadername-value-scopescommon)
@@ -69,16 +64,16 @@
6964

7065
## Setup
7166

72-
Install with npm:
67+
Install with yarn:
7368

7469
```bash
75-
>_ npm install @nuxtjs/axios
70+
>_ yarn add @nuxtjs/axios
7671
```
7772

78-
Install with yarn:
73+
Install with npm:
7974

8075
```bash
81-
>_ yarn add @nuxtjs/axios
76+
>_ npm install @nuxtjs/axios
8277
```
8378

8479
**nuxt.config.js**
@@ -130,8 +125,6 @@ async nuxtServerInit ({ commit }, { app }) {
130125

131126
### Store actions
132127

133-
(Needs Nuxt >= 1.0.0-RC8)
134-
135128
```js
136129
// In store
137130
{
@@ -152,7 +145,7 @@ You can pass options using module options or `axios` section in `nuxt.config.js`
152145

153146
This options are used for default values of `baseURL` and `browserBaseURL`.
154147

155-
Can be customized with `API_PREFIX`, `API_HOST` (or `HOST`) and `API_PORT` (or `PORT`) environment variables too.
148+
Can be customized with `API_PREFIX`, `API_HOST` (or `HOST`) and `API_PORT` (or `PORT`) environment variables.
156149

157150
Default value of `prefix` is `/`.
158151

@@ -174,7 +167,7 @@ Environment variable `API_URL_BROWSER` can be used to **override** `browserBaseU
174167

175168
### `credentials`
176169

177-
* Default: `true`
170+
* Default: `false`
178171

179172
Adds an interceptor to automatically set `withCredentials` config of axios when requesting to `baseUrl`
180173
which allows passing authentication headers to backend.
@@ -232,56 +225,13 @@ requestInterceptor: (config, { store }) => {
232225
}
233226
```
234227

235-
### `responseInterceptor`
236-
237-
* Default: `null`
238-
239-
```js
240-
responseInterceptor: (response, ctx) => {
241-
return response
242-
}
243-
```
244-
245-
Function for manipulating axios responses.
246-
247-
### `init`
248-
249-
* Default: `null`
250-
251-
Function `init(axios, ctx)` to do additional things with axios. Example:
252-
253-
```js
254-
axios: {
255-
init(axios, ctx) {
256-
axios.defaults.xsrfHeaderName = 'X-CSRF-TOKEN'
257-
}
258-
}
259-
```
260-
261228
### `disableDefaultErrorHandler`
262229

263230
* Default: `false`
264231

265232
If you want to disable the default error handler for some reason, you can do it so
266233
by setting the option `disableDefaultErrorHandler` to true.
267234

268-
### `errorHandler`
269-
270-
* Default: (Return promise rejection with error)
271-
272-
Function for custom global error handler.
273-
This example uses nuxt default error page.
274-
275-
If you define a custom error handler, the default error handler provided by this package will be overridden.
276-
277-
```js
278-
axios: {
279-
errorHandler (errorReason, { error }) {
280-
error('Request Error: ' + errorReason)
281-
}
282-
},
283-
```
284-
285235
## Helpers
286236

287237
### Fetch Style requests
@@ -355,59 +305,6 @@ this.$axios.setToken('123', 'Bearer', ['post', 'delete'])
355305
this.$axios.setToken(false)
356306
```
357307

358-
## Dynamic API Backend
359-
360-
Please notice that, `API_URL` is saved into bundle on build, CANNOT be changed
361-
on runtime! You may use [proxy](../proxy) module for dynamically route api requests to different backend on test/staging/production.
362-
363-
**Example: (`nuxt.config.js`)**
364-
365-
```js
366-
{
367-
modules: [
368-
'@nuxtjs/axios',
369-
'@nuxtjs/proxy'
370-
],
371-
proxy: [
372-
['/api', { target: process.env.PROXY_API_URL || 'http://www.mocky.io', pathRewrite: { '^/api': '/v2' } }]
373-
]
374-
}
375-
```
376-
377-
Start Nuxt
378-
379-
```
380-
[AXIOS] Base URL: http://localhost:3000/api | Browser: /api
381-
[HPM] Proxy created: /api -> http://www.mocky.io
382-
[HPM] Proxy rewrite rule created: "^/api" ~> "/v2"
383-
```
384-
385-
Now you can make requests to backend: (Works fine in both SSR and Browser)
386-
387-
```js
388-
async asyncData({ app }) {
389-
// Magically makes request to http://www.mocky.io/v2/59388bb4120000dc00a672e2
390-
const nuxt = await app.$axios.$get('59388bb4120000dc00a672e2')
391-
392-
return {
393-
nuxt // -> { nuxt: 'Works!' }
394-
}
395-
}
396-
```
397-
398-
Details
399-
400-
* `'@nuxtjs/axios'`
401-
402-
* By default axios plugin sets base url to `http://[host]:[port]/api` which is `http://localhost:3000/api`
403-
404-
* `'/api': 'http://www.mocky.io/v2'`
405-
* This line creates a server middleware to pass requests from `/api` to `http://www.mocky.io/v2`
406-
* We used `pathRewrite` to remove `/api` from starting of requests and change it to `/v2`
407-
* For more information and advanced usage please refer to [proxy](https://github.com/nuxt-community/modules/blob/master/packages/proxy) docs.
408-
409308
## License
410309

411-
[MIT License](./LICENSE)
412-
413-
Copyright (c) 2017 Nuxt Community
310+
[MIT License](./LICENSE) - Copyright (c) 2017 Nuxt Community

Diff for: lib/module.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ module.exports = function nuxtAxios (_moduleOptions) {
3636
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
3737
browserBaseURL: null,
3838
proxyMode: false,
39-
credentials: true,
39+
credentials: false,
4040
proxyHeaders: true,
4141
proxyHeadersIgnore: ['accept', 'host'],
4242
debug: false,
43-
disableDefaultErrorHandler: false,
44-
redirectError: {}
43+
disableDefaultErrorHandler: false
4544
},
4645
moduleOptions
4746
)

0 commit comments

Comments
 (0)