@noony-serverless/core 0.3.2 → 0.3.4

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.
@@ -42,7 +42,7 @@ export interface ProcessingMiddlewareConfig {
42
42
  parser?: BodyParserConfig;
43
43
  query?: QueryProcessingConfig;
44
44
  attributes?: AttributesConfig;
45
- skipProcessing?: (context: Context) => boolean;
45
+ skipProcessing?: <TBody, TUser>(context: Context<TBody, TUser>) => boolean;
46
46
  }
47
47
  /**
48
48
  * Consolidated ProcessingMiddleware that combines body parsing, query processing, and attribute extraction.
@@ -52,6 +52,9 @@ export interface ProcessingMiddlewareConfig {
52
52
  * - QueryParametersMiddleware
53
53
  * - HttpAttributesMiddleware
54
54
  *
55
+ * @template TBody - The type of the request body payload (preserves type chain)
56
+ * @template TUser - The type of the authenticated user (preserves type chain)
57
+ *
55
58
  * @example
56
59
  * Complete processing setup:
57
60
  * ```typescript
@@ -95,10 +98,10 @@ export interface ProcessingMiddlewareConfig {
95
98
  * }));
96
99
  * ```
97
100
  */
98
- export declare class ProcessingMiddleware implements BaseMiddleware {
101
+ export declare class ProcessingMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
99
102
  private config;
100
103
  constructor(config?: ProcessingMiddlewareConfig);
101
- before(context: Context): Promise<void>;
104
+ before(context: Context<TBody, TUser>): Promise<void>;
102
105
  private extractAttributes;
103
106
  private processQueryParameters;
104
107
  private parseBody;
@@ -10,6 +10,9 @@ const core_1 = require("../core");
10
10
  * - QueryParametersMiddleware
11
11
  * - HttpAttributesMiddleware
12
12
  *
13
+ * @template TBody - The type of the request body payload (preserves type chain)
14
+ * @template TUser - The type of the authenticated user (preserves type chain)
15
+ *
13
16
  * @example
14
17
  * Complete processing setup:
15
18
  * ```typescript
@@ -3,7 +3,9 @@ import { BaseMiddleware, Context } from '../core';
3
3
  * Middleware to inject dependencies into the request context using typedi.
4
4
  * This allows handlers to access shared services or data via context.container.
5
5
  *
6
- * @implements {BaseMiddleware}
6
+ * @template TBody - The type of the request body payload (preserves type chain)
7
+ * @template TUser - The type of the authenticated user (preserves type chain)
8
+ * @implements {BaseMiddleware<TBody, TUser>}
7
9
  *
8
10
  * @example
9
11
  * Basic service injection:
@@ -145,18 +147,20 @@ import { BaseMiddleware, Context } from '../core';
145
147
  * });
146
148
  * ```
147
149
  */
148
- export declare class DependencyInjectionMiddleware implements BaseMiddleware {
150
+ export declare class DependencyInjectionMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
149
151
  private services;
150
152
  constructor(services: {
151
153
  id: any;
152
154
  value: any;
153
155
  }[]);
154
- before(context: Context): Promise<void>;
156
+ before(context: Context<TBody, TUser>): Promise<void>;
155
157
  }
156
158
  /**
157
159
  * Factory function that creates a dependency injection middleware.
158
160
  * Creates a new container instance for each request to avoid shared state issues.
159
161
  *
162
+ * @template TBody - The type of the request body payload (preserves type chain)
163
+ * @template TUser - The type of the authenticated user (preserves type chain)
160
164
  * @param services - Array of service definitions with id and value
161
165
  * @returns BaseMiddleware object with dependency injection logic
162
166
  *
@@ -245,8 +249,8 @@ export declare class DependencyInjectionMiddleware implements BaseMiddleware {
245
249
  * });
246
250
  * ```
247
251
  */
248
- export declare const dependencyInjection: (services?: {
252
+ export declare const dependencyInjection: <TBody = unknown, TUser = unknown>(services?: {
249
253
  id: any;
250
254
  value: any;
251
- }[]) => BaseMiddleware;
255
+ }[]) => BaseMiddleware<TBody, TUser>;
252
256
  //# sourceMappingURL=dependencyInjectionMiddleware.d.ts.map
@@ -7,7 +7,9 @@ const typedi_1 = require("typedi");
7
7
  * Middleware to inject dependencies into the request context using typedi.
8
8
  * This allows handlers to access shared services or data via context.container.
9
9
  *
10
- * @implements {BaseMiddleware}
10
+ * @template TBody - The type of the request body payload (preserves type chain)
11
+ * @template TUser - The type of the authenticated user (preserves type chain)
12
+ * @implements {BaseMiddleware<TBody, TUser>}
11
13
  *
12
14
  * @example
13
15
  * Basic service injection:
@@ -166,6 +168,8 @@ exports.DependencyInjectionMiddleware = DependencyInjectionMiddleware;
166
168
  * Factory function that creates a dependency injection middleware.
167
169
  * Creates a new container instance for each request to avoid shared state issues.
168
170
  *
171
+ * @template TBody - The type of the request body payload (preserves type chain)
172
+ * @template TUser - The type of the authenticated user (preserves type chain)
169
173
  * @param services - Array of service definitions with id and value
170
174
  * @returns BaseMiddleware object with dependency injection logic
171
175
  *
@@ -3,7 +3,9 @@ import { BaseMiddleware, Context } from '../core';
3
3
  * Middleware class that validates the presence of required HTTP headers.
4
4
  * Throws a ValidationError if any required header is missing or empty.
5
5
  *
6
- * @implements {BaseMiddleware}
6
+ * @template TBody - The type of the request body payload (preserves type chain)
7
+ * @template TUser - The type of the authenticated user (preserves type chain)
8
+ * @implements {BaseMiddleware<TBody, TUser>}
7
9
  *
8
10
  * @example
9
11
  * API key authentication via headers:
@@ -63,15 +65,17 @@ import { BaseMiddleware, Context } from '../core';
63
65
  * });
