express-sweet 2.0.8 → 3.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +244 -173
  2. package/README.md +1031 -2112
  3. package/configuration_sample/cjs/authentication.js +50 -15
  4. package/configuration_sample/cjs/config.js +38 -21
  5. package/configuration_sample/cjs/logging.js +41 -0
  6. package/configuration_sample/cjs/view.js +19 -1
  7. package/configuration_sample/esm/authentication.js +48 -15
  8. package/configuration_sample/esm/config.js +39 -22
  9. package/configuration_sample/esm/logging.js +41 -0
  10. package/configuration_sample/esm/view.js +20 -2
  11. package/dist/build.cjs +9 -9
  12. package/dist/build.mjs +9 -9
  13. package/package.json +11 -6
  14. package/types/database/DatabaseManager.d.ts +95 -0
  15. package/types/database/Model.d.ts +183 -20
  16. package/types/database/index.d.ts +47 -3
  17. package/types/database/loadModels.d.ts +19 -4
  18. package/types/handlebars_helpers/array.d.ts +3 -1
  19. package/types/handlebars_helpers/comparison.d.ts +55 -19
  20. package/types/handlebars_helpers/date.d.ts +3 -1
  21. package/types/handlebars_helpers/html.d.ts +6 -2
  22. package/types/handlebars_helpers/index.d.ts +73 -9
  23. package/types/handlebars_helpers/layouts.d.ts +5 -1
  24. package/types/handlebars_helpers/math.d.ts +21 -7
  25. package/types/handlebars_helpers/number.d.ts +3 -1
  26. package/types/handlebars_helpers/object.d.ts +6 -2
  27. package/types/handlebars_helpers/string.d.ts +9 -3
  28. package/types/index.d.ts +92 -5
  29. package/types/interfaces/AuthenticationConfig.d.ts +38 -1
  30. package/types/interfaces/BasicConfig.d.ts +26 -1
  31. package/types/interfaces/DatabaseConfig.d.ts +45 -1
  32. package/types/interfaces/LoggingConfig.d.ts +49 -0
  33. package/types/interfaces/ViewConfig.d.ts +21 -2
  34. package/types/interfaces/index.d.ts +25 -11
  35. package/types/middlewares/Authentication.d.ts +43 -6
  36. package/types/middlewares/CORS.d.ts +23 -5
  37. package/types/middlewares/Environment.d.ts +30 -4
  38. package/types/middlewares/ErrorHandler.d.ts +27 -5
  39. package/types/middlewares/Global.d.ts +20 -6
  40. package/types/middlewares/Http.d.ts +22 -5
  41. package/types/middlewares/Local.d.ts +35 -8
  42. package/types/middlewares/View.d.ts +39 -8
  43. package/types/middlewares/index.d.ts +47 -7
  44. package/types/mount.d.ts +16 -3
  45. package/types/routing/Router.d.ts +49 -7
  46. package/types/routing/index.d.ts +37 -1
  47. package/types/services/Authentication.d.ts +79 -16
  48. package/types/services/index.d.ts +33 -1
  49. package/types/types/global.d.ts +29 -8
  50. package/types/utils/Types.d.ts +5 -3
  51. package/types/utils/index.d.ts +71 -9
  52. package/types/utils/isArray.d.ts +12 -3
  53. package/types/utils/isAsyncFunction.d.ts +12 -5
  54. package/types/utils/isFunction.d.ts +13 -6
  55. package/types/utils/isObject.d.ts +14 -3
  56. package/types/utils/isString.d.ts +15 -4
  57. package/types/utils/loadAuthenticationConfig.d.ts +13 -3
  58. package/types/utils/loadBasicConfig.d.ts +13 -3
  59. package/types/utils/loadDatabaseConfig.d.ts +34 -3
  60. package/types/utils/loadLoggingConfig.d.ts +16 -0
  61. package/types/utils/loadViewConfig.d.ts +28 -3
  62. package/types/database/Database.d.ts +0 -11
  63. package/types/interfaces/BoundingBox.d.ts +0 -25
  64. package/types/interfaces/FaceDetails.d.ts +0 -30
  65. package/types/interfaces/FaceDetailsEmotions.d.ts +0 -40
  66. package/types/interfaces/FaceMatch.d.ts +0 -31
  67. package/types/interfaces/IndexFaceDetails.d.ts +0 -29
  68. package/types/interfaces/RekognitionOptions.d.ts +0 -26
  69. package/types/interfaces/SesOptions.d.ts +0 -27
@@ -1,4 +1,4 @@
1
- const UserModel = require('../models/UserModel');
1
+ // const UserModel = require('../models/UserModel');
2
2
 
3
3
  /**
4
4
  * User authentication configuration interface.
@@ -23,6 +23,20 @@ module.exports = {
23
23
  */
24
24
  cookie_name: 'connect.sid',
25
25
 
26
+ /**
27
+ * Specifies the boolean value for the Secure Set-Cookie attribute.
28
+ * The default is true, which sets the Secure attribute on the cookie.
29
+ * @type {boolean|undefined}
30
+ */
31
+ cookie_secure: false,
32
+
33
+ /**
34
+ * Specifies the boolean value for the HttpOnly Set-Cookie attribute.
35
+ * Defaults to true, which sets the HttpOnly attribute on the cookie.
36
+ * @type {boolean|undefined}
37
+ */
38
+ cookie_httpOnly: true,
39
+
26
40
  /**
27
41
  * If the session is stored in "redis", this field is required and should be set to the hostname of the Redis server.
28
42
  * For example, to connect to redis on localhost on port 6379, set "redis://localhost:6379".
@@ -52,7 +66,18 @@ module.exports = {
52
66
 
53
67
  /**
54
68
  * URL to redirect after log off, defaults to `/login`.
55
- * @type {string}
69
+ * @type {string|((req: express.Request, res: express.Response) => string)}
70
+ * @example
71
+ * ```js
72
+ * // Set the URL to redirect to in case of login failure as a string.
73
+ * failure_redirect: '/login',
74
+ *
75
+ * // Dynamically set the url to redirect to on login failure.
76
+ * failure_redirect: (req, res) => {
77
+ * // If the role stored in the cookie is admin, redirect to the admin login screen.
78
+ * return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
79
+ * },
80
+ * ```
56
81
  */
57
82
  failure_redirect: '/login',
58
83
 
@@ -66,13 +91,14 @@ module.exports = {
66
91
  * @type {(username: string, password: string, req: express.Request) => Promise<{[key: string]: any}|null>}
67
92
  */
68
93
  authenticate_user: async (username, password, req) => {
69
- return UserModel.findOne({
70
- where: {
71
- email: username,
72
- password
73
- },
74
- raw: true
75
- });
94
+ // Sample hardcoded return for demonstration purposes.
95
+ // In actual implementation, retrieve user information from database.
96
+ // For example, using UserModel: UserModel.findOne({ where: { email: username, password }, raw: true });
97
+ return {
98
+ id: 1,
99
+ email: 'user@example.com',
100
+ name: 'Sample User'
101
+ };
76
102
  },
77
103
 
78
104
  /**
@@ -83,21 +109,30 @@ module.exports = {
83
109
  * @type {(id: number|string) => Promise<{[key: string]: any}>}
84
110
  */
85
111
  subscribe_user: async (id) => {
86
- return UserModel.findOne({
87
- where: {id},
88
- raw: true
89
- });
112
+ // Sample hardcoded return for demonstration purposes.
113
+ // In actual implementation, retrieve user information from database.
114
+ // For example, using UserModel: UserModel.findOne({ where: { id }, raw: true });
115
+ return {
116
+ id: 1,
117
+ email: 'user@example.com',
118
+ name: 'Sample User',
119
+ role: 'user'
120
+ };
90
121
  },
91
122
 
92
123
  /**
93
124
  * URL without authentication. If the URL described in the access URL partially matches, authentication will not be performed, defaults to none.
94
125
  * @type {(string|RegExp)[]}
95
126
  */
96
- allow_unauthenticated: ['/api'],
127
+ allow_unauthenticated: [
128
+ // Example: Allow login endpoint to bypass authentication
129
+ // In actual implementation, specify the endpoints you want to exclude from authentication
130
+ '/api/login'
131
+ ],
97
132
 
98
133
  /**
99
134
  * Authenticated user session expiration, defaults to 24 hours (24 * 3600000).
100
135
  * @type {number}
101
136
  */
102
- expiration: 24 * 3600000
137
+ expiration: 24 * 3600000,
103
138
  }
