express-sweet 4.0.0 → 5.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.
- package/CHANGELOG.md +221 -397
- package/LICENSE +1 -1
- package/README.md +210 -1174
- package/dist/build.cjs +32 -73
- package/dist/build.mjs +32 -73
- package/package.json +45 -61
- package/screenshots/avatar.png +0 -0
- package/screenshots/edit-profile.png +0 -0
- package/screenshots/hero-banner.png +0 -0
- package/screenshots/home.png +0 -0
- package/screenshots/mount-flow.mmd +11 -0
- package/screenshots/mount-flow.png +0 -0
- package/screenshots/new-person.png +0 -0
- package/screenshots/people.png +0 -0
- package/screenshots/sign-in.png +0 -0
- package/types/database/DatabaseManager.d.ts +15 -10
- package/types/database/Model.d.ts +29 -43
- package/types/database/index.d.ts +0 -1
- package/types/database/loadModels.d.ts +7 -13
- package/types/helpers/array.d.ts +21 -0
- package/types/helpers/comparison.d.ts +217 -0
- package/types/helpers/date.d.ts +20 -0
- package/types/helpers/html.d.ts +35 -0
- package/types/{handlebars_helpers → helpers}/index.d.ts +0 -2
- package/types/helpers/layouts.d.ts +29 -0
- package/types/helpers/math.d.ts +85 -0
- package/types/helpers/number.d.ts +19 -0
- package/types/helpers/object.d.ts +28 -0
- package/types/helpers/string.d.ts +45 -0
- package/types/index.d.ts +6 -9
- package/types/middleware/auth.d.ts +186 -0
- package/types/middleware/cors.d.ts +19 -0
- package/types/middleware/env.d.ts +40 -0
- package/types/middleware/error.d.ts +26 -0
- package/types/middleware/globals.d.ts +13 -0
- package/types/{middlewares → middleware}/index.d.ts +12 -13
- package/types/middleware/locals.d.ts +33 -0
- package/types/middleware/parser.d.ts +30 -0
- package/types/middleware/router.d.ts +31 -0
- package/types/middleware/views.d.ts +37 -0
- package/types/mount.d.ts +2 -3
- package/types/{interfaces → types}/AppConfig.d.ts +29 -18
- package/types/{interfaces → types}/AuthenticationConfig.d.ts +10 -11
- package/types/{interfaces → types}/DatabaseConfig.d.ts +0 -1
- package/types/{interfaces → types}/LoggingConfig.d.ts +0 -1
- package/types/{interfaces → types}/UploadConfig.d.ts +0 -1
- package/types/{interfaces → types}/ViewConfig.d.ts +1 -2
- package/types/{interfaces → types}/index.d.ts +1 -2
- package/types/utils/index.d.ts +0 -1
- package/types/utils/loadAppConfig.d.ts +1 -1
- package/types/utils/loadAuthenticationConfig.d.ts +1 -1
- package/types/utils/loadLoggingConfig.d.ts +1 -1
- package/types/utils/loadUploadConfig.d.ts +1 -1
- package/types/utils/loadViewConfig.d.ts +1 -1
- package/configuration_sample/cjs/authentication.js +0 -138
- package/configuration_sample/cjs/config.js +0 -92
- package/configuration_sample/cjs/database.js +0 -121
- package/configuration_sample/cjs/logging.js +0 -41
- package/configuration_sample/cjs/upload.js +0 -91
- package/configuration_sample/cjs/view.js +0 -54
- package/configuration_sample/esm/authentication.js +0 -136
- package/configuration_sample/esm/config.js +0 -92
- package/configuration_sample/esm/database.js +0 -121
- package/configuration_sample/esm/logging.js +0 -41
- package/configuration_sample/esm/upload.js +0 -91
- package/configuration_sample/esm/view.js +0 -54
- package/screencaps/belongs-to-many.png +0 -0
- package/screencaps/belongs-to.png +0 -0
- package/screencaps/demonstration.webp +0 -0
- package/screencaps/has-many.png +0 -0
- package/screencaps/has-one.png +0 -0
- package/types/handlebars_helpers/array.d.ts +0 -22
- package/types/handlebars_helpers/comparison.d.ts +0 -222
- package/types/handlebars_helpers/date.d.ts +0 -19
- package/types/handlebars_helpers/html.d.ts +0 -31
- package/types/handlebars_helpers/layouts.d.ts +0 -25
- package/types/handlebars_helpers/math.d.ts +0 -74
- package/types/handlebars_helpers/number.d.ts +0 -18
- package/types/handlebars_helpers/object.d.ts +0 -21
- package/types/handlebars_helpers/string.d.ts +0 -45
- package/types/middlewares/corsPolicy.d.ts +0 -28
- package/types/middlewares/envLoader.d.ts +0 -34
- package/types/middlewares/errorHandler.d.ts +0 -32
- package/types/middlewares/globalVars.d.ts +0 -25
- package/types/middlewares/localVars.d.ts +0 -44
- package/types/middlewares/passportAuth.d.ts +0 -15
- package/types/middlewares/requestParser.d.ts +0 -27
- package/types/middlewares/routeMapper.d.ts +0 -54
- package/types/middlewares/viewEngine.d.ts +0 -49
- package/types/services/Authentication.d.ts +0 -95
- package/types/services/index.d.ts +0 -33
- package/types/types/global.d.ts +0 -38
- package/types/utils/Types.d.ts +0 -29
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Configures Passport.js authentication middleware with session management and route protection.
|
|
4
|
+
*
|
|
5
|
+
* Sets up username/password authentication using Passport's local strategy,
|
|
6
|
+
* session storage (memory or Redis), and automatic route protection.
|
|
7
|
+
* Unauthenticated users accessing protected routes are redirected to `failure_redirect`
|
|
8
|
+
* (or receive a 401 response for AJAX requests).
|
|
9
|
+
*
|
|
10
|
+
* @param {express.Express} app Express application instance.
|
|
11
|
+
* @returns {Promise<void>}
|
|
12
|
+
* @example
|
|
13
|
+
* ```js
|
|
14
|
+
* // config/authentication.js
|
|
15
|
+
* import UserModel from '../models/UserModel.js';
|
|
16
|
+
*
|
|
17
|
+
* export default {
|
|
18
|
+
* enabled: true,
|
|
19
|
+
* session_store: 'memory',
|
|
20
|
+
* username: 'email',
|
|
21
|
+
* password: 'password',
|
|
22
|
+
* success_redirect: '/',
|
|
23
|
+
* failure_redirect: '/login',
|
|
24
|
+
* authenticate_user: async (username, password, req) => {
|
|
25
|
+
* return UserModel.findOne({where: {email: username, password}, raw: true});
|
|
26
|
+
* },
|
|
27
|
+
* subscribe_user: async (id) => {
|
|
28
|
+
* return UserModel.findOne({where: {id}, raw: true});
|
|
29
|
+
* },
|
|
30
|
+
* allow_unauthenticated: ['/api/login'],
|
|
31
|
+
* expiration: 24 * 3600000,
|
|
32
|
+
* };
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export default function auth(app: express.Express): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* User authentication service using Passport.js.
|
|
38
|
+
*
|
|
39
|
+
* Provides static methods for user authentication, logout, and redirect handling.
|
|
40
|
+
* Works with Passport.js local strategy configured in authentication middleware.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```js
|
|
44
|
+
* // Basic authentication with JSON response
|
|
45
|
+
* import {Router} from 'express';
|
|
46
|
+
* import * as expx from 'express-sweet';
|
|
47
|
+
*
|
|
48
|
+
* const router = Router();
|
|
49
|
+
* router.post('/api/login', async (req, res, next) => {
|
|
50
|
+
* const isAuthenticated = await expx.Authentication.authenticate(req, res, next);
|
|
51
|
+
* res.json({success: isAuthenticated});
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* export default router;
|
|
55
|
+
* ```
|
|
56
|
+
* @example
|
|
57
|
+
* ```js
|
|
58
|
+
* // Authentication with redirect handling
|
|
59
|
+
* import {Router} from 'express';
|
|
60
|
+
* import * as expx from 'express-sweet';
|
|
61
|
+
*
|
|
62
|
+
* const router = Router();
|
|
63
|
+
* router.post('/login', async (req, res, next) => {
|
|
64
|
+
* const isAuthenticated = await expx.Authentication.authenticate(req, res, next);
|
|
65
|
+
* if (isAuthenticated)
|
|
66
|
+
* await expx.Authentication.successRedirect(res);
|
|
67
|
+
* else
|
|
68
|
+
* await expx.Authentication.failureRedirect(req, res);
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* export default router;
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare class Authentication {
|
|
75
|
+
/**
|
|
76
|
+
* Authenticate user using username and password from request body.
|
|
77
|
+
*
|
|
78
|
+
* Uses Passport.js local strategy configured via authentication middleware.
|
|
79
|
+
* Expects `req.body.username` and `req.body.password` to be present.
|
|
80
|
+
*
|
|
81
|
+
* @param {express.Request} req HTTP request object containing user credentials in body
|
|
82
|
+
* @param {express.Response} res HTTP response object
|
|
83
|
+
* @param {express.NextFunction} next Next middleware function
|
|
84
|
+
* @returns {Promise<boolean>} Returns true if authentication successful, false otherwise
|
|
85
|
+
* @example
|
|
86
|
+
* ```js
|
|
87
|
+
* import {Router} from 'express';
|
|
88
|
+
* import * as expx from 'express-sweet';
|
|
89
|
+
*
|
|
90
|
+
* const router = Router();
|
|
91
|
+
* router.post('/login', async (req, res, next) => {
|
|
92
|
+
* try {
|
|
93
|
+
* const isAuthenticated = await expx.Authentication.authenticate(req, res, next);
|
|
94
|
+
* if (isAuthenticated) {
|
|
95
|
+
* res.json({success: true, user: req.user});
|
|
96
|
+
* } else {
|
|
97
|
+
* res.status(401).json({success: false, message: 'Invalid credentials'});
|
|
98
|
+
* }
|
|
99
|
+
* } catch (error) {
|
|
100
|
+
* next(error);
|
|
101
|
+
* }
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* export default router;
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
static authenticate(req: express.Request, res: express.Response, next: express.NextFunction): Promise<boolean>;
|
|
108
|
+
/**
|
|
109
|
+
* Log out the current user.
|
|
110
|
+
*
|
|
111
|
+
* Removes req.user property and clears the login session.
|
|
112
|
+
* This method is synchronous and does not require await.
|
|
113
|
+
*
|
|
114
|
+
* @param {express.Request} req HTTP request object containing the user session
|
|
115
|
+
* @example
|
|
116
|
+
* ```js
|
|
117
|
+
* import {Router} from 'express';
|
|
118
|
+
* import * as expx from 'express-sweet';
|
|
119
|
+
*
|
|
120
|
+
* const router = Router();
|
|
121
|
+
* router.get('/logout', (req, res) => {
|
|
122
|
+
* expx.Authentication.logout(req);
|
|
123
|
+
* res.redirect('/');
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* export default router;
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
static logout(req: express.Request): void;
|
|
130
|
+
/**
|
|
131
|
+
* Redirect to success page after successful authentication.
|
|
132
|
+
*
|
|
133
|
+
* Uses the URL specified in `success_redirect` option of config/authentication.js.
|
|
134
|
+
* Should be called after successful authentication.
|
|
135
|
+
*
|
|
136
|
+
* @param {express.Response} res HTTP response object
|
|
137
|
+
* @returns {Promise<void>}
|
|
138
|
+
* @example
|
|
139
|
+
* ```js
|
|
140
|
+
* import {Router} from 'express';
|
|
141
|
+
* import * as expx from 'express-sweet';
|
|
142
|
+
*
|
|
143
|
+
* const router = Router();
|
|
144
|
+
* router.post('/login', async (req, res, next) => {
|
|
145
|
+
* const isAuthenticated = await expx.Authentication.authenticate(req, res, next);
|
|
146
|
+
* if (isAuthenticated) {
|
|
147
|
+
* await expx.Authentication.successRedirect(res);
|
|
148
|
+
* } else {
|
|
149
|
+
* await expx.Authentication.failureRedirect(req, res);
|
|
150
|
+
* }
|
|
151
|
+
* });
|
|
152
|
+
*
|
|
153
|
+
* export default router;
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
static successRedirect(res: express.Response): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Redirect to failure page after authentication failure.
|
|
159
|
+
*
|
|
160
|
+
* Uses the URL specified in `failure_redirect` option of config/authentication.js.
|
|
161
|
+
* Supports both static URLs and dynamic URL functions.
|
|
162
|
+
* Should be called after failed authentication.
|
|
163
|
+
*
|
|
164
|
+
* @param {express.Request} req HTTP request object
|
|
165
|
+
* @param {express.Response} res HTTP response object
|
|
166
|
+
* @returns {Promise<void>}
|
|
167
|
+
* @example
|
|
168
|
+
* ```js
|
|
169
|
+
* import {Router} from 'express';
|
|
170
|
+
* import * as expx from 'express-sweet';
|
|
171
|
+
*
|
|
172
|
+
* const router = Router();
|
|
173
|
+
* router.post('/login', async (req, res, next) => {
|
|
174
|
+
* const isAuthenticated = await expx.Authentication.authenticate(req, res, next);
|
|
175
|
+
* if (isAuthenticated) {
|
|
176
|
+
* await expx.Authentication.successRedirect(res);
|
|
177
|
+
* } else {
|
|
178
|
+
* await expx.Authentication.failureRedirect(req, res);
|
|
179
|
+
* }
|
|
180
|
+
* });
|
|
181
|
+
*
|
|
182
|
+
* export default router;
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
static failureRedirect(req: express.Request, res: express.Response): Promise<void>;
|
|
186
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Enables Cross-Origin Resource Sharing (CORS) on the Express application.
|
|
4
|
+
*
|
|
5
|
+
* When enabled via `cors_enabled: true` in `config/config.js`, this middleware sets
|
|
6
|
+
* the appropriate CORS headers on all responses and handles preflight `OPTIONS` requests.
|
|
7
|
+
* The `Access-Control-Allow-Origin` header is dynamically set to the request's origin.
|
|
8
|
+
*
|
|
9
|
+
* @param {express.Express} app Express application instance.
|
|
10
|
+
* @returns {Promise<void>}
|
|
11
|
+
* @example
|
|
12
|
+
* ```js
|
|
13
|
+
* // Enable CORS in config/config.js
|
|
14
|
+
* export default {
|
|
15
|
+
* cors_enabled: true
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export default function cors(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads environment variables from a `.env` file into `process.env`.
|
|
3
|
+
*
|
|
4
|
+
* Reads the file path specified by `env_path` in `config/config.js`, parses it with dotenv,
|
|
5
|
+
* and populates `process.env`. Then runs the `hook_after_env_load` hook from the same config
|
|
6
|
+
* file, giving the application a chance to post-process environment variables before database
|
|
7
|
+
* models and the rest of the middleware stack read them. The hook runs even when `env_path`
|
|
8
|
+
* is unset, so it can also be used to build `process.env` from a secret manager.
|
|
9
|
+
*
|
|
10
|
+
* A global flag prevents duplicate execution across multiple calls.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* // Automatically called by express-sweet.mount()
|
|
16
|
+
* // To configure, set env_path in config/config.js:
|
|
17
|
+
* export default {
|
|
18
|
+
* env_path: '.env'
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
* @example
|
|
22
|
+
* ```bash
|
|
23
|
+
* # .env file example
|
|
24
|
+
* NODE_ENV=development
|
|
25
|
+
* DATABASE_URL=mysql://localhost/mydb
|
|
26
|
+
* SECRET_KEY=mysecret
|
|
27
|
+
* ```
|
|
28
|
+
* @example
|
|
29
|
+
* ```js
|
|
30
|
+
* // Post-process environment variables in config/config.js
|
|
31
|
+
* export default {
|
|
32
|
+
* env_path: '.env',
|
|
33
|
+
* hook_after_env_load: () => {
|
|
34
|
+
* if (process.env.DB_PASSWORD_B64)
|
|
35
|
+
* process.env.DB_PASSWORD = Buffer.from(process.env.DB_PASSWORD_B64, 'base64').toString();
|
|
36
|
+
* }
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export default function env(): Promise<void>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Registers error handling middleware on the Express application.
|
|
4
|
+
*
|
|
5
|
+
* Catches unmatched routes as 404 errors and delegates all errors to a custom hook
|
|
6
|
+
* (`hook_handle_error` in `config/config.js`) if configured, or responds with the
|
|
7
|
+
* appropriate HTTP status code.
|
|
8
|
+
*
|
|
9
|
+
* This middleware must be mounted last (after all routes) to function correctly.
|
|
10
|
+
*
|
|
11
|
+
* @param {express.Express} app Express application instance.
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* // Custom error handling in config/config.js
|
|
16
|
+
* export default {
|
|
17
|
+
* hook_handle_error: (error, req, res, next) => {
|
|
18
|
+
* if (error.status === 404)
|
|
19
|
+
* res.render('error/404');
|
|
20
|
+
* else
|
|
21
|
+
* res.render('error/500');
|
|
22
|
+
* }
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export default function errorHandler(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initializes global variables used throughout the application.
|
|
3
|
+
*
|
|
4
|
+
* Sets `global.APP_DIR` to the current working directory (`process.cwd()`),
|
|
5
|
+
* making the application root path accessible from any module.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // After mounting, access the application root directory anywhere:
|
|
10
|
+
* const configPath = `${global.APP_DIR}/config/custom.json`;
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export default function globals(): void;
|
|
@@ -5,51 +5,50 @@
|
|
|
5
5
|
* including CORS, authentication, view templating, error handling, and environment setup.
|
|
6
6
|
* These middleware are automatically mounted by the main mount function.
|
|
7
7
|
*
|
|
8
|
-
* @module middlewares
|
|
9
8
|
* @example
|
|
10
9
|
* ```js
|
|
11
10
|
* // Manual middleware mounting (usually not needed)
|
|
12
11
|
* import * as expx from 'express-sweet';
|
|
13
12
|
*
|
|
14
13
|
* const app = express();
|
|
15
|
-
* expx.
|
|
16
|
-
* await expx.
|
|
17
|
-
* await expx.
|
|
14
|
+
* expx.middleware.globals();
|
|
15
|
+
* await expx.middleware.env();
|
|
16
|
+
* await expx.middleware.parser(app);
|
|
18
17
|
* ```
|
|
19
18
|
*/
|
|
20
19
|
/**
|
|
21
20
|
* Cross-Origin Resource Sharing (CORS) middleware
|
|
22
21
|
*/
|
|
23
|
-
export { default as
|
|
22
|
+
export { default as cors } from './cors';
|
|
24
23
|
/**
|
|
25
24
|
* Environment variables loading middleware
|
|
26
25
|
*/
|
|
27
|
-
export { default as
|
|
26
|
+
export { default as env } from './env';
|
|
28
27
|
/**
|
|
29
28
|
* Global variables setup middleware
|
|
30
29
|
*/
|
|
31
|
-
export { default as
|
|
30
|
+
export { default as globals } from './globals';
|
|
32
31
|
/**
|
|
33
32
|
* HTTP parsing and static file serving middleware
|
|
34
33
|
*/
|
|
35
|
-
export { default as
|
|
34
|
+
export { default as parser } from './parser';
|
|
36
35
|
/**
|
|
37
36
|
* Local template variables middleware
|
|
38
37
|
*/
|
|
39
|
-
export { default as
|
|
38
|
+
export { default as locals } from './locals';
|
|
40
39
|
/**
|
|
41
40
|
* Handlebars view engine middleware
|
|
42
41
|
*/
|
|
43
|
-
export { default as
|
|
42
|
+
export { default as views, mountBeforeRender } from './views';
|
|
44
43
|
/**
|
|
45
44
|
* Error handling middleware
|
|
46
45
|
*/
|
|
47
|
-
export { default as errorHandler } from './
|
|
46
|
+
export { default as errorHandler } from './error';
|
|
48
47
|
/**
|
|
49
48
|
* Passport.js authentication middleware
|
|
50
49
|
*/
|
|
51
|
-
export { default as
|
|
50
|
+
export { default as auth } from './auth';
|
|
52
51
|
/**
|
|
53
52
|
* File-based routing middleware
|
|
54
53
|
*/
|
|
55
|
-
export { default as
|
|
54
|
+
export { default as router } from './router';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Sets view-accessible local variables (`baseUrl` and `currentPath`) on each request.
|
|
4
|
+
*
|
|
5
|
+
* - `baseUrl`: The application's origin URL (e.g., `https://example.com`).
|
|
6
|
+
* Supports reverse proxy environments via `x-forwarded-proto` header.
|
|
7
|
+
* May be `undefined` if URL construction fails (e.g., invalid Host header).
|
|
8
|
+
* - `currentPath`: The current request pathname without query string or hash.
|
|
9
|
+
*
|
|
10
|
+
* An optional `rewrite_base_url` hook in `config/config.js` allows transforming the base URL.
|
|
11
|
+
*
|
|
12
|
+
* @param {express.Express} app Express application instance.
|
|
13
|
+
* @returns {Promise<void>}
|
|
14
|
+
* @example
|
|
15
|
+
* ```handlebars
|
|
16
|
+
* {{!-- Using local variables in Handlebars templates --}}
|
|
17
|
+
* {{#if baseUrl}}
|
|
18
|
+
* <a href="{{baseUrl}}/home">Home</a>
|
|
19
|
+
* {{/if}}
|
|
20
|
+
* {{#if (eq currentPath '/dashboard')}}Active{{/if}}
|
|
21
|
+
* ```
|
|
22
|
+
* @example
|
|
23
|
+
* ```js
|
|
24
|
+
* // Custom base URL rewriting in config/config.js
|
|
25
|
+
* export default {
|
|
26
|
+
* rewrite_base_url: baseUrl => {
|
|
27
|
+
* if (!baseUrl) return 'http://localhost:3000';
|
|
28
|
+
* return `${baseUrl}/admin`;
|
|
29
|
+
* }
|
|
30
|
+
* };
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export default function locals(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Configures HTTP request processing middleware on the Express application.
|
|
4
|
+
*
|
|
5
|
+
* Sets up the following middleware stack:
|
|
6
|
+
* - **Morgan** for HTTP request logging (configured via `config/logging.js`)
|
|
7
|
+
* - **JSON body parser** (`application/json`)
|
|
8
|
+
* - **URL-encoded body parser** (`application/x-www-form-urlencoded`)
|
|
9
|
+
* - **Cookie parser**
|
|
10
|
+
* - **Static file serving** from the `public/` directory
|
|
11
|
+
*
|
|
12
|
+
* @param {express.Express} app Express application instance.
|
|
13
|
+
* @returns {Promise<void>}
|
|
14
|
+
* @example
|
|
15
|
+
* ```js
|
|
16
|
+
* // config/config.js - Body size limit
|
|
17
|
+
* export default {
|
|
18
|
+
* max_body_size: '10mb'
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
* @example
|
|
22
|
+
* ```js
|
|
23
|
+
* // config/logging.js - Request logging
|
|
24
|
+
* export default {
|
|
25
|
+
* format: 'dev',
|
|
26
|
+
* skip: (req, res) => req.path === '/health'
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export default function parser(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Configures file-based URL routing on the Express application.
|
|
4
|
+
*
|
|
5
|
+
* Scans the `routes/` directory (or the directory specified by `router_dir` in `config/config.js`)
|
|
6
|
+
* and automatically maps each file to a URL endpoint based on its path:
|
|
7
|
+
* - `routes/user.js` → `/user`
|
|
8
|
+
* - `routes/api/users.js` → `/api/users`
|
|
9
|
+
*
|
|
10
|
+
* Also injects Multer middleware for `multipart/form-data` handling based on `config/upload.js`.
|
|
11
|
+
* If `default_router` is configured, that route also handles the root URL (`/`).
|
|
12
|
+
*
|
|
13
|
+
* @param {express.Express} app Express application instance.
|
|
14
|
+
* @returns {Promise<void>}
|
|
15
|
+
* @example
|
|
16
|
+
* ```js
|
|
17
|
+
* // routes/user.js → responds to /user
|
|
18
|
+
* import {Router} from 'express';
|
|
19
|
+
* const router = Router();
|
|
20
|
+
* router.get('/', (req, res) => res.json({name: 'Alice'}));
|
|
21
|
+
* export default router;
|
|
22
|
+
* ```
|
|
23
|
+
* @example
|
|
24
|
+
* ```js
|
|
25
|
+
* // config/config.js - Set default route
|
|
26
|
+
* export default {
|
|
27
|
+
* default_router: '/home'
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export default function router(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Configures the Handlebars view engine on the Express application.
|
|
4
|
+
*
|
|
5
|
+
* Registers all built-in Handlebars helpers, sets up layout/partial directories,
|
|
6
|
+
* and configures the template file extension based on `config/view.js`.
|
|
7
|
+
*
|
|
8
|
+
* @param {express.Express} app Express application instance.
|
|
9
|
+
* @returns {Promise<void>}
|
|
10
|
+
* @example
|
|
11
|
+
* ```js
|
|
12
|
+
* // config/view.js
|
|
13
|
+
* import path from 'node:path';
|
|
14
|
+
*
|
|
15
|
+
* export default {
|
|
16
|
+
* views_dir: path.join(process.cwd(), 'views'),
|
|
17
|
+
* partials_dir: path.join(process.cwd(), 'views/partials'),
|
|
18
|
+
* layouts_dir: path.join(process.cwd(), 'views/layout'),
|
|
19
|
+
* default_layout: path.join(process.cwd(), 'views/layout/default.hbs'),
|
|
20
|
+
* extension: '.hbs',
|
|
21
|
+
* beforeRender: (req, res) => {
|
|
22
|
+
* res.locals.siteName = 'My App';
|
|
23
|
+
* }
|
|
24
|
+
* };
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export default function views(app: express.Express): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Mounts the `beforeRender` hook middleware from `config/view.js`.
|
|
30
|
+
*
|
|
31
|
+
* This is called after the authentication middleware to ensure `req.user`
|
|
32
|
+
* is available in the hook. Supports both synchronous and async hook functions.
|
|
33
|
+
*
|
|
34
|
+
* @param {express.Express} app Express application instance.
|
|
35
|
+
* @returns {Promise<void>}
|
|
36
|
+
*/
|
|
37
|
+
export declare function mountBeforeRender(app: express.Express): Promise<void>;
|
package/types/mount.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import express from 'express';
|
|
|
5
5
|
*
|
|
6
6
|
* **Initialization Order:**
|
|
7
7
|
* 1. Global variables
|
|
8
|
-
* 2. Environment variables (.env)
|
|
8
|
+
* 2. Environment variables (.env) and the `hook_after_env_load` hook
|
|
9
9
|
* 3. Database models
|
|
10
10
|
* 4. HTTP middleware (body parsing, cookies, static files)
|
|
11
11
|
* 5. View engine (Handlebars)
|
|
@@ -49,5 +49,4 @@ import express from 'express';
|
|
|
49
49
|
* app.listen(3000);
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
|
|
53
|
-
export default _default;
|
|
52
|
+
export default function mount(app: express.Express): Promise<void>;
|
|
@@ -2,7 +2,6 @@ import express from 'express';
|
|
|
2
2
|
/**
|
|
3
3
|
* Express Sweet application configuration interface.
|
|
4
4
|
* Defines configuration options for environment, CORS, routing, error handling, and more.
|
|
5
|
-
* @see {@link https://expressjs.com/ | Express.js}
|
|
6
5
|
* @example
|
|
7
6
|
* ```js
|
|
8
7
|
* // config/config.js
|
|
@@ -14,6 +13,10 @@ import express from 'express';
|
|
|
14
13
|
* default_router: '/blog',
|
|
15
14
|
* rewrite_base_url: baseUrl => `${baseUrl}/admin`,
|
|
16
15
|
* is_ajax: req => /^\/api/.test(req.path),
|
|
16
|
+
* hook_after_env_load: () => {
|
|
17
|
+
* if (process.env.DB_PASSWORD_B64)
|
|
18
|
+
* process.env.DB_PASSWORD = Buffer.from(process.env.DB_PASSWORD_B64, 'base64').toString();
|
|
19
|
+
* },
|
|
17
20
|
* hook_handle_error: (error, req, res, next) => {
|
|
18
21
|
* if (error.status === 404) res.render('error/404');
|
|
19
22
|
* else res.render('error/500');
|
|
@@ -48,9 +51,8 @@ export default interface AppConfig {
|
|
|
48
51
|
*/
|
|
49
52
|
default_router?: string;
|
|
50
53
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* The baseUrl parameter may be undefined if URL construction fails (e.g., invalid Host header).
|
|
54
|
+
* Hook to transform the auto-detected base URL (`app.locals.baseUrl`).
|
|
55
|
+
* Receives `undefined` if URL construction fails (e.g., invalid Host header).
|
|
54
56
|
* @type {(baseUrl: string | undefined) => string | undefined}
|
|
55
57
|
* @example
|
|
56
58
|
* ```js
|
|
@@ -62,34 +64,43 @@ export default interface AppConfig {
|
|
|
62
64
|
*/
|
|
63
65
|
rewrite_base_url?: (baseUrl: string | undefined) => string | undefined;
|
|
64
66
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
67
|
+
* Determines whether a request should be treated as an AJAX/API request.
|
|
68
|
+
* Defaults to checking `req.xhr` (X-Requested-With: XMLHttpRequest).
|
|
69
|
+
* Override this to customize AJAX detection (e.g., by URL prefix).
|
|
68
70
|
* @type {(req: express.Request) => boolean}
|
|
69
71
|
* @example
|
|
70
72
|
* ```js
|
|
71
|
-
* is_ajax: req =>
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
* is_ajax: req => /^\/api/.test(req.path)
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
is_ajax?: (req: express.Request) => boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Hook executed right after environment variables are loaded and before database
|
|
79
|
+
* models are initialized. Runs once per process, even when `env_path` is unset.
|
|
80
|
+
* Use it to decrypt secrets, fetch values from a secret manager, or validate
|
|
81
|
+
* required variables before the database connection is established.
|
|
82
|
+
* @type {() => Promise<void>|void}
|
|
83
|
+
* @example
|
|
84
|
+
* ```js
|
|
85
|
+
* hook_after_env_load: () => {
|
|
86
|
+
* if (process.env.DB_PASSWORD_B64)
|
|
87
|
+
* process.env.DB_PASSWORD = Buffer.from(process.env.DB_PASSWORD_B64, 'base64').toString();
|
|
75
88
|
* }
|
|
76
89
|
* ```
|
|
77
90
|
*/
|
|
78
|
-
|
|
91
|
+
hook_after_env_load?: () => Promise<void> | void;
|
|
79
92
|
/**
|
|
80
|
-
*
|
|
81
|
-
*
|
|
93
|
+
* Custom error handler hook. Overrides the default behavior of responding with
|
|
94
|
+
* `res.status(error.status || 500).end()`.
|
|
82
95
|
* @type {(error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
|
|
83
96
|
* @example
|
|
84
97
|
* ```js
|
|
85
98
|
* hook_handle_error: (error, req, res, next) => {
|
|
86
99
|
* if (error.status === 404)
|
|
87
|
-
*
|
|
88
|
-
* res.render('errors/404');
|
|
100
|
+
* res.render('error/404');
|
|
89
101
|
* else
|
|
90
|
-
* // For other errors, unknown error screen (views/error/500.hbs) is displayed.
|
|
91
102
|
* res.render('error/500');
|
|
92
|
-
* }
|
|
103
|
+
* }
|
|
93
104
|
* ```
|
|
94
105
|
*/
|
|
95
106
|
hook_handle_error?: (error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
|
@@ -2,7 +2,6 @@ import express from 'express';
|
|
|
2
2
|
/**
|
|
3
3
|
* User authentication configuration interface using Passport.js.
|
|
4
4
|
* Defines configuration for user authentication, session management, and security settings.
|
|
5
|
-
* @see {@link https://www.passportjs.org/ | Passport.js}
|
|
6
5
|
* @example
|
|
7
6
|
* ```js
|
|
8
7
|
* // config/authentication.js
|
|
@@ -86,7 +85,7 @@ export default interface AuthenticationConfig {
|
|
|
86
85
|
*/
|
|
87
86
|
success_redirect: string;
|
|
88
87
|
/**
|
|
89
|
-
* URL to redirect
|
|
88
|
+
* URL to redirect to on authentication failure, defaults to `/login`.
|
|
90
89
|
* @type {string|((req: express.Request, res: express.Response) => string)}
|
|
91
90
|
* @example
|
|
92
91
|
* ```js
|
|
@@ -102,10 +101,9 @@ export default interface AuthenticationConfig {
|
|
|
102
101
|
*/
|
|
103
102
|
failure_redirect: string | ((req: express.Request, res: express.Response) => string);
|
|
104
103
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* Note that the user information must include an ID value that can identify the user.
|
|
104
|
+
* Hook called during user authentication.
|
|
105
|
+
* Look up and return the user matching the given credentials, or `null` if not found.
|
|
106
|
+
* The returned object must include an `id` property for session serialization.
|
|
109
107
|
* @type {(username: string, password: string, req: express.Request) => Promise<{[key: string]: any}|null>}
|
|
110
108
|
* @example
|
|
111
109
|
* ```js
|
|
@@ -125,9 +123,9 @@ export default interface AuthenticationConfig {
|
|
|
125
123
|
[key: string]: any;
|
|
126
124
|
} | null>;
|
|
127
125
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* The returned
|
|
126
|
+
* Hook called on each request to deserialize the authenticated user from the session.
|
|
127
|
+
* Look up and return the user data for the given ID.
|
|
128
|
+
* The returned object is set on `req.user` and available in views as `session`.
|
|
131
129
|
* @type {(id: number|string) => Promise<{[key: string]: any}>}
|
|
132
130
|
* @example
|
|
133
131
|
* ```js
|
|
@@ -144,12 +142,13 @@ export default interface AuthenticationConfig {
|
|
|
144
142
|
[key: string]: any;
|
|
145
143
|
}>;
|
|
146
144
|
/**
|
|
147
|
-
* URL
|
|
145
|
+
* URL patterns that bypass authentication. Requests matching any pattern (partial string match
|
|
146
|
+
* or regex match) are allowed without authentication. Defaults to `[]` (all routes protected).
|
|
148
147
|
* @type {(string|RegExp)[]}
|
|
149
148
|
*/
|
|
150
149
|
allow_unauthenticated: (string | RegExp)[];
|
|
151
150
|
/**
|
|
152
|
-
*
|
|
151
|
+
* Session cookie max-age in milliseconds. Defaults to 24 hours (`24 * 3600000`).
|
|
153
152
|
* @type {number}
|
|
154
153
|
*/
|
|
155
154
|
expiration: number;
|
|
@@ -3,7 +3,6 @@ import sequelize from 'sequelize';
|
|
|
3
3
|
* Database configuration interface for Sequelize connections.
|
|
4
4
|
* Defines environment-specific database configurations (development, test, production).
|
|
5
5
|
* Each environment contains Sequelize connection options.
|
|
6
|
-
* @see {@link https://sequelize.org/docs/v6/other-topics/migrations/#configuration | Sequelize Configuration}
|
|
7
6
|
* @example
|
|
8
7
|
* ```js
|
|
9
8
|
* // config/database.js
|