dango-core
v2.0.0
Published
> Dumpling in japaneese.
Downloads
3
Readme
Dango
Dumpling in japaneese.
POC for functional controllers
and route-handlers
in express.
What is expected
- Functional way of writing routers
- Follow function programming concepts
- Composition over inheritance
Update v1.0.0
I'm not very familiar with functional programming
and it's concepts. I'm learning it now and it turns out the v0.0.2
dango code was not functional at all and it has been scraped see tag v0.0.2. I'll be proposing a new API
for dango again. As we have been doing OOP
for so long, FP
will take some time to be fully understood.
Breaking changes
v2.0.0
- The router handler ctx is now an object v1.0.0
CreateHandler
has been scrapedCreateController
params changed.
Proposed API
Core:
Express wrapper
- A wrapper on
express
to inject controllers. - Can be used as
import express from 'express';
import { createExpressServer } from 'dango-core';
const app = express();
createExpressServer(app, { controllers: ['./controllers/*.ts'] }).listen(3000, () => {
console.log('http://localhost:3000');
});
- controller definition should be a
glob
path.
Controllers
- A controller can be created with
createController
method.
import { createController } from 'dango-core';
export default createController(
'/',
[
{
path: '/',
method: 'get',
handler: ({req, res, params, query}) => {
res.send('is this working ?');
},
middlewares: [
(req, res, next) => {
res.send("I'm local");
},
],
},
],
[
(req, res, next) => {
res.send("I'm global");
},
],
);
- Should be default exported from the controller file.
Routes
This is a low level API which can be used to create routes with proper type definitions for request body,params and query. Can be used with createController
for modularity.
BREAKING CHANGE
v2.0.0
the handler ctx is now an object
standalone
import { createRoute } from 'dango-core';
const userRoute = createRoute<{ user: string }>({
path: '/user',
method: 'options',
handler: ({req, res, body}) => {
// proper ts definitions
req.body.user;
},
});
- with controller
import { createController } from 'dango-core';
export default createController('/aa', [userRoute]);
- With express router style
function chaining
import { createRoute } from 'dango-core';
const home = createRoute<{ user: string }>('/')
.method('get')
.handler(({ res }) => res.send('OK'));
Middlewares
Global
import express from 'express';
import { createExpressServer } from 'dango-core';
const app = express();
createExpressServer(app, {
controllers: ['controllers/**.ts'],
middlewares: [
(req, res, next) => {
res.send("I'm global");
},
],
}).listen(4000, () => console.log('http://localhost:4000'));
Controller specific
import { createController } from 'dango-core';
export default createController(
'/test',
[
{
path: '/yolo',
method: 'get',
handler: ({req, res}) => {
res.send('is this working ?');
},
},
],
[
(req, res, next) => {
res.send("I'm controller specific");
},
],
);
Route specific
import { createController } from 'dango-core';
export default createController('/test', [
{
path: '/yolo',
method: 'get',
handler: ({req, res}) => {
res.send('is this working ?');
},
middlewares: [
(req, res, next) => {
res.send("I'm route specific");
},
],
},
]);
Helpers
response.sendError
- send HTTP errors from response with proper intellisense
import { createController, createRoute } from 'dango-core'; const someRoute = createRoute<{ user: string }>({ path: '/', method: 'get', handler: ({req, res, body}) => { res.sendError(401,"Nope") }, }); export default createController('/home', [someRoute]);
- send HTTP errors from response with proper intellisense
Packages
| Package | version | | ------- | ----------------------------------------------- | | Core | | | Example | NA |