@@ -20,7 +20,7 @@ module.exports = {
20
20
  * Maximum body size you can request, defaults to `100kb`.
21
21
  * @type {string|number}
22
22
  */
23
- max_body_size: '100kb',
23
+ max_body_size: '100mb',
24
24
 
25
25
  /**
26
26
  * Absolute path to the router directory, defaults to `<application root directory>/routes`.
@@ -40,35 +40,52 @@ module.exports = {
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
41
  * @type {(baseUrl: string): string}
42
42
  * @example
43
+ * ```js
43
44
  * rewrite_base_url: baseUrl => {
44
45
  * return `${baseUrl}/admin`;
45
46
  * }
47
+ * ```
46
48
  */
47
49
  rewrite_base_url: baseUrl => {
48
50
  return baseUrl;
49
51
  },
50
52
 
51
53
  /**
52
- * This is a hook for error handling.
53
- * For example, you can use it when you want to send an external notification of the error received by this hook.
54
- * @type {(error: any): void|Promise<void>}
54
+ * How to determine if it is an ajax request.
55
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
+ * @type {(req: express.Request) => boolean}
58
+ * @example
59
+ * ```js
60
+ * is_ajax: req => {
61
+ * // If the request URL begins with /api, it is assumed to be Ajax.
62
+ * return /^\/api/.test(req.path);
63
+ * // return !!req.xhr;
64
+ * }
65
+ * ```
66
+ */
67
+ is_ajax: req => !!req.xhr,
68
+
69
+ /**
70
+ * Hooks the default behavior on request errors.
71
+ * If unset, simply returns an error HTTP status. (<code>res.status(error.status||500).end();</code>)
72
+ * @type {(error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
55
73
  * @example
56
- * error_handler: async error => {
57
- * // Notify system administrator of error.
58
- * return new Promise((resolve, reject) => {
59
- * const sendmail = require('sendmail')();
60
- * sendmail({
61
- * from: 'no-reply@example.com',
62
- * to: 'administrator@example.com',
63
- * subject: 'Error occurred',
64
- * text: error.message
65
- * }, error => {
66
- * error ? reject(error) : resolve();
67
- * });
68
- * });
69
- * }
74
+ * ```js
75
+ * hook_handle_error: (error, req, res, next) => {
76
+ * if (error.status === 404)
77
+ * // If the URL cannot be found, a 404 error screen (views/errors/404.hbs) is displayed.
78
+ * res.render('errors/404');
79
+ * else
80
+ * // For other errors, unknown error screen (views/error/500.hbs) is displayed.
81
+ * res.render('error/500');
82
+ * },
83
+ * ```
70
84
  */
71
- error_handler: error => {
72
- console.error(`An error has occurred. Error message: ${error.message}`);
73
- }
85
+ hook_handle_error: (error, req, res, next) => {
86
+ if (error.status === 404)
87
+ res.status(404).render('errors/404', {isErrorPage: true});
88
+ else
89
+ res.status(500).render('errors/500', {isErrorPage: true});
90
+ },
74
91
  }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Logging configuration interface.
