Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Last active January 29, 2016 21:40
Show Gist options
  • Save amcdnl/c3d43b4d52acedbdf6f1 to your computer and use it in GitHub Desktop.
Save amcdnl/c3d43b4d52acedbdf6f1 to your computer and use it in GitHub Desktop.
[ 'SyntaxError: Unexpected token ...',
' at exports.runInThisContext (vm.js:53:16)',
' at Module._compile (module.js:387:25)',
' at Object.Module._extensions..js (module.js:422:10)',
' at Module.load (module.js:357:32)',
' at Function.Module._load (module.js:314:12)',
' at Module.require (module.js:367:17)',
' at require (internal/module.js:16:19)',
' at Object.<anonymous> (/Node/dist/api/ApplicationController.js:20:19)',
' at Module._compile (module.js:413:34)',
' at Object.Module._extensions..js (module.js:422:10)',
' at Module.load (module.js:357:32)',
' at Function.Module._load (module.js:314:12)',
' at Module.require (module.js:367:17)',
' at require (internal/module.js:16:19)',
' at Node/dist/api/api.js:16:12',
' at Array.map (native)' ] }
import 'reflect-metadata';
const ROUTES_PREFIX: string = 'routes';
const ROUTE_PREFIX: string = 'route_';
const ACTION_TYPES = {
HEAD: 'head',
GET: 'get',
POST: 'post',
PUT: 'put',
DELETE: 'delete',
OPTIONS: 'options',
ALL: 'all'
};
export function Controller(path?: string, ...middlewares : Function[]) {
return function (target) {
const metadata = Reflect.getMetadataKeys(target);
let routes = metadata
.filter(prop => prop.indexOf(ROUTE_PREFIX) === 0)
.map(route => {
return {
method: route.method,
url: path + route.path,
middleware: middlewares.concat(route.middleware),
fnName: route.substring(ROUTE_PREFIX.length)
};
});
Reflect.defineMetadata(ROUTES_PREFIX, routes, target);
};
};
export function route(method: string, path?: string, ...middleware : Function[]) {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
Reflect.defineMetadata(ROUTE_PREFIX + propertyKey, { method, path, middleware }, target);
};
};
export function httpGet(path?: string, ...middlewares : Function[]) {
return route.bind(null, ACTION_TYPES.GET);
};
export function httpPost(path?: string, ...middlewares : Function[]) {
return route.bind(null, ACTION_TYPES.POST);
};
export function httpPut(path?: string, ...middlewares : Function[]) {
return route.bind(null, ACTION_TYPES.PUT);
};
export function httpDelete(path?: string, ...middlewares : Function[]) {
return route.bind(null, ACTION_TYPES.DELETE);
};
export function bindRoutes(controllers, routerRoutes) {
for(let ctrl of controllers) {
let routes = Reflect.getMetadata(ROUTES_PREFIX, ctrl.default);
for(let route of routes) {
routerRoutes[route.method](this, ...route.middleware, function(){
return new ctrl.default[route.fnName](this, ...arguments);
});
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment