decorate-express
Advanced tools
Comparing version 1.0.8 to 2.0.0
/// <reference types="express" /> | ||
import * as Express from 'express'; | ||
import 'reflect-metadata'; | ||
export declare class Route { | ||
export declare class RouteMetadata { | ||
method: string; | ||
@@ -13,20 +13,20 @@ path: string; | ||
export declare const basePathKey: symbol; | ||
export declare function getRouteMetadata(target: any): Route[]; | ||
export declare function route(method: string, path: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function basePath(path: string): { | ||
export declare function getRouteMetadata(target: any): RouteMetadata[]; | ||
export declare function Route(method: string, path: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function BasePath(path: string): { | ||
(target: Function): void; | ||
(target: Object, propertyKey: string | symbol): void; | ||
}; | ||
export declare function get(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function post(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function put(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function patch(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function del(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function options(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function head(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function use(path?: string): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function all(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function param(param: string): <T extends Express.RequestParamHandler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function middleware(fn: Middleware): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function getRoutes(target: Object): Route[]; | ||
export declare function register(router: Express.Router, target: Object): void; | ||
export declare function Get(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Post(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Put(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Patch(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Delete(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Options(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Head(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Use(path?: string): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function All(path?: string, middleware?: Middleware[]): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Param(param: string): <T extends Express.RequestParamHandler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function Middleware(fn: Middleware): <T extends Express.Handler>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>; | ||
export declare function getRoutes(target: Object): RouteMetadata[]; | ||
export declare function registerRoutes(router: Express.Router, target: Object): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("reflect-metadata"); | ||
class Route { | ||
class RouteMetadata { | ||
} | ||
exports.Route = Route; | ||
exports.RouteMetadata = RouteMetadata; | ||
exports.routesKey = Symbol('routesKey'); | ||
@@ -18,3 +18,3 @@ exports.basePathKey = Symbol('basePathKey'); | ||
exports.getRouteMetadata = getRouteMetadata; | ||
function route(method, path, middleware = []) { | ||
function Route(method, path, middleware = []) { | ||
return (target, key, descriptor) => { | ||
@@ -28,44 +28,44 @@ let routes = getRouteMetadata(target); | ||
} | ||
exports.route = route; | ||
function basePath(path) { | ||
exports.Route = Route; | ||
function BasePath(path) { | ||
return Reflect.metadata(exports.basePathKey, path); | ||
} | ||
exports.basePath = basePath; | ||
function get(path = '*', middleware = []) { | ||
return route('get', path, middleware); | ||
exports.BasePath = BasePath; | ||
function Get(path = '*', middleware = []) { | ||
return Route('get', path, middleware); | ||
} | ||
exports.get = get; | ||
function post(path = '*', middleware = []) { | ||
return route('post', path, middleware); | ||
exports.Get = Get; | ||
function Post(path = '*', middleware = []) { | ||
return Route('post', path, middleware); | ||
} | ||
exports.post = post; | ||
function put(path = '*', middleware = []) { | ||
return route('put', path, middleware); | ||
exports.Post = Post; | ||
function Put(path = '*', middleware = []) { | ||
return Route('put', path, middleware); | ||
} | ||
exports.put = put; | ||
function patch(path = '*', middleware = []) { | ||
return route('patch', path, middleware); | ||
exports.Put = Put; | ||
function Patch(path = '*', middleware = []) { | ||
return Route('patch', path, middleware); | ||
} | ||
exports.patch = patch; | ||
function del(path = '*', middleware = []) { | ||
return route('delete', path, middleware); | ||
exports.Patch = Patch; | ||
function Delete(path = '*', middleware = []) { | ||
return Route('delete', path, middleware); | ||
} | ||
exports.del = del; | ||
function options(path = '*', middleware = []) { | ||
return route('options', path, middleware); | ||
exports.Delete = Delete; | ||
function Options(path = '*', middleware = []) { | ||
return Route('options', path, middleware); | ||
} | ||
exports.options = options; | ||
function head(path = '*', middleware = []) { | ||
return route('head', path, middleware); | ||
exports.Options = Options; | ||
function Head(path = '*', middleware = []) { | ||
return Route('head', path, middleware); | ||
} | ||
exports.head = head; | ||
function use(path = '*') { | ||
return route('use', path); | ||
exports.Head = Head; | ||
function Use(path = '*') { | ||
return Route('use', path); | ||
} | ||
exports.use = use; | ||
function all(path = '*', middleware = []) { | ||
return route('all', path, middleware); | ||
exports.Use = Use; | ||
function All(path = '*', middleware = []) { | ||
return Route('all', path, middleware); | ||
} | ||
exports.all = all; | ||
function param(param) { | ||
exports.All = All; | ||
function Param(param) { | ||
return (target, key, descriptor) => { | ||
@@ -77,4 +77,4 @@ let routes = getRouteMetadata(target); | ||
} | ||
exports.param = param; | ||
function middleware(fn) { | ||
exports.Param = Param; | ||
function Middleware(fn) { | ||
return (target, key, descriptor) => { | ||
@@ -87,3 +87,3 @@ let routes = getRouteMetadata(target); | ||
} | ||
exports.middleware = middleware; | ||
exports.Middleware = Middleware; | ||
function getMiddleware(target, fn) { | ||
@@ -132,3 +132,3 @@ if (fn instanceof Function) { | ||
exports.getRoutes = getRoutes; | ||
function register(router, target) { | ||
function registerRoutes(router, target) { | ||
let routes = getRoutes(target); | ||
@@ -140,3 +140,3 @@ for (let route of routes) { | ||
} | ||
exports.register = register; | ||
exports.registerRoutes = registerRoutes; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "decorate-express", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"description": "Awesome decorators for express!", | ||
@@ -5,0 +5,0 @@ "main": "dist/lib/index.js", |
# decorate-express [![Travis CI](https://img.shields.io/travis/shroudedcode/decorate-express.svg)](https://travis-ci.org/shroudedcode/decorate-express) [![NPM](https://img.shields.io/npm/v/decorate-express.svg)](https://npm.im/decorate-express) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/shroudedcode/decorate-express.svg)](https://greenkeeper.io/) | ||
> This is a fork of [@stewartml](https://github.com/stewartml)'s no longer actively maintained [`express-decorators` package](https://github.com/stewartml/express-decorators). | ||
@@ -15,8 +17,8 @@ | ||
```js | ||
import * as web from 'decorate-express' | ||
```ts | ||
import { BasePath, Get, Use, registerRoutes } from 'decorate-express' | ||
import myMiddlewareFunction from './middleware' | ||
import express from 'express' | ||
@web.basePath('/test') | ||
@BasePath('/test') | ||
public class TestController { | ||
@@ -27,3 +29,3 @@ constructor(target) { | ||
@web.get('/hello', myMiddlewareFunction) | ||
@Get('/hello', myMiddlewareFunction) | ||
async sayHelloAction(request, response) { | ||
@@ -33,3 +35,3 @@ response.send(`Hello, ${this.target}!`) | ||
@web.use() | ||
@Use() | ||
async otherMiddleware(request, response, next) { | ||
@@ -43,3 +45,3 @@ // This middleware will be called for every action. | ||
let test = new TestController('world') | ||
web.register(app, test) | ||
registerRoutes(app, test) | ||
``` | ||
@@ -57,7 +59,7 @@ | ||
### `@basePath(path: string)` | ||
### `@BasePath(path: string)` | ||
Class decorator to add a base path to every route defined in the class. | ||
### `@middleware(fn: Middleware)` | ||
### `@Middleware(fn: Middleware)` | ||
@@ -68,3 +70,3 @@ If `fn` is a function, then the function is added as route-specific middleware for the action. Note that the middleware will be bound to the controller instance. | ||
### `@param(param: string)` | ||
### `@Param(param: string)` | ||
@@ -74,3 +76,3 @@ Marks the method as a handler for all routes that use the specified parameter. This can be useful if you want to do something with it before it's passed on to the actual route handler, for example converting a string to an integer: | ||
```js | ||
@param('id') | ||
@Param('id') | ||
idParam(request, response, next, id) { | ||
@@ -82,3 +84,3 @@ request.params.id = parseInt(request.params.id) | ||
### `@route(method: string, path: string, middleware: Middleware[])` | ||
### `@Route(method: string, path: string, middleware: Middleware[])` | ||
@@ -89,12 +91,12 @@ Marks the method as a handler for the specified path and HTTP method. | ||
Instead of passing the HTTP method into the `@route` decorator you can also use one of the provided shortcuts. `@get('/path')` for example is equivalent to `@route('get', '/path')`. | ||
Instead of passing the HTTP method into the `@Route` decorator you can also use one of the provided shortcuts. `@Get('/path')` for example is equivalent to `@Route('get', '/path')`. | ||
* `@all` | ||
* `@del` (not `@delete` because of [the `delete` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete)) | ||
* `@get` | ||
* `@options` | ||
* `@patch` | ||
* `@post` | ||
* `@put` | ||
* `@use` | ||
* `@All` | ||
* `@Delete` | ||
* `@Get` | ||
* `@Options` | ||
* `@Patch` | ||
* `@Post` | ||
* `@Put` | ||
* `@Use` | ||
@@ -105,3 +107,3 @@ ### `getRoutes(target: Object): Route[]` | ||
### `register(router: Express.Router, target: Object)` | ||
### `registerRoutes(router: Express.Router, target: Object)` | ||
@@ -108,0 +110,0 @@ Registers the routes found on the target object with an express Router instance. |
Sorry, the diff of this file is not supported yet
17425
105