express-sweet 3.0.2 → 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/CHANGELOG.md +224 -418
- package/LICENSE +1 -1
- package/README.md +211 -1026
- package/dist/build.cjs +37 -72
- package/dist/build.mjs +37 -72
- package/package.json +50 -64
- 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 +7 -52
- package/types/middleware/auth.d.ts +186 -0
- package/types/middleware/cors.d.ts +19 -0
- package/types/middleware/env.d.ts +24 -0
- package/types/middleware/error.d.ts +26 -0
- package/types/middleware/globals.d.ts +13 -0
- package/types/{middlewares → middleware}/index.d.ts +19 -12
- 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 +34 -3
- package/types/{interfaces/BasicConfig.d.ts → types/AppConfig.d.ts} +18 -25
- package/types/{interfaces → types}/AuthenticationConfig.d.ts +10 -11
- package/types/{interfaces → types}/DatabaseConfig.d.ts +1 -1
- package/types/{interfaces → types}/LoggingConfig.d.ts +0 -1
- package/types/types/UploadConfig.d.ts +72 -0
- package/types/{interfaces → types}/ViewConfig.d.ts +1 -2
- package/types/{interfaces → types}/index.d.ts +7 -4
- package/types/utils/index.d.ts +7 -4
- package/types/utils/loadAppConfig.d.ts +17 -0
- package/types/utils/loadAuthenticationConfig.d.ts +1 -1
- package/types/utils/loadLoggingConfig.d.ts +1 -1
- package/types/utils/loadUploadConfig.d.ts +20 -0
- package/types/utils/loadViewConfig.d.ts +1 -1
- package/configuration_sample/cjs/authentication.js +0 -138
- package/configuration_sample/cjs/config.js +0 -91
- package/configuration_sample/cjs/database.js +0 -121
- package/configuration_sample/cjs/logging.js +0 -41
- package/configuration_sample/cjs/view.js +0 -54
- package/configuration_sample/esm/authentication.js +0 -136
- package/configuration_sample/esm/config.js +0 -91
- package/configuration_sample/esm/database.js +0 -121
- package/configuration_sample/esm/logging.js +0 -41
- 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/Authentication.d.ts +0 -52
- package/types/middlewares/CORS.d.ts +0 -30
- package/types/middlewares/Environment.d.ts +0 -36
- package/types/middlewares/ErrorHandler.d.ts +0 -34
- package/types/middlewares/Global.d.ts +0 -27
- package/types/middlewares/Http.d.ts +0 -29
- package/types/middlewares/Local.d.ts +0 -42
- package/types/middlewares/View.d.ts +0 -49
- package/types/routing/Router.d.ts +0 -56
- package/types/routing/index.d.ts +0 -37
- 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 -23
- package/types/utils/loadBasicConfig.d.ts +0 -17
|
@@ -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,24 @@
|
|
|
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`. A global flag prevents duplicate loading across multiple calls.
|
|
6
|
+
*
|
|
7
|
+
* @returns {Promise<void>}
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* // Automatically called by express-sweet.mount()
|
|
11
|
+
* // To configure, set env_path in config/config.js:
|
|
12
|
+
* export default {
|
|
13
|
+
* env_path: '.env'
|
|
14
|
+
* };
|
|
15
|
+
* ```
|
|
16
|
+
* @example
|
|
17
|
+
* ```bash
|
|
18
|
+
* # .env file example
|
|
19
|
+
* NODE_ENV=development
|
|
20
|
+
* DATABASE_URL=mysql://localhost/mydb
|
|
21
|
+
* SECRET_KEY=mysecret
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
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;
|
|
@@ -1,47 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Express middleware components for Express Sweet.
|
|
3
3
|
*
|
|
4
|
-
* Provides a collection of middleware
|
|
4
|
+
* Provides a collection of middleware functions that handle various aspects of Express applications
|
|
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
|
-
*
|
|
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
|
|
46
|
+
export { default as errorHandler } from './error';
|
|
47
|
+
/**
|
|
48
|
+
* Passport.js authentication middleware
|
|
49
|
+
*/
|
|
50
|
+
export { default as auth } from './auth';
|
|
51
|
+
/**
|
|
52
|
+
* File-based routing middleware
|
|
53
|
+
*/
|
|
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
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
/**
|
|
3
3
|
* Mount Express Sweet extensions on your Express application.
|
|
4
|
-
* Initializes all middleware in a specific order
|
|
4
|
+
* Initializes all middleware components in a specific order to ensure proper functionality.
|
|
5
|
+
*
|
|
6
|
+
* **Initialization Order:**
|
|
7
|
+
* 1. Global variables
|
|
8
|
+
* 2. Environment variables (.env)
|
|
9
|
+
* 3. Database models
|
|
10
|
+
* 4. HTTP middleware (body parsing, cookies, static files)
|
|
11
|
+
* 5. View engine (Handlebars)
|
|
12
|
+
* 6. CORS configuration
|
|
13
|
+
* 7. Local variables
|
|
14
|
+
* 8. Authentication (Passport.js)
|
|
15
|
+
* 9. URL routing
|
|
16
|
+
* 10. Error handler
|
|
17
|
+
*
|
|
5
18
|
* @param {express.Express} app Express application instance
|
|
6
19
|
* @returns {Promise<void>}
|
|
7
20
|
* @example
|
|
8
21
|
* ```js
|
|
22
|
+
* // Basic usage
|
|
9
23
|
* import express from 'express';
|
|
10
24
|
* import * as expx from 'express-sweet';
|
|
11
25
|
*
|
|
@@ -16,6 +30,23 @@ import express from 'express';
|
|
|
16
30
|
* console.log('Server running on port 3000');
|
|
17
31
|
* });
|
|
18
32
|
* ```
|
|
33
|
+
* @example
|
|
34
|
+
* ```js
|
|
35
|
+
* // With custom middleware
|
|
36
|
+
* import express from 'express';
|
|
37
|
+
* import * as expx from 'express-sweet';
|
|
38
|
+
*
|
|
39
|
+
* const app = express();
|
|
40
|
+
*
|
|
41
|
+
* // Mount Express Sweet
|
|
42
|
+
* await expx.mount(app);
|
|
43
|
+
*
|
|
44
|
+
* // Add custom routes after mounting
|
|
45
|
+
* app.get('/custom', (req, res) => {
|
|
46
|
+
* res.send('Custom route');
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* app.listen(3000);
|
|
50
|
+
* ```
|
|
19
51
|
*/
|
|
20
|
-
|
|
21
|
-
export default _default;
|
|
52
|
+
export default function mount(app: express.Express): Promise<void>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
2
|
/**
|
|
3
|
-
* Express Sweet
|
|
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
|
|
@@ -21,15 +20,15 @@ import express from 'express';
|
|
|
21
20
|
* };
|
|
22
21
|
* ```
|
|
23
22
|
*/
|
|
24
|
-
export default interface
|
|
23
|
+
export default interface AppConfig {
|
|
25
24
|
/**
|
|
26
25
|
* Environment variable file (.env) path, defaults to none (undefined).
|
|
27
26
|
* @type {string}
|
|
28
27
|
*/
|
|
29
28
|
env_path?: string;
|
|
30
29
|
/**
|
|
31
|
-
* CORS permission, defaults to
|
|
32
|
-
* @type {
|
|
30
|
+
* CORS permission, defaults to disabled (false).
|
|
31
|
+
* @type {boolean}
|
|
33
32
|
*/
|
|
34
33
|
cors_enabled?: boolean;
|
|
35
34
|
/**
|
|
@@ -48,47 +47,41 @@ export default interface BasicConfig {
|
|
|
48
47
|
*/
|
|
49
48
|
default_router?: string;
|
|
50
49
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* @type {(baseUrl: string): string}
|
|
50
|
+
* Hook to transform the auto-detected base URL (`app.locals.baseUrl`).
|
|
51
|
+
* Receives `undefined` if URL construction fails (e.g., invalid Host header).
|
|
52
|
+
* @type {(baseUrl: string | undefined) => string | undefined}
|
|
55
53
|
* @example
|
|
56
54
|
* ```js
|
|
57
55
|
* rewrite_base_url: baseUrl => {
|
|
56
|
+
* if (!baseUrl) return 'http://localhost:3000'; // Fallback for undefined
|
|
58
57
|
* return `${baseUrl}/admin`;
|
|
59
58
|
* }
|
|
60
59
|
* ```
|
|
61
60
|
*/
|
|
62
|
-
rewrite_base_url?: (baseUrl: string) => string;
|
|
61
|
+
rewrite_base_url?: (baseUrl: string | undefined) => string | undefined;
|
|
63
62
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
63
|
+
* Determines whether a request should be treated as an AJAX/API request.
|
|
64
|
+
* Defaults to checking `req.xhr` (X-Requested-With: XMLHttpRequest).
|
|
65
|
+
* Override this to customize AJAX detection (e.g., by URL prefix).
|
|
67
66
|
* @type {(req: express.Request) => boolean}
|
|
68
67
|
* @example
|
|
69
68
|
* ```js
|
|
70
|
-
* is_ajax: req =>
|
|
71
|
-
* // If the request URL begins with /api, it is assumed to be Ajax.
|
|
72
|
-
* return /^\/api/.test(req.path);
|
|
73
|
-
* // return !!req.xhr;
|
|
74
|
-
* }
|
|
69
|
+
* is_ajax: req => /^\/api/.test(req.path)
|
|
75
70
|
* ```
|
|
76
71
|
*/
|
|
77
|
-
is_ajax
|
|
72
|
+
is_ajax?: (req: express.Request) => boolean;
|
|
78
73
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
74
|
+
* Custom error handler hook. Overrides the default behavior of responding with
|
|
75
|
+
* `res.status(error.status || 500).end()`.
|
|
81
76
|
* @type {(error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
|
|
82
77
|
* @example
|
|
83
78
|
* ```js
|
|
84
79
|
* hook_handle_error: (error, req, res, next) => {
|
|
85
80
|
* if (error.status === 404)
|
|
86
|
-
*
|
|
87
|
-
* res.render('errors/404');
|
|
81
|
+
* res.render('error/404');
|
|
88
82
|
* else
|
|
89
|
-
* // For other errors, unknown error screen (views/error/500.hbs) is displayed.
|
|
90
83
|
* res.render('error/500');
|
|
91
|
-
* }
|
|
84
|
+
* }
|
|
92
85
|
* ```
|
|
93
86
|
*/
|
|
94
87
|
hook_handle_error?: (error: any, req: express.Request, res: express.Response, next: express.NextFunction) => void;
|