@autofleet/super-express 1.1.21-beta-4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +43 -0
  2. package/index.js +16 -42
  3. package/package.json +11 -8
package/index.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import express from 'express';
2
+
3
+ /**
4
+ * Options to customize the behavior of the SuperExpress application.
5
+ */
6
+ export type DefaultOptions = {
7
+ /**
8
+ * Enables or disables body parser middleware.
9
+ */
10
+ bodyParser?: boolean;
11
+ // Add other options from default-options.json as needed
12
+ };
13
+
14
+ /**
15
+ * Type for the callback function used in the listen method.
16
+ */
17
+ type ListenCallback = () => void;
18
+
19
+ /**
20
+ * Extends the express.Application interface to include nativeListen and the overridden listen method.
21
+ */
22
+ export interface SuperExpressApp extends express.Application {
23
+ /**
24
+ * Original express listen method.
25
+ */
26
+ nativeListen: express.Application['listen'];
27
+
28
+ /**
29
+ * Overridden listen method to add custom behavior.
30
+ * @param port - The port number to listen on.
31
+ * @param cb - Optional callback function to execute after the server starts listening.
32
+ */
33
+ listen(port: number, cb?: ListenCallback): void;
34
+ }
35
+
36
+ /**
37
+ * Creates a new SuperExpress application with the given options.
38
+ * @param options - Optional settings to customize the application.
39
+ * @returns A SuperExpress application instance.
40
+ */
41
+ export default function createSuperExpressApp(
42
+ options?: Partial<DefaultOptions & express.ApplicationOptions>
43
+ ): SuperExpressApp;
package/index.js CHANGED
@@ -1,53 +1,27 @@
1
- const express = require('express')
1
+ import express from 'express';
2
+ import helmet from 'helmet';
3
+ import morgan from 'morgan';
2
4
 
3
- const chalk = require('chalk');
5
+ import defaultOptions from './default-options.json';
4
6
 
5
- const helmet = require('helmet')
6
- const bodyParser = require('body-parser')
7
- const morgan = require('morgan')
7
+ export default function(options = {}) {
8
+ const app = express(options);
9
+ const isProd = process.env.NODE_ENV === 'production';
10
+ const mergedOptions = Object.assign({}, defaultOptions, options);
8
11
 
9
- const defaultOptions = require('./default-options.json')
10
-
11
- module.exports = function(options = {}) {
12
- const app = express(options)
13
-
14
- const isProd = process.env.NODE_ENV === 'production'
15
- const isStage = process.env.NODE_ENV === 'staging'
16
- const mergedOptions = Object.assign({}, defaultOptions, options)
17
-
18
- if (options.httpLogCb) {
19
- app.use(morgan((tokens, req, res) => {
20
- options.httpLogCb({
21
- method: tokens.method(req, res),
22
- url: tokens.url(req, res),
23
- status: tokens.status(req, res),
24
- 'content-length': tokens.res(req, res, 'content-length'),
25
- 'response-time': tokens['response-time'](req, res),
26
- 'user-agent': tokens['user-agent'](req, res),
27
- })
28
- }))
29
- } else {
30
- app.use(morgan(':method :url :status :res[content-length] - :response-time ms'))
31
- }
32
-
33
- // Timeout
34
- app.use((req, res, next) => {
35
- req.socket.setKeepAlive(true);
36
- next();
37
- });
38
-
39
-
40
- // General
12
+ app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
41
13
  app.use(helmet());
42
- if( mergedOptions.bodyParser ) {
43
- app.use(bodyParser.json({ limit: '1000mb' }))
14
+ if (mergedOptions.bodyParser) {
15
+ app.use(express.json({ limit: '1000mb' }))
16
+ } else {
17
+ console.log('[SuperExpress] Body parser is disabled');
44
18
  }
45
19
 
46
20
  app.nativeListen = app.listen
47
21
  app.listen = function (port, cb) {
48
- console.log(chalk.blue(`Super express will listen on port ${port}`))
49
- console.log(chalk.blue(`Production mode: ${isProd}`))
50
- return app.nativeListen(port, cb)
22
+ console.log(`[SuperExpress] will listen on port ${port}`);
23
+ console.log(`[SuperExpress] Production mode: ${isProd}`);
24
+ app.nativeListen(port, cb)
51
25
  }
52
26
 
53
27
  return app
package/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "@autofleet/super-express",
3
- "version": "1.1.21-beta-4",
3
+ "version": "2.1.0",
4
4
  "description": "AF Express with built in boilerplate",
5
5
  "main": "index.js",
6
- "author": "",
6
+ "type": "module",
7
+ "author": "Autofleet",
7
8
  "license": "MIT",
8
9
  "dependencies": {
9
- "body-parser": "^1.19.0",
10
- "chalk": "^2.4.1",
11
- "express": "^4.17.1",
12
- "helmet": "^4.6.0",
13
- "morgan": "^1.10.0"
10
+ "express": "^4.19.2",
11
+ "helmet": "^3.21.2",
12
+ "morgan": "^1.9.1"
14
13
  },
15
14
  "scripts": {
16
15
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -22,5 +21,9 @@
22
21
  "bugs": {
23
22
  "url": "https://github.com/Autofleet/super-express/issues"
24
23
  },
25
- "homepage": "https://github.com/Autofleet/super-express#readme"
24
+ "homepage": "https://github.com/Autofleet/super-express#readme",
25
+ "devDependencies": {
26
+ "@types/express": "^4.17.21",
27
+ "@types/helmet": "^4.0.0"
28
+ }
26
29
  }