3
+ */
4
+ module.exports = {
5
+ /**
6
+ * Morgan logging format. Common formats: 'combined', 'common', 'dev', 'short', 'tiny'
7
+ * You can also define custom format string, defaults to 'combined'.
8
+ * @type {string}
9
+ * @example
10
+ * ```js
11
+ * // Use predefined format
12
+ * format: 'combined',
13
+ *
14
+ * // Use custom format
15
+ * format: ':method :url :status :res[content-length] - :response-time ms',
16
+ * ```
17
+ */
18
+ format: 'combined',
19
+
20
+ /**
21
+ * Function to determine if logging should be skipped for a request.
22
+ * Return true to skip logging, false to log the request, defaults to none (undefined).
23
+ * @type {(req: express.Request, res: express.Response) => boolean}
24
+ * @example
25
+ * ```js
26
+ * // Skip logging for successful requests
27
+ * skip: (req, res) => res.statusCode < 400,
28
+ *
29
+ * // Skip logging for specific routes
30
+ * skip: (req, res) => req.path === '/health',
31
+ *
32
+ * // Skip logging in test environment
33
+ * skip: (req, res) => process.env.NODE_ENV === 'test',
34
+ * ```
35
+ */
36
+ skip: (req, res) => {
37
+ // Example: Skip logging for health check endpoints
38
+ // In actual implementation, customize based on your requirements
39
+ return req.path === '/health';
40
+ }
41
+ }
@@ -32,5 +32,23 @@ module.exports = {
32
32
  * Extension for templates & partials, defaults to `.hbs`,
33
33
  * @type {string}
34
34
  */
35
- extension: '.hbs'
35
+ extension: '.hbs',
36
+
37
+ /**
38
+ * Hook function just before the view is rendered.
39
+ * For example, you can set your own local variables that can be used within the view.
40
+ * @type {(res: express.Response) => Promise<void>|void}
41
+ * @example
42
+ * ```js
43
+ * // The message set here can be referenced in the view as {{message}}.
44
+ * beforeRender: (req, res) => {
45
+ * res.locals.extra = 'Extra';
46
+ * }
47
+ * ```
48
+ */
49
+ beforeRender: (req, res) => {
50
+ // Example: Set local variables that can be used in templates
51
+ // In actual implementation, set the variables you need in your views
52
+ res.locals.message = 'Hello World';
53
+ }
36
54
  }
@@ -1,4 +1,4 @@
1
- import UserModel from '../models/UserModel';
1
+ // import UserModel from '../models/UserModel.js';
2
2
 
3
3
  /**
4
4
  * User authentication configuration interface.
@@ -23,6 +23,20 @@ export default {
23
23
  */
24
24
  cookie_name: 'connect.sid',
25
25
 
26
+ /**
27
+ * Specifies the boolean value for the Secure Set-Cookie attribute.
28
+ * The default is true, which sets the Secure attribute on the cookie.
29
+ * @type {boolean|undefined}
30
+ */
31
+ cookie_secure: false,
32
+
33
+ /**
34
+ * Specifies the boolean value for the HttpOnly Set-Cookie attribute.
35
+ * Defaults to true, which sets the HttpOnly attribute on the cookie.
36
+ * @type {boolean|undefined}
37
+ */
38
+ cookie_httpOnly: true,
39
+
26
40
  /**
27
41
  * If the session is stored in "redis", this field is required and should be set to the hostname of the Redis server.
28
42
  * For example, to connect to redis on localhost on port 6379, set "redis://localhost:6379".
@@ -52,7 +66,16 @@ export default {
52
66
 
53
67
  /**
54
68
  * URL to redirect after log off, defaults to `/login`.
55
- * @type {string}
69
+ * @type {string|((req: express.Request, res: express.Response) => string)}
70
+ * @example
71
+ * // Set the URL to redirect to in case of login failure as a string.
72
+ * failure_redirect: '/login',
73
+ *
74
+ * // Dynamically set the url to redirect to on login failure.
75
+ * failure_redirect: (req, res) => {
76
+ * // If the role stored in the cookie is admin, redirect to the admin login screen.
77
+ * return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
78
+ * },
56
79
  */
