hitoha

A TypeScript + Restify based framework for creating speedy and organized web backends

Usage no npm install needed!

<script type="module">
  import hitoha from 'https://cdn.skypack.dev/hitoha';
</script>

README

Hitoha

An extremely opinionated framework based around Restify, Typescript, and Knex (with Postgres) that basically only exists for my personal usage, but here it is anyway!!

Basic Architecture

Hitoha is built around Routes, Controllers, and Services. A Route calls a Controller, and said Controller fufils the Route's request, which usually entails using a Service. The controller returns a ControllerResponse, which the Controller the automatically sends to the client.

Example Usage

import * as Hitoha from 'hitoha';
import * as Restify from 'restify';

// A service returning "Hello, World", utilized by our controller
class HelloService extends Hitoha.Service {
    public constructor() {
        base(undefined); // would usually take a knex instance
    }

    public GetHello(): string {
        return 'Hello, World!';
    }
}

// Creates a controller that responses to a route using the supplied Handler callback in the constructor
let worldController = new Yuru.Controller((req: Restify.Request, res: Restify.Response, next: Restify.Next) => {
    return Yuru.CreateControllerResponse(200, new HelloService().GetHello());
});

// Creates a route of HTTP method "GET", at path /world, with exampleController as it's handler
let worldRoute = new Yuru.Route(Yuru.RouteType.Get, 'world', exampleController);
// The same route, but at path /hello
let helloRoute = new Yuru.Route(Yuru.RouteType.Get, 'hello', exampleController);

// Creates a route group, which holds multiple routes under an optional prefix (in this case, /hello)
// This would make worldRoute and helloRoute accessable at /hello/world, and /hello/hello respectivley
let exampleRouteGroup = new Yuru.RouteGroup([exampleRoute], 'hello');

// Creates a server, accepting restify server options, an array of route groups, a knex instance, an optional array of middleware to use, and an optional apiPrefix
// to use for all routes. With the apiPrefix, now our routes are accessed at /api/hello/world, and /api/hello/hello
let exampleServer = new Yuru.Server(undefined, [exampleRouteGroup], '/api', [Yuru.JsonOnlyPost]);

// Start the server at port 8070
exampleServer.Start(8070);