0.4.4 • Published 3 years ago
@weedify/core v0.4.4
Weedify
A web framework that makes it easy to develop under weed!
Weedify is a Node.JS/TypeScript/Express powered framework with a minimal API surface to learn. It aims to look similar in usage to NestJS (especially when used with TypeORM) without all the complex things. Just plain and simple express router decorators, controllers, services, loaders and dependency injection.
The framework encourages you to use a "model-service-controller" pattern while profiting of DI goods
Examples
// src/controllers/home.ts
import { Request } from "@types/express";
import { Controller, Get, Post, Errors } from "@weedify/core";
import { DIKeys } from "@app/constants";
import { HelloService } from "@app/services";
// Declares a new controller, to be registered automagically
@Controller("/")
export class HomeController {
// Simple DI decorator to inject dependencies on-the-fly
@Inject(DIKeys.HELLO_SERVICE)
private helloService: HelloService;
// API Endpoint : GET /hello
@Get("/hello")
async getHello(req: Request) {
const { who } = req.query;
const person = await this.helloService.getPerson(who);
// Returns an Object response that is transformed to JSON
return {
_statusCode: 202, // Accepted
greeting: `Hello, ${person}!`
};
}
// API Endpoint: POST /hello
@Post("/hello")
postHello() {
// Throw a HTTP standard error
throw new Errors.BadRequest("Cannot post on /hello endpoint.");
}
}
// src/index.ts
import "reflect-metadata";
import express from "express";
import weedify from "@weedify/core";
const PORT = process.env.PORT || 4001;
const main = async () => {
// Creates a new express application;
const app = express();
// Loaders are functions called to initialize your database/redis/logger/security/etc
// const loaders = await import('./loaders');
// await weedify.registerLoaders(loaders, app);
// Services are reusable sets of "domain-specific" functions
const services = await import('./services');
await weedify.registerServices(services, app);
// Controllers are responsible for handling requests and responding to them
const controllers = await import('./controllers');
await weedify.registerControllers(controllers, app);
// JSON Error handler
app.use(weedify.defaultErrorHandler());
// Let's make the express app accessible from the browser
app.listen(PORT, () => {
console.log(`INFO : 🔥 API Server is ready on http://localhost:${PORT}`);
});
};
main();
$ PORT=3000 ts-node src/index.ts
# INFO : 🔥 API Server is ready on http://localhost:3000
License
Distributed under the MIT License for free usage/modification/sharing.