@cloud-cli/gw

A REST HTTP server implementation mapping resources to JS classes

Usage no npm install needed!

<script type="module">
  import cloudCliGw from 'https://cdn.skypack.dev/@cloud-cli/gw';
</script>

README

Gateway

A minimal REST HTTP server implementation. Provides resources as JS classes.

REST API

List available resources

List all registered resources for discovery

GET /

Invoke a method in a resource

Just like any REST service, invoke a resource with an HTTP method:

[HEAD/GET/POST/PUT/DELETE/PATCH/OPTIONS] /[resource name]/...

JS API

Set up a Gateway instance and add resources to it

import { Gateway, Resource } from '@cloud-cli/gw';
import { createServer } from 'http';

class Ponies extends Resource {
  get(request, response) {
    response.writeHead(200);
    response.end(JSON.stringify(['list', 'of', 'ponies']));
  }
}

const gw = new Gateway();
gw.add('ponies', new Ponies());

createServer((request, response) => gw.dispatch(request, response)).listen(80);

Then invoke the resource with an HTTP call

GET /ponies

A resource can implement any of the valid HTTP methods: get, post, put, delete, patch and options. The methods are called with the request/response objects from a vanilla Node.JS HTTP server.

CORS

Add a property to a resource implmementation with the options to enable CORS. The available options are the same as the cors module

import { Resource } from '@cloud-cli/gw';

export class ResourceWithCors extends Resource {
  cors = { ... }
}

Body parser

Add a property to a resource implmementation with the options to enable CORS. The available options are the same as the body-parser module options

import { Resource } from '@cloud-cli/gw';

export class ResourceWithBody extends Resource {
  // provide one of the 4 available options
  body = {
    json: {};
    raw: {};
    text: {};
    urlencoded: {};
  };
}