@arabesque/core
v1.0.0-beta.1
Published
A nano-framework to build applications
Maintainers
Readme
Arabesque Core
A nano-framework to build applications
Getting started
Arabesque Core provides a minimal foundation to create applications, consisting of three types and a function.
Listener
type Listener<Channel, Context> = (channel: Channel, handler: (context: Context) => Promise<Context>) => Promise<() => Promise<void>>;The fundamental part of an Arabesque application, the Listener is passed by the application a channel, that the Listener is expected to subscribe to, and a handler, that the Listener must call whenever it receives something from the channel. The Listener must return a promise that resolves to an argument-less function that, when executed, stop the Listener.
A channel can take any imaginable form, for example:
- a number in the context of a TCP service, representing a port
- a structured object in the context of a Kafka consumer, representing a broker, a group ID and a topic
- a string in the context of a file watcher, representing a pattern of files to watch
Middleware
type Middleware<Signal> = (context: Signal, next: (context: Signal) => Promise<Signal>) => Promise<Signal>;The operational part of an Arabesque application, the Middleware is passed by the application a context, coming from either the Listener or a previous Middleware, and a next function, that the Middleware may call to pass the context forward.
Application
type Application<Channel> = (channel: Channel) => Promise<() => Promise<void>>;The Arabesque Application itself that, when executed, returns a Promise that resolves to an argument-less function that, when executed, stop the Application.
createApplication
import type {Middleware} from "./index";
declare const createApplication: <Channel, Context>(
listen: Listener<Channel, Context>,
middleware: Middleware<Context>
) => Application<Channel, Context>;The factory that creates an Arabesque Application from a Listener and a Middleware.
Code sample
import type {Listener} from "@arabesque/core";
import {createApplication} from "@arabesque/core";
const listener: Listener<string, string> = (channel, handler) => {
return handler(channel).then(() => {
return () => Promise.resolve();
});
};
const start = createApplication(listener, (context, next) => {
console.log(`I am the middleware and I received ${context}`);
return next(context);
});
return start('foo').then((stop) => {
return stop();
});