0.0.38 • Published 3 years ago

@mrteapot/stellajs v0.0.38

Weekly downloads
245
License
ISC
Repository
github
Last release
3 years ago

stellajs

A small Typescript framework to make more structured Nodejs Rest APIs. It includes decorators for defining controllers, endpoints, error handlers, middleware and request validation using JSON schema. It also includes tsyringe for lightweight dependency injection through constructors. Everything I need for my projects.

Here is some code showing how to use the framework.

A controller handling auth (login/register/user info):

import { AuthMiddleware } from "./Auth.middleware";
import {
  Controller,
  Endpoint,
  UseMiddleware,
} from "@mrteapot/stellajs";
import { AuthService } from "./Auth.service";
import { RegistrationSchema } from "./schemas/Registration.schema";
import { LoginSchema } from "./schemas/Login.schema";
import { UserUpdateSchema } from "./schemas/UserUpdate.schema";
import { ChangePasswordSchema } from "./schemas/ChangePassword.schema";
import { HTTPMethod } from "@mrteapot/stellajs/dist/core/decorators/Endpoint";
import { StellaRequest } from "@mrteapot/stellajs/dist/core/interfaces";
import { User } from "shared/types/User";

@Controller("/auth")
export class AuthController {
  constructor(private authService: AuthService) {}

  @Endpoint({
    method: HTTPMethod.POST,
    schema: RegistrationSchema,
    path: "/register",
  })
  async registerUser(req: StellaRequest) {
    await this.authService.register(req.getBody());
  }

  @Endpoint({
    method: HTTPMethod.POST,
    path: "/login",
    schema: LoginSchema
  })
  async login(req: StellaRequest) {
    const token = await this.authService.login(req.getBody());
    return {
      token,
    };
  }

  @Endpoint({
    method: HTTPMethod.GET,
    path: "/me",
  })
  @UseMiddleware(AuthMiddleware)
  async me(req: StellaRequest) {
    const user = await this.authService.getUserInfo(req.getData<User>('user').id);
    return { user };
  }

  @Endpoint({
    method: HTTPMethod.PATCH,
    path: "/me",
    schema: UserUpdateSchema,
  })
  @UseMiddleware(AuthMiddleware)
  async update(req: StellaRequest) {
    await this.authService.updateProfile(req.getData<User>('user').id, req.getBody());
  }

  @Endpoint({
    method: HTTPMethod.PATCH,
    path: "/change-password",
    schema: ChangePasswordSchema,
  })
  @UseMiddleware(AuthMiddleware)
  async updatePassword(req: StellaRequest) {
    await this.authService.changePassword(
      req.getData<User>("user").email,
      req.getBody().oldPassword,
      req.getBody().newPassword
    );
  }
}

Starting the application

import { StellaApplication } from '@mrteapot/stellajs';
import { AuthController } from "./auth/Auth.controller"

const stella = new StellaApplication({
  port: 8989,
  controllers: [
    AuthController,
  ]
});

await this.stella.bootstrap();
0.0.38

3 years ago

0.0.37

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.36

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.25

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.22

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago