39
39
* [ Store] ( #store-nuxtserverinit )
40
40
* [ Store Actions] ( #store-actions )
41
41
* [ Options] ( #options )
42
- * [ Prefix, Host and port ] ( #prefix-host-and-port )
42
+ * [ Prefix, Host and Port ] ( #prefix-host-and-port )
43
43
* [ baseURL] ( #baseurl )
44
44
* [ browserBaseURL] ( #browserbaseurl )
45
45
* [ credentials] ( #credentials )
46
46
* [ debug] ( #debug )
47
47
* [ proxyHeaders] ( #proxyheaders )
48
48
* [ proxyHeadersIgnore] ( #proxyheadersignore )
49
- * [ redirectError] ( #redirecterror )
50
- * [ requestInterceptor] ( #requestinterceptor )
51
- * [ responseInterceptor] ( #responseinterceptor )
52
- * [ init] ( #init )
53
49
* [ disableDefaultErrorHandler] ( #disabledefaulterrorhandler )
54
- * [ errorHandler] ( #errorhandler )
55
50
* [ Helpers] ( #helpers )
56
51
* [ Fetch Style Requests] ( #fetch-style-requests )
57
52
* [ Set Header] ( #setheadername-value-scopescommon )
69
64
70
65
## Setup
71
66
72
- Install with npm :
67
+ Install with yarn :
73
68
74
69
``` bash
75
- > _ npm install @nuxtjs/axios
70
+ > _ yarn add @nuxtjs/axios
76
71
```
77
72
78
- Install with yarn :
73
+ Install with npm :
79
74
80
75
``` bash
81
- > _ yarn add @nuxtjs/axios
76
+ > _ npm install @nuxtjs/axios
82
77
```
83
78
84
79
** nuxt.config.js**
@@ -130,8 +125,6 @@ async nuxtServerInit ({ commit }, { app }) {
130
125
131
126
### Store actions
132
127
133
- (Needs Nuxt >= 1.0.0-RC8)
134
-
135
128
``` js
136
129
// In store
137
130
{
@@ -152,7 +145,7 @@ You can pass options using module options or `axios` section in `nuxt.config.js`
152
145
153
146
This options are used for default values of ` baseURL ` and ` browserBaseURL ` .
154
147
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.
156
149
157
150
Default value of ` prefix ` is ` / ` .
158
151
@@ -174,7 +167,7 @@ Environment variable `API_URL_BROWSER` can be used to **override** `browserBaseU
174
167
175
168
### ` credentials `
176
169
177
- * Default: ` true `
170
+ * Default: ` false `
178
171
179
172
Adds an interceptor to automatically set ` withCredentials ` config of axios when requesting to ` baseUrl `
180
173
which allows passing authentication headers to backend.
@@ -232,56 +225,13 @@ requestInterceptor: (config, { store }) => {
232
225
}
233
226
```
234
227
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
-
261
228
### ` disableDefaultErrorHandler `
262
229
263
230
* Default: ` false `
264
231
265
232
If you want to disable the default error handler for some reason, you can do it so
266
233
by setting the option ` disableDefaultErrorHandler ` to true.
267
234
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
-
285
235
## Helpers
286
236
287
237
### Fetch Style requests
@@ -355,59 +305,6 @@ this.$axios.setToken('123', 'Bearer', ['post', 'delete'])
355
305
this .$axios .setToken (false )
356
306
```
357
307
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
-
409
308
## License
410
309
411
- [ MIT License] ( ./LICENSE )
412
-
413
- Copyright (c) 2017 Nuxt Community
310
+ [ MIT License] ( ./LICENSE ) - Copyright (c) 2017 Nuxt Community
0 commit comments