express-fp
Type safe request handlers for Express. TypeScript compatible.
- Validate Express requests (body and query objects) using io-ts.
- Construct Express responses without mutation using express-result-types.
Example
Below is small example that demonstrates request body and query validation using io-ts and fully typed response construction using express-result-types.
See the full example.
const Query = t.interface({ age: NumberFromString });
const Body = composeTypes(
JSONFromString,
t.interface({
name: t.string,
}),
'Body',
);
const requestHandler = wrap(req =>
req.body
.validate(Body)
.chain(body => req.query.validate(Query).map(query => createTuple(body, query)))
.fold(validationErrorsToBadRequest, ([body, query]) =>
Ok.apply(
new JsValue({
name: body.name,
age: query.age,
}),
jsValueWriteable,
),
),
);
app.post('/', requestHandler);
Installation
yarn add express-fp
Development
yarn
npm run compile
npm run lint