@noony-serverless/core 0.6.0 → 0.7.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.
@@ -23,6 +23,30 @@ export interface BaseMiddleware<T = unknown, U = unknown> {
23
23
  * process a request/response flow either before the main handler (via `before`),
24
24
  * after the main handler (via `after`), or handle errors (via `onError`).
25
25
  *
26
+ * @example Type-safe handler with explicit types
27
+ * interface LoginRequest {
28
+ * email: string;
29
+ * password: string;
30
+ * }
31
+ *
32
+ * interface AuthUser extends BaseAuthenticatedUser {
33
+ * role: 'admin' | 'user';
34
+ * }
35
+ *
36
+ * const handler = new Handler<LoginRequest, AuthUser>()
37
+ * .use(new ErrorHandlerMiddleware<LoginRequest, AuthUser>())
38
+ * .use(new BodyValidationMiddleware<LoginRequest, AuthUser>(loginSchema))
39
+ * .handle(loginController); // ✅ No 'as any' needed
40
+ *
41
+ * @example Type inference with createTypedHandler
42
+ * async function loginController(context: Context<LoginRequest, AuthUser>) {
43
+ * // controller logic
44
+ * }
45
+ *
46
+ * const handler = createTypedHandler(loginController)
47
+ * .use(new ErrorHandlerMiddleware()) // Types inferred
48
+ * .handle(loginController); // ✅ No 'as any' needed
49
+ *
26
50
  * interface MessagePayload {
27
51
  * action: string;
28
52
  * data: Record<string, unknown>;
@@ -45,7 +69,7 @@ export declare class Handler<T = unknown, U = unknown> {
45
69
  private errorMiddlewares;
46
70
  private middlewaresPrecomputed;
47
71
  static use<T = unknown, U = unknown>(middleware: BaseMiddleware<T, U>): Handler<T, U>;
48
- use<NewT = T, NewU = U>(middleware: BaseMiddleware<NewT, NewU>): Handler<NewT, NewU>;
72
+ use(middleware: BaseMiddleware<T, U>): Handler<T, U>;
49
73
  handle(handler: (context: Context<T, U>) => Promise<void | unknown>): Handler<T, U>;
50
74
  /**
51
75
  * Performance optimization: Pre-compute middleware arrays to avoid runtime array operations
@@ -69,4 +93,30 @@ export declare class Handler<T = unknown, U = unknown> {
69
93
  */
70
94
  executeGeneric(req: GenericRequest<T>, res: GenericResponse): Promise<void>;
71
95
  }
96
+ /**
97
+ * Helper to infer types automatically from the controller function.
98
+ *
99
+ * This helper is a permanent feature that improves Developer Experience (DX).
100
+ * It eliminates the need to write explicit generic type parameters when they
101
+ * are already defined in the controller signature.
102
+ *
103
+ * @example
104
+ * // Controller with explicit types
105
+ * async function loginController(context: Context<LoginRequest, AuthUser>) {
106
+ * const { email, password } = context.req.validatedBody!;
107
+ * // ... authentication logic
108
+ * }
109
+ *
110
+ * // Helper infers LoginRequest and AuthUser automatically
111
+ * const handler = createTypedHandler(loginController)
112
+ * .use(new ErrorHandlerMiddleware()) // Types inferred
113
+ * .use(new BodyValidationMiddleware(loginSchema))
114
+ * .handle(loginController); // ✅ Types match perfectly, no 'as any' needed
115
+ *
116
+ * @template T - The request body type (inferred from controller's Context<T, U>)
117
+ * @template U - The user type (inferred from controller's Context<T, U>)
118
+ * @param controller - The controller function with explicit Context<T, U> types
119
+ * @returns A new Handler instance with types inferred from the controller
120
+ */
121
+ export declare function createTypedHandler<T, U>(_controller: (context: Context<T, U>) => Promise<void | unknown>): Handler<T, U>;
72
122
  //# sourceMappingURL=handler.d.ts.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Handler = void 0;
4
+ exports.createTypedHandler = createTypedHandler;
4
5
  // Container import removed - now using containerPool for performance
5
6
  const core_1 = require("./core");
6
7
  const containerPool_1 = require("./containerPool");
@@ -12,6 +13,30 @@ const containerPool_1 = require("./containerPool");
12
13
  * process a request/response flow either before the main handler (via `before`),
13
14
  * after the main handler (via `after`), or handle errors (via `onError`).
14
15
  *
16
+ * @example Type-safe handler with explicit types
17
+ * interface LoginRequest {
18
+ * email: string;
19
+ * password: string;
20
+ * }
21
+ *
22
+ * interface AuthUser extends BaseAuthenticatedUser {
23
+ * role: 'admin' | 'user';
24
+ * }
25
+ *
26
+ * const handler = new Handler<LoginRequest, AuthUser>()
27
+ * .use(new ErrorHandlerMiddleware<LoginRequest, AuthUser>())
28
+ * .use(new BodyValidationMiddleware<LoginRequest, AuthUser>(loginSchema))
29
+ * .handle(loginController); // ✅ No 'as any' needed
30
+ *
31
+ * @example Type inference with createTypedHandler
32
+ * async function loginController(context: Context<LoginRequest, AuthUser>) {
33
+ * // controller logic
34
+ * }
35
+ *
36
+ * const handler = createTypedHandler(loginController)
37
+ * .use(new ErrorHandlerMiddleware()) // Types inferred
38
+ * .handle(loginController); // ✅ No 'as any' needed
39
+ *
15
40
  * interface MessagePayload {
16
41
  * action: string;
17
42
  * data: Record<string, unknown>;
@@ -40,12 +65,8 @@ class Handler {
40
65
  return handler;
41
66
  }
42
67
  use(middleware) {
43
- const handler = new Handler();
44
- handler.baseMiddlewares = [
45
- ...this.baseMiddlewares,
46
- middleware,
47
- ];
48
- return handler;
68
+ this.baseMiddlewares.push(middleware);
69
+ return this;
49
70
  }
50
71
  handle(handler) {
51
72
  this.handler = handler;
@@ -152,4 +173,32 @@ class Handler {
152
173
  }
153
174
  }
154
175
  exports.Handler = Handler;
176
+ /**
177
+ * Helper to infer types automatically from the controller function.
178
+ *
179
+ * This helper is a permanent feature that improves Developer Experience (DX).
180
+ * It eliminates the need to write explicit generic type parameters when they
181
+ * are already defined in the controller signature.
182
+ *
183
+ * @example
184
+ * // Controller with explicit types
185
+ * async function loginController(context: Context<LoginRequest, AuthUser>) {
186
+ * const { email, password } = context.req.validatedBody!;
187
+ * // ... authentication logic
188
+ * }
189
+ *
190
+ * // Helper infers LoginRequest and AuthUser automatically
191
+ * const handler = createTypedHandler(loginController)
192
+ * .use(new ErrorHandlerMiddleware()) // Types inferred
193
+ * .use(new BodyValidationMiddleware(loginSchema))
194
+ * .handle(loginController); // ✅ Types match perfectly, no 'as any' needed
195
+ *
196
+ * @template T - The request body type (inferred from controller's Context<T, U>)
197
+ * @template U - The user type (inferred from controller's Context<T, U>)
198
+ * @param controller - The controller function with explicit Context<T, U> types
199
+ * @returns A new Handler instance with types inferred from the controller
200
+ */
201
+ function createTypedHandler(_controller) {
202
+ return new Handler();
203
+ }
155
204
  //# sourceMappingURL=handler.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noony-serverless/core",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A Middy base framework compatible with Firebase and GCP Cloud Functions with TypeScript",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",