Skip to content

Commit 25b3298

Browse files
committed
Add docs
1 parent 168eb26 commit 25b3298

File tree

6 files changed

+289
-4
lines changed

6 files changed

+289
-4
lines changed

docs/.vuepress/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const sidebars = {
55
collapsable: false,
66
children: [
77
'/guide/installation',
8+
'/guide/getting-started',
9+
'/guide/basic-usage',
10+
'/guide/configurations',
811
'/guide/sponsors'
912
]
1013
},

docs/api/model.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,42 @@ sidebarDepth: 2
44

55
# Model
66

7-
Coming soon...
7+
Vuex ORM Axios adds a few properties and methods to the Model object.
8+
9+
## Static Properties
10+
11+
### axios
12+
13+
- **`static axios: AxiosInstance | null`**
14+
15+
The axios isntance which was installed during the plugin installation process. Vuex ORM Axios adds a few properties and methods to the Model object. Vuex ORM Axios will use this Axios instance to perform any HTTP request.
16+
17+
Usually, this property will not become `null`. However, there's a case where you may need to add Axios instance manually, for example, when using Vuex ORM Axios with Nuxt.js. Int hat case, this property will temporary be `null`.
18+
19+
### globalApiConfig
20+
21+
- **`globalApiConfig: GlobalConfig`**
22+
23+
The property that holds global configuration. The value will be set automatically during the plugin installation process. **Do not mutate this property manually**.
24+
25+
- **`apiConfig: Config | null`**
26+
27+
The property that defines the Model configuration for the API call.
28+
29+
## Static Methods
30+
31+
### setAxios
32+
33+
- **`static setAxios(axios: AxiosInstance): void`**
34+
35+
The method to set the Axios instance manually. Usually, you don't have to call this method yourself. However, you might need to use this method to set Axios instance properly in some situations. Please [refer here](../guide/getting-started.html#nuxt-js-integration) for more detail.
36+
37+
### api
38+
39+
- **`static api(): Request`**
40+
41+
This method is going to return a new Request instance. Request instance is the wrapper for Axios, and it's used to perform any API request.
42+
43+
```js
44+
User.api().get('/api/users')
45+
```

docs/guide/basic-usage.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Basic Usage
2+
3+
After setting up Vuex ORM Axios, you may use `Model.api` method to perform api call.
4+
5+
```js
6+
User.api().get('/api/users')
7+
```
8+
9+
## Request
10+
11+
Vuex ORM Axios can perform all basic Axios requests, which is `get`, `post`, `put`, `patch`, `delete`, and `request`. These methods take the same arguments as Axios and perform exactly as same as Axios, except it's going to store response data to the store corresponding to the Model that is calling the api.
12+
13+
Available methods and the argument pattern is listed below. Remember, the argument is the same as the corresponding Axios methods. The `data` or `config` will be passed directly to the underlining Axios instance.
14+
15+
```js
16+
User.api().get(url, config)
17+
User.api().post(url, data, config)
18+
User.api().put(url, data, config)
19+
User.api().patch(url, data, config)
20+
User.api().delete(url, config)
21+
User.api().request(config)
22+
```
23+
24+
When calling any of the above methods, Vuex ORM Axios will persist the response data to the store. Let's say your api response is as follows.
25+
26+
```js
27+
// Response body data.
28+
29+
{
30+
"id": 1,
31+
"name": "John Doe",
32+
"age": 24
33+
}
34+
```
35+
36+
And if you call api from User Model, the above data will be inserted as User records to the store.
37+
38+
```js
39+
// Call api from User Model.
40+
User.api().get('/api/users')
41+
42+
// Then, inside Vuex Store.
43+
{
44+
users: {
45+
data: {
46+
1: { id: 1, name: 'John Doe', age: 24 }
47+
}
48+
}
49+
}
50+
```
51+
52+
Now, remember that Vuex ORM Axios will try to insert any response data to the store. For example, if you call the same API from Post Model, it's going to try to insert the response as Post records. So be aware of what Model to call the API from.
53+
54+
Also, note that these methods can take the same configuration as Axios.
55+
56+
```js
57+
User.api().get('users', {
58+
baseURL: 'https://example.com/api/'
59+
})
60+
```
61+
62+
There're additional configuration specific to Vuex ORM Axios as well. Please check out [Configurations page](configurations) for more.
63+
64+
## Response
65+
66+
The response object is a bit different from Axios response and contains 2 properties. One is `response`, which is the pure Axios response object, and the second one is the `entities`, which holds Vuex ORM persistent result.
67+
68+
```js
69+
const result = await User.api().get('/api/users')
70+
71+
// You may access Axios response object like this.
72+
result.response.status // <- 200
73+
74+
// And Vuex ORM persisted entities like so.
75+
result.entities // <- { users: [{ ... }] }
76+
```

