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

kno-rest

v1.0.2

Published

simple rest client helper

Downloads

5

Readme

KnoRest

an opinionated client for interacting with JSON rest endpoints

Installation

npm i --save-dev kno-rest

Notes

This client has several opinions, and was initially built for quickly working with rails JSON apis that I often end up building. The conventions it follows work well with my workflow, but your mileage may vary.

As an example of this, see the src/default_headers.js file where you'll see that all the rest calls include the X-CSRF-Token header which is plucked from the rails page on which the application lives. Also, we always set the Accept header to application/json

Basic Usage

Create a new instance and call a crud endpoint:

cont ENDPOINT = "/api/v1/messages"
async function getMessages() {
  let messageService = new KRest(ENDPOINT)
  let messages = await messagesService.index() // makes a GET call to endpoint
  return messages
}

Methods

index

Run a GET call against endpoint:

kRest.index()

create

Run a POST call against endpoint:

kRest.create({message: {title: "hi", user_id: 3}})

show

Run a GET call against member route of endpoint with an id

kRest.show(45)

update

Run a PUT call against member route of endpoint with an id

let message = {id: 45, title:"hello", user_id: 3}
kRest.update(message.id, {message})

destroy

Run a DELETE call against member route of endpoint with an id

kRest.destroy(45)

returns true if call was successful, false if not

non-standard REST endpoints

The above methods all take advantage of two methods on the kno-rest object. collection and member. Using them you can buildup endpoint urls and execute ajax calls to non-standard REST endpoints as well

For example assume you have typical REST crud stuff for a Foo at /api/v1/foo, but you also have a member route at /api/v1/foo/:id/bars. You can use the member method to hit it with various method requests.

let ENDPOINT = "/api/v1/foo"
krest = new KnoRest(ENDPOINT)
let bar = {name: 'baz'}
kRest.member('POST', 32, {bar}, 'bar')
// sends  POST to '/api/v1/foo/32/bar' with the {bar} object

Same for collection routes:

let ENDPOINT = "/api/v1/foo"
krest = new KnoRest(ENDPOINT)
krest.collection('GET', {from: '7d', to: '2d'}, 'sort')
// calls GET on /api/v1/foo/sort with the from and to params