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

srai

v0.0.1-dev

Published

Simplistic RESTful API framework

Downloads

6

Readme

restLib

Note this project is only intended for personal projects

restLib is a simplistic API framework, built on top of expressJS. It is intended to help aid in the setup of RESTful APIs in a structured way. It is ismilar in concept to frameworks such as Loopback, however, is entended to be far less combersome.

Installation

  1. Clone this repository
  2. run npm ci

Usage

restLib generates RESTful endpoints based on model descriptions. It also allows for custom controllers.

Creating Models

To add a new RESTful model, add a new file with the extention model.js into the models directory. This file will export a model description.

Sample

module.exports = {
  provider: "mongodb",
  schema: {
    _id: {
      type: "mongoid",
      primary: true,
      required: true,
    },
    name: {
      type: "string",
      required: false,
    },
  },
};

Provider

Every model description needs to define what database provider to use to store and retrieve the data.

Available Providers

  • mongodb

Schema

Every model description needs to define a schema. Each schema entry equates to one column in a database table.

Schema Options

type defines the datatype for the schema key

| name | description | | ------- | ---------------------- | | mongoid | MongoDB UUID | | string | Basic string (varchar) |

primary is a boolean value (defaults to false) which deternines if the key should be treated as a primary key

required is a boolean value (defaults to false) which determines if the key is required to be set on RESTful create and update opperations.

Creating Controllers

Controllers can be added to augment the RESTful endpoints with custom logic. Models are NOT required to have a controller. To create a controller add a file with the extension .controler.js to the controllers directory.

Sample

module.exports = {
  auth: (request, response, next) => {
    next(request, response);
  },
  deleteMany: {
    skip: true,
    fn: (request, response, results) => {
      response.send(405);
    },
  },
  endpoints: [
    {
      path: "and/stuff",
      method: "get",
      fn: (request, response) => {
        response.send("Custom!!!");
      },
    },
  ],
};

auth

Every controller can have a custom auth middleware which will be applied to every RESTful request on the model.

Override endoints

Every controller can accept an override object for each RESTful endpoint.

skip is a boolean value (default false) that disables the default CRUD opperation of the endpoint

fn is a function that will be ran after the default CRUD operation has completed (unless skipped)

Available Override Endpoints

  • get
  • getAll
  • put
  • putAll
  • delete
  • deleteMany

endpoints

Every controller can have a number of custom endpoints. These are provided as an array of endpoint objects

Endpoint options

path is a string path to append to the model's default path (/{modelname}/{path}). This path conforms to Express' path format.

method is the HTTP method the endpoint will listen on.

fn is the express function the endpoint will use.

Default RESTful Endpoints

  • get (GET:/model/:id)
  • getAll (GET:/model/)
  • put (PUT:/model/:id)
  • putAll (PUT:/model/)
  • delete (DELETE:/model/:id)
  • deleteMany (DELETE:/model/)