npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

purity-client-js

v1.2.0

Published

Purity Client: Filter and Sort Laravel Queries Elegantly

Downloads

12

Readme

Install

npm install purity-client-js

pnpm install purity-client-js

yarn add purity-client-js

Usage

Simple Laravel Controller

public function index(Request $request)
{
    return Model::filter()->sort()->paginate($request->query('perPage'));
}

Create Client

import { PurityClient } from 'purity-client-js';

const apiClient = new PurityClient('http://localhost:8000/api')

Laravel Pagination

Pass page, perPage for default laravel paginate method

GET /api/articles?page=1&perPage=15

apiClient.get('articles', {
    page: 1, perPage: 15
})

Basic Sorting

Queries can accept a sort parameter that allows sorting on one or multiple fields with the following syntax's.

GET /api/articles?sort[0]=title&sort[1]=slug:desc

apiClient.get('articles', {
    sort: ['title', 'slug:desc']
})

Sort by Relationships

All the usages of basic sorting are applicable. Use dot(.) notation to apply relationship in the following format.

GET /api/posts?sort=tags.name:asc

apiClient.get('articles', {
    sort: ['tags.name:asc']
})

Eq Filter

Find users with 'John' as their first name

GET /api/users?filters[name][$eq]=JalalLinuX

apiClient.get('users', {
  filters: {
    username: {
      $eq: 'JalalLinuX'
    }
  }
})

In Filter

Find multiple restaurants with ids 3, 6, 8

GET /api/restaurants?filters[id][$in][0]=3&filters[id][$in][1]=6&filters[id][$in][2]=8

apiClient.get('restaurants', {
  filters: {
    id: {
      $in: [3, 6, 8],
    },
  }
})

Between Filter

Find users with age between 20 and 30

GET /api/users?filters[age][$between][0]=20&filters[age][$between][1]=30

apiClient.get('users', {
  filters: {
    age: {
      $between: [20, 30],
    },
  }
})

Complex Filtering

Complex filtering is combining multiple filters using advanced methods such as combining $and & $or. This allows for more flexibility to request exactly the data needed.

Find books with two possible dates and a specific author.

GET /api/books?filters[$or][0][date][$eq]=2020-01-01&filters[$or][1][date][$eq]=2020-01-02&filters[author][name][$eq]=Kai%20doe

apiClient.get('books', {
  filters: {
    $or: [
      {
        date: {
          $eq: '2020-01-01',
        },
      },
      {
        date: {
          $eq: '2020-01-02',
        },
      },
    ],
    author: {
      name: {
        $eq: 'Kai doe',
      },
    },
  }
})

Relation Filtering

Relation filtering is filtering on a relation's fields.

Find restaurants owned by a chef who belongs to a 5-star restaurant

GET /api/restaurants?filters[chef][restaurants][stars][$eq]=5

apiClient.get('restaurants', {
  filters: {
    chef: {
      restaurants: {
        stars: {
          $eq: 5,
        },
      },
    },
  }
})

Complex Relation Filtering

Complex relation filtering is combining multiple relation filters using advanced methods such as combining $and & $or. This allows for more flexibility to request exactly the data needed.

Find restaurants owned by a chef who belongs to a 5-star restaurant and has a specific cuisine

GET /api/restaurants?filters[chef][restaurants][stars][$eq]=5&filters[chef][restaurants][cuisine][$eq]=Italian

apiClient.get('restaurants', {
  filters: {
    chef: {
      restaurants: {
        stars: {
          $eq: 5,
        },
        cuisine: {
          $eq: 'Italian',
        },
      },
    },
  }
})