express-sweet 3.0.1 → 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.
Files changed (40) hide show
  1. package/CHANGELOG.md +719 -706
  2. package/README.md +206 -57
  3. package/configuration_sample/cjs/config.js +5 -4
  4. package/configuration_sample/cjs/upload.js +91 -0
  5. package/configuration_sample/esm/config.js +5 -4
  6. package/configuration_sample/esm/upload.js +91 -0
  7. package/dist/build.cjs +41 -35
  8. package/dist/build.mjs +41 -35
  9. package/package.json +6 -4
  10. package/types/database/Model.d.ts +2 -2
  11. package/types/handlebars_helpers/object.d.ts +2 -2
  12. package/types/index.d.ts +1 -43
  13. package/types/interfaces/{BasicConfig.d.ts → AppConfig.d.ts} +8 -7
  14. package/types/interfaces/DatabaseConfig.d.ts +1 -0
  15. package/types/interfaces/UploadConfig.d.ts +73 -0
  16. package/types/interfaces/index.d.ts +6 -2
  17. package/types/middlewares/corsPolicy.d.ts +28 -0
  18. package/types/middlewares/{Environment.d.ts → envLoader.d.ts} +13 -15
  19. package/types/middlewares/errorHandler.d.ts +32 -0
  20. package/types/middlewares/globalVars.d.ts +25 -0
  21. package/types/middlewares/index.d.ts +19 -11
  22. package/types/middlewares/localVars.d.ts +44 -0
  23. package/types/middlewares/passportAuth.d.ts +15 -0
  24. package/types/middlewares/requestParser.d.ts +27 -0
  25. package/types/{routing/Router.d.ts → middlewares/routeMapper.d.ts} +14 -16
  26. package/types/middlewares/viewEngine.d.ts +49 -0
  27. package/types/mount.d.ts +33 -1
  28. package/types/utils/Types.d.ts +17 -11
  29. package/types/utils/index.d.ts +7 -3
  30. package/types/utils/loadAppConfig.d.ts +17 -0
  31. package/types/utils/loadUploadConfig.d.ts +20 -0
  32. package/types/middlewares/Authentication.d.ts +0 -52
  33. package/types/middlewares/CORS.d.ts +0 -30
  34. package/types/middlewares/ErrorHandler.d.ts +0 -34
  35. package/types/middlewares/Global.d.ts +0 -27
  36. package/types/middlewares/Http.d.ts +0 -29
  37. package/types/middlewares/Local.d.ts +0 -42
  38. package/types/middlewares/View.d.ts +0 -49
  39. package/types/routing/index.d.ts +0 -37
  40. package/types/utils/loadBasicConfig.d.ts +0 -17
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: Global → Environment → Database → HTTP → View → CORS → Local → Authentication → Router → Error Handler.
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,24 @@ 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
52
  declare const _default: (app: express.Express) => Promise<void>;
21
53
  export default _default;
@@ -1,23 +1,29 @@
1
1
  /**
2
- * Type judgment processing.
2
+ * Type checking utility class.
3
+ * Provides static methods for runtime type validation.
3
4
  */
4
5
  export default class Types {
5
6
  /**
6
- * Returns true for asynchronous functions, false otherwise.
7
- * @param {Function} value.
8
- * @returns {boolean}
7
+ * Check if a function is async (returns a Promise).
8
+ * @param {Function} value The function to check
9
+ * @returns {boolean} True if async function, false otherwise
9
10
  * @example
10
11
  * ```js
11
- * Types.isAsyncFunction(async () => {});// true
12
- * Types.isAsyncFunction(() => {});// false
12
+ * Types.isAsyncFunction(async () => {}); // true
13
+ * Types.isAsyncFunction(() => {}); // false
13
14
  * ```
14
15
  */
15
16
  static isAsyncFunction(value: Function): boolean;
16
17
  /**
17
- * Return true if `val` is a non-empty string.
18
- * @param {any} `val` The value to check
19
- * @returns {Boolean}
20
- * @api public
21
- */
18
+ * Check if a value is a non-empty string.
19
+ * @param {any} value The value to check
20
+ * @returns {boolean} True if non-empty string, false otherwise
21
+ * @example
22
+ * ```js
23
+ * Types.isString('hello'); // true
24
+ * Types.isString(''); // false
25
+ * Types.isString(123); // false
26
+ * ```
27
+ */
22
28
  static isString(value: any): boolean;
23
29
  }
