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

wb-rest

v0.2.4

Published

Simple abstraction over express to create RESTful APIs.

Downloads

9

Readme

wb-rest

IMPORTANT NOTICE: This library is still work in progress, so be aware that it may contain bugs or it may not have (yet) some feature you need. That being said, you are welcome to join us in the development if you will.

wb-rest is a library designed to ease the creation of REST HTTP APIs in node.js with Typescript. It tries to take away all the pain involved in developing an API (such as structuring the project, defining the models and the relationships between them) and, at the same time, this library tries to reduce the amount of code required to do so.

The way it achieves its goal is by providing you with base classes, helpers and decorators to define endpoints, database models, routing and so on.

Installation

In order to install wb-rest simply run:

npm install --save wb-rest

if you are using Typescript, you may want to add the typings folder to the "typeRoots" array in your tsconfig.json.

Let's see it in action

Let's see an example of the simplest API we can build using this library, so that you can get a glance of how it works.

import {HttpServer, Endpoint, GET} from 'wb-rest';

// Let's define a very simple endpoint.
@Endpoint('greetings')
class GreetingsEndpoint {
  /**
   * Here we are saying that all the GET requests to /greetings/say-hello should
   * be handled by the function below.
   */
  @GET('/say-hello')
  public static sayHello(req, res) {
    res.send('Hello there, stranger!');
  }
}

// Now, we need to create our HTTP server.
const server = new HttpServer({
  env: 'dev',
  version: 'v1',
  endpoints: [GreetingsEndpoint]
});

// Start the HTTP server.
server
  .listen(8080, 'localhost')
  .then(() => console.log('Server running on port 8080'))
  .catch(err => console.log(`Oops! Something went wrong: ${err.message}`));

If you run this code you should see the message Server running on port 8080 on your terminal. Try opening http://localhost:8080/v1/greetings/say-hello and you should see the greeting message.

Creating an HTTP Server

Creating an HTTP API with wb-rest is quite straigt-forward. Simply import the HttpServer class and instance it including your endpoints (we'll talk about this in the following section) in the config.

import {HttpServer} from 'wb-rest';

// Import your own middlewares and endpoints.
import UsersEndpoint from './endpoints/UsersEndpoint';

const server = new HttpServer({
  version: 'v1',  // API version.
  environment: 'dev',
  endpoints: [UsersEndpoint]
});

// Start the HTTP server.
server
  .listen(8080, 'localhost')
  .then(() => console.log('Server running on port 8080'))
  .catch(err => console.log(`Oops! Something went wrong: ${err.message}`));

Creating an Endpoint

An endpoint is simply a class whose methods are all public static. The class represents a resource in our REST API. Also, this class must be decorated with the Endpoint decorator. This decorator takes a string which will be the name of the resource in the API.

In this case we'll be implementing the Users endpoint, so the name of the resource will be users. This means that this API endpoint will be accessible under the URL http://localhost:8080/v1/users.

As you may have noted the resource URL is prefixed by the version of the API. You can prevent this by leaving the version field empty when initializing the server.

Well, this is what we have so far.

// File: endpoints/UsersEndpoint.ts
import {Endpoint} from 'wb-rest';

@Endpoint('users')
export default class UsersEndpoint {

}

But, of course, we only have an empty endpoint which does nothing, let's add some functionality to it. Let's say we want to return a hard-coded list of names when we perform a GET request to /v1/users/.

We can do that by using the @GET decorator form wb-rest. Let's add a getAll() method to the endpoint.

import {Endpoint, GET} from 'wb-rest';

@Endpoint('users')
export default class UsersEndpoint {
  @GET('/')
  public static getAll(req, res) {
    const names = ['Nick', 'John', 'Ann', 'Dean'];
    res.send(JSON.stringify(names));      
  }
}

You may have noticed that the method signature is pretty similar to what you would do if you were using express framework directly. You'll receive the request and response params and optionally a third param: next.

wb-rest also provides the @POST, @PUT, @PATCH and @DELETE decorators and they work the same way as @GET.

Permissions and Access Control

TODO: Add documentation for the @AccessControl decorator.

Persisting Data

TODO: Add documentation about the Database class, the @Model decorator and the Database.Model base class.