You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/configurations.md
+33-19Lines changed: 33 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,11 +57,11 @@ The lower level configuration will overwrite a higher level of configs. Which me
57
57
58
58
All Axios configurations are available. For those, please refer to [the Axios documentation](https://github.com/axios/axios#request-config). In addition to Axios options, Vuex ORM Axios takes few more options specific to the plugin usage.
This option will let you transform the response before send it to the store. Let's say your response from the server looks like below.
64
+
This option will define which key to look for when persisting response data. Let's say your response from the server looks like below.
65
65
66
66
```js
67
67
{
@@ -73,50 +73,64 @@ All Axios configurations are available. For those, please refer to [the Axios do
73
73
}
74
74
```
75
75
76
-
For these situations, you can use `dataTransform` property to specify how you want to transform the data. The whole response is send as callback param.
76
+
In this case, data persistent to the store will probably fail, since actual data is inside the `data` key, but Vuex ORM Axios is going to insert whole object including `ok` property.
77
+
78
+
For these situations, you can use `dataKey` property to specify which key to look for data.
77
79
78
80
```js
79
81
User.api().get('/api/users', {
80
-
dataTransformer: ({ data, headers }) => {
81
-
// Do stuff with headers
82
-
// Do stuff with data
83
-
84
-
returndata.data
85
-
}
82
+
dataKey:'data'
86
83
})
87
84
```
88
85
89
86
With the above config, the data inside `data` key will be inserted to the store.
90
87
91
-
It is very useful when you need to transform a given response to be handle by Vuex ORM. For instance, if you format your response with the [JSON:API specs](https://jsonapi.org/), you can transform your response with this callback.
88
+
> **NOTE:** When `dataTransformer` key is set, this option will be ignored.
92
89
93
-
### dataKey
90
+
### dataTransformer
94
91
95
-
-**`dataKey?: string | null`**
92
+
-**`dataTransformer?: ((response: AxiosResponse) => Record | Record[])`**
96
93
97
-
This option will define which key to look for when persisting response data. Let's say your response from the server looks like below.
94
+
This option will let you transform the response before persisting it to the store. Let's say your response from the server looks like below.
98
95
99
96
```js
100
97
{
101
98
ok:true,
102
-
data: {
99
+
record: {
103
100
id:1,
104
101
name:'John Doe'
105
102
}
106
103
}
107
104
```
108
105
109
-
In this case, data persistent to the store will probably fail, since actual data is inside the `data` key, but Vuex ORM Axios is going to insert whole object including `ok` property.
106
+
You can use `dataTransform` property to specify how you want to transform the data. The whole Axios response will be passed as callback argument.
110
107
111
-
For these situations, you can use `dataKey` property to specify which key to look for data.
108
+
```js
109
+
User.api().get('/api/users', {
110
+
dataTransformer: (response) => {
111
+
returnresponse.data.record
112
+
}
113
+
})
114
+
```
115
+
116
+
With the above config, the data inside `record` key will be inserted to the store.
117
+
118
+
It is very useful when you need to transform a given response to be handle by Vuex ORM. For instance, if you format your response with the [JSON:API specs](https://jsonapi.org/), you can transform your response with this callback.
119
+
120
+
You can always use object destructuring to get specific properties from the response object too.
112
121
113
122
```js
114
123
User.api().get('/api/users', {
115
-
dataKey:'data'
124
+
dataTransformer: ({ data, headers }) => {
125
+
// Do stuff with headers...
126
+
// Do stuff with data...
127
+
128
+
return data
129
+
}
116
130
})
117
131
```
118
132
119
-
With the above config, the data inside `data` key will be inserted to the store.
133
+
> **NOTE:** When `dataTransformer` key is set, `dataKey` option will be ignored.
0 commit comments