Comparing version 1.0.0 to 1.0.1
@@ -12,7 +12,15 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setRoutes = exports.CMiddleware = exports.Controller = exports.Middleware = void 0; | ||
__exportStar(require("./decorators/routeDecorators"), exports); | ||
__exportStar(require("./decorators/routeMiddlewareDecorator"), exports); | ||
__exportStar(require("./decorators/controllerDecorator"), exports); | ||
__exportStar(require("./decorators/controllerMiddlewareDecorator"), exports); | ||
__exportStar(require("./utils/getRoutesConfig"), exports); | ||
var routeMiddlewareDecorator_1 = require("./decorators/routeMiddlewareDecorator"); | ||
Object.defineProperty(exports, "Middleware", { enumerable: true, get: function () { return __importDefault(routeMiddlewareDecorator_1).default; } }); | ||
var controllerDecorator_1 = require("./decorators/controllerDecorator"); | ||
Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return __importDefault(controllerDecorator_1).default; } }); | ||
var controllerMiddlewareDecorator_1 = require("./decorators/controllerMiddlewareDecorator"); | ||
Object.defineProperty(exports, "CMiddleware", { enumerable: true, get: function () { return __importDefault(controllerMiddlewareDecorator_1).default; } }); | ||
var setRoutes_1 = require("./utils/setRoutes"); | ||
Object.defineProperty(exports, "setRoutes", { enumerable: true, get: function () { return __importDefault(setRoutes_1).default; } }); |
{ | ||
"name": "decopress", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "ExpressJS with TypeScript Decorators", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
187
README.md
@@ -1,1 +0,186 @@ | ||
# DecoPress | ||
# _Decopress_ | ||
###### Decopress is a tool to help you write cleaner Express.js code using typescript decorators. | ||
Please if you need a feature that is not supported by decopress and its related to decorators and express open an issue in github and ask for this feature. | ||
## _Quick Access_ | ||
* [Installation](https://github.com/Dev-Addict/decopress#installation) | ||
* [Quick Start](https://github.com/Dev-Addict/decopress#quick-start) | ||
* [Controller Class](https://github.com/Dev-Addict/decopress#controller-class) | ||
* [Controller Decorator](https://github.com/Dev-Addict/decopress#controller-decorator) | ||
* [CMiddleware Decorator](https://github.com/Dev-Addict/decopress#cmiddleware-decorator) | ||
* [Method Decorators](https://github.com/Dev-Addict/decopress#method-decorators) | ||
* [Middleware Decorator](https://github.com/Dev-Addict/decopress#middleware-decorator) | ||
* [Use Method Decorator](https://github.com/Dev-Addict/decopress#use-method-decorator) | ||
* [Get Result](https://github.com/Dev-Addict/decopress#get-result) | ||
### _Installation_ | ||
* Using **_npm_**: \ | ||
`npm install decopress` | ||
* Using **_yarn_**: \ | ||
`yarn add decopress` | ||
### _Quick Start_ | ||
creating a controller | ||
``` | ||
import {NextFunction, Request, Response} from 'express'; | ||
import {Controller, Get, Middleware} from 'decopress'; | ||
const middleware1 = (req: Request, res: Response, next: NextFunction) => { | ||
console.log('in middleware1'); | ||
next(); | ||
}; | ||
const middleware2 = (req: Request, res: Response, next: NextFunction) => { | ||
console.log('in middleware2'); | ||
next(); | ||
}; | ||
@Controller('/') | ||
class ExampleController { | ||
@Middleware(middleware1, middleware2) | ||
@Get('/') | ||
hello(req: Request, res: Response) { | ||
res.json({ | ||
message: 'hello' | ||
}); | ||
} | ||
} | ||
``` | ||
connect controller with app | ||
``` | ||
import express from 'express'; | ||
import {setRoutes} from 'decopress'; | ||
const app = express(); | ||
// controller we made in the top code example | ||
const controller = new ExampleController(); | ||
setRoutes(controller, app); | ||
``` | ||
### _Controller Class_ | ||
#### _Controller Decorator_ | ||
You can use **Controller** decorator to create decorator classes. | ||
> Note: if you don't apply **Controller** decorator to controller class while you are trying to convert your controllers into routes you will face errors. | ||
**Controller** accepts one argument and that the url(path) and url is required. | ||
``` | ||
import {Controller} from 'decopress'; | ||
const url = '/path' | ||
@Controller(url) | ||
class ExampleController { | ||
... | ||
} | ||
``` | ||
#### _CMiddleware Decorator_ | ||
You can use **CMiddleware** decorator to add middleware to your controller class. | ||
> Note: You should use **CMiddleware** decorator before **Controller** decorator and you can use **CMiddleware** as much as you like and it will run in order. | ||
**CMiddleware** decorator accepts spread middleware of type RequestHandler from express package. | ||
``` | ||
import {RequestHandler} from 'express'; | ||
import {Controller, CMiddleware} from 'decopress'; | ||
const middleware1: RequestHandler = (req, res, next) => { | ||
console.log('in middleware1'); | ||
next(); | ||
}; | ||
const middleware1: RequestHandler = (req, res, next) => { | ||
console.log('in middleware1'); | ||
next(); | ||
}; | ||
@CMiddleware(middleware1, middleware2) | ||
@Controller('/') | ||
class ExampleController { | ||
... | ||
} | ||
``` | ||
### _Method Decorators_ | ||
There is **Get**, **Post**, **Put**, **Patch**, **Delete**, and **All** method decorators they all accept only url(path) and the method of type Request handler in the class and represent the method of the name. | ||
> Note: by my convention the method name is the path(it's not required) | ||
``` | ||
import {Controller, Get} from 'decopress'; | ||
@Controller('/') | ||
class ExampleController { | ||
@Get('/hello') | ||
hello(req: Request, res: Response) { | ||
res.json({ | ||
message: "hello" | ||
}); | ||
} | ||
} | ||
``` | ||
#### _Middleware Decorator_ | ||
**Middleware** decorator is used to add middleware to request stack. it works the same way as **CMiddleware** decorator. and it accept arguments of type RequestHandler from express package. | ||
> Note: it is required to use it before **Method** Decorators. | ||
``` | ||
import {RequestHandler} from 'express'; | ||
import {Controller, Get, Middleware} from 'decopress'; | ||
const middleware1: RequestHandler = (req, res, next) => { | ||
console.log('in middleware1'); | ||
next(); | ||
}; | ||
const middleware1: RequestHandler = (req, res, next) => { | ||
console.log('in middleware1'); | ||
next(); | ||
}; | ||
@Controller('/') | ||
class ExampleController { | ||
@Middleware(middleware1, middleware2) | ||
@Get('/hello') | ||
hello(req: Request, res: Response) { | ||
res.json({ | ||
message: "hello" | ||
}); | ||
} | ||
} | ||
``` | ||
#### _Use Method Decorator_ | ||
**Use** method decorator is an special method decorator that can help you handle sub controllers all you have to do is in the method return instance of your controller. | ||
> Note: by my convention the method name for **Use** method decorator is as same as class name | ||
``` | ||
import {Controller, Use} from 'decopress'; | ||
@Controller('/2') | ||
class ExampleController2 { | ||
} | ||
@Controller('/') | ||
class ExampleController { | ||
@Use('/') | ||
ExampleController2() { | ||
return new ExampleControler2(); | ||
} | ||
} | ||
``` | ||
### _Get Result_ | ||
getting results is easy in decopress just you have to import **setRoutes** and then **setRoutes** accept to arguments the first one is instance of your controller class and the second one is either instance of Router from express package or your app. | ||
``` | ||
import express from 'express'; | ||
import {setRoutes} from 'decopress'; | ||
const app = express(); | ||
... | ||
// I'm using ExampleController from top examples | ||
setRoutes(new ExampleController(), app); | ||
``` |
export * from "./decorators/routeDecorators"; | ||
export * from "./decorators/routeMiddlewareDecorator"; | ||
export * from "./decorators/controllerDecorator"; | ||
export * from "./decorators/controllerMiddlewareDecorator"; | ||
export * from "./utils/getRoutesConfig"; | ||
export {default as Middleware} from "./decorators/routeMiddlewareDecorator"; | ||
export {default as Controller} from "./decorators/controllerDecorator"; | ||
export {default as CMiddleware} from "./decorators/controllerMiddlewareDecorator"; | ||
export {default as setRoutes} from "./utils/setRoutes"; |
@@ -11,5 +11,7 @@ import {RequestHandler, Router} from "express"; | ||
const router = Router() ; | ||
const router = Router(); | ||
router.use(...controller.__routes_config__.middleware); | ||
const routes = Object.values(controller.__routes_config__.routes); | ||
for (const {url, method, stack} of routes) | ||
@@ -19,3 +21,2 @@ router[method](url, ...stack); | ||
const subControllers = Object.values(controller.__routes_config__.subControllers); | ||
for (const subController of subControllers) { | ||
@@ -22,0 +23,0 @@ const subRouter = convertToRouter(subController); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
187
20190
12
215