64
66
  * ```
65
67
  */
66
- export declare class HeaderVariablesMiddleware implements BaseMiddleware {
68
+ export declare class HeaderVariablesMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
67
69
  private requiredHeaders;
68
70
  constructor(requiredHeaders: string[]);
69
- before(context: Context): Promise<void>;
71
+ before(context: Context<TBody, TUser>): Promise<void>;
70
72
  }
71
73
  /**
72
74
  * Factory function that creates a header validation middleware.
73
75
  * Validates that all required headers are present in the request.
74
76
  *
77
+ * @template TBody - The type of the request body payload (preserves type chain)
78
+ * @template TUser - The type of the authenticated user (preserves type chain)
75
79
  * @param requiredHeaders - Array of header names that must be present
76
80
  * @returns BaseMiddleware object with header validation logic
77
81
  *
@@ -122,5 +126,5 @@ export declare class HeaderVariablesMiddleware implements BaseMiddleware {
122
126
  * });
123
127
  * ```
124
128
  */
125
- export declare const headerVariablesMiddleware: (requiredHeaders: string[]) => BaseMiddleware;
129
+ export declare const headerVariablesMiddleware: <TBody = unknown, TUser = unknown>(requiredHeaders: string[]) => BaseMiddleware<TBody, TUser>;
126
130
  //# sourceMappingURL=headerVariablesMiddleware.d.ts.map
