Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
express-ts-annotations
Advanced tools
An annotation system for express. ## Installation ```sh $ npm install express-ts-annotations ``` ## Usage Controller.js ```JavaScript import { BaseController, controller, route, middlewares } from "express-annotations"; ...
An annotation system for express.
$ npm install express-ts-annotations
Controller.js
import { BaseController, controller, route, middlewares } from "express-annotations";
...
@controller("/orders")
class OrderController extends BaseController {
// This will generate the ALL route /orders/greeting
@route("all", "greeting")
public greeting(req: Request, res: Response) {
...
}
// This will generate the GET route /orders/my-orders and apply the middleware auth
@route() // We can omit this decorator since we use the default behaviour and we use @middlewares below
@middlewares([auth])
public getMyOrders(req: Request, res: Response) {
...
}
// This will generate the POST route /orders/ and apply the middleware auth
@route("post", "/")
@middlewares([auth])
public addNewOrder(req: Request, res: Response) {
...
}
// This will generate the DELETE route /orders/all-my-orders and apply the middleware auth
@route("delete")
@middlewares([auth])
public allMyOrders(req: Request, res: Response) {
...
}
}
export default new OrderController();
index.js
import { injectControllers } from "express-annotations";
...
const app = express();
...
injectControllers(app, [orderController]);
BaseController
This is a class that all controllers must extend. It contains some internal information used by the system.
@controller(path: string)
A class decorator that sets up the path
in which the controller will be injected. If the path
doesn't start with a /
, it will be automatically added by the system. The default path
is /
.
@route(method?: RESTMethod, path?: string)
This is a method decorator that assigns the method to a path
.
By default if no parameters are provided the system will analyze the method name and assign a rest method
and path
automatically,
for this the method name must start with a RESTMethod (i.e get
, post
, put
, patch
, delete
, all
) followed by the path written in CamelCase
which will be transformed automatically in kebab case. If only the method
is specified the name will still be asigned automatically. If the method
or path
is specified, they will be used instead of the system generated ones.
If the path
doesn't start with /
, it will be added automatically.
@middlewares(middlewares: Middleware[])
This method decorator is used to specify the middlewares
that should apply to the route
of the method.
It can be used without @route()
decorator, in which case the default behaviour of @route()
specified above will apply.
injectControllers(app: Express, controllers: BaseController[])
This function is used to inject the controllers
into the express
application.
FAQs
An annotation system for express. ## Installation ```sh $ npm install express-ts-annotations ``` ## Usage The easiest way of using the annotations is by explictily stating the routes and methods of the controller. ```JavaScript import { Request, Response
The npm package express-ts-annotations receives a total of 63 weekly downloads. As such, express-ts-annotations popularity was classified as not popular.
We found that express-ts-annotations demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.