项目作者: fluture-js

项目描述 :
Create Fastify handlers using Futures
高级语言: JavaScript
项目地址: git://github.com/fluture-js/fluture-fastify.git
创建时间: 2019-02-04T09:09:47Z
项目社区:https://github.com/fluture-js/fluture-fastify

开源协议:MIT License

下载


Fluture Fastify

Build Status Greenkeeper badge

Create Fastify handlers using Futures from Fluture.

Usage

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

  1. import {handler, Typed} from 'fluture-fastify';
  2. import {after} from 'fluture';
  3. import createApp from 'fastify';
  4. const app = createApp({logger: true});
  5. const Json = Typed ('application/json; charset=utf-8');
  6. const action = (req, {number}) => after (100, Json (200, number));
  7. app.decorateReply ('locals', {number: 42});
  8. app.get ('/number', handler ('getNumber', action));
  9. app.listen (3000, '0.0.0.0');

API

Responses

Typed :: (Str, Num, Any) -⁠> Response a" class="reference-link">Typed :: (Str, Num, Any) -⁠> Response a

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

  1. const Plain = Typed ('text/plain; charset=utf-8');
  2. Plain (200, 'Number 42');

Json

  1. const Json = Typed ('application/json; charset=utf-8');
  2. Json (200, {number: 42});

Stream

  1. const Stream = Typed ('application/octet-stream');
  2. Stream (200, createReadStream ('file', 'utf8'));

Serialized :: (b -⁠> c, Str, Num, b) -⁠> Response a" class="reference-link">Serialized :: (b -⁠> c, Str, Num, b) -⁠> Response a

A typed response with a custom serializer.

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

Redirect :: (Num, Str) -⁠> Response a" class="reference-link">Redirect :: (Num, Str) -⁠> Response a

A redirection consisting of a URL and the status code.

Empty :: Response a" class="reference-link">Empty :: Response a

An empty response with status code 204.

NotFound :: Response a" class="reference-link">NotFound :: Response a

The default NotFound response.

Functions

handler :: (Str, (Req, a) -⁠> Future b (Response a)) -⁠> (Req, Res a) -⁠> undefined" class="reference-link">handler :: (Str, (Req, a) -⁠> Future b (Response a)) -⁠> (Req, Res a) -⁠> undefined

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.