项目作者: milroyfraser

项目描述 :
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
高级语言: JavaScript
项目地址: git://github.com/milroyfraser/sarala.git
创建时间: 2018-02-05T15:01:09Z
项目社区:https://github.com/milroyfraser/sarala

开源协议:MIT License

下载










Coverage Status

Sarala JS

Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent

API Documentation | Background Story

Install

  1. $ npm i sarala --save
  1. $ yarn add sarala

Basic Usage

Model Implementation

app/models/BaseModel.js
  1. import { Model } from 'sarala';
  2. import axios from 'axios';
  3. export default class BaseModel extends Model
  4. {
  5. baseUrl () {
  6. return 'https://sarala-demo.app/api';
  7. }
  8. request (config) {
  9. return axios.request(config);
  10. }
  11. }
app/models/Post.js
  1. import Model from './BaseModel';
  2. import Tag from './Tag';
  3. export default class Post extends Model {
  4. resourceName () {
  5. return 'posts';
  6. }
  7. fields () {
  8. return ['title', 'subtitle', 'body', 'slug'];
  9. }
  10. relationships () {
  11. return {
  12. tags: new Tag()
  13. };
  14. }
  15. }
app/models/Tag.js
  1. import Model from './BaseModel';
  2. export default class Tag extends Model {
  3. resourceName () {
  4. return 'tags';
  5. }
  6. fields () {
  7. return ['name'];
  8. }
  9. }

Fetching data

  1. import Post from './../models/Post';
  2. const post = new Post();
  3. // makes a GET request to https://sarala-demo.app/api/posts
  4. const fetchAllPosts = async () => {
  5. let posts = await post.with(['tags']).all();
  6. };

Insert

app/components/MyComponent.js
  1. import Tag from './../models/Tag';
  2. const tag = new Tag();
  3. tag.name = 'json-api';
  4. // makes a POST request to https://sarala-demo.app/api/tags
  5. tag.save();
  6. // or you can directly call tag.create();

Change log

Please see CHANGELOG for more information on what has changed recently.

API Documentation | Background Story