express-sweet 1.0.34 → 1.0.36

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 (34) hide show
  1. package/CHANGELOG.md +83 -1
  2. package/dist/build.common.js +12 -12
  3. package/dist/build.esm.js +12 -12
  4. package/docs/index.html +63 -39
  5. package/package.json +1 -1
  6. package/types/database/index.d.ts +3 -3
  7. package/types/exceptions/index.d.ts +5 -5
  8. package/types/handlebars_helpers/index.d.ts +6 -6
  9. package/types/index.d.ts +7 -7
  10. package/types/interfaces/{AuthenticationOptions.d.ts → AuthenticationConfig.d.ts} +13 -16
  11. package/types/interfaces/BasicConfig.d.ts +74 -0
  12. package/types/interfaces/FaceMatch.d.ts +1 -1
  13. package/types/interfaces/index.d.ts +9 -9
  14. package/types/middlewares/CORS.d.ts +0 -1
  15. package/types/middlewares/Environment.d.ts +0 -1
  16. package/types/middlewares/{ErrorHandling.d.ts → ErrorHandler.d.ts} +0 -1
  17. package/types/middlewares/Http.d.ts +0 -1
  18. package/types/middlewares/Local.d.ts +0 -1
  19. package/types/middlewares/View.d.ts +0 -1
  20. package/types/middlewares/index.d.ts +8 -8
  21. package/types/routing/Router.d.ts +0 -1
  22. package/types/routing/index.d.ts +1 -1
  23. package/types/services/Authentication.d.ts +2 -2
  24. package/types/services/index.d.ts +3 -3
  25. package/types/utils/index.d.ts +8 -3
  26. package/types/utils/isAsyncFunction.d.ts +10 -10
  27. package/types/utils/isFunction.d.ts +13 -0
  28. package/types/utils/loadAuthenticationConfig.d.ts +8 -0
  29. package/types/utils/loadBasicConfig.d.ts +8 -0
  30. package/types/utils/loadDatabaseConfig.d.ts +8 -0
  31. package/types/utils/loadViewConfig.d.ts +8 -0
  32. package/types/interfaces/Config.d.ts +0 -66
  33. /package/types/interfaces/{DatabaseOptions.d.ts → DatabaseConfig.d.ts} +0 -0
  34. /package/types/interfaces/{ViewOptions.d.ts → ViewConfig.d.ts} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,10 +1,90 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [1.0.36] - 2023/7/12
5
+ ### Changed
6
+ - Moved the Ajax determination option (is_ajax) in the authentication configuration (config/authentication) to the basic configuration (config/config).
7
+
8
+ config/config.js:
9
+ ```js
10
+ /**
11
+ * How to determine if it is an ajax request.
12
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
13
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
14
+ *
15
+ * @type {(req: express.Request) => boolean}
16
+ * @example
17
+ * is_ajax: req => {
18
+ * // If the request URL begins with /api, it is assumed to be Ajax.
19
+ * return /^\/api/.test(req.path);
20
+ * // return !!req.xhr;
21
+ * }
22
+ */
23
+ is_ajax: req => !!req.xhr,
24
+ ```
25
+ - The error handle option (error_handler) in the basic configuration (config/config.js) has been removed and an option to hook error handles (hook_handle_error) added instead.
26
+
27
+ config/config.js:
28
+ ```js
29
+ /**
30
+ * Hooks the default behavior on request errors.
31
+ * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
32
+ *
33
+ * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
34
+ * @example
35
+ * hook_handle_error: (err, req, res, next) => {
36
+ * if (err.status === 404)
37
+ * // If the URL cannot be found, a 404 error screen (views/error-404.hbs) is displayed.
38
+ * res.render('error-404');
39
+ * else
40
+ * // For other errors, unknown error screen (views/error-unknown.hbs) is displayed.
41
+ * res.render('error-unknown');
42
+ * },
43
+ */
44
+ hook_handle_error: undefined,
45
+ ```
46
+
47
+ ## [1.0.35] - 2023/7/11
48
+ ### Changed
49
+ - The URL to redirect to when login fails (failure_redirect) option in the authentication configuration (config/authentication.js) can now be defined with a function.
50
+
51
+ config/authentication.js:
52
+ ```js
53
+ // Set the URL to redirect to in case of login failure as a string.
54
+ failure_redirect: '/login',
55
+
56
+ // Dynamically set the url to redirect to on login failure.
57
+ failure_redirect: (req, res) => {
58
+ // If the role stored in the cookie is admin, redirect to the admin login screen.
59
+ return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
60
+ },
61
+ ```
62
+ - The arguments of the failureRedirect method of the authentication service class (services/Authentication) have changed.
63
+ The argument to the Authentication.failureRedirect method used to be just express.Response, but now it requires express.Request and express.Response.
64
+
65
+ Example of login routes:
66
+ ```js
67
+ import {Router} from 'express';
68
+ import * as sweet from 'express-sweet';
69
+ const router = Router();
70
+ const Authentication = sweet.services.Authentication;
71
+
72
+ router.post('/login', async (req, res, next) => {
73
+ const isAuth = await Authentication.authenticate(req, res, next);
74
+ if (isAuth)
75
+ Authentication.successRedirect(res);
76
+ else
77
+ Authentication.failureRedirect(req, res);
78
+ });
79
+ export default router;
80
+ ```
81
+
4
82
  ## [1.0.34] - 2023/7/1
83
+ ### Changed
5
84
  - Changed the response status code from 403 to 401 when asynchronous communication requiring authentication fails to authenticate.
6
85
 
7
86
  ## [1.0.33] - 2023/7/1
87
+ ### Added
8
88
  - Added regular expression comparison view helper.
9
89
  ```html
10
90
  {{!-- results in: true --}}
@@ -588,4 +668,6 @@ All notable changes to this project will be documented in this file.
588
668
  [1.0.31]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.30...v1.0.31
589
669
  [1.0.32]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.31...v1.0.32
590
670
  [1.0.33]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.32...v1.0.33
591
- [1.0.34]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.33...v1.0.34
671
+ [1.0.34]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.33...v1.0.34
672
+ [1.0.35]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.34...v1.0.35
673
+ [1.0.36]: https://github.com/takuya-motoshima/express-sweet/compare/v1.0.35...v1.0.36