0.0.6 • Published 3 years ago

slowtify v0.0.6

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

Slowtify

TypeScript decorators for Express.

Install

npm install slowtify

Usage

/* index.ts */
import Slowtify from "slowtify";
import express from "express";

const app = express();
app.use(express.json());
app.use(router);

const slow = new Slowtify({
  controllers: [UserController],
});
slow.useExpress(router);

app.listen(port, () => {
  console.info(`Listening on port ${port}`);
});
/* UserController.ts */
@Controller("/users")
export class UserController {
  @Get(":id")
  async getUserById(@Params() params: UserParams) {
    return params.id;
  }

  @Post()
  async createUser(@Req() req: Request, @Body() body: CreateUser) {
    console.log(req.headers);
    return {
      body,
    };
  }
}

See the full example that adds input validation.

Metadata

Slowtify allows adding metadata to the route via the decorator. For example, @Get('/', { permissions: ['test'] }) sets the permission for that route and will be accessible in the request object of the handler and middleware registered with Slowtify.

Middleware

Express middleware can be added outside of Slowtify with the standard app.use syntax. To register middleware via Slowtify, slowtify.use adds the middleware function(s) to each route and appends route metadata to the request object.

Middleware execution order follows Express' syntax and will always be executed before the route handler.

Example:

slowtify.use((req, res, next) => {
  console.log(req.metadata);
  next();
});

License

MIT

0.0.6

3 years ago

0.0.5

3 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