57
80
  failure_redirect: '/login',
58
81
 
@@ -66,13 +89,14 @@ export default {
66
89
  * @type {(username: string, password: string, req: express.Request) => Promise<{[key: string]: any}|null>}
67
90
  */
68
91
  authenticate_user: async (username, password, req) => {
69
- return UserModel.findOne({
70
- where: {
71
- email: username,
72
- password
73
- },
74
- raw: true
75
- });
92
+ // Sample hardcoded return for demonstration purposes.
93
+ // In actual implementation, retrieve user information from database.
94
+ // For example, using UserModel: UserModel.findOne({ where: { email: username, password }, raw: true });
95
+ return {
96
+ id: 1,
97
+ email: 'user@example.com',
98
+ name: 'Sample User'
99
+ };
76
100
  },
77
101
 
78
102
  /**
@@ -83,21 +107,30 @@ export default {
83
107
  * @type {(id: number|string) => Promise<{[key: string]: any}>}
84
108
  */
85
109
  subscribe_user: async (id) => {
86
- return UserModel.findOne({
87
- where: {id},
88
- raw: true
89
- });
110
+ // Sample hardcoded return for demonstration purposes.
111
+ // In actual implementation, retrieve user information from database.
112
+ // For example, using UserModel: UserModel.findOne({ where: { id }, raw: true });
113
+ return {
114
+ id: 1,
115
+ email: 'user@example.com',
116
+ name: 'Sample User',
117
+ role: 'user'
118
+ };
90
119
  },
91
120
 
92
121
  /**
93
122
  * URL without authentication. If the URL described in the access URL partially matches, authentication will not be performed, defaults to none.
94
123
  * @type {(string|RegExp)[]}
95
124
  */
96
- allow_unauthenticated: ['/api'],
125
+ allow_unauthenticated: [
126
+ // Example: Allow login endpoint to bypass authentication
127
+ // In actual implementation, specify the endpoints you want to exclude from authentication
128
+ '/api/login'
129
+ ],
97
130
 
98
131
  /**
99
132
  * Authenticated user session expiration, defaults to 24 hours (24 * 3600000).
100
133
  * @type {number}
101
134
  */
102
- expiration: 24 * 3600000
135
+ expiration: 24 * 3600000,
103
136
  }
@@ -1,4 +1,4 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
 
3
3
  /**
4
4
  * EXPRESS SWEET basic configuration interface.
@@ -20,7 +20,7 @@ export default {
20
20
  * Maximum body size you can request, defaults to `100kb`.
21
21
  * @type {string|number}
22
22
  */
23
- max_body_size: '100kb',
23
+ max_body_size: '100mb',
24
24
 
25
25
  /**
26
26
  * Absolute path to the router directory, defaults to `<application root directory>/routes`.
@@ -40,35 +40,52 @@ export default {
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
41
  * @type {(baseUrl: string): string}
42
42
  * @example
43
+ * ```js
43
44
  * rewrite_base_url: baseUrl => {
44
45
  * return `${baseUrl}/admin`;
45
46
  * }
47
+ * ```
46
48
  */
47
49
  rewrite_base_url: baseUrl => {
48
50
  return baseUrl;
49
51
  },
50
52
 