docs/guide/configurations.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Configurations
2+
3+
You may configure many options in Vuex ORM Axios and its underlining Axios instance. There are 3 places to set options.
4+
5+
- Global Configuration
6+
- Model Configuration
7+
- Request Configuration
8+
9+
Global Configuration can be defined at the installation process by passing the option as the second argument of `VuexORM.use` method.
10+
11+
```js
12+
import axios from 'axios'
13+
import VuexORM from '@vuex-orm/core'
14+
import VuexORMAxios from '@vuex-orm/plugin-axios'
15+
16+
VuexORM.use(VuexORMAxios, {
17+
axios,
18+
headers: {'X-Requested-With': 'XMLHttpRequest'},
19+
baseURL: 'https://example.com/api/'
20+
})
21+
```
22+
23+
You may define configuration at the Model level too. To do so, define `static apiConfig` property to the Model.
24+
25+
```js
26+
import { Model } from '@vuex-orm/core'
27+
28+
class User extends Model {
29+
static entity = 'users'
30+
31+
static fields () {
32+
return {
33+
id: this.attr(null),
34+
name: this.attr('')
35+
}
36+
}
37+
38+
static apiConfig = {
39+
headers: {'X-Requested-With': 'XMLHttpRequest'},
40+
baseURL: 'https://example.com/api/'
41+
}
42+
}
43+
```
44+
45+
Finally, you can pass configuration when making the api call.
46+
47+
```js
48+
User.api().get('/api/users', {
49+
headers: {'X-Requested-With': 'XMLHttpRequest'},
50+
baseURL: 'https://example.com/api/'
51+
})
52+
```
53+
54+
The lower level configuration will overwrite a higher level of configs. Which means that the Request Config will overwrite Model Config, and Model Config will overwrite Global Config.
55+
56+
## Available Options
57+
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.
59+
60+
### dataKey
61+
62+
- **`dataKey: string | null`**
63+
64+
```js
65+
{
66+
ok: true,
67+
data: {
68+
id: 1,
69+
name: 'John Doe'
70+
}
71+
}
72+
```
73+
74+
In this case, data persistent to the store will probably fail, since actual data is inside `data` key, but Vuex ORM Axios is going to insert whole object including `ok` property.
75+
76+
For these situations, you can use `dataKey` property to specify which key to look for data.
77+
78+
```js
79+
User.api().get('/api/users', {
80+
dataKey: 'data'
81+
})
82+
```
83+
84+
With the above config, the data inside `data` key will be inserted to the store.

