Elegant and easy way to build requests for REST APIs.
This package helps you quickly to build requests for REST APIs. Keep your code clean and elegant.
🔥 If you use Laravel, this package matchs perfectly with spatie/laravel-query-builder.
WARNING ❗️
This is a draft. Do not use it yet. Stable release soon.
Basic usage
Give me the result for a given criteria, include some entities, append extra fields and order the result!
let posts = await Post
.where('status', 'ACTIVE')
.include('user', 'category')
.append('likes')
.orderBy('-created_at')
.orderBy('category_id')
.get()
Just give me the first occurrence from response:
let post = await Post
.where('status', 'ACTIVE')
.first()
Nice! Now i want a specific object:
let post = await Post.find(1)
Edit this and send it back:
post.title = 'Awsome!'
post.save()
Lets create a new object and post it:
let post = new Post()
post.title = 'Another one'
post.save()
We can use relationships:
let user = await User.find(1)
let posts = user
.posts()
.get()
Installation
yarn add vue-api-query
NUXT
Create a plugin ~/plugins/vue-api-query.js
import { Model } from 'vue-api-query'
export default function (ctx, injext) {
Model.$http = ctx.$axios
}
And register it on nuxt.config.js
plugins: [
'~plugins/vue-api-query'
]
VUE
// TODO
Configuration
Define a base model
Your base model should extend from vue-api-query
Model. Use base models is good practice in order to abstract configurations from your domain models.
models/Model.js
import { Model as BaseModel } from 'vue-api-query'
export default class Model extends BaseModel {
baseURL () {
return 'http://my-api.com'
}
request (config) {
return this.$http.request(config)
}
}
Define your domain models
Just extends from your base model... and done!
It automatically pluralizes based on class name. So REST API base resource for User
class would be /users
.
models/User.js
import Model from './Model'
export default class User extends Model {
}
If you need to customize the resource name just implement the resource()
method.
import Model from './Model'
export default class User extends Model {
resource()
{
return 'userz'
}
}
Of course you can add extra methods and computed properties like this:
import Model from './Model'
export default class User extends Model {
get fullname()
{
return `${this.firstname} ${this.lastname}`
}
makeBirthday()
{
this.age += 1
}
}
You can set up relationships:
import Model from './Model'
import Post from './Post'
export default class User extends Model {
posts () {
return this.hasMany(Post)
}
}
Full example
/models/Post.js
import Model from './Model'
export default class Post extends Model {
}
/models/User.js
import Model from './Model'
import Post from './Post'
export default class User extends Model {
posts () {
return this.hasMany(Post)
}
get fullname()
{
return `${this.firstname} ${this.lastname}`
}
makeBirthday()
{
this.age += 1
}
}
If the backend responds with ...
{
id: 1,
firstname: "John",
lastname: "Doe",
age: 25
}
We can do this:
let user = await User.find(1)
console.log(user.fullname)
user.makeBirthday()
user.save()
Then save()
method will send back the new payload:
{
firstname: "John",
lastname: "Doe",
age: 26
}
Play with relationships:
let user = await User.find(1)
let posts = await user.posts().get()
let posts = await user
.posts()
.where(...)
.append(...)
.include(...)
.orderBy(...)
.get()
Thanks
Why another package if we have those ... Because currently (march, 2018) they restricted backend response to JSON API Specification :(
Contact
Twitter @robsontenorio