Giter Club home page Giter Club logo

protopost's Introduction

ProtoPost

Quickly create POST-only REST APIs using Express routers.

Installation

npm install https://github.com/tehzevo/protopost

Usage

Server

var ProtoPost = require("protopost");

var api = new ProtoPost({
  //callbacks take an object as input, and return a json-serializable object
  echo: (data) => data,
  ping: (data) => Date.now(),
  one: (data) => 1,
  add: (data) => data[0] + data[1],
  //supports async routes
  promise: async (data) => await new Promise((res, rej) => setTimeout(() => res(""), 1000)),
  //ProtoPost objects can be used in place of callbacks for a nested structure
  test: new ProtoPost({
    foo: (data) => "foo",
    bar: (data) => "bar",
  }),
  errors: new ProtoPost({
    //errors returned result in http status 400
    "your-fault": (data) => new Error("your fault"),
    //errors thrown result in http status 500
    "my-fault": (data) => {throw Error("my fault")},
  }),
  get: new ProtoPost(
    () => "hello get"
  , get=true),
//the final argument to ProtoPost is an optional callback for "/"
}, (data) => "welcome to the api!");

//start the server using express on port 3000 at /api
api.start(3000, "/api");

//or, if you want to use your own express instance instead of .start()
var express = require("express");
var app = express();
app.use("/api", api.router);
app.listen(3000, () => console.log("Listening on port 3000!"))

This will create an api with the following POST routes:

/api                    # "welcome to the api!"
/api/echo               # echos back the json sent
/api/ping               # gives the current time
/api/one                # returns 1
/api/add                # adds inputs [0] and [1]
/api/promise            # waits one second and then returns an empty string
/api/test/foo           # "foo"
/api/test/bar           # "bar"
/api/errors/your-fault  # returns an error 400
/api/errors/my-fault    # returns an error 500

Client

var protopost = require("protopost").client;

var HOST = "http://127.0.0.1:3000";

(async () => {
  var hello = await protopost(`${HOST}/api`);
  console.log(hello);
  var time = await protopost(`${HOST}/api/ping`);
  console.log(`The time is now ${new Date(time).toLocaleString()}`);
  var wait = await protopost(`${HOST}/api/promise`);
  console.log("Hey that took a while!");
  var get = await protopost(`${HOST}/api/get`, get=true);
  console.log(get);
})();

TODO

  • Allow removal of routes

protopost's People

Contributors

tehzevo avatar

Stargazers

 avatar Anna Allen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.