Skip to content

Instantly share code, notes, and snippets.

@MarkusPfundstein
Created March 10, 2017 15:05
Show Gist options
  • Save MarkusPfundstein/dece230b540a3cf4053c0af0ee31f709 to your computer and use it in GitHub Desktop.
Save MarkusPfundstein/dece230b540a3cf4053c0af0ee31f709 to your computer and use it in GitHub Desktop.
example of Reader.T(Task) and express
const Task = require('data.task');
const { Reader, Maybe } = require('ramda-fantasy');
const ReaderTask = Reader.T(Task);
const R = require('ramda');
const express = require('express');
const app = express();
const MOCK_DB = {
5 : {
user: 'markus'
}
};
const GLOBALENV = {
db : {
get(id) {
const user = MOCK_DB[id];
return user != null
? Task.of(user)
: Task.rejected({ code: 404, message: 'user not found'});
}
}
}
app.getF = function(route, requestFn) {
this.get(route, (req, res) =>
requestFn(req)
.run(GLOBALENV)
.fork(error => res.status(error.code).send(error.message),
data => res.json(data)));
}
/* HERE IS OUR USER LEVEL CODE */
const fetchUser = id => ReaderTask(env => env.db.get(id));
app.getF('/user/:id', req => fetchUser(req.params.id));
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment