# wb-rest

> Simple abstraction over express to create RESTful APIs.

Latest version **0.2.4** (published 2017-06-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install wb-rest
pnpm add wb-rest
yarn add wb-rest
bun add wb-rest
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.4 |
| Published | 2017-06-26 |
| First published | 2017-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 14 |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | yes |
| Author | Nicolas Gonzalvez |
| Maintainers | ngonzalvez |
| Keywords | rest, api, http, server |

## Links

- npm: https://www.npmjs.com/package/wb-rest
- npm.io page: https://npm.io/package/wb-rest

## Dependencies (14)

- [pg](https://npm.io/package/pg.md) ^6.2.2
- [ejs](https://npm.io/package/ejs.md) ^2.5.6
- [knex](https://npm.io/package/knex.md) ^0.13.0
- [express](https://npm.io/package/express.md) ^4.15.2
- [bookshelf](https://npm.io/package/bookshelf.md) ^0.10.3
- [typescript](https://npm.io/package/typescript.md) ^2.3.2
- [@types/knex](https://npm.io/package/@types/knex.md) 0.0.50
- [boom-express](https://npm.io/package/boom-express.md) ^0.2.1
- [express-boom](https://npm.io/package/express-boom.md) ^2.0.0
- [pretty-error](https://npm.io/package/pretty-error.md) ^2.1.0
- [@types/express](https://npm.io/package/@types/express.md) ^4.0.35
- [@types/bookshelf](https://npm.io/package/@types/bookshelf.md) ^0.9.1
- [bookshelf-modelbase](https://npm.io/package/bookshelf-modelbase.md) ^2.10.3
- [bookshelf-cascade-delete](https://npm.io/package/bookshelf-cascade-delete.md) ^2.0.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.2.4 (latest) — 2017-06-26
- 0.2.3 — 2017-06-26
- 0.2.2 — 2017-06-26
- 0.2.1 — 2017-06-26
- 0.2.0 — 2017-06-22

## 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.

~~~typescript
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.

~~~typescript
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.

~~~typescript
// 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.

~~~typescript
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<T>** base class.

---
_Source: https://npm.io/package/wb-rest · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
