Skip to content

Commit 992062e

Browse files
authored
Merge pull request #7 from kimuraz/vuepress
Vuepress
2 parents 21eb095 + 13878f7 commit 992062e

File tree

12 files changed

+5027
-1453
lines changed

12 files changed

+5027
-1453
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ dist/
44
npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
7+
test/unit/coverage
78
coverage/
9+
deploy-docs.sh
810

911
# Editor directories and files
1012
.editorconfig

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
[![Build Status](https://travis-ci.org/kimuraz/vue-rest.svg?branch=dev)](https://travis-ci.org/kimuraz/vue-rest)
22
# vue-rest
33

4-
> Plugin for using vue + axios for REST API consumption
4+
Plugin for using vue + axios for REST API CRUD operations
55

66
## Installation
77

88
```shell
99
$ npm i -S vue-rest
10+
11+
OR
12+
13+
$ yarn add vue-rest
1014
```
1115

1216
## Documentation
1317

14-
[Gitbook](https://kimuraz.gitbooks.io/vue-rest/content/)
18+
Checkout the docs:
19+
20+
:book: [Docs](https://kimuraz.github.io/vue-rest/)
1521

1622
## Examples
1723

assets/vue-rest.png

38 KB
Loading

docs/.vuepress/config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
base: '/vue-rest/',
3+
title: 'Vue Rest',
4+
description: 'A fast way to make REST cruds',
5+
serviceWorker: false,
6+
themeConfig: {
7+
repo: 'kimuraz/vue-rest',
8+
editLinks: true,
9+
docsDir: 'docs',
10+
locales: {
11+
'/': {
12+
label: 'Home',
13+
editLinkText: 'Edit this page on GitHub',
14+
lastUpdated: 'Last Updated',
15+
nav: [
16+
{
17+
text: 'Getting started',
18+
link: '/started.md'
19+
},
20+
{
21+
text: '$api',
22+
link: '/prototype.md'
23+
},
24+
{
25+
text: 'Mixins',
26+
link: '/mixins.md'
27+
}
28+
],
29+
sidebar: [
30+
'intro.md',
31+
'started.md',
32+
'prototype.md',
33+
'mixins.md'
34+
]
35+
}
36+
}
37+
}
38+
}

docs/.vuepress/public/vue-rest.png

38 KB
Loading

docs/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
home: true
3+
heroImage: /vue-rest.png
4+
actionText: Get Started →
5+
actionLink: intro.md
6+
features:
7+
- title: Easy
8+
details: Use etiher the $api prototype or one of the plugins
9+
- title: Axios
10+
details: It uses axios as requests library
11+
- title: CRUD operations faster
12+
details: Use save() to save your form or load() to load you list
13+
footer: MIT Licensed | Copyright © 2018 J. Eduardo Kimura Reis
14+
---

docs/intro.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Vue Rest Docs
2+
3+
The vue-rest plugin was developed to help in VueJS applications that needs API request to do CRUD operations.
4+
5+
It implements two mixins: ApiList and ApiForm, and a prototype: $api.
6+
7+
I believe through this mixins you will be able to implement easily any CRUD that you need.
8+
9+
You're welcome to contribute to the project.

docs/mixins.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Mixins
2+
3+
## ApiForm
4+
### Intro
5+
6+
The ApiForm is responsible for implementing the methods for API requesting that usually a form does.
7+
8+
It requires a `route prop` and, for convenience, can handle two others props `objId` and `obj`. Both props are handled in mixin's `created()` hook, and the `obj` sets the `requestObj` data to its value and the `objId` loads a new instance of a object to `requestObj` returned from API in the `load()` method.
9+
10+
> See: [Vue Lifecycle Hooks](https://vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks)
11+
12+
### Methods
13+
14+
The following methods are implemented through this mixin:
15+
16+
`load(id, errCallback)`:
17+
18+
- Params: id, errCallback
19+
20+
- Functionality: It does a GET request to the given route /id and loads the response.data to the requestObj data.
21+
22+
`save(callback, errCallback)`:
23+
- Params: callback, errCallback
24+
- Functionality: It passes the received callback and errCallback to one of the following methods:
25+
- If the `requestObj` has an id `(requestObj.id)`:
26+
* `update(callback, errCallback)`:
27+
- Params: callback, errCallback
28+
- Functionality: It uses the $api prototype to do a PATCH requests using the given route and passing the requestObj as payload, passing the success response to the callback function and if there's any error it passes the it to the errCallback function.
29+
- Otherwise:
30+
* create(callback, errCallback):
31+
- Params: callback, errCallback
32+
- Functionality: It does the same thing as the update method but instead of a PATCH it does a POST to the API.`
33+
34+
### Using it
35+
36+
> Note that you need to pass a `route` prop or override the prop as:
37+
>
38+
> `route: { required: false, default: 'your-route' }`
39+
40+
```vue
41+
<template>
42+
<form @submit.prevent="save(showOk, showErr)">
43+
<input v-model="requestObj.firstName"/>
44+
<input v-model="requestObj.lastName"/>
45+
<button type="submit">
46+
Add
47+
</button>
48+
</form>
49+
</template>
50+
51+
<script>
52+
import { ApiForm } from 'vue-rest'
53+
54+
export default {
55+
name: 'personForm',
56+
mixins: [ApiForm],
57+
methods: {
58+
showOk(res) {
59+
console.log(res)
60+
alert('Sucessfully saved')
61+
},
62+
showErr(err) {
63+
console.log(err)
64+
alert(`An error ocurred: ${err}`)
65+
}
66+
}
67+
}
68+
</script>
69+
```
70+
71+
72+
## ApiList
73+
### Intro
74+
75+
The ApiList is responsible for implementing a route mandatory prop and the three following methods to load and handle list actions:
76+
77+
78+
### Methods
79+
80+
`loadList(callback, errCallback)`:
81+
82+
- Params: callback, errCallback
83+
- Functionality: It uses $api prototype to do a GET request in the given route and pass the response for the callback function if the response was successfully made, otherwise passes the error to the errCallback function.
84+
85+
`deleteObj(objId, callback, errCallback)`:
86+
87+
- Params: objId, callback, errCallback
88+
- Functionality: It uses $api prototype to do a DELETE request in the given route concatenated with the objIdand passes the response for the callback function if the response was successfully made, then it emits a delete event with the objId.
89+
90+
`editObj(obj)`:
91+
92+
- Params: obj
93+
- Functionality: It just emits an edit event with the given obj in its value.
94+
95+
> Note: You should probably handle the emitted value to pass to a ApiForm instance
96+
97+
98+
### Using it
99+
```vue
100+
<template>
101+
<ul>
102+
<li v-for="p in people" :key="p.id">
103+
{{ p.firstName }} {{ p.lastName }}
104+
</li>
105+
</ul>
106+
</template>
107+
108+
<script>
109+
import { ApiList } from 'vue-rest'
110+
111+
export default {
112+
name: 'peopleList',
113+
mixins: [ApiList],
114+
data() {
115+
return {
116+
people: [],
117+
}
118+
},
119+
created() {
120+
this.loadList(this.loadPeople, this.showErr)
121+
},
122+
methods: {
123+
loadPeople(res) {
124+
console.log(res)
125+
this.people = [...res.data.results]
126+
},
127+
showErr(err) {
128+
console.log(err)
129+
alert(`An error ocurred ${err}`)
130+
}
131+
}
132+
}
133+
134+
</script>
135+
136+
```

docs/prototype.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The $api prototype
2+
3+
The $api prototype is a wrapper for [axios](https://github.com/mzabriskie/axios) and you can use it easily anywhere in your application, it accepts the same options that axios does, just pass the option object when setting VueRest up (see [Adding to your project](started.md)).
4+
5+
Example:
6+
7+
```js
8+
export default {
9+
name: 'foodList',
10+
data() {
11+
return {
12+
ingredients: []
13+
}
14+
},
15+
created() {
16+
this.$api.get('/ingredients').then(res => {
17+
this.ingredients = res.data.results
18+
})
19+
}
20+
}
21+
```

docs/started.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Getting Started
2+
3+
## Installing
4+
5+
To install the plugin simply do:
6+
7+
```sh
8+
$ yarn add vue-rest
9+
10+
# OR
11+
12+
$ npm install vue-rest
13+
```
14+
15+
## Adding to your project
16+
17+
To add to your project, import it and the use `Vue.use(VueRest)` to properly use it.
18+
19+
> Optionally you can add `axiosOptions` that is responsible for any options related to [axios]('https://github.com/mzabriskie/axios%29/')
20+
21+
```js
22+
...
23+
import VueRest from 'vue-rest';
24+
...
25+
26+
Vue.use(VueRest, {
27+
axiosOptions: {
28+
baseURL: 'http://localhost/api/',
29+
}
30+
});
31+
```
32+
33+
## Using ApiForm and ApiList
34+
35+
These two mixins implements some methods, props and data to your component, to use them, do:
36+
37+
```js
38+
import { ApiForm } from 'vue-rest';
39+
40+
export default {
41+
...
42+
mixins: [ApiForm]
43+
...
44+
}
45+
```
46+
47+
```js
48+
import { ApiList } from 'vue-rest';
49+
50+
export default {
51+
...
52+
mixins: [ApiList]
53+
...
54+
}
55+
```

0 commit comments

Comments
 (0)