Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
cogent-js
Advanced tools
Readme
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.
Make a url by calling the functions you need in a beautiful and elegant way:
// Import
const { Query } = require("cogent-js");
// 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('-created_at') // order by -created_at desc
.get(); // generate the url and pass it into fetch!
npm i cogent-js
yarn add cogent-js
You can optionally set the base_url
property when instantiating the class to automatically preprend the url to all urls:
const { Query } = require('cogent-js');
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();
// /users?filter[name]=Bob
const url = query.for('users').where('name', 'Bob').url(); // or .get();
// /users?filter[name]=bob,jerry
const url = query.for('users').whereIn('name', ['bob', 'jerry']).url(); // or .get();
// /users?fields=name,age,date_of_birth
const url = query.for('users').select('name', 'age', 'date_of_birth').url(); // or .get();
// /users?include=posts
const url = query.for('users').includes('posts').url(); // or .get();
// /users?append=full_name,age
const url = query.for('users').appends('full_name', 'age').url(); // or .get();
// /users?limit=5
const url = query.for('users').limit(5).url(); // or .get();
// /users?page=2&limit=5
const url = query.for('users').limit(5).page(2).url(); // or .get();
// /users?sort=-name,age
const url = query.for('users').sort('-name', 'age').url(); // or .get();
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();
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.
const { Query } = require("cogent-js");
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'
}
});
Please see CONTRIBUTING for details.
Twitter @joelwmale
FAQs
A simple and elegant way to build urls for your REST API
The npm package cogent-js receives a total of 40 weekly downloads. As such, cogent-js popularity was classified as not popular.
We found that cogent-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.