Git Product home page Git Product logo

destjs's Introduction

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.

destjs's People

Contributors

somecodingwitch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.