Giter Site home page Giter Site logo

fluture-fastify's Introduction

Fluture Fastify

Build Status Greenkeeper badge

Create Fastify handlers using Futures from Fluture.

Usage

npm install --save fluture fluture-fastify

Allows using pure functions as Fastify handlers. Expects actions to return Futures and the reply to be decorated with a property locals that will be passed to every action as its second argument.

import {handler, Typed} from 'fluture-fastify';
import {after} from 'fluture';
import createApp from 'fastify';

const app = createApp({logger: true});

const Json = Typed ('application/json; charset=utf-8');
const action = (req, {number}) => after (100, Json (200, number));

app.decorateReply ('locals', {number: 42});
app.get ('/number', handler ('getNumber', action));

app.listen (3000, '0.0.0.0');

API

Responses

A typed response requires status code, some content and the content type. The type defines how the value is serialized. In order to see all the possible combinations see reply.send docs.

Plain text

const Plain = Typed ('text/plain; charset=utf-8');

Plain (200, 'Number 42');

Json

const Json = Typed ('application/json; charset=utf-8');

Json (200, {number: 42});

Stream

const Stream = Typed ('application/octet-stream');

Stream (200, createReadStream ('file', 'utf8'));

A typed response with a custom serializer.

const Proto = Serialized (protoBuf.serialize, 'application/x-protobuf');

Proto (200, new protoBuf ());

A redirection consisting of a URL and the status code.

An empty response with status code 204.

The default NotFound response.

Functions

Creates a Fastify handler from a named action. The action needs to either resolve to a Response or reject with anything. The rejected value will be send as a response with status code 500. The status code can be overwritten by rejecting with an Error that contains the prop statusCode.

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.