@@ -23,7 +23,7 @@
23
23
  * // Using configuration loaders
24
24
  * import * as expx from 'express-sweet';
25
25
  *
26
- * const basicConfig = await expx.utils.loadBasicConfig();
26
+ * const appConfig = await expx.utils.loadAppConfig();
27
27
  * const dbConfig = await expx.utils.loadDatabaseConfig();
28
28
  * const authConfig = await expx.utils.loadAuthenticationConfig();
29
29
  * const loggingConfig = await expx.utils.loadLoggingConfig();
@@ -54,9 +54,9 @@ export { default as isString } from './isString';
54
54
  */
55
55
  export { default as loadAuthenticationConfig } from './loadAuthenticationConfig';
56
56
  /**
57
- * Load basic application configuration from config/config.js
57
+ * Load application configuration from config/config.js
58
58
  */
59
- export { default as loadBasicConfig } from './loadBasicConfig';
59
+ export { default as loadAppConfig } from './loadAppConfig';
60
60
  /**
61
61
  * Load database configuration from config/database.js
62
62
  */
@@ -69,3 +69,7 @@ export { default as loadViewConfig } from './loadViewConfig';
69
69
  * Load logging configuration from config/logging.js
70
70
  */
71
71
  export { default as loadLoggingConfig } from './loadLoggingConfig';
