@autofleet/super-express 4.0.0 → 5.0.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.
- package/package.json +2 -3
- package/src/index.d.ts +71 -67
- package/src/index.js +22 -15
- package/test/index.test.js +19 -28
- package/test/mock-package.json +1 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@autofleet/super-express",
|
3
|
-
"version": "
|
3
|
+
"version": "5.0.0",
|
4
4
|
"description": "AF Express with built in boilerplate",
|
5
5
|
"main": "src/index.js",
|
6
6
|
"author": "Autofleet",
|
@@ -12,8 +12,7 @@
|
|
12
12
|
"morgan": "^1.10.0"
|
13
13
|
},
|
14
14
|
"scripts": {
|
15
|
-
"test": "node --test test/index.test.js"
|
16
|
-
"publish": "npm publish"
|
15
|
+
"test": "node --test test/index.test.js"
|
17
16
|
},
|
18
17
|
"repository": {
|
19
18
|
"type": "git",
|
package/src/index.d.ts
CHANGED
@@ -1,82 +1,86 @@
|
|
1
1
|
import * as express from 'express';
|
2
2
|
|
3
|
-
|
3
|
+
/**
|
4
|
+
* Options to customize the behavior of the SuperExpress application.
|
5
|
+
*/
|
6
|
+
export interface DefaultOptions {
|
4
7
|
/**
|
5
|
-
*
|
8
|
+
* The name of the application.
|
6
9
|
*/
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
/**
|
29
|
-
* Path to the package.json file for the stats endpoint.
|
30
|
-
*/
|
31
|
-
packageJsonPath?: string;
|
32
|
-
/**
|
33
|
-
* Enables or disables request tracing middleware.
|
34
|
-
*/
|
35
|
-
tracing?: boolean;
|
36
|
-
/**
|
37
|
-
* Enables or disables eager loading of user permissions for tracing middleware.
|
38
|
-
*/
|
39
|
-
eagerLoadUserPermissions?: boolean;
|
40
|
-
/**
|
41
|
-
* Options to customize the alive endpoint middleware.
|
42
|
-
*/
|
43
|
-
aliveEndpointOptions?: Record<string, unknown>;
|
44
|
-
// Add other options from config/default-options.json as needed
|
45
|
-
}
|
46
|
-
|
10
|
+
name?: string;
|
11
|
+
/**
|
12
|
+
* The version of the application.
|
13
|
+
*/
|
14
|
+
version?: string;
|
15
|
+
/**
|
16
|
+
* Enables or disables body parser middleware.
|
17
|
+
*/
|
18
|
+
bodyParser?: boolean;
|
19
|
+
/**
|
20
|
+
* Enables or disables security headers middleware.
|
21
|
+
*/
|
22
|
+
helmet?: boolean;
|
23
|
+
/**
|
24
|
+
* Enables or disables HTTP request logging middleware.
|
25
|
+
*/
|
26
|
+
morgan?: boolean;
|
27
|
+
/**
|
28
|
+
* Enables or disables the alive endpoint middleware.
|
29
|
+
*/
|
30
|
+
nitur?: boolean;
|
47
31
|
/**
|
48
|
-
*
|
32
|
+
* Enables or disables the stats endpoint middleware.
|
49
33
|
*/
|
50
|
-
|
34
|
+
stats?: boolean;
|
35
|
+
/**
|
36
|
+
* Enables or disables request tracing middleware.
|
37
|
+
*/
|
38
|
+
tracing?: boolean;
|
39
|
+
/**
|
40
|
+
* Enables or disables eager loading of user permissions for tracing middleware.
|
41
|
+
*/
|
42
|
+
eagerLoadUserPermissions?: boolean;
|
43
|
+
/**
|
44
|
+
* Options to customize the alive endpoint middleware.
|
45
|
+
*/
|
46
|
+
aliveEndpointOptions?: Record<string, unknown>;
|
47
|
+
// Add other options from config/default-options.json as needed
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Type for the callback function used in the listen method.
|
52
|
+
*/
|
53
|
+
export type ListenCallback = () => void;
|
51
54
|
|
52
|
-
|
53
|
-
|
55
|
+
type Listen = express.Application['listen'];
|
56
|
+
type Server = Listen extends (port: any, cb: any) => infer R ? R : never;
|
54
57
|
|
58
|
+
/**
|
59
|
+
* Extends the express.Application interface to include nativeListen and the overridden listen method.
|
60
|
+
*/
|
61
|
+
export interface SuperExpressApp extends express.Application {
|
55
62
|
/**
|
56
|
-
*
|
63
|
+
* Original express listen method.
|
57
64
|
*/
|
58
|
-
|
59
|
-
/**
|
60
|
-
* Original express listen method.
|
61
|
-
*/
|
62
|
-
nativeListen: Listen;
|
63
|
-
|
64
|
-
/**
|
65
|
-
* Overridden listen method to add custom behavior.
|
66
|
-
* @param port - The port number to listen on.
|
67
|
-
* @param cb - Optional callback function to execute after the server starts listening.
|
68
|
-
*/
|
69
|
-
listen(port: number, cb?: ListenCallback): Server;
|
70
|
-
}
|
65
|
+
nativeListen: Listen;
|
71
66
|
|
72
67
|
/**
|
73
|
-
*
|
74
|
-
* @param
|
75
|
-
* @
|
68
|
+
* Overridden listen method to add custom behavior.
|
69
|
+
* @param port - The port number to listen on.
|
70
|
+
* @param cb - Optional callback function to execute after the server starts listening.
|
76
71
|
*/
|
77
|
-
|
78
|
-
options?: Partial<DefaultOptions & express.ApplicationOptions>
|
79
|
-
): SuperExpressApp;
|
72
|
+
listen(port: number, cb?: ListenCallback): Server;
|
80
73
|
}
|
81
74
|
|
82
|
-
|
75
|
+
/**
|
76
|
+
* Creates a new SuperExpress application with the given options.
|
77
|
+
* @param options - Optional settings to customize the application.
|
78
|
+
* @param expressOptions - Optional settings to customize express.
|
79
|
+
* @returns A SuperExpress application instance.
|
80
|
+
*/
|
81
|
+
declare function createSuperExpressApp(
|
82
|
+
options?: Partial<DefaultOptions>,
|
83
|
+
expressOptions?: express.ApplicationOptions,
|
84
|
+
): SuperExpressApp;
|
85
|
+
|
86
|
+
export = createSuperExpressApp;
|
package/src/index.js
CHANGED
@@ -7,16 +7,19 @@ const fs = require('fs');
|
|
7
7
|
const path = require('path');
|
8
8
|
|
9
9
|
const defaultOptions = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/default-options.json'), 'utf8'));
|
10
|
+
const packageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../package.json'), 'utf8'));
|
10
11
|
|
11
|
-
module.exports = function (options = {}) {
|
12
|
-
const app = express(
|
12
|
+
module.exports = function (options = {}, expressOptions = {}) {
|
13
|
+
const app = express(expressOptions);
|
13
14
|
const mergedOptions = Object.assign({}, defaultOptions, options);
|
14
15
|
/** Formatting */
|
15
16
|
if (mergedOptions.morgan) {
|
17
|
+
console.log('[SuperExpress] formatting is enabled ✅');
|
16
18
|
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));
|
17
19
|
}
|
18
20
|
/** Security */
|
19
21
|
if (mergedOptions.helmet) {
|
22
|
+
console.log('[SuperExpress] security is enabled ✅');
|
20
23
|
// this is what helmet does by default. https://helmetjs.github.io/faq/you-might-not-need-helmet/
|
21
24
|
const HEADERS = {
|
22
25
|
"Content-Security-Policy":
|
@@ -41,26 +44,27 @@ module.exports = function (options = {}) {
|
|
41
44
|
}
|
42
45
|
/** Body Parser */
|
43
46
|
if (mergedOptions.bodyParser) {
|
47
|
+
console.log('[SuperExpress] body-parser is enabled ✅');
|
44
48
|
app.use(express.json({ limit: '1000mb' }))
|
45
49
|
} else {
|
46
|
-
console.log('[SuperExpress]
|
50
|
+
console.log('[SuperExpress] body-parser is disabled ❌');
|
47
51
|
}
|
48
52
|
/** Alive Endpoint */
|
49
53
|
if (mergedOptions.nitur) {
|
50
54
|
app.get('/alive', aliveEndpoint(mergedOptions.aliveEndpointOptions || {}));
|
55
|
+
console.log('[SuperExpress] added /alive endpoint ✅');
|
51
56
|
}
|
52
57
|
/** Stats Endpoint */
|
53
|
-
if (mergedOptions.stats
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
// });
|
58
|
+
if (mergedOptions.stats) {
|
59
|
+
const serverRunningSince = new Date();
|
60
|
+
app.get('/stats', (req, res) => {
|
61
|
+
res.json({
|
62
|
+
name: mergedOptions.name || 'default-name',
|
63
|
+
version: mergedOptions.version || 'default-version',
|
64
|
+
serverRunningSince,
|
65
|
+
});
|
66
|
+
});
|
67
|
+
console.log('[SuperExpress] added /stats endpoint ✅');
|
64
68
|
}
|
65
69
|
/** Tracing */
|
66
70
|
if (mergedOptions.tracing) {
|
@@ -73,13 +77,16 @@ module.exports = function (options = {}) {
|
|
73
77
|
app.use(zehut.middleware({
|
74
78
|
eagerLoadUserPermissions: mergedOptions.eagerLoadUserPermissions,
|
75
79
|
}));
|
80
|
+
console.log('[SuperExpress] tracing is enabled ✅');
|
76
81
|
}
|
77
82
|
|
78
83
|
app.nativeListen = app.listen
|
79
84
|
app.listen = function (port, cb) {
|
80
85
|
const isProd = process.env.NODE_ENV === 'production';
|
86
|
+
const version = packageJson?.version || 'unknown';
|
87
|
+
console.log(`[SuperExpress] is started with version ${version}`);
|
81
88
|
console.log(`[SuperExpress] will listen on port ${port}`);
|
82
|
-
console.log(`[SuperExpress]
|
89
|
+
console.log(`[SuperExpress] production mode: ${isProd}`);
|
83
90
|
return app.nativeListen(port, cb)
|
84
91
|
}
|
85
92
|
|
package/test/index.test.js
CHANGED
@@ -1,17 +1,7 @@
|
|
1
|
-
// index.test.js
|
2
1
|
const assert = require('assert').strict;
|
3
2
|
const { test } = require('node:test');
|
4
3
|
const supertest = require('supertest');
|
5
4
|
const createSuperExpressApp = require('../src/index.js');
|
6
|
-
const fs = require('fs');
|
7
|
-
const path = require('path');
|
8
|
-
|
9
|
-
// Mock the package.json file content
|
10
|
-
const mockPackageJson = {
|
11
|
-
name: 'test-package',
|
12
|
-
version: '1.0.0',
|
13
|
-
commit: 'abcdef123456'
|
14
|
-
};
|
15
5
|
|
16
6
|
// Mock the default-options.json file content
|
17
7
|
const defaultOptions = {
|
@@ -20,15 +10,10 @@ const defaultOptions = {
|
|
20
10
|
morgan: true,
|
21
11
|
nitur: true,
|
22
12
|
stats: true,
|
23
|
-
packageJsonPath: '../test/mock-package.json',
|
24
13
|
tracing: true,
|
25
14
|
eagerLoadUserPermissions: true,
|
26
|
-
aliveEndpointOptions: { customOption: 'value' }
|
27
15
|
};
|
28
16
|
|
29
|
-
// Write the mock package.json file to disk
|
30
|
-
fs.writeFileSync(path.resolve(__dirname, './mock-package.json'), JSON.stringify(mockPackageJson));
|
31
|
-
|
32
17
|
// Test for the existence of default middleware
|
33
18
|
test('should create an app with default middleware', async () => {
|
34
19
|
const app = createSuperExpressApp();
|
@@ -43,7 +28,7 @@ test('should create an app with default middleware', async () => {
|
|
43
28
|
const originalConsoleLog = console.log;
|
44
29
|
let logOutput = [];
|
45
30
|
console.log = (...args) => logOutput.push(...args);
|
46
|
-
const server = app.listen(3000, () => {});
|
31
|
+
const server = app.listen(3000, () => { });
|
47
32
|
assert(logOutput.includes('[SuperExpress] will listen on port 3000'));
|
48
33
|
|
49
34
|
console.log = originalConsoleLog;
|
@@ -53,6 +38,7 @@ test('should create an app with default middleware', async () => {
|
|
53
38
|
// Test for custom options
|
54
39
|
test('should create an app with custom options', async () => {
|
55
40
|
const app = createSuperExpressApp(defaultOptions);
|
41
|
+
const server = app.listen(3000, () => { });
|
56
42
|
|
57
43
|
// Test for the alive endpoint
|
58
44
|
await supertest(app)
|
@@ -60,14 +46,15 @@ test('should create an app with custom options', async () => {
|
|
60
46
|
.expect(200);
|
61
47
|
|
62
48
|
// Test for the stats endpoint
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
49
|
+
await supertest(app)
|
50
|
+
.get('/stats')
|
51
|
+
.expect(200)
|
52
|
+
.expect((res) => {
|
53
|
+
assert(res.body.name);
|
54
|
+
assert(res.body.version);
|
55
|
+
assert(res.body.serverRunningSince);
|
56
|
+
});
|
57
|
+
server.close();
|
71
58
|
});
|
72
59
|
|
73
60
|
// Test for disabled body parser
|
@@ -77,13 +64,17 @@ test('should disable body parser when option is set to false', async () => {
|
|
77
64
|
console.log = (...args) => logOutput.push(...args);
|
78
65
|
|
79
66
|
const app = createSuperExpressApp({ bodyParser: false });
|
67
|
+
const server = app.listen(3000, () => { });
|
80
68
|
|
81
|
-
|
69
|
+
try {
|
70
|
+
assert(logOutput.includes('[SuperExpress] body-parser is disabled ❌'));
|
71
|
+
await supertest(app)
|
82
72
|
.post('/')
|
83
73
|
.send({ key: 'value' })
|
84
74
|
.expect(404);
|
75
|
+
} finally {
|
76
|
+
console.log = originalConsoleLog;
|
77
|
+
server.close();
|
78
|
+
}
|
85
79
|
|
86
|
-
assert(logOutput.includes('[SuperExpress] Body parser is disabled'));
|
87
|
-
|
88
|
-
console.log = originalConsoleLog;
|
89
80
|
});
|
package/test/mock-package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"test-package","version":"1.0.0"
|
1
|
+
{"name":"test-package","version":"1.0.0"}
|