express-sweet 3.0.2 → 4.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 +112 -118
- package/README.md +206 -57
- package/configuration_sample/cjs/config.js +5 -4
- package/configuration_sample/cjs/upload.js +91 -0
- package/configuration_sample/esm/config.js +5 -4
- package/configuration_sample/esm/upload.js +91 -0
- package/dist/build.cjs +41 -35
- package/dist/build.mjs +41 -35
- package/package.json +6 -4
- package/types/database/Model.d.ts +2 -2
- package/types/handlebars_helpers/object.d.ts +2 -2
- package/types/index.d.ts +1 -43
- package/types/interfaces/{BasicConfig.d.ts → AppConfig.d.ts} +8 -7
- package/types/interfaces/DatabaseConfig.d.ts +1 -0
- package/types/interfaces/UploadConfig.d.ts +73 -0
- package/types/interfaces/index.d.ts +6 -2
- package/types/middlewares/corsPolicy.d.ts +28 -0
- package/types/middlewares/{Environment.d.ts → envLoader.d.ts} +13 -15
- package/types/middlewares/errorHandler.d.ts +32 -0
- package/types/middlewares/globalVars.d.ts +25 -0
- package/types/middlewares/index.d.ts +19 -11
- package/types/middlewares/localVars.d.ts +44 -0
- package/types/middlewares/passportAuth.d.ts +15 -0
- package/types/middlewares/requestParser.d.ts +27 -0
- package/types/{routing/Router.d.ts → middlewares/routeMapper.d.ts} +14 -16
- package/types/middlewares/viewEngine.d.ts +49 -0
- package/types/mount.d.ts +33 -1
- package/types/utils/Types.d.ts +17 -11
- package/types/utils/index.d.ts +7 -3
- package/types/utils/loadAppConfig.d.ts +17 -0
- package/types/utils/loadUploadConfig.d.ts +20 -0
- package/types/middlewares/Authentication.d.ts +0 -52
- package/types/middlewares/CORS.d.ts +0 -30
- 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/index.d.ts +0 -37
- package/types/utils/loadBasicConfig.d.ts +0 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-sweet",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "EXPRESS SWEET is a powerful Express.js extension that streamlines your workflow and boosts productivity with a suite of utilities and enhancements.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/build.cjs",
|
|
@@ -63,17 +63,19 @@
|
|
|
63
63
|
],
|
|
64
64
|
"author": "Takuya Motoshima <developer.takuyamotoshima@gmail.com> (https://x.com/takuya_motech)",
|
|
65
65
|
"license": "MIT",
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": ">=18.0.0"
|
|
68
|
+
},
|
|
66
69
|
"bugs": {
|
|
67
70
|
"url": "https://github.com/takuya-motoshima/express-sweet/issues",
|
|
68
71
|
"email": "developer.takuyamotoshima@gmail.com"
|
|
69
72
|
},
|
|
70
73
|
"homepage": "https://github.com/takuya-motoshima/express-sweet#readme",
|
|
71
74
|
"dependencies": {
|
|
72
|
-
"body-parser": "^2.2.0",
|
|
73
75
|
"connect-redis": "^7.1.0",
|
|
74
76
|
"cookie-parser": "^1.4.7",
|
|
75
77
|
"dotenv": "^8.2.0",
|
|
76
|
-
"express": "^
|
|
78
|
+
"express": "^5.2.1",
|
|
77
79
|
"express-handlebars": "^7.1.3",
|
|
78
80
|
"express-session": "^1.18.2",
|
|
79
81
|
"glob": "^10.3.12",
|
|
@@ -94,7 +96,7 @@
|
|
|
94
96
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
95
97
|
"@rollup/plugin-terser": "^0.4.4",
|
|
96
98
|
"@types/cookie-parser": "^1.4.2",
|
|
97
|
-
"@types/express": "^
|
|
99
|
+
"@types/express": "^5.0.6",
|
|
98
100
|
"@types/express-session": "^1.18.2",
|
|
99
101
|
"@types/glob": "^8.1.0",
|
|
100
102
|
"@types/http-errors": "^1.8.0",
|
|
@@ -120,8 +120,8 @@ export default class Model extends Model_base {
|
|
|
120
120
|
};
|
|
121
121
|
/**
|
|
122
122
|
* An enum of query types used by sequelize.query.
|
|
123
|
-
* @see https://sequelize.org/api/v6/variable/index.html#static-variable-QueryTypes
|
|
124
123
|
* @type {sequelize.QueryTypes}
|
|
124
|
+
* @see {@link https://sequelize.org/api/v6/variable/index.html#static-variable-QueryTypes | Sequelize QueryTypes}
|
|
125
125
|
*/
|
|
126
126
|
static readonly QueryTypes: {
|
|
127
127
|
[key: string]: string;
|
|
@@ -251,8 +251,8 @@ export default class Model extends Model_base {
|
|
|
251
251
|
/**
|
|
252
252
|
* Reference to sequelize.Transaction.
|
|
253
253
|
* This includes properties such as isolation level enums used with the transaction option.
|
|
254
|
-
* @see https://sequelize.org/api/v6/class/src/transaction.js~transaction
|
|
255
254
|
* @type {sequelize.Transaction}
|
|
255
|
+
* @see {@link https://sequelize.org/api/v6/class/src/transaction.js~transaction | Sequelize Transaction}
|
|
256
256
|
* @example
|
|
257
257
|
* ```js
|
|
258
258
|
* BookModel.Transaction.ISOLATION_LEVELS.READ_UNCOMMITTED // "READ UNCOMMITTED"
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
export declare const jsonStringify: (value: any, indent?: number | string) => string | undefined;
|
|
12
12
|
/**
|
|
13
13
|
* Parses the given string using JSON.parse.
|
|
14
|
-
* @param {any} value
|
|
15
|
-
* @returns {any} JavaScript value or object described by
|
|
14
|
+
* @param {any} value JSON string to parse.
|
|
15
|
+
* @returns {any} JavaScript value or object described by the string.
|
|
16
16
|
* @example
|
|
17
17
|
* ```handlebars
|
|
18
18
|
* {{jsonParse val}}
|
package/types/index.d.ts
CHANGED
|
@@ -20,74 +20,32 @@
|
|
|
20
20
|
* console.log('Server running on port 3000');
|
|
21
21
|
* });
|
|
22
22
|
* ```
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```js
|
|
26
|
-
* // Using database models
|
|
27
|
-
* import * as expx from 'express-sweet';
|
|
28
|
-
*
|
|
29
|
-
* export default class extends expx.database.Model {
|
|
30
|
-
* static get table() {
|
|
31
|
-
* return 'users';
|
|
32
|
-
* }
|
|
33
|
-
* static get attributes() {
|
|
34
|
-
* return {
|
|
35
|
-
* id: { type: this.DataTypes.INTEGER, primaryKey: true },
|
|
36
|
-
* name: this.DataTypes.STRING,
|
|
37
|
-
* email: this.DataTypes.STRING
|
|
38
|
-
* };
|
|
39
|
-
* }
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```js
|
|
45
|
-
* // Using authentication service
|
|
46
|
-
* import * as expx from 'express-sweet';
|
|
47
|
-
*
|
|
48
|
-
* router.post('/login', async (req, res, next) => {
|
|
49
|
-
* const isAuthenticated = await expx.services.Authentication.authenticate(req, res, next);
|
|
50
|
-
* res.json(isAuthenticated);
|
|
51
|
-
* });
|
|
52
|
-
* ```
|
|
53
23
|
*/
|
|
54
24
|
/**
|
|
55
25
|
* Main application mount function
|
|
56
|
-
* @function mount
|
|
57
26
|
*/
|
|
58
|
-
export { default as mount } from '
|
|
27
|
+
export { default as mount } from './mount';
|
|
59
28
|
/**
|
|
60
29
|
* Database utilities and ORM functionality
|
|
61
|
-
* @namespace database
|
|
62
30
|
*/
|
|
63
31
|
export * as database from './database';
|
|
64
32
|
/**
|
|
65
33
|
* Authentication and other services
|
|
66
|
-
* @namespace services
|
|
67
34
|
*/
|
|
68
35
|
export * as services from './services';
|
|
69
36
|
/**
|
|
70
37
|
* Utility functions for type checking and configuration loading
|
|
71
|
-
* @namespace utils
|
|
72
38
|
*/
|
|
73
39
|
export * as utils from './utils';
|
|
74
40
|
/**
|
|
75
41
|
* Express middleware components
|
|
76
|
-
* @namespace middlewares
|
|
77
42
|
*/
|
|
78
43
|
export * as middlewares from './middlewares';
|
|
79
44
|
/**
|
|
80
45
|
* TypeScript interfaces for configuration
|
|
81
|
-
* @namespace interfaces
|
|
82
46
|
*/
|
|
83
47
|
export * as interfaces from './interfaces';
|
|
84
48
|
/**
|
|
85
49
|
* Custom Handlebars template helpers
|
|
86
|
-
* @namespace handlebars_helpers
|
|
87
50
|
*/
|
|
88
51
|
export * as handlebars_helpers from './handlebars_helpers';
|
|
89
|
-
/**
|
|
90
|
-
* Express routing utilities
|
|
91
|
-
* @namespace routing
|
|
92
|
-
*/
|
|
93
|
-
export * as routing from './routing';
|
|
@@ -1,6 +1,6 @@
|
|
|
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
5
|
* @see {@link https://expressjs.com/ | Express.js}
|
|
6
6
|
* @example
|
|
@@ -21,15 +21,15 @@ import express from 'express';
|
|
|
21
21
|
* };
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
export default interface
|
|
24
|
+
export default interface AppConfig {
|
|
25
25
|
/**
|
|
26
26
|
* Environment variable file (.env) path, defaults to none (undefined).
|
|
27
27
|
* @type {string}
|
|
28
28
|
*/
|
|
29
29
|
env_path?: string;
|
|
30
30
|
/**
|
|
31
|
-
* CORS permission, defaults to
|
|
32
|
-
* @type {
|
|
31
|
+
* CORS permission, defaults to disabled (false).
|
|
32
|
+
* @type {boolean}
|
|
33
33
|
*/
|
|
34
34
|
cors_enabled?: boolean;
|
|
35
35
|
/**
|
|
@@ -50,16 +50,17 @@ export default interface BasicConfig {
|
|
|
50
50
|
/**
|
|
51
51
|
* This is a hook that rewrites the base URL.
|
|
52
52
|
* If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL.
|
|
53
|
-
* The
|
|
54
|
-
* @type {(baseUrl: string)
|
|
53
|
+
* The baseUrl parameter may be undefined if URL construction fails (e.g., invalid Host header).
|
|
54
|
+
* @type {(baseUrl: string | undefined) => string | undefined}
|
|
55
55
|
* @example
|
|
56
56
|
* ```js
|
|
57
57
|
* rewrite_base_url: baseUrl => {
|
|
58
|
+
* if (!baseUrl) return 'http://localhost:3000'; // Fallback for undefined
|
|
58
59
|
* return `${baseUrl}/admin`;
|
|
59
60
|
* }
|
|
60
61
|
* ```
|
|
61
62
|
*/
|
|
62
|
-
rewrite_base_url?: (baseUrl: string) => string;
|
|
63
|
+
rewrite_base_url?: (baseUrl: string | undefined) => string | undefined;
|
|
63
64
|
/**
|
|
64
65
|
* How to determine if it is an ajax request.
|
|
65
66
|
* The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
|
|
@@ -46,6 +46,7 @@ export default interface DatabaseConfig {
|
|
|
46
46
|
* Environment-specific database configuration.
|
|
47
47
|
* Key represents environment name (development, test, production, etc.)
|
|
48
48
|
* Value contains Sequelize connection options for that environment.
|
|
49
|
+
* @type {sequelize.Options}
|
|
49
50
|
*/
|
|
50
51
|
[key: string]: sequelize.Options;
|
|
51
52
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* File upload configuration interface using Multer.
|
|
4
|
+
* Defines dynamic upload middleware resolution per request based on path and method.
|
|
5
|
+
* @see {@link https://github.com/expressjs/multer | Multer Documentation}
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* // config/upload.js
|
|
9
|
+
* export default {
|
|
10
|
+
* enabled: true,
|
|
11
|
+
* resolve_middleware: (req, multer) => {
|
|
12
|
+
* if (req.path === '/api/admin/firms' && req.method === 'POST') {
|
|
13
|
+
* const upload = multer({ storage: multer.memoryStorage() });
|
|
14
|
+
* return upload.fields([
|
|
15
|
+
* { name: 'logo', maxCount: 1 },
|
|
16
|
+
* { name: 'eyecatch', maxCount: 1 }
|
|
17
|
+
* ]);
|
|
18
|
+
* }
|
|
19
|
+
* return null;
|
|
20
|
+
* }
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export default interface UploadConfig {
|
|
25
|
+
/**
|
|
26
|
+
* Enable file upload middleware, defaults to disabled (false).
|
|
27
|
+
* If config/upload.js doesn't exist, upload middleware won't be applied.
|
|
28
|
+
* @type {boolean}
|
|
29
|
+
*/
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Hook function to dynamically resolve multer middleware based on request.
|
|
33
|
+
* Return a multer middleware instance or null to skip upload handling for the request.
|
|
34
|
+
* @type {(req: express.Request, multer: typeof import('multer')) => Function|null}
|
|
35
|
+
* @example
|
|
36
|
+
* ```js
|
|
37
|
+
* // Single file upload
|
|
38
|
+
* resolve_middleware: (req, multer) => {
|
|
39
|
+
* if (req.path === '/api/user/avatar' && req.method === 'POST') {
|
|
40
|
+
* const upload = multer({ storage: multer.memoryStorage() });
|
|
41
|
+
* return upload.single('avatar');
|
|
42
|
+
* }
|
|
43
|
+
* return null;
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
* @example
|
|
47
|
+
* ```js
|
|
48
|
+
* // Multiple files with same field name
|
|
49
|
+
* resolve_middleware: (req, multer) => {
|
|
50
|
+
* if (req.path === '/api/gallery' && req.method === 'POST') {
|
|
51
|
+
* const upload = multer({ storage: multer.memoryStorage() });
|
|
52
|
+
* return upload.array('photos', 10);
|
|
53
|
+
* }
|
|
54
|
+
* return null;
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
* @example
|
|
58
|
+
* ```js
|
|
59
|
+
* // Multiple file fields
|
|
60
|
+
* resolve_middleware: (req, multer) => {
|
|
61
|
+
* if (req.path === '/api/admin/firms' && req.method === 'POST') {
|
|
62
|
+
* const upload = multer({ storage: multer.memoryStorage() });
|
|
63
|
+
* return upload.fields([
|
|
64
|
+
* { name: 'logo', maxCount: 1 },
|
|
65
|
+
* { name: 'eyecatch', maxCount: 1 }
|
|
66
|
+
* ]);
|
|
67
|
+
* }
|
|
68
|
+
* return null;
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
resolve_middleware: (req: express.Request, multer: typeof import('multer')) => Function | null;
|
|
73
|
+
}
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
*/
|
|
9
9
|
export { default as AuthenticationConfig } from './AuthenticationConfig';
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Application configuration interface
|
|
12
12
|
*/
|
|
13
|
-
export { default as
|
|
13
|
+
export { default as AppConfig } from './AppConfig';
|
|
14
14
|
/**
|
|
15
15
|
* Database configuration interface for Sequelize connections
|
|
16
16
|
*/
|
|
@@ -23,3 +23,7 @@ export { default as ViewConfig } from './ViewConfig';
|
|
|
23
23
|
* Logging configuration interface using Morgan
|
|
24
24
|
*/
|
|
25
25
|
export { default as LoggingConfig } from './LoggingConfig';
|
|
26
|
+
/**
|
|
27
|
+
* File upload configuration interface using Multer
|
|
28
|
+
*/
|
|
29
|
+
export { default as UploadConfig } from './UploadConfig';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Enables Cross-Origin Resource Sharing (CORS) middleware.
|
|
4
|
+
* Allows requests from another domain to the application by setting appropriate headers.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS | MDN CORS}
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // Enable CORS in config/config.js
|
|
10
|
+
* export default {
|
|
11
|
+
* cors_enabled: true
|
|
12
|
+
* };
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Mount CORS middleware on Express application.
|
|
17
|
+
* Sets appropriate CORS headers to allow cross-origin requests.
|
|
18
|
+
* @param {express.Express} app Express application instance
|
|
19
|
+
* @returns {Promise<void>}
|
|
20
|
+
* @example
|
|
21
|
+
* ```js
|
|
22
|
+
* // This method is called automatically by express-sweet.mount()
|
|
23
|
+
* import corsPolicy from './middlewares/corsPolicy';
|
|
24
|
+
*
|
|
25
|
+
* await corsPolicy(app);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export default function corsPolicy(app: express.Express): Promise<void>;
|
|
@@ -19,18 +19,16 @@
|
|
|
19
19
|
* SECRET_KEY=mysecret
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
static mount(): Promise<void>;
|
|
36
|
-
}
|
|
22
|
+
/**
|
|
23
|
+
* Mount environment variables on application.
|
|
24
|
+
* Loads .env file and sets variables in process.env if not already loaded.
|
|
25
|
+
* @returns {Promise<void>}
|
|
26
|
+
* @example
|
|
27
|
+
* ```js
|
|
28
|
+
* // This method is called automatically by express-sweet.mount()
|
|
29
|
+
* import envLoader from './middlewares/envLoader';
|
|
30
|
+
*
|
|
31
|
+
* await envLoader();
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export default function envLoader(): Promise<void>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Error handling middleware for Express applications.
|
|
4
|
+
* Catches 404 errors and provides customizable error handling through hooks.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* // Custom error handling in config/config.js
|
|
9
|
+
* export default {
|
|
10
|
+
* hook_handle_error: (error, req, res, next) => {
|
|
11
|
+
* if (error.status === 404)
|
|
12
|
+
* res.render('error/404');
|
|
13
|
+
* else
|
|
14
|
+
* res.render('error/500');
|
|
15
|
+
* }
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Mount error handling middleware on Express application.
|
|
21
|
+
* Sets up 404 error catching and custom error handling hooks.
|
|
22
|
+
* @param {express.Express} app Express application instance
|
|
23
|
+
* @returns {Promise<void>}
|
|
24
|
+
* @example
|
|
25
|
+
* ```js
|
|
26
|
+
* // This method is called automatically by express-sweet.mount()
|
|
27
|
+
* import errorHandler from './middlewares/errorHandler';
|
|
28
|
+
*
|
|
29
|
+
* await errorHandler(app);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export default function errorHandler(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set global variables accessible throughout the application.
|
|
3
|
+
* Variables can be accessed like "global.xxx" in all router and model classes.
|
|
4
|
+
*
|
|
5
|
+
* Global variables set:
|
|
6
|
+
* - APP_DIR: The directory where app.js is located (process.cwd())
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* // Access in any router or model
|
|
11
|
+
* console.log(global.APP_DIR); // "/path/to/your/app"
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Mount global variables on application.
|
|
16
|
+
* Sets up global.APP_DIR for access throughout the application.
|
|
17
|
+
* @example
|
|
18
|
+
* ```js
|
|
19
|
+
* // This method is called automatically by express-sweet.mount()
|
|
20
|
+
* import globalVars from './middlewares/globalVars';
|
|
21
|
+
*
|
|
22
|
+
* globalVars();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export default function globalVars(): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
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
|
*
|
|
@@ -12,36 +12,44 @@
|
|
|
12
12
|
* import * as expx from 'express-sweet';
|
|
13
13
|
*
|
|
14
14
|
* const app = express();
|
|
15
|
-
*
|
|
16
|
-
* await expx.middlewares.
|
|
17
|
-
* await expx.middlewares.
|
|
15
|
+
* expx.middlewares.globalVars();
|
|
16
|
+
* await expx.middlewares.envLoader();
|
|
17
|
+
* await expx.middlewares.requestParser(app);
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
/**
|
|
21
21
|
* Cross-Origin Resource Sharing (CORS) middleware
|
|
22
22
|
*/
|
|
23
|
-
export { default as
|
|
23
|
+
export { default as corsPolicy } from './corsPolicy';
|
|
24
24
|
/**
|
|
25
25
|
* Environment variables loading middleware
|
|
26
26
|
*/
|
|
27
|
-
export { default as
|
|
27
|
+
export { default as envLoader } from './envLoader';
|
|
28
28
|
/**
|
|
29
29
|
* Global variables setup middleware
|
|
30
30
|
*/
|
|
31
|
-
export { default as
|
|
31
|
+
export { default as globalVars } from './globalVars';
|
|
32
32
|
/**
|
|
33
33
|
* HTTP parsing and static file serving middleware
|
|
34
34
|
*/
|
|
35
|
-
export { default as
|
|
35
|
+
export { default as requestParser } from './requestParser';
|
|
36
36
|
/**
|
|
37
37
|
* Local template variables middleware
|
|
38
38
|
*/
|
|
39
|
-
export { default as
|
|
39
|
+
export { default as localVars } from './localVars';
|
|
40
40
|
/**
|
|
41
41
|
* Handlebars view engine middleware
|
|
42
42
|
*/
|
|
43
|
-
export { default as
|
|
43
|
+
export { default as viewEngine, mountBeforeRender } from './viewEngine';
|
|
44
44
|
/**
|
|
45
45
|
* Error handling middleware
|
|
46
46
|
*/
|
|
47
|
-
export { default as
|
|
47
|
+
export { default as errorHandler } from './errorHandler';
|
|
48
|
+
/**
|
|
49
|
+
* Passport.js authentication middleware
|
|
50
|
+
*/
|
|
51
|
+
export { default as passportAuth } from './passportAuth';
|
|
52
|
+
/**
|
|
53
|
+
* File-based routing middleware
|
|
54
|
+
*/
|
|
55
|
+
export { default as routeMapper } from './routeMapper';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Set local variables accessible in Handlebars views.
|
|
4
|
+
* Variables can be accessed in all views as {{var}} or {{{var}}}.
|
|
5
|
+
*
|
|
6
|
+
* Local variables set:
|
|
7
|
+
* - baseUrl: The base URL for this application (undefined if URL construction fails)
|
|
8
|
+
* - currentPath: The current request path
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```handlebars
|
|
12
|
+
* // Using in Handlebars templates
|
|
13
|
+
* {{#if baseUrl}}
|
|
14
|
+
* <a href="{{baseUrl}}/home">Home</a>
|
|
15
|
+
* {{/if}}
|
|
16
|
+
* {{#if (eq currentPath '/dashboard')}}Active{{/if}}
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Base URL rewriting in config/config.js
|
|
21
|
+
* // The hook receives undefined if URL construction fails
|
|
22
|
+
* ```js
|
|
23
|
+
* export default {
|
|
24
|
+
* rewrite_base_url: baseUrl => {
|
|
25
|
+
* if (!baseUrl) return 'http://localhost:3000'; // Fallback
|
|
26
|
+
* return `${baseUrl}/admin`;
|
|
27
|
+
* }
|
|
28
|
+
* };
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Mount local variables middleware on Express application.
|
|
33
|
+
* Sets up baseUrl and currentPath variables for use in views.
|
|
34
|
+
* @param {express.Express} app Express application instance
|
|
35
|
+
* @returns {Promise<void>}
|
|
36
|
+
* @example
|
|
37
|
+
* ```js
|
|
38
|
+
* // This method is called automatically by express-sweet.mount()
|
|
39
|
+
* import localVars from './middlewares/localVars';
|
|
40
|
+
*
|
|
41
|
+
* await localVars(app);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export default function localVars(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Mount authentication middleware on Express application.
|
|
4
|
+
* Sets up Passport.js with local strategy, session management, and route protection.
|
|
5
|
+
* @param {express.Express} app Express application instance
|
|
6
|
+
* @returns {Promise<void>}
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // This method is called automatically by express-sweet.mount()
|
|
10
|
+
* import passportAuth from './middlewares/passportAuth';
|
|
11
|
+
*
|
|
12
|
+
* await passportAuth(app);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export default function passportAuth(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* HTTP middleware setup for Express application.
|
|
4
|
+
* Configures JSON/URL-encoded parsing, cookie parsing, static files, and request logging.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```js
|
|
8
|
+
* // Body size configuration in config/config.js
|
|
9
|
+
* export default {
|
|
10
|
+
* max_body_size: '100mb' // Controls request body size limit
|
|
11
|
+
* };
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Mount HTTP middleware on Express application.
|
|
16
|
+
* Sets up request parsing, cookies, static files, and logging.
|
|
17
|
+
* @param {express.Express} app Express application instance
|
|
18
|
+
* @returns {Promise<void>}
|
|
19
|
+
* @example
|
|
20
|
+
* ```js
|
|
21
|
+
* // This method is called automatically by express-sweet.mount()
|
|
22
|
+
* import requestParser from './middlewares/requestParser';
|
|
23
|
+
*
|
|
24
|
+
* await requestParser(app);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export default function requestParser(app: express.Express): Promise<void>;
|
|
@@ -38,19 +38,17 @@ import express from 'express';
|
|
|
38
38
|
* };
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
static mount(app: express.Express): Promise<void>;
|
|
56
|
-
}
|
|
41
|
+
/**
|
|
42
|
+
* Mount file-based routing on Express application.
|
|
43
|
+
* Scans the routes directory and automatically maps files to URL endpoints.
|
|
44
|
+
* @param {express.Express} app Express application instance
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
* @example
|
|
47
|
+
* ```js
|
|
48
|
+
* // This method is called automatically by express-sweet.mount()
|
|
49
|
+
* import routeMapper from './middlewares/routeMapper';
|
|
50
|
+
*
|
|
51
|
+
* await routeMapper(app);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export default function routeMapper(app: express.Express): Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Enable Handlebars template engine with custom helpers and configuration.
|
|
4
|
+
* Sets up view engine, layouts, partials, and before-render hooks.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://handlebarsjs.com/guide/ | Handlebars Guide}
|
|
7
|
+
* @example
|
|
8
|
+
* ```js
|
|
9
|
+
* // View configuration in config/view.js
|
|
10
|
+
* export default {
|
|
11
|
+
* views_dir: 'views',
|
|
12
|
+
* partials_dir: 'views/partials',
|
|
13
|
+
* layouts_dir: 'views/layout',
|
|
14
|
+
* default_layout: 'views/layout/default',
|
|
15
|
+
* extension: '.hbs',
|
|
16
|
+
* beforeRender: (req, res) => {
|
|
17
|
+
* res.locals.extra = 'Extra';
|
|
18
|
+
* }
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Mount Handlebars view engine on Express application.
|
|
24
|
+
* Configures template engine, helpers, layouts, and partials.
|
|
25
|
+
* @param {express.Express} app Express application instance
|
|
26
|
+
* @returns {Promise<void>}
|
|
27
|
+
* @example
|
|
28
|
+
* ```js
|
|
29
|
+
* // This method is called automatically by express-sweet.mount()
|
|
30
|
+
* import viewEngine from './middlewares/viewEngine';
|
|
31
|
+
*
|
|
32
|
+
* await viewEngine(app);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export default function viewEngine(app: express.Express): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Mount middleware to be executed just before rendering the view.
|
|
38
|
+
* Allows setting view variables and executing beforeRender hooks from configuration.
|
|
39
|
+
* @param {express.Express} app Express application instance
|
|
40
|
+
* @returns {Promise<void>}
|
|
41
|
+
* @example
|
|
42
|
+
* ```js
|
|
43
|
+
* // Called automatically after authentication middleware setup
|
|
44
|
+
* import {mountBeforeRender} from './middlewares/viewEngine';
|
|
45
|
+
*
|
|
46
|
+
* await mountBeforeRender(app);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function mountBeforeRender(app: express.Express): Promise<void>;
|