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

@simstudio/json-api-query

v3.0.11

Published

## A beautiful and elegant way to build urls for your REST API It's the fork of the archived [joelwmale/cogent-js](https://github.com/joelwmale/cogent-js) package. I've updated it to support TypeScript and provided some additional changes. The package i

Downloads

25

Readme

Json Api Query

A beautiful and elegant way to build urls for your REST API

It's the fork of the archived joelwmale/cogent-js package. I've updated it to support TypeScript and provided some additional changes.
The package is fully tested.

This package helps you to quickly build urls for a REST API, using fluent syntax.

🔥 If you use Laravel - the defaults of this package will work with spatie/laravel-query-builder.

Basic usage

Make a url by calling the functions you need in a beautiful and elegant way:

// Import
import { Query } from '@simstudio/json-api-query';

// If custom configuration is required, see the Additional Configuration section
const query = new Query();

// /posts?filter[name]=Bob&include=posts,comments&orderBy=-created_at
const url = query
  .for('posts') // the model you're selecting
  .where('name', 'Bob') // where the models `name` is 'Bob'
  .includes('posts', 'comments') // include the models related relationships: posts and comments
  .orderBy({field: 'created_at', direction: 'desc'}) // order by -created_at desc
  .orderBy({field: 'updated_at'}) // order by updated_at asc
  .get(); // generate the url and pass it into fetch!

Installation

Npm

npm i @simstudio/json-api-query

Yarn

yarn add @simstudio/json-api-query

Additional Configuration

Base Url

You can optionally set the base_url property when instantiating the class to automatically prepend the url to all urls:

import { Query } from '@simstudio/json-api-query';

const query = new Query({
  base_url: 'http://api.example.com'
});

// http://api.example.com/users?filter[name]=Bob
const url = query.for('users').where('name', 'Bob').url(); // or .get();

Available Methods

where()

// /users?filter[name]=Bob
const url = query.for('users').where('name', 'Bob').url(); // or .get();

whereIn()

// /users?filter[name]=bob,jerry
const url = query.for('users').whereIn('name', ['bob', 'jerry']).url(); // or .get();

select()

// /users?fields=name,age,date_of_birth
const url = query.for('users').select('name', 'age', 'date_of_birth').url(); // or .get();

includes()

// /users?include=posts
const url = query.for('users').includes('posts').url(); // or .get();

appends()

// /users?append=full_name,age
const url = query.for('users').appends('full_name', 'age').url(); // or .get();

limit()

// /users?limit=5
const url = query.for('users').limit(5).url(); // or .get();

limit() | Pagination

// /users?page=2&limit=5
const url = query.for('users').limit(5).page(2).url(); // or .get();

sort()

// /users?sort=-name,age
const url = query.for('users').sort({ field: 'name', direction: 'desc' }, { field: 'age' }).url(); // or .get();

Custom parameters

If required, you can also append your own custom parameters to the url by passing an object to the params() function.

// /users?format=admin
const url = query.for('users').params({ format: 'admin' }).url(); // or .get();

Customizing Query Parameters

If you need to change the default values for the query parameters, you can optionally pass in a configuration object when initializing your query object.

import { Query } from '@simstudio/json-api-query';

const query = new Query({
  queryParameters: {
    include: 'include_custom',
    filters: 'filter_custom',
    sort: 'sort_custom',
    fields: 'fields_custom',
    append: 'append_custom',
    page: 'page_custom',
    limit: 'limit_custom'
  }
});

Contributing

Please see CONTRIBUTING for details.