Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
/ destjs Public archive

Make beautiful APIs with the NestJS inspired framework for Deno

Notifications You must be signed in to change notification settings

SampaioLeal/destjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DestJS

Make beautiful APIs with the NestJS inspired framework for Deno

CodeFactor deno.land/x

Goals

Goal State
Controllers store and routes creation with Decorators Complete ✔
Other methods decorators (PUT, DELETE, POST, PATCH) Complete ✔
Global Middlewares folder and management Complete ✔
Interceptors for methods Complete ✔
Input validation Complete ✔
Context Helpers In Progress ⛔
Format endpoint paths In Progress ⛔
Logger Waiting
Create example repository Waiting
Create documentation Waiting
Handle OPTIONS and HEAD Waiting
DestJS CLI Waiting
Dynamic Configs Class Waiting

Example Project

To get started with DestJS you have to instatiate the app with the createApp function and build a minimal structure to the framework works properly. At the root of your project you need to create two folders: controllers and middlewares They will take care of your controllers and middlewares that run in every request

// main.ts
import { createApp } from "./deps.ts";

createApp({
  port: 8000,
});
// deps.ts
export { createApp } from "https://deno.land/x/[email protected]/mod.ts";

This will initialize things and configure then before creating a new oak server. Take alook at what DestJS will do:

  • Read the controllers folder at the root of your project and store them with methods interceptors
  • Read the middlewares folder at the root of your project and store them
  • Configure stored middlewares to oak Application
  • Configure stored controllers to oak Router with interceptors
  • Start oak server at specified port

Creating your first Controller

By default DestJS will look at *.controller.ts files at controllers folder and initialize them in order to decorators works as expected. So, let's create a CatsController:

// controllers/cats.controller.ts
import { Controller, Get, HttpContext } from "../deps.ts";

@Controller("/cats")
export default class CatsController {
  @Get("/")
  getOne(context: HttpContext) {
    console.log(context.state);
    return { name: "Michael Scott", cute: true, crazy: true };
  }
}
// deps.ts
export { createApp, Controller, Get } from "https://deno.land/x/[email protected]/mod.ts";
export type { HttpContext } from "https://deno.land/x/[email protected]/types.ts";

Now we can start our API and test the endpoint /cats by running the following command:

deno run --allow-net --allow-read ./main.ts

Creating your first Middleware

By default DestJS will look at *.middleware.ts files at middlewares folder and initialize them in order to decorators works as expected.

Have in mind that middlewares will intercept every request and can manipulate the context and throw errors that will be catched by module middleware handler, returning an Internal Server Error to the client or a custom error using `HttpError class.

So, let's create a DateMiddleware that will inject the actual Date in request state:

// middlewares/date.middleware.ts
import {
  Middleware,
  DestMiddleware,
  HttpContext,
  NextFunction,
} from "../deps.ts";

@Middleware()
export class DateMiddleware implements DestMiddleware {
  use(context: HttpContext) {
    context.state.nowMiddleware = Date.now();
  }
}
// deps.ts
export { createApp, Controller, Get, Middleware } from "https://deno.land/x/[email protected]/mod.ts";
export type {
  DestMiddleware,
  HttpContext,
  NextFunction,
} from "https://deno.land/x/[email protected]/types.ts";

Now we can start our API and test the endpoint /cats. Looking at the terminal we can see the state with a nowMiddleware key with the actual time.