0.0.76 • Published 2 days ago

@obisiket1/express-utils v0.0.76

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

Express Utils

This is an express based utility library that provides utility decorators that can be used together with express typescript.

UseHTTPDecorator

The useHTTPDecorator function is an improvement over the useDecorator. It supports the this variable. The extended class constructor is instantiated within the $register static method that is shipped with the useHTTPDecorator().

Routes can be easily setup using:

import { useHttpDecorator } from "@obisiket1/express-utils";
import { Router } from "express";

const { BaseController, Http, P } = useHttpDecorator();

@Http.Controller()
export default class AuthCtrl extends BaseController(Router) {
  @Http.Get("/api")
  index(@P.Query() query: Record<string, any>) {
    return {
      data: query,
    };
  }
}

That sets up the controller with the route /api, which can then be registered to the express app setup using the mount function.

import { mount } from "@obisiket1/express-utils";
import express from "express";

const app = express();

app.use("/", mount(AuthCtrl))

...

export default app

That also supports for multiple Controllers in the form.

app.use("/v1", mount([
  Controller1,
  Controller2
  ...
  ControllerN
]))

Server

You can also use our Server class to setup the server. The Server class provides the ability to add setup functions to the server. It also provides the graceful shutdown out of the box.

Check out the example below:

import { Server } from "@obisiket1/express-utils";
import app from "./app";

Server.start({
  force: true,
  expressApp: app,
  onStart: ({ port }) => {
    console.log("Server has started @", port);
  },
});

Logger

To set up a logger for a given application use:

const logger = createLogger({
  scope: __filename,
  logDebug: false,
});

CreateConfig

This is a helper function that not only helps to manage configuration but also provides a way to validate the configuration. To use this function use the following:

import { createConfig } from "@obisiket1/express-utils";

export default createConfig({
  config: {
    // configuration goes here
  },
});

Mail

This is still experimental, it provides a way to handle mails on your server; Here's an example:

class Sender implements ISender {
  send(mail: IMail): void {
    console.log("Sent!", mail);
  }
}

const { Mail } = useMailer({
  sender: new Sender(),
  templatePath: path.join(__dirname, "templates"),
});

Mail.create({
  template: "text",
  subject: "Hello",
  data: {
    name: "Treasure",
    first_name: "Obisike",
  }
}).send();

It basically makes your templates readily available using a lightweight template compiler. You can also build your template compiler and add it to the useMailer options.

const { Mail } = useMailer({
  ...,
  templateEngine: new TemplateEngine(),
  ...,
});

The TemplateEngine must implement the ITemplateEngine interface.

Here is the default option:

const defaultOptions = {
  templateEngine: new StringEngine(),
};

UseDecorator

The useDecorator is a function that returns decorators that can be used to setup your routes

import { Logger, useDecorator } from "@obisiket1/express-utils";
import { NextFunction, Request, Response, Router } from "express";

const { GlobalMiddleware, Success, Controller, Post } = useDecorator();

@GlobalMiddleware({ path: "/auth" })
export default class AuthController extends Controller(Router) {
  @Post()
  async register(req: Request, res: Response, next: NextFunction) {
    const result = {
      data: {
        test: "red",
        jesus: ["red", "green", "blue"],
      },
    };

    return Success(res, result);
  }
}

Decorators Contained in the useDecorator include:

0.0.74

2 days ago

0.0.75

2 days ago

0.0.76

2 days ago

0.0.73

4 days ago

0.0.72

4 days ago

0.0.71

5 days ago

0.0.70

18 days ago

0.0.68

18 days ago

0.0.69

18 days ago

0.0.66

22 days ago

0.0.67

22 days ago

0.0.40

11 months ago

0.0.41

10 months ago

0.0.42

10 months ago

0.0.43

10 months ago

0.0.44

10 months ago

0.0.45

10 months ago

0.0.46

10 months ago

0.0.47

10 months ago

0.0.62

10 months ago

0.0.63

10 months ago

0.0.64

10 months ago

0.0.65

9 months ago

0.0.60

10 months ago

0.0.61

10 months ago

0.0.59

10 months ago

0.0.51

10 months ago

0.0.52

10 months ago

0.0.53

10 months ago

0.0.54

10 months ago

0.0.55

10 months ago

0.0.56

10 months ago

0.0.57

10 months ago

0.0.58

10 months ago

0.0.50

10 months ago

0.0.48

10 months ago

0.0.49

10 months ago

0.0.38

11 months ago

0.0.39

11 months ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.32

12 months ago

0.0.33

12 months ago

0.0.34

12 months ago

0.0.35

12 months ago

0.0.36

11 months ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.25

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.29

1 year ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago