https://github.com/fluture-js/fluture-fastify
Create Fastify handlers using Futures
https://github.com/fluture-js/fluture-fastify
fastify fluture
Last synced: about 1 month ago
JSON representation
Create Fastify handlers using Futures
- Host: GitHub
- URL: https://github.com/fluture-js/fluture-fastify
- Owner: fluture-js
- License: mit
- Created: 2019-02-04T09:09:47.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-21T11:06:41.000Z (about 6 years ago)
- Last Synced: 2025-11-24T03:16:02.208Z (7 months ago)
- Topics: fastify, fluture
- Language: JavaScript
- Size: 21.5 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluture Fastify
[](https://travis-ci.com/wearereasonablepeople/fluture-fastify) [](https://greenkeeper.io/)
Create [Fastify][] handlers using [Future][]s from [Fluture][].
## Usage
```console
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.
```javascript
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
### `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
```javascript
const Plain = Typed ('text/plain; charset=utf-8');
Plain (200, 'Number 42');
```
#### Json
```javascript
const Json = Typed ('application/json; charset=utf-8');
Json (200, {number: 42});
```
#### Stream
```javascript
const Stream = Typed ('application/octet-stream');
Stream (200, createReadStream ('file', 'utf8'));
```
### `Serialized :: (b -> c, Str, Num, b) -> Response a`
A typed response with a custom serializer.
```javascript
const Proto = Serialized (protoBuf.serialize, 'application/x-protobuf');
Proto (200, new protoBuf ());
```
### `Redirect :: (Num, Str) -> Response a`
A redirection consisting of a URL and the status code.
An empty response with status code 204.
The default NotFound response.
### Functions
### `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`.
[Fluture]: https://github.com/fluture-js/Fluture
[Future]: https://github.com/fluture-js/Fluture#future
[Fastify]: https://github.com/fastify/Fastify
[reply.send]: https://github.com/fastify/fastify/blob/master/docs/Reply.md#senddata