docs/guide/getting-started.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Setup
2+
3+
This page is a quick start guide to begin using Vuex ORM. It assumes you have a basic understanding of Vuex ORM. If you are not familiar with Vuex ORM, please check out the [Vuex ORM Documentation](https://vuex-orm.github.io/vuex-orm/).
4+
5+
At first, Vuex ORM Axios requires manually passing Axios instance during the installation process. Please make sure you have axios installed to your app.
6+
7+
To install Vuex ORM Axios to Vuex ORM, pass Vuex ORM Axios to the `VuexORM.use` method. Here, you should pass your axios instance as an option.
8+
9+
```js
10+
import axios from 'axios'
11+
import VuexORM from '@vuex-orm/core'
12+
import VuexORMAxios from '@vuex-orm/plugin-axios'
13+
14+
VuexORM.use(VuexORMAxios, { axios })
15+
```
16+
17+
Here is a more realistic example that shows how to install Vuex ORM to Vuex along with Vex ORM Axios.
18+
19+
```js
20+
import axios from 'axios'
21+
import Veux from 'vuex'
22+
import VuexORM from '@vuex-orm/core'
23+
import VuexORMAxios from '@vuex-orm/plugin-axios'
24+
import User from '@/models/User'
25+
26+
VuexORM.use(VuexORMAxios, { axios })
27+
28+
const database = new VuexORM.Database()
29+
30+
database.register(User)
31+
32+
const store = new Vuex({
33+
Plugins: [VuexORM.install(database)]
34+
})
35+
```
36+
37+
## Nuxt.js Integration
38+
39+
When using Vuex ORM Axios with Nuxt.js Axios Module, you can’t pass Axios instance directly from `store/index.js` file. Hence you must create a Nuxt.js plugin. Let's work through the process of setting up Vuex ORM Axios with Nuxt.js.
40+
41+
First, leave `axios` option empty at the plugin installation part.
42+
43+
```js
44+
// store/index.js
45+
46+
import Veux from 'vuex'
47+
import VuexORM from '@vuex-orm/core'
48+
import VuexORMAxios from '@vuex-orm/plugin-axios'
49+
import User from '@/models/User'
50+
51+
VuexORM.use(VuexORMAxios) // <- No axios option.
52+
53+
const database = new VuexORM.Database()
54+
55+
database.register(User)
56+
57+
const plugins = [VuexORM.install(database)]
58+
```
59+
60+
Next, create a plugin to set Axios instance to Vuex ORM Model directly.
61+
62+
```js
63+
// plugins/vuex-orm-axios.js
64+
65+
import { Model } from '@vuex-orm/core'
66+
67+
export default ({ $axios }) => {
68+
Model.setAxios($axios)
69+
}
70+
```
71+
72+
Finally, don’t forget to register the plugin to `nuxt.config.js`.
73+
74+
```js
75+
// nuxt.config.js
76+
77+
export default {
78+
//
79+
80+
modules: ['@nuxtjs/axios'],
81+
82+
plugins: ['@/plugins/vuex-orm-axios']
83+
}
84+
```

docs/guide/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Installation
22

3-
You can install Vuex ORM Axios via NPM, Yarn, or download it directly. Remember since Vuex ORM Axios is a plugin of [Vuex ORM](https://github.com/vuex-orm/vuex-orm), you need to install Vuex ORM and Vuex alongside with Vuex ORM Axios.
3+
You can install Vuex ORM Axios via NPM, Yarn, or download it directly. Remember since Vuex ORM Axios is a plugin of [Vuex ORM](https://github.com/vuex-orm/vuex-orm), you need to install Vuex ORM and Vuex alongside with Vuex ORM Axios. Also, Vuex ORM Axios requires manually passing Axios Instance. Don't forget to install Axios as well.
44

55
## NPM
66

77
```console
8-
$ npm install vue vuex @vuex-orm/core @vuex-orm/plugin-axios --save
8+
$ npm install axios vue vuex @vuex-orm/core @vuex-orm/plugin-axios --save
99
```
1010

1111
## Yarn
1212

1313
```console
14-
$ yarn add vue vuex @vuex-orm/core @vuex-orm/plugin-axios
14+
$ yarn add axios vue vuex @vuex-orm/core @vuex-orm/plugin-axios
1515
```
1616

1717
## Direct Download / CDN

0 commit comments

Comments
 (0)