@@ -15,7 +15,9 @@ const validateHeaders = (requiredHeaders, headers) => {
15
15
  * Middleware class that validates the presence of required HTTP headers.
16
16
  * Throws a ValidationError if any required header is missing or empty.
17
17
  *
18
- * @implements {BaseMiddleware}
18
+ * @template TBody - The type of the request body payload (preserves type chain)
19
+ * @template TUser - The type of the authenticated user (preserves type chain)
20
+ * @implements {BaseMiddleware<TBody, TUser>}
19
21
  *
20
22
  * @example
21
23
  * API key authentication via headers:
@@ -90,6 +92,8 @@ exports.HeaderVariablesMiddleware = HeaderVariablesMiddleware;
90
92
  * Factory function that creates a header validation middleware.
91
93
  * Validates that all required headers are present in the request.
92
94
  *
95
+ * @template TBody - The type of the request body payload (preserves type chain)
96
+ * @template TUser - The type of the authenticated user (preserves type chain)
93
97
  * @param requiredHeaders - Array of header names that must be present
94
98
  * @returns BaseMiddleware object with header validation logic
95
99
  *
@@ -5,7 +5,9 @@ import { ZodSchema } from 'zod';
5
5
  * Middleware class that extracts path parameters from the URL.
6
6
  * Parses URL segments and extracts parameters based on colon-prefixed patterns.
7
7
  *
8
- * @implements {BaseMiddleware}
8
+ * @template TBody - The type of the request body payload (preserves type chain)
9
+ * @template TUser - The type of the authenticated user (preserves type chain)
10
+ * @implements {BaseMiddleware<TBody, TUser>}
9
11
  *
10
12
  * @example
11
13
  * Basic path parameter extraction:
@@ -56,8 +58,8 @@ import { ZodSchema } from 'zod';
56
58
  * });
57
59
  * ```
58
60
  */
59
- export declare class PathParametersMiddleware implements BaseMiddleware {
60
- before(context: Context): Promise<void>;
61
+ export declare class PathParametersMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
62
+ before(context: Context<TBody, TUser>): Promise<void>;
61
63
  }
62
64
  /**
63
65
  * Factory function that creates a path parameters extraction middleware.
@@ -7,7 +7,9 @@ const zod_1 = require("zod");
7
7
  * Middleware class that extracts path parameters from the URL.
8
8
  * Parses URL segments and extracts parameters based on colon-prefixed patterns.
9
9
  *
10
- * @implements {BaseMiddleware}
10
+ * @template TBody - The type of the request body payload (preserves type chain)
11
+ * @template TUser - The type of the authenticated user (preserves type chain)
12
+ * @implements {BaseMiddleware<TBody, TUser>}
11
13
  *
12
14
  * @example
13
15
  * Basic path parameter extraction:
@@ -14,7 +14,7 @@ export interface RateLimitOptions {
14
14
  * Function to generate rate limiting key
15
15
  * @default Uses IP address
16
16
  */
17
- keyGenerator?: (context: Context) => string;
17
+ keyGenerator?: <TBody, TUser>(context: Context<TBody, TUser>) => string;
18
18
  /**
19
19
  * Custom error message
20
20
  * @default 'Too many requests, please try again later'
@@ -28,7 +28,7 @@ export interface RateLimitOptions {
28
28
  /**
29
29
  * Skip rate limiting for certain requests
30
30
  */
31
- skip?: (context: Context) => boolean;
31
+ skip?: <TBody, TUser>(context: Context<TBody, TUser>) => boolean;
32
32
  /**
33
33
  * Headers to include in response
34
34
  */
@@ -40,7 +40,7 @@ export interface RateLimitOptions {
40
40
  [key: string]: {
41
41
  maxRequests: number;
42
42
  windowMs: number;
43
- matcher: (context: Context) => boolean;
43
+ matcher: <TBody, TUser>(context: Context<TBody, TUser>) => boolean;
44
44
  };
45
45
  };
46
46
  /**
@@ -120,7 +120,9 @@ declare class MemoryStore implements RateLimitStore {
120
120
  * - Implement multiple protection layers within middleware
121
121
  * - Critical for security in simple deployments
122
122
  *
123
- * @implements {BaseMiddleware}
123
+ * @template TBody - The type of the request body payload (preserves type chain)
124
+ * @template TUser - The type of the authenticated user (preserves type chain)
125
+ * @implements {BaseMiddleware<TBody, TUser>}
124
126
  *
125
127
  * @example
126
128
  * Basic API rate limiting:
@@ -274,11 +276,11 @@ declare class MemoryStore implements RateLimitStore {
274
276
  * });
275
277
  * ```
