A JavaScript/TypeScript web framework for the Node runtime built on top of the ShapeX event-driven application framework.
import { Delto, type DeltoState, type RouteParams } from "delto";
// Define app state
type AppState = DeltoState & {
name: string | null;
};
// Create app instance with default state
const app = Delto<AppState>({
name: null,
});
// Create routes that dispatch events
app.get("/hello/:who", "http.request.hello");
// Subscribe to events
app.subscribe(
"http.request.hello",
(state, params: RouteParams<{ who: string }>) => {
return {
state: {
...state,
name: params.who,
},
dispatch: {
to: "http.response.plain",
with: {
body: `Hello: ${params.who}`,
},
},
};
}
);
// Serve requests
app.serve({
port: 3222,
});
npm i delto
Delto does away with the classical MVC pattern for web backends and instead encourages the use of events and subscriptions. The idea being that if everything is an event or a subscription listening to an event, then it's easier to reason about the complexity of your application as you can focus on just that, without getting lost in the sea of terminology and different abstraction patterns. It's all just action and reaction.
Much like using ShapeX on its own, at the core of your application is state. You start by initiating with some initial state, which is an intersection type of DeltoState
:
import { Delto, type DeltoState, type RouteParams } from "delto";
type AppState = DeltoState & {
name: string | null;
};
const app = Delto<AppState>({
name: null,
});
In other words, some of the state will be created and managed by Rose itself, which is DeltoState
, and your state will be an addition to DeltoState
.
Routes in Delto dispatch ShapeX events. Routes are created like so:
app.get("/hello/:who", "http.request.hello");
Route events will automatically get RouteParams
passed to them, so if you subscribe to route events, you can receive the route params like so:
app.subscribe(
"http.request.hello",
(state, params: RouteParams<{ who: string }>) => {
return {
state: {
...state,
name: params.who,
},
dispatch: {
to: "http.response.plain",
with: {
body: `Hello: ${params.who}`,
},
},
};
}
);
Notice the RouteParams
type definition here, which supports generics so you can specify exactly what shape of data you expect to get. Other than that, all subscriptions are just like ShapeX subscriptions.
Delto stores request information in the http
state key, so to access request information you'd do something like this:
app.subscribe("my-event", (state) => {
// log pathname
console.log(state.http?.request.url.pathname);
return {};
});
The http.request
state consists of the following information:
export type DeltoRequest = {
url: URL;
method: string;
body: unknown; // this differs based on runtime
};
Delto comes with some built-in events.
You can dispatch response events to return data to the client. All responses must conform to the DeltoResponse
type which looks like this:
export type DeltoResponse = {
body?: unknown; // this differs based on runtime
status?: number;
headers?: {
[key: string]: string;
};
};
Return a plain response with the http.response.plain
event like so:
app.subscribe("my-event", (state) => {
return {
dispatch: {
to: "http.response.plain",
with: {
body: "Hello, World",
},
},
};
});
Return a JSON response with the http.response.json
event like so:
app.subscribe("my-event", (state) => {
return {
dispatch: {
to: "http.response.json",
with: {
body: {
hello: "world",
},
},
},
};
});
Return an HTML response with the http.response.html
event like so:
app.subscribe("my-event", (state) => {
return {
dispatch: {
to: "http.response.html",
with: {
body: "<h1>Hello, World</h1>",
},
},
};
});
Return a custom response with the http.response
event like so:
app.subscribe("my-event", (state) => {
return {
dispatch: {
to: "http.response",
with: {
status: 302,
headers: {
Location: "/hello/world",
},
},
},
};
});