From 956d06c2072723562d49568c10ee40672511c108 Mon Sep 17 00:00:00 2001 From: scott-wyatt Date: Wed, 8 May 2019 08:52:00 -0500 Subject: [PATCH] [feat] cleanup --- lib/ExpressSpool.ts | 2 +- lib/server.ts | 92 ++++++++++++++++++++++++++++----------------- package-lock.json | 16 ++++---- package.json | 6 +-- 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/lib/ExpressSpool.ts b/lib/ExpressSpool.ts index 8008f03..62b8ae4 100644 --- a/lib/ExpressSpool.ts +++ b/lib/ExpressSpool.ts @@ -73,7 +73,7 @@ export class ExpressSpool extends ServerSpool { */ async initialize () { - this.server = Server.createServer(this.app) + this.server = Server.createExpressServer(this.app) return Promise.all([ Server.registerMiddlewares(this.app, this.server), diff --git a/lib/server.ts b/lib/server.ts index 808ac66..4c693e7 100755 --- a/lib/server.ts +++ b/lib/server.ts @@ -16,14 +16,19 @@ import * as consolidate from 'consolidate' import * as expressBoom from 'express-boom' -export const Server = { +export interface Server { + [key: string]: any + nativeServers: Map +} + +export const Server: Server = { BreakException: {}, port: null, + portHttp: null, host: null, ssl: null, redirectToHttps: false, - // nativeServer: null, - nativeServers: new Map(), // : Map, + nativeServers: new Map(), serverRoutes: {}, serverPolicies: {}, serverHandlers: {}, @@ -31,7 +36,7 @@ export const Server = { middlewares: {}, middlewaresOrder: [], - createServer(app: FabrixApp) { + createExpressServer(app: FabrixApp) { const main = app.config.get('main') const sess = app.config.get('session') const express = app.config.get('web.express') @@ -80,6 +85,7 @@ export const Server = { this.middlewares.addMethods = (req: Request, res: Response, next) => { req.log = app.log req.fabrixApp = app + const accept = req.get('accept') || '' req.wantsJSON = accept.indexOf('json') !== -1 @@ -99,36 +105,48 @@ export const Server = { return str } - res.serverError = err => { - this.middlewares['500'](err, req, res, next) + // Helper 500 + if (!res.serverError) { + res.serverError = err => { + this.middlewares['500'](err, req, res, next) + } } - res.notFound = () => { - this.middlewares['404'](req, res, next) + // Helper 404 + if (!res.notFound) { + res.notFound = () => { + this.middlewares['404'](req, res, next) + } } - res.forbidden = (msg) => { - res.serverError({ - statusCode: 403, - code: 'E_FORBIDDEN', - message: msg || 'forbidden' - }) + // Helper 403 + if (!res.forbidden) { + res.forbidden = (msg) => { + res.serverError({ + statusCode: 403, + code: 'E_FORBIDDEN', + message: msg || 'forbidden' + }) + } } - res.paginate = (count, limit = 0, offset = 0, sort = []) => { - limit = Number(limit) - offset = Number(offset) - - const pages = Math.ceil(count / limit) === 0 ? 1 : Math.ceil(count / limit) - const page = Math.round(((offset + limit) / limit)) - - res.set('X-Pagination-Total', count) - res.set('X-Pagination-Pages', pages.toString()) - res.set('X-Pagination-Page', page.toString()) - res.set('X-Pagination-Offset', offset.toString()) - res.set('X-Pagination-Limit', limit.toString()) - res.set('X-Pagination-Sort', Server.sortToString(sort)) - return res + // Helper Paginate Utility + if (!res.paginate) { + res.paginate = (count, limit = 0, offset = 0, sort = []) => { + limit = Number(limit) + offset = Number(offset) + + const pages = Math.ceil(count / limit) === 0 ? 1 : Math.ceil(count / limit) + const page = Math.round(((offset + limit) / limit)) + + res.set('X-Pagination-Total', count) + res.set('X-Pagination-Pages', pages.toString()) + res.set('X-Pagination-Page', page.toString()) + res.set('X-Pagination-Offset', offset.toString()) + res.set('X-Pagination-Limit', limit.toString()) + res.set('X-Pagination-Sort', Server.sortToString(sort)) + return res + } } next() @@ -138,6 +156,10 @@ export const Server = { return server }, + /** + * This shouldn't be here! + * @param sort + */ sortToString(sort = []) { if (typeof sort === 'string') { return sort @@ -364,10 +386,10 @@ export const Server = { init(app, server) - const promises = Array.from(this.nativeServers.values()).map(s => { - console.log('BRK', s) - return Server.listenPromise(s) - }) + const promises = Array.from(this.nativeServers.values()) + .map((s) => { + return Server.listenPromise(s) + }) return Promise.all(promises) }, @@ -392,7 +414,7 @@ export const Server = { this.nativeServers.set('http', { server: http.createServer(server), host: this.host, - port: this.portHttp, + port: this.portHttp || this.port, }) } return resolve() @@ -408,9 +430,9 @@ export const Server = { }) }, - listenPromise(server) { + listenPromise(config: {host: string, port: number, server: any}) { return new Promise((resolve, reject) => { - server.server.listen(server.port, server.host, function (err) { + config.server.listen(config.port, config.host, function (err) { if (err) { reject(err) } diff --git a/package-lock.json b/package-lock.json index 477bf1f..d185614 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@fabrix/spool-express", - "version": "1.6.1", + "version": "1.6.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -913,9 +913,9 @@ "dev": true }, "component-emitter": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz", - "integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY=", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz", + "integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==", "dev": true }, "compressible": { @@ -9011,7 +9011,7 @@ "integrity": "sha512-GLQtLMCoEIK4eDv6OGtkOoSMt3D+oq0y3dsxMuYuDvaNUvuT8eFBuLmfR0iYYzHC1e8hpzC6ZsxbuP6DIalMFA==", "dev": true, "requires": { - "component-emitter": "1.2.1", + "component-emitter": "1.3.0", "cookiejar": "2.1.2", "debug": "3.2.6", "extend": "3.0.2", @@ -9041,9 +9041,9 @@ } }, "supertest": { - "version": "3.4.2", - "resolved": "https://registry.npmjs.org/supertest/-/supertest-3.4.2.tgz", - "integrity": "sha512-WZWbwceHUo2P36RoEIdXvmqfs47idNNZjCuJOqDz6rvtkk8ym56aU5oglORCpPeXGxT7l9rkJ41+O1lffQXYSA==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-4.0.2.tgz", + "integrity": "sha512-1BAbvrOZsGA3YTCWqbmh14L0YEq0EGICX/nBnfkfVJn7SrxQV1I3pMYjSzG9y/7ZU2V9dWqyqk2POwxlb09duQ==", "dev": true, "requires": { "methods": "1.1.2", diff --git a/package.json b/package.json index d703b34..24c9e0d 100755 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@fabrix/spool-express", - "version": "1.6.1", - "description": "Spool Express - Binds the routes compiled in spool-router to a Express 4 Server.", + "version": "1.6.2", + "description": "Spool Express - Binds the routes compiled in spool-router to an Express 4 Server.", "homepage": "https://fabrix.app", "author": { "name": "Scott Wyatt", @@ -89,7 +89,7 @@ "pug": "^2.0.1", "smokesignals": "^3", "sqlite3": "^4.0.2", - "supertest": "^3.4.2", + "supertest": "^4.0.2", "tslib": "~1.9.0", "tslint": "~5.10.0", "tslint-microsoft-contrib": "~5.0.3",