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

restleaf

v0.0.3

Published

a nodejs rest api toolkit

Downloads

5

Readme

restleaf

restleaf is a nodejs rest api toolkit, that allows you to easily get started with creating a restfull api

instalation

restleaf depends on leafless, so you have to install leafless first

$ npm install leafless
# or with yarn
$ yarn add leafless

Then install restleaf

$ npm install restleaf
# or with yarn
$ yarn add restleaf

initialization

set up restleaf by initializing leafless, and then passing it into the expose restleaf function

let LL = require('leafless');
let restleaf = require('restleaf');
let app = new LL({});

let options = {
  version:1, // the api version
}
let rest = restleaf(app, options);

app.listen(process.env.PORT || 4000);

restleaf options

when you initialize restleaf you provide an instance of Leafless and a set of options the options include

version

options.version is the api version and is prependended infront of all your endpoints
so that if options.version === 1 then you endpoints will look like this http://domain/v1/resource/param
and if options.version === 12 then you endpoints will look like this http://domain/v12/resource/param

adding your models

a restleaf model is just a javascript object that exposes functions for performing CRUD operations on a certain resource
you add a model by calling the rest.addModel passing in the model name, and the object the functions expected on the object are

fetchAll() called when we want a Array of everything
fetchById(id) called when we want a resource identified by a specific id
insert(data) called when we are creating a new entry
update(id, data) called when we are updating an existing value
delete(id) called when we want to delete an item of the specied id

the functions are implemented as an object with a method called exec that is the javascript function that is actually called during execution

exec MUST always return a PROMISE

That is...

let rest = restleaf(app, options);

rest.addModel('users', {
  // return all users
  fetchAll: {
    exec() {
      return db.users.fetch();
    }
  },

  // get a user by id
  fetchById: {
    exec(id) {
      return db.users.get(id);
    }
  },

  // insert a new user
  insert: {
    exec(data) {
      return db.users.insert(data);
    }
  },

  // update and existing user
  update: {
    exec(id, data) {
      return db.users.get(id).update(data);
    }
  },

  // delete and exisitig user
  delete: {
    exec(data) {
      return db.users.get(id).delete();
    }
  }
})