Skip to content

Commit 80e238c

Browse files
committed
Add fetch method
1 parent 0e056f9 commit 80e238c

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed

docs/.vuepress/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const sidebars = {
77
'/guide/installation',
88
'/guide/getting-started',
99
'/guide/basic-usage',
10+
'/guide/advanced-usage',
1011
'/guide/configurations',
1112
'/guide/sponsors'
1213
]
@@ -18,7 +19,8 @@ const sidebars = {
1819
title: 'API',
1920
collapsable: false,
2021
children: [
21-
'/api/model'
22+
'/api/model',
23+
'/api/request'
2224
]
2325
}
2426
]

docs/api/request.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
sidebarDepth: 2
3+
---
4+
5+
# Request
6+
7+
The Request object is the foundation for the Vuex ORM Axios, and you can call many methods to perform api request. You can obtain Request instance by `api` method on the Model.
8+
9+
```js
10+
const request = User.api()
11+
```
12+
13+
Usually, you could just call Request methods directly by method chaining.
14+
15+
```js
16+
User.api().get()
17+
```
18+
19+
## Constructor
20+
21+
- **``constructor(model: typeof Model)``**
22+
23+
Request instance requires the Model class to be passed to the constructor. It's automatically done when obtaining the Request object through `Model.api()` method. But you could manually construct Request object your self.
24+
25+
```js
26+
// This is equivalent to...
27+
const request = new Request(User)
28+
29+
// This.
30+
const request = User.api()
31+
```
32+
33+
## Instance Properties
34+
35+
### model
36+
37+
- **`model: typeof Model`**
38+
39+
The Model class that is attached to the Request instance.
40+
41+
### axios
42+
43+
- **`axios: AxiosInstance`**
44+
45+
The Axios instance that will be used to perform the request.
46+
47+
## Instance Methods
48+
49+
### get
50+
51+
- **`get(url: string, config: Config = {}): Promise<Response>`**
52+
53+
Performs a `GET` request. It takes the same argument as Axios's `get` method.
54+
55+
### post
56+
57+
- **`post(url: string, data: any = {}, config: Config = {}): Promise<Response>`**
58+
59+
Performs a `POST` request. It takes the same argument as Axios's `post` method.
60+
61+
### put
62+
63+
- **`put(url: string, data: any = {}, config: Config = {}): Promise<Response>`**
64+
65+
Performs a `PUT` request. It takes the same argument as Axios's `put` method.
66+
67+
### patch
68+
69+
- **`patch(url: string, data: any = {}, config: Config = {}): Promise<Response>`**
70+
71+
Performs a `PATCH` request. It takes the same argument as Axios's `patch` method.
72+
73+
### delete
74+
75+
- **`delete(url: string, config: Config = {}): Promise<Response>`**
76+
77+
Performs a `DELETE` request. It takes the same argument as Axios's `delete` method.
78+
79+
### fetch
80+
81+
- **`fetch(url: string, params: any, config: Config = {}): Promise<Response>`**
82+
83+
The `fetch` method works almost as same as `get` method, but it will take URL query parameters as the 2nd argument. Please [refer here](../guide/advanced-usage.html#fetch) for more details.

docs/guide/advanced-usage.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Advanced Usage
2+
3+
In addition to basic Axios request methods such as `get` and `post`, Vuex ORM Axios defines a more powerful and convenient method that you can use to call API and integrate with Vuex ORM better.
4+
5+
Vuex ORM defines the following methods.
6+
7+
- [fetch](#fetch)
8+
9+
## fetch
10+
11+
The `fetch` method works almost as same as `get` method, but it will take URL query parameters as the 2nd argument.
12+
13+
```js
14+
User.api().fetch('/api/users', {
15+
search: 'John'
16+
})
17+
```
18+
19+
The `fetch` method works almost as same as `get` method, but it will take URL query parameters as the 2nd argument.
20+
21+
The above method will call endpoint at `/api/users?search=john`.
22+
23+
You can also pass general config at 3rd argument.
24+
25+
```js
26+
User.api().fetch(
27+
'/api/users',
28+
{ search: 'John' },
29+
{ dataKey: 'data' }
30+
)
31+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"build:main": "node build/build.js",
1717
"lint": "tslint '{src,test}/**/*.ts' -c tslint.json -p . --fix",
1818
"test": "jest",
19-
"test:watch": "jest --watchAll",
19+
"test:watch": "jest --watch",
2020
"coverage": "jest --collect-coverage",
2121
"clean": "rm -rf dist && rm -rf lib && rm -rf coverage && rm -rf .nyc_output && rm -rf .tmp",
2222
"docs": "vuepress dev docs",

src/api/Request.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export default class Request {
6363
return this.request({ method: 'delete', url, ...config })
6464
}
6565

66+
/**
67+
* Fetch data from the api.
68+
*/
69+
fetch (url: string, params: any = {}, config: Config = {}): Promise<Response> {
70+
return this.get(url, { params, ...config })
71+
}
72+
6673
/**
6774
* Perform an api request.
6875
*/

test/feature/Request_Fetch.spec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import axios from 'axios'
2+
import MockAdapter from 'axios-mock-adapter'
3+
import { createStore, createState } from 'test/support/Helpers'
4+
import { Model, Fields } from '@vuex-orm/core'
5+
6+
describe('Feature - Request - Fetch', () => {
7+
let mock: MockAdapter
8+
9+
class User extends Model {
10+
static entity = 'users'
11+
12+
static fields (): Fields {
13+
return {
14+
id: this.attr(null),
15+
name: this.attr('')
16+
}
17+
}
18+
}
19+
20+
beforeEach(() => { mock = new MockAdapter(axios) })
21+
afterEach(() => { mock.reset() })
22+
23+
it('can perform a get request', async () => {
24+
mock.onGet('/api/users').reply(200, [
25+
{ id: 1, name: 'John Doe' },
26+
{ id: 2, name: 'Jane Doe' }
27+
])
28+
29+
const store = createStore([User])
30+
31+
await User.api().fetch('/api/users')
32+
33+
const expected = createState({
34+
users: {
35+
1: { $id: 1, id: 1, name: 'John Doe' },
36+
2: { $id: 2, id: 2, name: 'Jane Doe' }
37+
}
38+
})
39+
40+
expect(store.state.entities).toEqual(expected)
41+
})
42+
43+
it('can perform a get request with params', async () => {
44+
mock.onGet('/api/users', {
45+
params: { search: 'John' }
46+
}).reply(200, [
47+
{ id: 1, name: 'John Doe' },
48+
{ id: 2, name: 'Jane Doe' }
49+
])
50+
51+
const store = createStore([User])
52+
53+
await User.api().fetch('/api/users', { search: 'John' })
54+
55+
const expected = createState({
56+
users: {
57+
1: { $id: 1, id: 1, name: 'John Doe' },
58+
2: { $id: 2, id: 2, name: 'Jane Doe' }
59+
}
60+
})
61+
62+
expect(store.state.entities).toEqual(expected)
63+
})
64+
65+
it('can perform a get request with config', async () => {
66+
mock.onGet('/api/users', {
67+
params: { search: 'John' }
68+
}).reply(200, {
69+
data: [
70+
{ id: 1, name: 'John Doe' },
71+
{ id: 2, name: 'Jane Doe' }
72+
]
73+
})
74+
75+
const store = createStore([User])
76+
77+
await User.api().fetch('/api/users', { search: 'John' }, { dataKey: 'data' })
78+
79+
const expected = createState({
80+
users: {
81+
1: { $id: 1, id: 1, name: 'John Doe' },
82+
2: { $id: 2, id: 2, name: 'Jane Doe' }
83+
}
84+
})
85+
86+
expect(store.state.entities).toEqual(expected)
87+
})
88+
})

0 commit comments

Comments
 (0)