@autofleet/super-express 4.1.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 +1 -1
- package/src/index.d.ts +11 -5
- package/src/index.js +22 -15
- package/test/index.test.js +19 -28
- package/test/mock-package.json +1 -1
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
@@ -4,6 +4,14 @@ import * as express from 'express';
|
|
4
4
|
* Options to customize the behavior of the SuperExpress application.
|
5
5
|
*/
|
6
6
|
export interface DefaultOptions {
|
7
|
+
/**
|
8
|
+
* The name of the application.
|
9
|
+
*/
|
10
|
+
name?: string;
|
11
|
+
/**
|
12
|
+
* The version of the application.
|
13
|
+
*/
|
14
|
+
version?: string;
|
7
15
|
/**
|
8
16
|
* Enables or disables body parser middleware.
|
9
17
|
*/
|
@@ -24,10 +32,6 @@ export interface DefaultOptions {
|
|
24
32
|
* Enables or disables the stats endpoint middleware.
|
25
33
|
*/
|
26
34
|
stats?: boolean;
|
27
|
-
/**
|
28
|
-
* Path to the package.json file for the stats endpoint.
|
29
|
-
*/
|
30
|
-
packageJsonPath?: string;
|
31
35
|
/**
|
32
36
|
* Enables or disables request tracing middleware.
|
33
37
|
*/
|
@@ -71,10 +75,12 @@ export interface SuperExpressApp extends express.Application {
|
|
71
75
|
/**
|
72
76
|
* Creates a new SuperExpress application with the given options.
|
73
77
|
* @param options - Optional settings to customize the application.
|
78
|
+
* @param expressOptions - Optional settings to customize express.
|
74
79
|
* @returns A SuperExpress application instance.
|
75
80
|
*/
|
76
81
|
declare function createSuperExpressApp(
|
77
|
-
options?: Partial<DefaultOptions
|
82
|
+
options?: Partial<DefaultOptions>,
|
83
|
+
expressOptions?: express.ApplicationOptions,
|
78
84
|
): SuperExpressApp;
|
79
85
|
|
80
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"}
|