Package Exports
- laravel-rest-api-query-builder
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (laravel-rest-api-query-builder) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
laravel-rest-api-query-builder
JavaScript library to build RESTful API HTTP calls with Eloquent's-like syntax. This is made to work with RESTful APIs that use laravel-rest-api package.
This package is just a modification to milroyfraser/sarala in order to make it compatible with laravel-rest-api package.
All credits goes to milroyfraser.
Original package's documentation
Install
$ npm i laravel-rest-api-query-builder --save
$ yarn add laravel-rest-api-query-builder
Basic Usage
Model Implementation
app/models/BaseModel.js
import { Model } from 'laravel-rest-api-query-builder';
import axios from 'axios';
export default class BaseModel extends Model
{
request (config) {
return axios.request(config);
}
}
app/models/Post.js
import Model from './BaseModel';
import Comment from './Comment';
import Tag from './Tag';
import User from './User';
export default class Post extends Model {
getNamespace () {
return 'posts';
}
getFields () {
return ['title', 'subtitle', 'body', 'slug'];
}
getDates () {
return ['published_at'];
}
getRelations () {
return {
author: new User(),
comments: new Comment(),
tags: new Tag()
};
}
computed () {
return {
full_date (post) {
return post.published_at.format('MMMM Do YYYY');
},
human_date (post) {
return post.published_at.fromNow();
}
};
}
}
app/models/Tag.js
import Model from './BaseModel';
export default class Tag extends Model {
getNamespace () {
return 'tags';
}
getFields () {
return ['name'];
}
}
Fetching data
import Post from './../models/Post';
const post = new Post();
// makes a GET request to https://sarala-demo.app/api/posts/{id}
const findPost = async (id) => {
let post = await post.find(id);
};
// makes a GET request to https://sarala-demo.app/api/posts
const fetchAllPosts = async () => {
let posts = await post.all();
};
// makes a GET request to https://sarala-demo.app/api/posts/?page[size]=10&page[number]={page}
const paginatePosts = async (page) => {
let posts = await post.paginate(10, page);
};
Insert
app/components/MyComponent.js
import Tag from './../models/Tag';
const tag = new Tag();
tag.name = 'json-api';
// makes a POST request to https://sarala-demo.app/api/tags
tag.save();
// or you can directly call tag.create();