276
278
  */
277
- export declare class RateLimitingMiddleware implements BaseMiddleware {
279
+ export declare class RateLimitingMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
278
280
  private store;
279
281
  private options;
280
282
  constructor(options?: RateLimitOptions);
281
- before(context: Context): Promise<void>;
283
+ before(context: Context<TBody, TUser>): Promise<void>;
282
284
  }
283
285
  /**
284
286
  * Factory function that creates a rate limiting middleware.
@@ -138,7 +138,9 @@ const getRateLimit = (context, options) => {
138
138
  * - Implement multiple protection layers within middleware
139
139
  * - Critical for security in simple deployments
140
140
  *
141
- * @implements {BaseMiddleware}
141
+ * @template TBody - The type of the request body payload (preserves type chain)
142
+ * @template TUser - The type of the authenticated user (preserves type chain)
143
+ * @implements {BaseMiddleware<TBody, TUser>}
142
144
  *
143
145
  * @example
144
146
  * Basic API rate limiting:
@@ -72,12 +72,15 @@ declare const securityEventTracker: SecurityEventTracker;
72
72
  /**
73
73
  * Security Audit Middleware
74
74
  * Provides comprehensive security event logging and monitoring
75
+ *
76
+ * @template TBody - The type of the request body payload (preserves type chain)
77
+ * @template TUser - The type of the authenticated user (preserves type chain)
75
78
  */
