1.0.4 • Published 4 years ago
@nur840/frame v1.0.4
Layer
Layer trying to cover unprivileged side of any Framework.It's trying to achieve
- 0 Dependencies.
- More Control
- Better Error Handling
- Raw nodejs performance
- Lightweight.
- Make asynchronous programming easy and fast
- And Plugin System
API - Form - Request - Control
Request(ctx => any)
Entry Point.Catch((err, ctx) => any)
It run whenRequest()
throw any error!Control(flow, ctx) => any
It run in the end.1st argument is the returned value fromRequest()
.Context
Prototype ofctx
object
import http from "http";
import { App, Request, Catch, Control, Context } from "layer";
http.createServer(App).listen(8080); // initialization
Request((ctx) => {
if (condition) throw new CustomError("Bad Request");
return "This is so Awesome!";
});
Catch((err, { res }) => {
if (err instanceof CustomError) res.end(err.msg);
});
Control((flow, ctx) => {
if (!flow) return; // checking if flow isn't falsy value
ctx.send(flow);
});
let ctx = Context.prototype;
ctx.prop = "Hello World";
ctx.msg = function () {
return this.prop;
};
ctx.send = function (prefix) {
this.res.end(prefix + this.msg());
};
Alias
App.CTX = Context;
App.Catch = Catch;
App.control = Control;
App.request = Request;
Form - Request(use(m1,m2...))
Note: Only use it for Asynchronous Task!
use()
return a function.- This function take one argument and pass it to All Middleware.
- This function return or throw Error depend on Middleware.
Request(use(middleware1, middleware2, middleware3));
use() can be nested!
// You don't need nesting.Its hard to understand the flow of control.
Request(
use(
// if Something Return from Any middleware it will pass to the Control().
use(Fn1, Fn2),
use(use(A, B), use(C, D))
)
);
// Although It could be very useful.Router plugin has `use()` builtin.
// But the `return` value pass to the next middleware.
// where (ctx,flow) => flow; is the returned value from 1st middleware.
let auth = (ctx) => isValid(ctx.id); // return true | false
Route.GET(auth, (ctx, flow) => {
if (flow == true) return "Layer is so awesome!"; // pass it to the Controller!
});
plugin - Router - Route
init
init
return value or throw Error depend onRoute
import { init, Route } from "layer/Router";
Request(init); // initialization
Route("/")
.GET((ctx) => {
ctx.res.end("Hello World");
})
.POST(({ res }) => {
res.end("I can chaining!");
});
Router - Route (controlling)
If Route
returned or Throw any error.It just a returned Value of init
Route("/").GET((ctx) => {
(() => {
if (someCondition) throw "I am more then any middleware!";
})();
if (someCondition) return "pass to Control()";
});
Control((flow, ctx) => {
if (!flow) return;
// flow = throw value | return value
ctx.res.end(flow);
});
Manually handel Error on specific route!
Route("/")
.GET(() => {
throw new CustomError("Error!");
})
.POST(() => {
throw new Error("Error!");
})
.catch((err, ctx) => {
if (err instanceof CustomError) ctx.res.end(err.msg);
// throw value of `init`
if (err instanceof Error) throw "pass err to the Catch()";
else return err; // return value of `init`
});
Note: In catch()
block, init
throw exception if catch
block throw any Error!