millas 0.2.12-beta → 0.2.12-beta-1

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 (52) hide show
  1. package/package.json +3 -16
  2. package/src/auth/Auth.js +13 -8
  3. package/src/auth/AuthController.js +3 -1
  4. package/src/auth/AuthUser.js +98 -0
  5. package/src/cli.js +1 -1
  6. package/src/commands/serve.js +81 -110
  7. package/src/container/AppInitializer.js +158 -0
  8. package/src/container/Application.js +278 -253
  9. package/src/container/HttpServer.js +156 -0
  10. package/src/container/MillasApp.js +23 -280
  11. package/src/container/MillasConfig.js +163 -0
  12. package/src/core/auth.js +9 -0
  13. package/src/core/db.js +8 -0
  14. package/src/core/foundation.js +67 -0
  15. package/src/core/http.js +11 -0
  16. package/src/core/mail.js +6 -0
  17. package/src/core/queue.js +7 -0
  18. package/src/core/validation.js +29 -0
  19. package/src/facades/Admin.js +1 -1
  20. package/src/facades/Auth.js +22 -39
  21. package/src/facades/Cache.js +21 -10
  22. package/src/facades/Database.js +1 -1
  23. package/src/facades/Events.js +18 -17
  24. package/src/facades/Facade.js +197 -0
  25. package/src/facades/Http.js +42 -45
  26. package/src/facades/Log.js +25 -49
  27. package/src/facades/Mail.js +27 -32
  28. package/src/facades/Queue.js +22 -15
  29. package/src/facades/Storage.js +18 -10
  30. package/src/facades/Url.js +53 -0
  31. package/src/http/HttpClient.js +673 -0
  32. package/src/http/ResponseDispatcher.js +18 -111
  33. package/src/http/UrlGenerator.js +375 -0
  34. package/src/http/WelcomePage.js +273 -0
  35. package/src/http/adapters/ExpressAdapter.js +315 -0
  36. package/src/http/adapters/HttpAdapter.js +168 -0
  37. package/src/http/adapters/index.js +9 -0
  38. package/src/index.js +5 -144
  39. package/src/logger/formatters/PrettyFormatter.js +15 -5
  40. package/src/logger/internal.js +2 -2
  41. package/src/logger/patchConsole.js +91 -81
  42. package/src/middleware/MiddlewareRegistry.js +62 -82
  43. package/src/orm/migration/ModelInspector.js +339 -340
  44. package/src/providers/AuthServiceProvider.js +9 -5
  45. package/src/providers/CacheStorageServiceProvider.js +3 -1
  46. package/src/providers/EventServiceProvider.js +2 -1
  47. package/src/providers/LogServiceProvider.js +3 -2
  48. package/src/providers/MailServiceProvider.js +3 -2
  49. package/src/providers/QueueServiceProvider.js +3 -2
  50. package/src/router/Router.js +119 -152
  51. package/src/scaffold/templates.js +8 -7
  52. package/src/facades/Validation.js +0 -69
@@ -1,54 +1,68 @@
1
1
  'use strict';
2
2
 
3
- const MillasRequest = require('../http/MillasRequest');
4
- const MillasResponse = require('../http/MillasResponse');
5
- const ResponseDispatcher = require('../http/ResponseDispatcher');
6
- const RequestContext = require('../http/RequestContext');
7
-
8
3
  /**
9
4
  * MiddlewareRegistry
10
5
  *
11
- * Maps string aliases → middleware handler classes or functions.
12
- * Resolves them into Express-compatible functions that wrap with MillasRequest.
6
+ * Maps string aliases → Millas middleware classes or instances.
7
+ * Resolution produces adapter-native handler functions via the adapter,
8
+ * so this class has zero knowledge of Express (or any HTTP engine).
9
+ *
10
+ * The adapter is injected at resolution time (not construction time)
11
+ * so the registry can be built before the adapter exists.
13
12
  */
