Skip to main content
Deno 2 is finally here 🎉️
Learn more

NHttp Services

Service management for nhttp via decorators.

requires nhttp_controller

Installation

deno.land

import {...} from "https://deno.land/x/nhttp_services@0.0.2/mod.ts";

Usage

import { NHttp } from "https://deno.land/x/nhttp@{version}/mod.ts";
import {
  addControllers,
  BaseController,
  Controller,
  Get,
  Post,
  Status,
} from "https://deno.land/x/nhttp_controller@{version}/mod.ts";
import { addServices, Service } from "https://deno.land/x/nhttp_services@0.0.2/mod.ts";

class UsersService {
    private users: User[];
    constructor() {
        ...
    }
    
    findUser(id: string) {
        return users.find(u => u.id == id)
    }
    
    allUsers() {
        return users;
    }
}

@Controller("/hello")
class HelloController extends BaseController {
  @Get()
  @Service(UsersService)
  findAll() {
    return this.usersService.allUsers();
  }

  @Get("/:id")
  @Service(UsersService)
  findById() {
    const { params } = this.requestEvent;
    return this.usersService.findUser(params.id);
  }

  @Status(201)
  @Post()
  save() {
    const { body } = this.requestEvent;
    return body;
  }
}

class App extends NHttp {
  constructor() {
    super();
    this.use("/api", addServices([UsersService])
    this.use("/api", addControllers([HelloController]));
  }
}

new App().listen(3000);

License

MIT