51
53
  /**
52
- * This is a hook for error handling.
53
- * For example, you can use it when you want to send an external notification of the error received by this hook.
54
- * @type {(error: any): void|Promise<void>}
54
+ * How to determine if it is an ajax request.
55
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
+ * @type {(req: express.Request) => boolean}
58
+ * @example
59
+ * ```js
60
+ * is_ajax: req => {
61
+ * // If the request URL begins with /api, it is assumed to be Ajax.
62
+ * return /^\/api/.test(req.path);
63
+ * // return !!req.xhr;
64
+ * }
65
+ * ```
66
+ */
67
+ is_ajax: req => !!req.xhr,
68
+
69
+ /**
70
+ * Hooks the default behavior on request errors.
71
+ * If unset, simply returns an error HTTP status. (<code>res.status(error.status||500).end();</code>)
72
+ * @type {(error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
55
73
  * @example
56
- * error_handler: async error => {
57
- * // Notify system administrator of error.
58
- * return new Promise((resolve, reject) => {
59
- * const sendmail = require('sendmail')();
60
- * sendmail({
61
- * from: 'no-reply@example.com',
62
- * to: 'administrator@example.com',
63
- * subject: 'Error occurred',
64
- * text: error.message
65
- * }, error => {
66
- * error ? reject(error) : resolve();
67
- * });
68
- * });
69
- * }
74
+ * ```js
75
+ * hook_handle_error: (error, req, res, next) => {
76
+ * if (error.status === 404)
77
+ * // If the URL cannot be found, a 404 error screen (views/errors/404.hbs) is displayed.
78
+ * res.render('errors/404');
79
+ * else
80
+ * // For other errors, unknown error screen (views/error/500.hbs) is displayed.
81
+ * res.render('error/500');
82
+ * },
83
+ * ```
70
84
  */
71
- error_handler: error => {
72
- console.error(`An error has occurred. Error message: ${error.message}`);
73
- }
85
+ hook_handle_error: (error, req, res, next) => {
86
+ if (error.status === 404)
87
+ res.status(404).render('errors/404', {isErrorPage: true});
88
+ else
89
+ res.status(500).render('errors/500', {isErrorPage: true});
90
+ },
74
91
  }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Logging configuration interface.
3
+ */
4
+ export default {
5
+ /**
6
+ * Morgan logging format. Common formats: 'combined', 'common', 'dev', 'short', 'tiny'
7
+ * You can also define custom format string, defaults to 'combined'.
8
+ * @type {string}
9
+ * @example
10
+ * ```js
11
+ * // Use predefined format
12
+ * format: 'combined',
13
+ *
14
+ * // Use custom format
15
+ * format: ':method :url :status :res[content-length] - :response-time ms',
16
+ * ```
17
+ */
18
+ format: 'combined',
19
+
20
+ /**
21
+ * Function to determine if logging should be skipped for a request.
22
+ * Return true to skip logging, false to log the request, defaults to none (undefined).
23
+ * @type {(req: express.Request, res: express.Response) => boolean}
24
+ * @example
25
+ * ```js
26
+ * // Skip logging for successful requests
27
+ * skip: (req, res) => res.statusCode < 400,
28
+ *
29
+ * // Skip logging for specific routes
30
+ * skip: (req, res) => req.path === '/health',
31
+ *
32
+ * // Skip logging in test environment
33
+ * skip: (req, res) => process.env.NODE_ENV === 'test',
34
+ * ```
35
+ */
36
+ skip: (req, res) => {
37
+ // Example: Skip logging for health check endpoints
38
+ // In actual implementation, customize based on your requirements
39
+ return req.path === '/health';
40
+ }
41
+ }
@@ -1,4 +1,4 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
 
3
3
  /**
4
4
  * View's configuration interface.
@@ -32,5 +32,23 @@ export default {
32
32
  * Extension for templates & partials, defaults to `.hbs`,
33
33
  * @type {string}
34
34
  */
35
- extension: '.hbs'
35
+ extension: '.hbs',
36
+
37
+ /**
38
+ * Hook function just before the view is rendered.
39
+ * For example, you can set your own local variables that can be used within the view.
40
+ * @type {(res: express.Response) => Promise<void>|void}
41
+ * @example
42
+ * ```js
43
+ * // The message set here can be referenced in the view as {{message}}.
44
+ * beforeRender: (req, res) => {
45
+ * res.locals.extra = 'Extra';
46
+ * }
47
+ * ```
48
+ */
49
+ beforeRender: (req, res) => {
50
+ // Example: Set local variables that can be used in templates
51
+ // In actual implementation, set the variables you need in your views
52
+ res.locals.message = 'Hello World';
53
+ }
36
54
  }