ts-actionpack
v1.0.0-alpha.0
Published
Tiny library for "one controller instance per request" backend in typescript
Downloads
27
Readme
TS Action Pack
Tiny library to help writing "one controller instance per request" style backend in typescript.
This library includes:
The aim is to provide something simple but type-safe equivalent of Ruby on Rails Action Pack.
Usage
import * as express from "express";
import { makeRouterDSL, AbstractController } from "ts-actionpack";
// Define controller
class HomeController extends AbstractController {
index() {
this.res.send("Hello");
}
show() {
this.res.send(`Hello: id = ${this.req.params.id}`);
}
}
// Define router
const router = express.Router();
const { GET } = makeRouterDSL(router);
GET("/", HomeController, "index");
GET("/:id", HomeController, "show");
// Use router
const app = express();
app.use(router);
app.listen(8080);
Examples
Via controller inheritance, it's easy to extend functionalities e.g.
- Helper function to format response
- Action callback
- Authentication
- Handling multipart/form-data
- Handling error
- Parameter validation
See more examples at tests/examples.
Development
# Development
npm install
npm run build -- -w
# Run example
npm run example -- ex00-simple
# Testing
npm run test
# Formating
npm run format
# Publish
npm run deploy