1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var express = require("express");
|
4 | var cors = require("cors");
|
5 | var cookieParser = require("cookie-parser");
|
6 | var bodyParser = require("body-parser");
|
7 | var libs_1 = require("../libs");
|
8 | var listEndpoints = require('express-list-endpoints');
|
9 | exports.Router = express.Router;
|
10 | var staticRoutes_1 = require("./middleware/staticRoutes");
|
11 | exports.staticRoutes = staticRoutes_1.staticRoutes;
|
12 | var IS_PRODUCTION = process.env.NODE_ENV === 'production';
|
13 | function formatErrorStack(stack) {
|
14 | if (stack === void 0) { stack = ''; }
|
15 | var lines = stack.split('\n');
|
16 | var message = lines[0];
|
17 | lines.shift();
|
18 | return {
|
19 | message: message,
|
20 | stack: lines.map(function (line) { return line.trim(); }),
|
21 | };
|
22 | }
|
23 | function sendError(code, res, err) {
|
24 | var error = IS_PRODUCTION ? err.message : formatErrorStack(err.stack);
|
25 | res.status(code).send({
|
26 | status: code,
|
27 | error: error,
|
28 | });
|
29 | }
|
30 | exports.sendError = sendError;
|
31 | function app(options) {
|
32 | if (options === void 0) { options = {}; }
|
33 | var app = express()
|
34 | .use(cors(options.cors))
|
35 | .use(bodyParser.json(options.json))
|
36 | .use(cookieParser());
|
37 | if (options.static) {
|
38 | app.use(express.static(options.static));
|
39 | }
|
40 | return app;
|
41 | }
|
42 | exports.app = app;
|
43 | function router() {
|
44 | var routes = express.Router();
|
45 | return routes;
|
46 | }
|
47 | exports.router = router;
|
48 | function routes(router) {
|
49 | var items = listEndpoints(router);
|
50 | return items.reduce(function (acc, next) {
|
51 | var index = acc.findIndex(function (r) { return r.path === next.path; });
|
52 | if (index > -1) {
|
53 | var route = items[index];
|
54 | route.methods = libs_1.R.uniq(route.methods.concat(next.methods));
|
55 | return acc;
|
56 | }
|
57 | return acc.concat([next]);
|
58 | }, []);
|
59 | }
|
60 | exports.routes = routes;
|
61 | function logStarted(port, args) {
|
62 | if (args === void 0) { args = {}; }
|
63 | var PACKAGE = require(libs_1.fsPath.resolve('./package.json'));
|
64 | libs_1.log.info("\n> Ready on " + libs_1.log.cyan('localhost') + ":" + libs_1.log.magenta(port));
|
65 | libs_1.log.info();
|
66 | libs_1.log.info.gray(" name: " + libs_1.log.white(PACKAGE.name) + "@" + PACKAGE.version);
|
67 | libs_1.log.info.gray(" dev: " + libs_1.constants.IS_DEV);
|
68 | libs_1.log.info();
|
69 | }
|
70 | exports.logStarted = logStarted;
|
71 |
|
\ | No newline at end of file |