@foal/core
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -14,3 +14,4 @@ import * as Ajv from 'ajv'; | ||
* This configuration can be overrided using the file `config/default.json` or through environment | ||
* variables: SETTINGS_AJV_COERCE_TYPES, SETTINGS_AJV_REMOVE_ADDITIONAL, SETTINGS_AJV_USE_DEFAULTS. | ||
* variables: SETTINGS_AJV_COERCE_TYPES, SETTINGS_AJV_REMOVE_ADDITIONAL, SETTINGS_AJV_USE_DEFAULTS, | ||
* SETTINGS_AJV_NULLABLE. | ||
* | ||
@@ -17,0 +18,0 @@ * @export |
@@ -20,3 +20,4 @@ "use strict"; | ||
* This configuration can be overrided using the file `config/default.json` or through environment | ||
* variables: SETTINGS_AJV_COERCE_TYPES, SETTINGS_AJV_REMOVE_ADDITIONAL, SETTINGS_AJV_USE_DEFAULTS. | ||
* variables: SETTINGS_AJV_COERCE_TYPES, SETTINGS_AJV_REMOVE_ADDITIONAL, SETTINGS_AJV_USE_DEFAULTS, | ||
* SETTINGS_AJV_NULLABLE. | ||
* | ||
@@ -30,2 +31,3 @@ * @export | ||
coerceTypes: core_1.Config.get('settings.ajv.coerceTypes', true), | ||
nullable: core_1.Config.get('settings.ajv.nullable'), | ||
removeAdditional: core_1.Config.get('settings.ajv.removeAdditional', true), | ||
@@ -32,0 +34,0 @@ useDefaults: core_1.Config.get('settings.ajv.useDefaults', true), |
@@ -0,10 +1,24 @@ | ||
import * as express from 'express'; | ||
import { Class } from '../core'; | ||
interface ExpressApplication extends express.Express { | ||
[name: string]: any; | ||
} | ||
interface ExpressOptions { | ||
preMiddlewares?: (express.RequestHandler | express.ErrorRequestHandler)[]; | ||
postMiddlewares?: (express.RequestHandler | express.ErrorRequestHandler)[]; | ||
} | ||
/** | ||
* Create an express application from the root controller of the Foal project. | ||
* Create an Express application from the root controller. | ||
* | ||
* @export | ||
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`. | ||
* @param {*} [expressInstance] - Optional express instance to be used as base. | ||
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining | ||
* Express middlewares. | ||
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express | ||
* middlewares to be executed before the controllers and hooks. | ||
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.postMiddlewares] Express | ||
* middlewares to be executed after the controllers and hooks, but before the 500 or 404 handler get called. | ||
* @returns The express application. | ||
*/ | ||
export declare function createApp(rootControllerClass: Class, expressInstance?: any): any; | ||
export declare function createApp(rootControllerClass: Class, expressInstanceOrOptions?: ExpressApplication | ExpressOptions): ExpressApplication; | ||
export {}; |
@@ -14,13 +14,29 @@ "use strict"; | ||
/** | ||
* Create an express application from the root controller of the Foal project. | ||
* Create an Express application from the root controller. | ||
* | ||
* @export | ||
* @param {Class} rootControllerClass - The root controller, usually called `AppController` and located in `src/app`. | ||
* @param {*} [expressInstance] - Optional express instance to be used as base. | ||
* @param {(ExpressApplication|ExpressOptions)} [expressInstanceOrOptions] - Express instance or options containaining | ||
* Express middlewares. | ||
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.preMiddlewares] Express | ||
* middlewares to be executed before the controllers and hooks. | ||
* @param {(express.RequestHandler | express.ErrorRequestHandler)[]} [expressInstanceOrOptions.postMiddlewares] Express | ||
* middlewares to be executed after the controllers and hooks, but before the 500 or 404 handler get called. | ||
* @returns The express application. | ||
*/ | ||
function createApp(rootControllerClass, expressInstance) { | ||
const app = expressInstance || express(); | ||
function createApp(rootControllerClass, expressInstanceOrOptions) { | ||
let app = express(); | ||
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'function') { | ||
app = expressInstanceOrOptions; | ||
} | ||
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'object') { | ||
for (const middleware of expressInstanceOrOptions.preMiddlewares || []) { | ||
app.use(middleware); | ||
} | ||
} | ||
const LOG_FORMAT_NONE = 'none'; | ||
const loggerFormat = core_1.Config.get('settings.loggerFormat', '[:date] ":method :url HTTP/:http-version" :status - :response-time ms'); | ||
app.use(logger(loggerFormat)); | ||
if (loggerFormat !== LOG_FORMAT_NONE) { | ||
app.use(logger(loggerFormat)); | ||
} | ||
app.use((_, res, next) => { | ||
@@ -79,2 +95,7 @@ res.removeHeader('X-Powered-By'); | ||
} | ||
if (expressInstanceOrOptions && typeof expressInstanceOrOptions === 'object') { | ||
for (const middleware of expressInstanceOrOptions.postMiddlewares || []) { | ||
app.use(middleware); | ||
} | ||
} | ||
app.use(not_found_1.notFound()); | ||
@@ -81,0 +102,0 @@ app.use(handle_errors_1.handleErrors(core_1.Config.get('settings.debug', false), console.error)); |
@@ -12,2 +12,2 @@ /** | ||
(message?: any, ...optionalParams: any[]): void; | ||
}): (err: any, req: any, res: any, next: any) => void; | ||
}): (err: any, req: any, res: any, next: any) => Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = require("fs"); | ||
const path_1 = require("path"); | ||
const util_1 = require("util"); | ||
const render_util_1 = require("../common/utils/render.util"); | ||
const page500 = '<html><head><title>INTERNAL SERVER ERROR</title></head><body>' | ||
+ '<h1>500 - INTERNAL SERVER ERROR</h1></body></html>'; | ||
function renderDebug500(stack) { | ||
return '<html>' | ||
+ '<head>' | ||
+ '<title>INTERNAL SERVER ERROR</title>' | ||
+ '</head>' | ||
+ '<body>' | ||
+ '<h1>500 - INTERNAL SERVER ERROR</h1>' | ||
+ `<pre>${stack}</pre>` | ||
+ 'You are seeing this error because you have debug set to true in your configuration file.' | ||
+ '</body>' | ||
+ '</html>'; | ||
} | ||
/** | ||
@@ -26,3 +18,3 @@ * Create an express middleware to return a 500 HTML page if an error is thrown and is not caught. | ||
function handleErrors(debug, logFn = console.error) { | ||
return (err, req, res, next) => { | ||
return async (err, req, res, next) => { | ||
if (err.expose && err.status) { | ||
@@ -33,12 +25,14 @@ next(err); | ||
logFn(err.stack); | ||
if (debug) { | ||
res.status(500) | ||
.send(renderDebug500(err.stack)); | ||
if (!debug) { | ||
res.status(500).send(page500); | ||
return; | ||
} | ||
else { | ||
res.status(500) | ||
.send(page500); | ||
} | ||
const template = await util_1.promisify(fs_1.readFile)(path_1.join(__dirname, '500.debug.html'), 'utf8'); | ||
res.status(500).send(render_util_1.renderToString(template, { | ||
message: err.message, | ||
name: err.name, | ||
stack: err.stack, | ||
})); | ||
}; | ||
} | ||
exports.handleErrors = handleErrors; |
@@ -11,3 +11,3 @@ import { Session } from './session'; | ||
* | ||
* Examples of SessionStore: TypeORMStore, RedisStore. | ||
* Examples of SessionStore: TypeORMStore, RedisStore, MongoDBStore. | ||
* | ||
@@ -14,0 +14,0 @@ * @export |
@@ -13,3 +13,3 @@ "use strict"; | ||
* | ||
* Examples of SessionStore: TypeORMStore, RedisStore. | ||
* Examples of SessionStore: TypeORMStore, RedisStore, MongoDBStore. | ||
* | ||
@@ -16,0 +16,0 @@ * @export |
@@ -84,3 +84,3 @@ "use strict"; | ||
/* Verify the session content */ | ||
const userId = session.get('userId'); | ||
let userId = session.get('userId'); | ||
if (!options.user) { | ||
@@ -90,2 +90,5 @@ ctx.user = userId; | ||
else { | ||
if (typeof userId === 'object' && userId !== null) { | ||
userId = userId.toString(); | ||
} | ||
if (typeof userId !== 'number' && typeof userId !== 'string') { | ||
@@ -92,0 +95,0 @@ throw new Error(`The "userId" value of the session ${sessionID} must be a string or a number. Got "${typeof userId}".`); |
{ | ||
"name": "@foal/core", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "High level web framework to create enterprise-grade Node.JS applications.", | ||
@@ -18,3 +18,3 @@ "main": "./lib/index.js", | ||
"dev:test:sessions": "mocha --require ts-node/register --watch --watch-extensions ts \"./src/sessions/**/*.spec.ts\"", | ||
"build": "rimraf lib && tsc -p tsconfig-build.json", | ||
"build": "rimraf lib && copy-cli \"./src/**/500.debug.html\" lib && tsc -p tsconfig-build.json", | ||
"prepublish": "npm run build" | ||
@@ -90,6 +90,7 @@ }, | ||
"devDependencies": { | ||
"@foal/ejs": "^1.0.1", | ||
"@foal/ejs": "^1.1.0", | ||
"@types/mocha": "^2.2.43", | ||
"@types/node": "^10.1.2", | ||
"@types/supertest": "^2.0.5", | ||
"copy": "^0.3.2", | ||
"ejs": "^2.6.2", | ||
@@ -111,3 +112,3 @@ "mocha": "^5.2.0", | ||
}, | ||
"gitHead": "f5b0c70aa42c89cbad7cd95788e9f9a15b0d62e2" | ||
"gitHead": "59007957fec8bb041dde5acc525f111deb901084" | ||
} |
@@ -39,10 +39,4 @@ <p align="center"> | ||
Github: [https://github.com/FoalTS/foal](https://github.com/FoalTS/foal) | ||
[Github](https://github.com/FoalTS/foal) - [Twitter](https://twitter.com/FoalTs) - [Website](https://foalts.org/) - [Documentation](https://foalts.gitbook.io/docs/) - [YouTube](https://www.youtube.com/channel/UCQFojM334E0YdoDq56MjfOQ) | ||
Twitter: [https://twitter.com/FoalTs](https://twitter.com/FoalTs) | ||
Website: [https://foalts.org/](https://foalts.org/) | ||
Documentation: [https://foalts.gitbook.io/docs/](https://foalts.gitbook.io/docs/) | ||
FoalTS is a Node.js framework for building HTTP APIs and Web applications with a rich interface (Angular / React / Vue). It is written in TypeScript and offers many built-in dev tools and components to handle extremely common scenarios. Simple, testable and progressive, Foal accelerates development while leaving you in control of your code. | ||
@@ -49,0 +43,0 @@ |
328969
188
8339
20
67
9