π
abstain
Lightweight Typescript-first framework built on top of Express [WIP]
π
api
// index.ts
import { Application } from '@pinkcig/abstain';
import { resolve } from 'node:path';
new Application({
name: 'my-api',
port: process.env.PORT ?? 3000,
address: process.env.ADDRESS ?? '0.0.0.0',
// Set by default, but can be overridden
controllers: resolve(__dirname, 'controllers'),
// Set by default, but can be overridden
logger: new MyOwnLoggerImplementation(),
}).launch((app) => {
// Some services to run after the application is launched
});
// controllers/index.ts
import { Controller, Get, Post } from '@pinkcig/abstain';
import { Response, Request } from 'express';
@
export class AppController extends Controller {
@Get('/')
async index() {
// https://github.com/omniti-labs/jsend
return this.respond({ status: 'success', data: { message: 'Hello, World!' } } /*, 200 is used by default */);
}
@Post('/')
async post(request: Request, response: Response) {
// WIP, prone to MUCH change
const { name, from } = this.validate(
request.body,
s.object({
name: s.string.default('Faye'),
from: s.string,
}).ignore
);
return this.respond({ status: 'success', data: { message: `Hello ${name} from ${from}!` } });
}
}