76
- export declare class SecurityAuditMiddleware implements BaseMiddleware {
79
+ export declare class SecurityAuditMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
77
80
  private options;
78
81
  constructor(options?: SecurityAuditOptions);
79
- before(context: Context): Promise<void>;
80
- after(context: Context): Promise<void>;
82
+ before(context: Context<TBody, TUser>): Promise<void>;
83
+ after(context: Context<TBody, TUser>): Promise<void>;
81
84
  onError(error: Error, context: Context): Promise<void>;
82
85
  private logSecurityEvent;
83
86
  private sanitizeHeaders;
@@ -156,6 +156,9 @@ const extractClientInfo = (context) => ({
156
156
  /**
157
157
  * Security Audit Middleware
158
158
  * Provides comprehensive security event logging and monitoring
159
+ *
160
+ * @template TBody - The type of the request body payload (preserves type chain)
161
+ * @template TUser - The type of the authenticated user (preserves type chain)
159
162
  */
160
163
  class SecurityAuditMiddleware {
161
164
  options;
@@ -75,11 +75,14 @@ export interface SecurityHeadersOptions {
75
75
  /**
76
76
  * Security Headers Middleware
77
77
  * Implements comprehensive security headers following OWASP recommendations
78
+ *
79
+ * @template TBody - The type of the request body payload (preserves type chain)
80
+ * @template TUser - The type of the authenticated user (preserves type chain)
78
81
  */
79
- export declare class SecurityHeadersMiddleware implements BaseMiddleware {
82
+ export declare class SecurityHeadersMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
80
83
  private options;
81
84
  constructor(options?: SecurityHeadersOptions);
82
- before(context: Context): Promise<void>;
85
+ before(context: Context<TBody, TUser>): Promise<void>;
83
86
  }
84
87
  /**
85
88
  * Security Headers Middleware Factory
@@ -40,6 +40,9 @@ const isOriginAllowed = (origin, allowedOrigins) => {
40
40
  /**
41
41
  * Security Headers Middleware
42
42
  * Implements comprehensive security headers following OWASP recommendations
43
+ *
44
+ * @template TBody - The type of the request body payload (preserves type chain)
45
+ * @template TUser - The type of the authenticated user (preserves type chain)
43
46
  */
44
47
  class SecurityHeadersMiddleware {
45
48
  options;
@@ -4,7 +4,9 @@ import { z } from 'zod';
4
4
  * Middleware class that validates request data (body or query parameters) using Zod schemas.
5
5
  * Automatically detects GET requests and validates query parameters, or validates body for other methods.
6
6
  *
7
- * @implements {BaseMiddleware}
7
+ * @template TBody - The type of the request body payload (preserves type chain)
8
+ * @template TUser - The type of the authenticated user (preserves type chain)
9
+ * @implements {BaseMiddleware<TBody, TUser>}
8
10
  *
9
11
  * @example
10
12
  * User registration validation:
@@ -76,15 +78,17 @@ import { z } from 'zod';
76
78
  * });
77
79
  * ```
78
80
  */
79
- export declare class ValidationMiddleware implements BaseMiddleware {
81
+ export declare class ValidationMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
80
82
  private readonly schema;
81
83
  constructor(schema: z.ZodSchema);
82
- before(context: Context): Promise<void>;
84
+ before(context: Context<TBody, TUser>): Promise<void>;
83
85
  }
84
86
  /**
85
87
  * Factory function that creates a validation middleware using Zod schema.
86
88
  * Automatically validates request body for non-GET requests or query parameters for GET requests.
87
89
  *
90
+ * @template TBody - The type of the request body payload (preserves type chain)
91
+ * @template TUser - The type of the authenticated user (preserves type chain)
88
92
  * @param schema - Zod schema to validate against
89
93
  * @returns BaseMiddleware object with validation logic
90
94
  *
@@ -150,5 +154,5 @@ export declare class ValidationMiddleware implements BaseMiddleware {
150
154
  * });
151
155
  * ```
152
156
  */
153
- export declare const validationMiddleware: (schema: z.ZodSchema) => BaseMiddleware;
157
+ export declare const validationMiddleware: <TBody = unknown, TUser = unknown>(schema: z.ZodSchema) => BaseMiddleware<TBody, TUser>;
154
158
  //# sourceMappingURL=validationMiddleware.d.ts.map
@@ -25,7 +25,9 @@ const validate = async (schema, context) => {
25
25
  * Middleware class that validates request data (body or query parameters) using Zod schemas.
26
26
  * Automatically detects GET requests and validates query parameters, or validates body for other methods.
27
27
  *
28
- * @implements {BaseMiddleware}
28
+ * @template TBody - The type of the request body payload (preserves type chain)
29
+ * @template TUser - The type of the authenticated user (preserves type chain)
30
+ * @implements {BaseMiddleware<TBody, TUser>}
29
31
  *
30
32
  * @example
31
33
  * User registration validation:
@@ -111,6 +113,8 @@ exports.ValidationMiddleware = ValidationMiddleware;
111
113
  * Factory function that creates a validation middleware using Zod schema.
112
114
  * Automatically validates request body for non-GET requests or query parameters for GET requests.
113
115
  *
116
+ * @template TBody - The type of the request body payload (preserves type chain)
117
+ * @template TUser - The type of the authenticated user (preserves type chain)
114
118
  * @param schema - Zod schema to validate against
115
119
  * @returns BaseMiddleware object with validation logic
116
120
  *
@@ -2,12 +2,12 @@ import type { Context } from '../core/core';
2
2
  /**
3
3
  * Get a service from the dependency injection container
4
4
  *
5
- * Type-safe utility to resolve services from the TypeDI container
6
- * without casting.
5
+ * Type-safe utility to resolve services from the TypeDI container.
6
+ * Supports both class constructors and string identifiers.
7
7
  *
8
8
  * @template T The service type to resolve
9
9
  * @param context - Request context containing the DI container
10
- * @param serviceClass - Service class constructor
10
+ * @param serviceIdentifier - Service class constructor OR string identifier
11
11
  * @returns Service instance
12
12
  * @throws Error if container is not initialized
13
13
  *
@@ -16,13 +16,26 @@ import type { Context } from '../core/core';
16
16
  * import { getService } from '@noony-serverless/core';
17
17
  * import { UserService } from '../services/user.service';
18
18
  *
19
- * export async function createUserController(context: Context<CreateUserRequest>) {
20
- * const userService = getService(context, UserService); // Type-safe!
19
+ * // Best: Class-based access (type inferred automatically)
20
+ * export async function handler(context: Context) {
21
+ * const userService = getService(context, UserService);
22
+ * // userService is typed as UserService automatically
23
+ * const users = await userService.getUsers();
24
+ * }
25
+ *
26
+ * // ✅ Good: String-based with explicit generic (when manual instantiation required)
27
+ * export async function handler(context: Context) {
28
+ * const planRepo = getService<ActionPlanRepository>(context, 'ActionPlanRepository');
29
+ * // planRepo is typed as ActionPlanRepository via explicit generic
30
+ * const plan = await planRepo.findById(id);
31
+ * }
21
32
  *
22
- * const user = await userService.createUser(context.req.parsedBody);
23
- * context.res.status(201).json({ data: user });
33
+ * // ⚠️ Avoid: String without generic (only for quick prototypes)
34
+ * export async function handler(context: Context) {
35
+ * const repo = getService(context, 'ActionPlanRepository');
36
+ * // repo has type 'unknown' - requires manual type assertion
24
37
  * }
25
38
  * ```
26
39
  */
27
- export declare function getService<T>(context: Context<unknown, unknown>, serviceClass: new (...args: any[]) => T): T;
40
+ export declare function getService<T>(context: Context<unknown, unknown>, serviceIdentifier: (new (...args: any[]) => T) | string): T;
28
41
  //# sourceMappingURL=container.utils.d.ts.map
@@ -4,12 +4,12 @@ exports.getService = getService;
4
4
  /**
5
5
  * Get a service from the dependency injection container
6
6
  *
7
- * Type-safe utility to resolve services from the TypeDI container
8
- * without casting.
7
+ * Type-safe utility to resolve services from the TypeDI container.
8
+ * Supports both class constructors and string identifiers.
9
9
  *
10
10
  * @template T The service type to resolve
11
11
  * @param context - Request context containing the DI container
12
- * @param serviceClass - Service class constructor
12
+ * @param serviceIdentifier - Service class constructor OR string identifier
13
13
  * @returns Service instance
14
14
  * @throws Error if container is not initialized
15
15
  *
@@ -18,18 +18,31 @@ exports.getService = getService;
18
18
  * import { getService } from '@noony-serverless/core';
19
19
  * import { UserService } from '../services/user.service';
20
20
  *
21
- * export async function createUserController(context: Context<CreateUserRequest>) {
22
- * const userService = getService(context, UserService); // Type-safe!
21
+ * // Best: Class-based access (type inferred automatically)
22
+ * export async function handler(context: Context) {
23
+ * const userService = getService(context, UserService);
24
+ * // userService is typed as UserService automatically
25
+ * const users = await userService.getUsers();
26
+ * }
27
+ *
28
+ * // ✅ Good: String-based with explicit generic (when manual instantiation required)
29
+ * export async function handler(context: Context) {
30
+ * const planRepo = getService<ActionPlanRepository>(context, 'ActionPlanRepository');
31
+ * // planRepo is typed as ActionPlanRepository via explicit generic
32
+ * const plan = await planRepo.findById(id);
33
+ * }
23
34
  *
24
- * const user = await userService.createUser(context.req.parsedBody);
25
- * context.res.status(201).json({ data: user });
35
+ * // ⚠️ Avoid: String without generic (only for quick prototypes)
36
+ * export async function handler(context: Context) {
37
+ * const repo = getService(context, 'ActionPlanRepository');
38
+ * // repo has type 'unknown' - requires manual type assertion
26
39
  * }
27
40
  * ```
28
41
  */
29
- function getService(context, serviceClass) {
42
+ function getService(context, serviceIdentifier) {
30
43
  if (!context.container) {
31
44
  throw new Error('Container not initialized. Did you forget to add DependencyInjectionMiddleware?');
32
45
  }
33
- return context.container.get(serviceClass);
46
+ return context.container.get(serviceIdentifier);
34
47
  }
35
48
  //# sourceMappingURL=container.utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noony-serverless/core",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
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",