makine

1.0.2 • Public • Published

makine

Basis for a reactive HTTP event-driven machine, typically useful for CI/CD triggers.

This allows for a quick implementation of logic in HTTP-based flows involving Web Hooks, REST APIs and scheduled monitoring.

Installation

npm install makine --save

Usage

As a new engine

const { extract on, reply, request, response, serve } = require('makine')();
 
serve()(
    on('GET', '/ok')(
        reply(response.empty())
    ),
    on('GET', '/hello')(
        reply(response.body({ message: "ciao" })
    )  
)

In an existing Express application

const app = ...
const { extract on, reply, request, response, serve } = require('makine')(app);
 
serve()(
    on('GET', '/ok')(
        reply(response.empty())
    ),
    on('GET', '/hello')(
        reply(response.body({ message: "ciao" })
    )  
)

Use another port

serve(7777)(
    ...
)

Request Pipeline

Makine request handling with .on() is actually an RxJS pipeline with helpers.

  on('POST', '/test')(
    reply(req => of(req).pipe(
      extract.body('path'), // extract the path from the original request
      map(path => 
        ({ uri: `http://localhost:3000/${path}` })), // prepare the new request
      request.perform(), // perform the new request
      extract.body('greeting'), // extracts the message from the response
      map(greeting => ({ message: `${greeting} you!` })), // prepare our response body
      request.onErrorMap(404, // in case the new request failed with 404 
        res => of({ message: `We had problem with ${res.uri}` })),
      flatMap(response.body())), // prepare the response with the prepared body

Package Sidebar

Install

npm i makine

Weekly Downloads

2

Version

1.0.2

License

Apache-2.0

Unpacked Size

23.7 kB

Total Files

8

Last publish

Collaborators

  • lbovet