@alterior/core
Advanced tools
Comparing version 0.0.16 to 0.0.23
@@ -5,3 +5,2 @@ export * from './lib/accesscontrol'; | ||
export * from './lib/upload'; | ||
export * from './lib/clone'; | ||
export * from './lib/express'; | ||
@@ -8,0 +7,0 @@ export * from './lib/middleware'; |
@@ -8,3 +8,2 @@ "use strict"; | ||
__export(require('./lib/application')); | ||
__export(require('./lib/clone')); | ||
__export(require('./lib/express')); | ||
@@ -11,0 +10,0 @@ __export(require('./lib/middleware')); |
@@ -5,4 +5,3 @@ | ||
export * from './lib/application'; | ||
export * from './lib/upload'; | ||
export * from './lib/clone'; | ||
export * from './lib/upload'; | ||
export * from './lib/express'; | ||
@@ -9,0 +8,0 @@ export * from './lib/middleware'; |
@@ -212,12 +212,6 @@ "use strict"; | ||
args_2.push(function (req, res) { | ||
if (!silent) | ||
console.log("[" + new Date().toLocaleString() + "] " + route.path + " => " + controller.name + "." + route.method + "()"); | ||
// Execute our function by resolving the parameter factories into a set of parameters to provide to the | ||
// function. | ||
var ev = new route_1.RouteEvent(req, res); | ||
var result; | ||
try { | ||
result = controllerInstance[route.method].apply(controllerInstance, paramFactories.map(function (x) { return x(ev); })); | ||
} | ||
catch (e) { | ||
/** | ||
* Handle exception response | ||
*/ | ||
function handleExceptionResponse(e) { | ||
if (e.constructor === errors_1.HttpException) { | ||
@@ -245,36 +239,38 @@ var httpException = e; | ||
} | ||
// Return value handling | ||
if (result === undefined) | ||
if (!silent) | ||
console.log("[" + new Date().toLocaleString() + "] " + route.path + " => " + controller.name + "." + route.method + "()"); | ||
// Execute our function by resolving the parameter factories into a set of parameters to provide to the | ||
// function. | ||
var ev = new route_1.RouteEvent(req, res); | ||
var result; | ||
try { | ||
result = controllerInstance[route.method].apply(controllerInstance, paramFactories.map(function (x) { return x(ev); })); | ||
} | ||
catch (e) { | ||
handleExceptionResponse(e); | ||
return; | ||
if (result.then) { | ||
result = result; | ||
result.then(function (result) { | ||
if (typeof result === 'object') | ||
result = JSON.stringify(result); | ||
res.status(200).header('Content-Type', 'application/json').send(result); | ||
}).catch(function (e) { | ||
if (e.constructor === errors_1.HttpException) { | ||
var httpException = e; | ||
res.status(httpException.statusCode); | ||
httpException.headers | ||
.forEach(function (header) { return res.header(header[0], header[1]); }); | ||
res.send(httpException.body); | ||
} | ||
else { | ||
res.status(500).send(JSON.stringify({ | ||
message: 'Failed to resolve this resource.', | ||
error: e | ||
})); | ||
} | ||
}); | ||
} | ||
else if (result.constructor === response_1.Response) { | ||
var response = result; | ||
res.status(response.status); | ||
response.headers.forEach(function (x) { return res.header(x[0], x[1]); }); | ||
res.send(response.body); | ||
// Return value handling | ||
function handleResponse(result) { | ||
if (result === undefined) | ||
return; | ||
if (result.then) { | ||
result = result; | ||
result | ||
.then(function (result) { return handleResponse(result); }) | ||
.catch(function (e) { return handleExceptionResponse(e); }); | ||
} | ||
else if (result.constructor === response_1.Response) { | ||
var response = result; | ||
res.status(response.status); | ||
response.headers.forEach(function (x) { return res.header(x[0], x[1]); }); | ||
res.send(response.body); | ||
} | ||
else { | ||
res.status(200) | ||
.header('Content-Type', 'application/json') | ||
.send(JSON.stringify(result)); | ||
} | ||
} | ||
else { | ||
res.status(200).header('Content-Type', 'application/json').send(JSON.stringify(result)); | ||
} | ||
handleResponse(result); | ||
}); | ||
@@ -281,0 +277,0 @@ // Send into express (registrar is one of express.get, express.put, express.post etc) |
@@ -265,14 +265,7 @@ import 'reflect-metadata'; | ||
if (!silent) | ||
console.log(`[${new Date().toLocaleString()}] ${route.path} => ${controller.name}.${route.method}()`); | ||
// Execute our function by resolving the parameter factories into a set of parameters to provide to the | ||
// function. | ||
let ev = new RouteEvent(req, res); | ||
let result; | ||
try { | ||
result = controllerInstance[route.method].apply(controllerInstance, paramFactories.map(x => x(ev))); | ||
} catch (e) { | ||
/** | ||
* Handle exception response | ||
*/ | ||
function handleExceptionResponse(e) { | ||
if (e.constructor === HttpException) { | ||
@@ -304,39 +297,45 @@ let httpException = <HttpException>e; | ||
// Return value handling | ||
if (!silent) | ||
console.log(`[${new Date().toLocaleString()}] ${route.path} => ${controller.name}.${route.method}()`); | ||
if (result === undefined) | ||
// Execute our function by resolving the parameter factories into a set of parameters to provide to the | ||
// function. | ||
let ev = new RouteEvent(req, res); | ||
let result; | ||
try { | ||
result = controllerInstance[route.method].apply(controllerInstance, paramFactories.map(x => x(ev))); | ||
} catch (e) { | ||
handleExceptionResponse(e); | ||
return; | ||
} | ||
if (result.then) { | ||
result = <Promise<any>>result; | ||
result.then(result => { | ||
if (typeof result === 'object') | ||
result = JSON.stringify(result); | ||
// Return value handling | ||
res.status(200).header('Content-Type', 'application/json').send(result); | ||
}).catch(e => { | ||
if (e.constructor === HttpException) { | ||
let httpException = <HttpException>e; | ||
res.status(httpException.statusCode); | ||
httpException.headers | ||
.forEach(header => res.header(header[0], header[1])); | ||
function handleResponse(result) { | ||
if (result === undefined) | ||
return; | ||
res.send(httpException.body); | ||
} else { | ||
res.status(500).send(JSON.stringify({ | ||
message: 'Failed to resolve this resource.', | ||
error: e | ||
})); | ||
} | ||
}); | ||
} else if (result.constructor === Response) { | ||
let response = <Response>result; | ||
res.status(response.status); | ||
response.headers.forEach(x => res.header(x[0], x[1])); | ||
res.send(response.body); | ||
} else { | ||
res.status(200).header('Content-Type', 'application/json').send(JSON.stringify(result)); | ||
if (result.then) { | ||
result = <Promise<any>>result; | ||
result | ||
.then(result => handleResponse(result)) | ||
.catch(e => handleExceptionResponse(e)) | ||
; | ||
} else if (result.constructor === Response) { | ||
let response = <Response>result; | ||
res.status(response.status); | ||
response.headers.forEach(x => res.header(x[0], x[1])); | ||
res.send(response.body); | ||
} else { | ||
res .status(200) | ||
.header('Content-Type', 'application/json') | ||
.send(JSON.stringify(result)) | ||
; | ||
} | ||
} | ||
handleResponse(result); | ||
}); | ||
@@ -343,0 +342,0 @@ |
export declare type EncodingType = 'json' | 'raw'; | ||
export declare class Response { | ||
status: number; | ||
headers: string[][]; | ||
body: string; | ||
/** | ||
@@ -11,4 +9,12 @@ * Constructs a new Response, with the given status code, headers, and | ||
*/ | ||
constructor(status: number, headers: string[][], body: string, isRawBody?: boolean); | ||
constructor(status: number, headers: string[][] | any, body: any, isRawBody?: boolean); | ||
/** | ||
* Headers for the response, as an array of [key, value] tuple arrays. | ||
*/ | ||
headers: string[][]; | ||
/** | ||
* Body of the response. | ||
*/ | ||
body: string; | ||
/** | ||
* Stores the raw, unencoded body in case the user calls encodeAs() | ||
@@ -15,0 +21,0 @@ */ |
"use strict"; | ||
var underscore_1 = require('underscore'); | ||
var errors_1 = require('./errors'); | ||
@@ -10,12 +11,12 @@ var Response = (function () { | ||
function Response(status, headers, body, isRawBody) { | ||
// Normalize headers | ||
this.status = status; | ||
if (typeof headers === 'object' && !headers.length) | ||
headers = underscore_1.pairs(headers); | ||
this.headers = headers; | ||
this.body = body; | ||
if (isRawBody === undefined) | ||
isRawBody = false; | ||
headers = headers || []; | ||
if (!isRawBody) { | ||
this.unencodedBody = body; | ||
this.body = JSON.stringify(body); | ||
} | ||
this.unencodedBody = body; | ||
this.encodeAs(isRawBody ? 'raw' : 'json'); | ||
} | ||
@@ -22,0 +23,0 @@ /** |
@@ -1,2 +0,3 @@ | ||
import { pairs } from 'underscore'; | ||
export type EncodingType = 'json' | 'raw'; | ||
@@ -13,6 +14,12 @@ import { HttpException } from './errors'; | ||
public status : number, | ||
public headers : string[][], | ||
public body : string, | ||
headers : string[][] | any, | ||
body : any, | ||
isRawBody? : boolean | ||
) { | ||
// Normalize headers | ||
if (typeof headers === 'object' && !headers.length) | ||
headers = pairs(headers); | ||
this.headers = headers; | ||
if (isRawBody === undefined) | ||
@@ -22,9 +29,17 @@ isRawBody = false; | ||
if (!isRawBody) { | ||
this.unencodedBody = body; | ||
this.body = JSON.stringify(body); | ||
} | ||
this.unencodedBody = body; | ||
this.encodeAs(isRawBody ? 'raw' : 'json'); | ||
} | ||
/** | ||
* Headers for the response, as an array of [key, value] tuple arrays. | ||
*/ | ||
public headers : string[][]; | ||
/** | ||
* Body of the response. | ||
*/ | ||
public body : string; | ||
/** | ||
* Stores the raw, unencoded body in case the user calls encodeAs() | ||
@@ -31,0 +46,0 @@ */ |
{ | ||
"name": "@alterior/core", | ||
"version": "0.0.16", | ||
"version": "0.0.23", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "An Express-based Typescript MVC framework using decorators and Angular 2 dependency injection.", |
@@ -14,4 +14,5 @@ { | ||
"superagent": "registry:dt/superagent#2.0.0+20160906143408", | ||
"supertest": "registry:dt/supertest#1.1.0+20160724070541" | ||
"supertest": "registry:dt/supertest#1.1.0+20160724070541", | ||
"underscore": "registry:dt/underscore#1.8.3+20160908111004" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
2505
181197