Coder Social home page Coder Social logo

lapis's Introduction

lapis

A toolkit building on API Resources in Laravel.

Build Status Coverage Status


Concept

RESTful API endpoints typically require more functionality than pagination alone. For example, a front-end or mobile application may implement a filtering scheme or require access to the endpoint's nested resources. Lapis intends to provide a lightweight filtering and nested resource pattern leveraging the existing facilities in Laravel 5.5.

Includes

Let's say we have an endpoint in a blogging platform to list our post resources returning a response like so:
GET https://api.myrecipeblog.com/posts

{
  "posts": [
    {
      "name": "Asparagus Steak Oscar",
      "category": "food",
      "postedAt": "2017-09-12 16:04:33",
      "authorId": 14
    },
    {
      "name": "Churchill Downs Mint Julep",
      "category": "drinks",
      "postedAt": "2017-10-05 18:32:12",
      "authorId": 15
    }
  ]
}

Ideally we would want to see details of a post's author in our front-end. Right, so make another request to a hypothetical users endpoint referencing a distinct list of authorIds from the posts response...No, absolutely not! We (hopefully) spent the time in our blogging platform's backend to model our data's relationships and set up foreign key constraints with appropriate indices. So let's put those models to work.

Lapis, leveraging Laravel's API Resources, allows us to add an include parameter on our request URL to retrieve the nested author (User) relationship. Our request URL and response would look something like this:

GET https://api.myrecipeblog.com/posts?include=author

{
  "posts": [
    {
      "name": "Asparagus Steak Oscar",
      "category": "food",
      "postedAt": "2017-09-12 16:04:33",
      "authorId": 14,
      "author": {
        "id": 14,
        "name": "John Doe",
        "forHire": true,
        "chefRating": 5
      }
    },
    {
      "name": "Churchill Downs Mint Julep",
      "category": "drinks",
      "postedAt": "2017-10-05 18:32:12",
      "authorId": 15,
      "author": {
        "id": 15,
        "name": "James Smith",
        "forHire": false,
        "chefRating": 1
      }
    }
  ]
}
Nested Includes

This will also work for nested relationships. For example, if our User model contained a favorites relationship we could request GET https://api.myrecipeblog.com/posts?include=author.favorites and an array of the author's favorites would be nested inside the author object.

Filtering

Let's go back to our original response above, for which we called the posts endpoint. Maybe we only want to see posts from the drinks category. We could alter our call from above like so:

GET https://api.myrecipeblog.com/posts?filter[category]=drinks

{
  "posts": [
    {
      "name": "Churchill Downs Mint Julep",
      "category": "drinks",
      "postedAt": "2017-10-05 18:32:12",
      "authorId": 15
    }
  ]
}

We can also specify multiple filters using this structure:

GET https://api.myrecipeblog.com/posts?filter[category]=drinks&filter[authorId]=15

Nested Filters

We discussed including an author relationship above. We can also filter on the author's details by making a request like so:

GET https://api.myrecipeblog.com/posts?filter[author.forHire]=true

Filter Operators

Up until this point we've just used filters to assert a field is equal to a given value. Filters understand the concept of operators. Here are a few examples:

List posts later than October 1, 2017.

GET https://api.myrecipeblog.com/posts?filter[postedAt{gte}]=2017-10-01

List posts where the author's name starts with John.

GET https://api.myrecipeblog.com/posts?filter[author.name{starts}]=John

Reference Implementation

Installation & Usage

lapis's People

Contributors

aejnsn avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

lapis's Issues

Logical operators for filters

Just a question, I assume filters are using logical AND when more than one filter is present? Any thoughts on how the URL schema may look for supporting OR?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.