Skip to content

Commit 5fc2dc4

Browse files
committed
Refactor few coding styles and types
1 parent b3a4d2e commit 5fc2dc4

File tree

5 files changed

+48
-31
lines changed

5 files changed

+48
-31
lines changed

docs/guide/configurations.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ The lower level configuration will overwrite a higher level of configs. Which me
5757

5858
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.
5959

60-
### dataTransformer
60+
### dataKey
6161

62-
- **`dataTransformer?: ((response: AxiosResponse) => object)`**
62+
- **`dataKey?: string | null`**
6363

64-
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.
6565

6666
```js
6767
{
@@ -73,50 +73,64 @@ All Axios configurations are available. For those, please refer to [the Axios do
7373
}
7474
```
7575

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.
7779

7880
```js
7981
User.api().get('/api/users', {
80-
dataTransformer: ({ data, headers }) => {
81-
// Do stuff with headers
82-
// Do stuff with data
83-
84-
return data.data
85-
}
82+
dataKey: 'data'
8683
})
8784
```
8885

8986
With the above config, the data inside `data` key will be inserted to the store.
9087

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.
9289
93-
### dataKey
90+
### dataTransformer
9491

95-
- **`dataKey?: string | null`**
92+
- **`dataTransformer?: ((response: AxiosResponse) => Record | Record[])`**
9693

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.
9895

9996
```js
10097
{
10198
ok: true,
102-
data: {
99+
record: {
103100
id: 1,
104101
name: 'John Doe'
105102
}
106103
}
107104
```
108105

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.
110107

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+
return response.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.
112121

113122
```js
114123
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+
}
116130
})
117131
```
118132

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.
120134
121135
### save
122136

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
7979
"collectCoverageFrom": [
8080
"src/**/*.ts",
81+
"!src/index.cjs.ts",
8182
"!**/node_modules/**"
8283
],
8384
"coverageReporters": [

src/api/Request.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosInstance, AxiosResponse } from 'axios'
2-
import { Model } from '@vuex-orm/core'
2+
import { Model, Record, Collections } from '@vuex-orm/core'
33
import Config from '../contracts/Config'
44
import Response from './Response'
55

@@ -140,13 +140,15 @@ export default class Request {
140140
/**
141141
* Persist the response data to the vuex store.
142142
*/
143-
private async persistResponseData (response: AxiosResponse, config: Config): Promise<any> {
143+
private async persistResponseData (response: AxiosResponse, config: Config): Promise<Collections | null> {
144144
if (!config.save) {
145145
return null
146146
}
147147

148148
if (config.delete !== undefined) {
149-
return this.model.delete(config.delete as any)
149+
await this.model.delete(config.delete as any)
150+
151+
return null
150152
}
151153

152154
return this.model.insertOrUpdate({
@@ -156,10 +158,10 @@ export default class Request {
156158

157159
/**
158160
* Get data from the given response object. If the `dataTransformer` config is
159-
* provided, it tries to execute the method with the response as param.
160-
* If the `dataKey` config is provided, it tries to fetch the data at that key.
161+
* provided, it tries to execute the method with the response as param. If the
162+
* `dataKey` config is provided, it tries to fetch the data at that key.
161163
*/
162-
private getDataFromResponse (response: AxiosResponse, config: Config): any {
164+
private getDataFromResponse (response: AxiosResponse, config: Config): Record | Record[] {
163165
if (config.dataTransformer) {
164166
return config.dataTransformer(response)
165167
}

src/api/Response.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosResponse } from 'axios'
2-
import { Model } from '@vuex-orm/core'
2+
import { Model, Collections } from '@vuex-orm/core'
33
import Config from '../contracts/Config'
44

55
export default class Response {
@@ -21,12 +21,12 @@ export default class Response {
2121
/**
2222
* Entities created by Vuex ORM.
2323
*/
24-
entities: any
24+
entities: Collections | null
2525

2626
/**
2727
* Create a new response instance.
2828
*/
29-
constructor (model: typeof Model, config: Config, response: AxiosResponse, entities: any) {
29+
constructor (model: typeof Model, config: Config, response: AxiosResponse, entities: Collections | null) {
3030
this.model = model
3131
this.config = config
3232
this.response = response

src/contracts/Config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AxiosRequestConfig, AxiosResponse } from 'axios'
2-
import { Model } from '@vuex-orm/core'
2+
import { Model, Record } from '@vuex-orm/core'
33

44
export interface Config extends AxiosRequestConfig {
5-
dataKey?: string,
6-
dataTransformer?: ((response: AxiosResponse) => object),
5+
dataKey?: string
6+
dataTransformer?: ((response: AxiosResponse) => Record | Record[])
77
save?: boolean
88
delete?: string | number | ((model: Model) => boolean)
99
actions?: {

0 commit comments

Comments
 (0)