72
+ /**
73
+ * Load upload configuration from config/upload.js
74
+ */
75
+ export { default as loadUploadConfig } from './loadUploadConfig';
@@ -0,0 +1,17 @@
1
+ import AppConfig from '~/interfaces/AppConfig';
2
+ /**
3
+ * Load application configuration from config/config.js file.
4
+ * Returns default configuration if config file doesn't exist.
5
+ * @returns {Promise<AppConfig>} The loaded application configuration with defaults
6
+ * @example
7
+ * ```js
8
+ * import loadAppConfig from '~/utils/loadAppConfig';
9
+ *
10
+ * const config = await loadAppConfig();
11
+ * console.log(config.cors_enabled); // false (default)
12
+ * console.log(config.max_body_size); // '100kb' (default)
13
+ * console.log(config.router_dir); // '/path/to/app/routes' (default)
14
+ * ```
15
+ */
16
+ declare const _default: () => Promise<AppConfig>;
17
+ export default _default;
@@ -0,0 +1,20 @@
1
+ import type UploadConfig from '~/interfaces/UploadConfig';
2
+ /**
3
+ * Load upload configuration from config/upload.js file.
4
+ * Returns default configuration if config file doesn't exist.
5
+ *
6
+ * @returns {Promise<UploadConfig>} The loaded upload configuration with defaults
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import loadUploadConfig from '~/utils/loadUploadConfig';
11
+ *
12
+ * const config = await loadUploadConfig();
13
+ * if (config.enabled) {
14
+ * const middleware = config.resolve_middleware(req, multer);
15
+ * // Apply middleware...
16
+ * }
17
+ * ```
18
+ */
19
+ declare const _default: () => Promise<UploadConfig>;
20
+ export default _default;
@@ -1,52 +0,0 @@
1
- import express from 'express';
2
- /**
3
- * Incorporate user authentication into your application using Passport.js middleware.
4
- * Provides username/password authentication with session management and route protection.
5
- * If an unauthenticated user makes a request to a URL that allows access only if authenticated,
6
- * the user will be redirected to the page specified by "failure_redirect".
7
- * If that access is asynchronous, a 401 error is returned.
8
- *
9
- * @see {@link https://www.passportjs.org/ | Passport.js}
10
- * @example
11
- * ```js
12
- * // Authentication configuration in config/authentication.js
13
- * export default {
14
- * enabled: true,
15
- * username: 'email',
16
- * password: 'password',
17
- * success_redirect: '/',
18
- * failure_redirect: '/login',
19
- * authenticate_user: async (username, password, req) => {
20
- * const UserModel = require('../models/UserModel');
21
- * return UserModel.findOne({
22
- * where: { email: username, password },
23
- * raw: true
24
- * });
25
- * },
26
- * subscribe_user: async (id) => {
27
- * const UserModel = require('../models/UserModel');
28
- * return UserModel.findOne({
29
- * where: { id },
30
- * raw: true
31
- * });
32
- * }
33
- * }
34
- * ```
35
- */
36
- export default class Authentication {
37
- #private;
38
- /**
39
- * Mount authentication middleware on Express application.
40
- * Sets up Passport.js with local strategy, session management, and route protection.
41
- * @param {express.Express} app Express application instance
42
- * @returns {Promise<void>}
43
- * @example
44
- * ```js
45
- * // This method is called automatically by express-sweet.mount()
46
- * import Authentication from './middlewares/Authentication';
47
- *
48
- * await Authentication.mount(app);
49
- * ```
50
- */
51
- static mount(app: express.Express): Promise<void>;
52
- }
@@ -1,30 +0,0 @@
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
- export default class CORS {
16
- /**
17
- * Mount CORS middleware on Express application.
18
- * Sets appropriate CORS headers to allow cross-origin requests.
19
- * @param {express.Express} app Express application instance
20
- * @returns {Promise<void>}
21
- * @example
22
- * ```js
23
- * // This method is called automatically by express-sweet.mount()
24
- * import CORS from './middlewares/CORS';
25
- *
26
- * await CORS.mount(app);
27
- * ```
28
- */
29
- static mount(app: express.Express): Promise<void>;
30
- }
@@ -1,34 +0,0 @@
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
- export default class ErrorHandler {
20
- /**
21
- * Mount error handling middleware on Express application.
22
- * Sets up 404 error catching and custom error handling hooks.
23
- * @param {express.Express} app Express application instance
24
- * @returns {Promise<void>}
25
- * @example
26
- * ```js
27
- * // This method is called automatically by express-sweet.mount()
28
- * import ErrorHandler from './middlewares/ErrorHandler';
29
- *
30
- * await ErrorHandler.mount(app);
31
- * ```
32
- */
33
- static mount(app: express.Express): Promise<void>;
34
- }
@@ -1,27 +0,0 @@
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
- export default class Global {
15
- /**
16
- * Mount global variables on application.
17
- * Sets up global.APP_DIR for access throughout the application.
18
- * @example
19
- * ```js
20
- * // This method is called automatically by express-sweet.mount()
21
- * import Global from './middlewares/Global';
22
- *
23
- * Global.mount();
24
- * ```
25
- */
26
- static mount(): void;
27
- }
@@ -1,29 +0,0 @@
1
- import express from 'express';
2
- /**
3
- * HTTP middleware setup for Express application.
4
- * Configures JSON/URL-encoded parsing, file uploads, 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
- export default class Http {
15
- /**
16
- * Mount HTTP middleware on Express application.
17
- * Sets up request parsing, file uploads, cookies, static files, and logging.
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 Http from './middlewares/Http';
24
- *
25
- * await Http.mount(app);
26
- * ```
27
- */
28
- static mount(app: express.Express): Promise<void>;
29
- }
@@ -1,42 +0,0 @@
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 (can be rewritten via config hook)
8
- * - currentPath: The current request path
9
- *
10
- * @example
11
- * ```handlebars
12
- * // Using in Handlebars templates
13
- * <a href="{{baseUrl}}/home">Home</a>
14
- * {{#if (eq currentPath '/dashboard')}}Active{{/if}}
15
- * ```
16
- *
17
- * @example
18
- * // Base URL rewriting in config/config.js
19
- * ```js
20
- * export default {
21
- * rewrite_base_url: baseUrl => {
22
- * return `${baseUrl}/admin`;
23
- * }
24
- * };
25
- * ```
26
- */
27
- export default class Local {
28
- /**
29
- * Mount local variables middleware on Express application.
30
- * Sets up baseUrl and currentPath variables for use in views.
31
- * @param {express.Express} app Express application instance
32
- * @returns {Promise<void>}
33
- * @example
34
- * ```js
35
- * // This method is called automatically by express-sweet.mount()
36
- * import Local from './middlewares/Local';
37
- *
38
- * await Local.mount(app);
39
- * ```
40
- */
41
- static mount(app: express.Express): Promise<void>;
42
- }
@@ -1,49 +0,0 @@
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
- export default class View {
23
- /**
24
- * Mount Handlebars view engine on Express application.
25
- * Configures template engine, helpers, layouts, and partials.
26
- * @param {express.Express} app Express application instance
27
- * @returns {Promise<void>}
28
- * @example
29
- * ```js
30
- * // This method is called automatically by express-sweet.mount()
31
- * import View from './middlewares/View';
32
- *
33
- * await View.mount(app);
34
- * ```
35
- */
36
- static mount(app: express.Express): Promise<void>;
37
- /**
38
- * Mount middleware to be executed just before rendering the view.
39
- * Allows setting view variables and executing beforeRender hooks from configuration.
40
- * @param {express.Express} app Express application instance
41
- * @returns {Promise<void>}
42
- * @example
43
- * ```js
44
- * // Called automatically after authentication middleware setup
45
- * await View.mountBeforeRender(app);
46
- * ```
47
- */
48
- static mountBeforeRender(app: express.Express): Promise<void>;
49
- }
@@ -1,37 +0,0 @@
1
- /**
2
- * File-based routing system for Express Sweet.
3
- *
4
- * Provides automatic route mapping based on file structure in the routes directory.
5
- * Routes are automatically mounted based on their file path and name.
6
- *
7
- * @module routing
8
- * @example
9
- * ```bash
10
- * // File-based routing structure
11
- * routes/
12
- * ├── index.js // responds to GET /
13
- * ├── users.js // responds to GET /users
14
- * ├── api/
15
- * │ ├── auth.js // responds to GET /api/auth
16
- * │ └── users.js // responds to GET /api/users
17
- * └── admin/
18
- * └── dashboard.js // responds to GET /admin/dashboard
19
- * ```
20
- *
21
- * @example
22
- * ```js
23
- * // Basic route file (routes/users.js)
24
- * import {Router} from 'express';
25
- *
26
- * const router = Router();
27
- * router.get('/', (req, res) => {
28
- * res.send('Users list');
29
- * });
30
- *
31
- * export default router;
32
- * ```
33
- */
34
- /**
35
- * File-based routing class that automatically maps route files to URL endpoints
36
- */
37
- export { default as Router } from './Router';
@@ -1,17 +0,0 @@
1
- import BasicConfig from '~/interfaces/BasicConfig';
2
- /**
3
- * Load basic application configuration from config/config.js file.
4
- * Returns default configuration if config file doesn't exist.
5
- * @returns {Promise<BasicConfig>} The loaded basic configuration with defaults
6
- * @example
7
- * ```js
8
- * import loadBasicConfig from '~/utils/loadBasicConfig';
9
- *
10
- * const config = await loadBasicConfig();
11
- * console.log(config.cors_enabled); // false (default)
12
- * console.log(config.max_body_size); // '100kb' (default)
13
- * console.log(config.router_dir); // '/path/to/app/routes' (default)
14
- * ```
15
- */
16
- declare const _default: () => Promise<BasicConfig>;
17
- export default _default;