14
13
  class MiddlewareRegistry {
15
- constructor(container = null) {
16
- this._map = {};
17
- this._container = container;
14
+ constructor() {
15
+ this._map = {};
18
16
  }
19
17
 
18
+ /**
19
+ * Register a middleware alias.
20
+ *
21
+ * registry.register('auth', AuthMiddleware)
22
+ * registry.register('throttle', new ThrottleMiddleware({ max: 60 }))
23
+ */
20
24
  register(alias, handler) {
21
25
  this._map[alias] = handler;
22
26
  return this;
23
27
  }
24
28
 
25
29
  /**
26
- * Resolve a middleware alias or function into an Express-compatible handler.
27
- *
28
- * Millas middleware (class with handle(req, next)):
29
- * - Receives MillasRequest
30
- * - Returns MillasResponse or calls next()
31
- * - Kernel dispatches the MillasResponse if returned
30
+ * Resolve a middleware alias or class/instance into an adapter-native handler.
32
31
  *
33
- * Raw Express functions (legacy/escape hatch):
34
- * - Passed through as-is
32
+ * @param {string|Function|object} aliasOrFn
33
+ * @param {import('../http/adapters/HttpAdapter')} adapter
34
+ * @param {object|null} container
35
+ * @returns {Function} adapter-native handler
35
36
  */
36
- resolve(aliasOrFn) {
37
- // Raw function check if it's a Millas middleware class or legacy Express fn
38
- if (typeof aliasOrFn === 'function') {
39
- return this._wrapHandler(aliasOrFn);
40
- }
37
+ resolve(aliasOrFn, adapter, container = null) {
38
+ const Handler = typeof aliasOrFn === 'string'
39
+ ? this._map[aliasOrFn]
40
+ : aliasOrFn;
41
41
 
42
- const Handler = this._map[aliasOrFn];
43
42
  if (!Handler) {
44
43
  throw new Error(`Middleware "${aliasOrFn}" is not registered.`);
45
44
  }
46
45
 
47
- return this._wrapHandler(Handler);
46
+ return this._wrap(Handler, adapter, container);
47
+ }
48
+
49
+ /**
50
+ * Resolve all aliases in a list.
51
+ */
52
+ resolveAll(list = [], adapter, container = null) {
53
+ return list.map(m => this.resolve(m, adapter, container));
48
54
  }
49
55
 
50
- resolveAll(list = []) {
51
- return list.map(m => this.resolve(m));
56
+ /**
57
+ * Return a no-op passthrough handler for the given adapter.
58
+ * Used when a middleware alias is missing but should not crash the app.
59
+ */
60
+ resolvePassthrough(adapter) {
61
+ // Adapter-agnostic: return a function matching the native signature
62
+ // by asking the adapter to wrap a no-op middleware instance.
63
+ return adapter.wrapMiddleware({
64
+ handle: (_ctx, next) => next(),
65
+ }, null);
52
66
  }
53
67
 
54
68
  has(alias) {
@@ -56,71 +70,37 @@ class MiddlewareRegistry {
56
70
  }
57
71
 
58
72
  all() {
59
- return { ...this._map }; }
73
+ return { ...this._map };
74
+ }
60
75
 
61
- // ─── Internal ──────────────────────────────────────────────────────────────
76
+ // ── Internal ────────────────────────────────────────────────────────────────
62
77
 
63
- _wrapHandler(Handler) {
64
- // Pre-instantiated Millas middleware object: { handle(req, next) }
65
- if (typeof Handler === 'object' && Handler !== null &&
66
- typeof Handler.handle === 'function') {
67
- return this._buildMillasWrapper(Handler);
78
+ _wrap(Handler, adapter, container) {
79
+ // Pre-instantiated Millas middleware object with handle()
80
+ if (
81
+ typeof Handler === 'object' &&
82
+ Handler !== null &&
83
+ typeof Handler.handle === 'function'
84
+ ) {
85
+ return adapter.wrapMiddleware(Handler, container);
68
86
  }
69
87
 
70
- // Millas middleware class (has handle on prototype, not Express signature)
71
- if (typeof Handler === 'function' &&
72
- Handler.prototype &&
73
- typeof Handler.prototype.handle === 'function') {
74
- const instance = new Handler();
75
- return this._buildMillasWrapper(instance);
88
+ // Millas middleware class (handle on prototype)
89
+ if (
90
+ typeof Handler === 'function' &&
91
+ Handler.prototype &&
92
+ typeof Handler.prototype.handle === 'function'
93
+ ) {
94
+ return adapter.wrapMiddleware(new Handler(), container);
76
95
  }
77
96
 
78
- // Legacy raw Express function: (req, res, next) => void
79
- // Pass through unchanged — developers using old style still work
97
+ // Raw adapter-native function pass through as-is (escape hatch)
80
98
  if (typeof Handler === 'function') {
81
99
  return Handler;
82
100
  }
83
101
 
84
- throw new Error(`Middleware must be a function or a class with handle().`);
85
- }
86
-
87
- /**
88
- * Build an Express-compatible function from a Millas middleware instance.
89
- *
90
- * The middleware's handle(req, next) is called with a MillasRequest.
91
- * If it returns a MillasResponse, that response is dispatched immediately.
92
- * If it calls next(), Express continues down the chain.
93
- */
94
- _buildMillasWrapper(instance) {
95
- const container = this._container;
96
-
97
- return (expressReq, expressRes, expressNext) => {
98
- const millaReq = new MillasRequest(expressReq);
99
- const ctx = new RequestContext(millaReq, container);
100
-
101
- const next = () => {
102
- expressNext();
103
- return undefined;
104
- };
105
-
106
- new Promise((resolve, reject) => {
107
- try {
108
- resolve(instance.handle(ctx, next));
109
- } catch (err) {
110
- reject(err);
111
- }
112
- })
113
- .then(value => {
114
- if (value !== undefined && value !== null && !expressRes.headersSent) {
115
- const response = MillasResponse.isResponse(value)
116
- ? value
117
- : ResponseDispatcher.autoWrap(value);
118
- ResponseDispatcher.dispatch(response, expressRes);
119
- }
120
- })
121
- .catch(expressNext);
122
- };
102
+ throw new Error('Middleware must be a function or a class with handle().');
123
103
  }
124
104
  }
125
105
 
126
- module.exports = MiddlewareRegistry;
106
+ module.exports = MiddlewareRegistry;