@noony-serverless/core 0.3.0 → 0.3.2

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.
@@ -209,7 +209,8 @@ export interface AuthenticationOptions {
209
209
  * Class-based authentication middleware with comprehensive security features.
210
210
  * Provides JWT validation, rate limiting, token blacklisting, and security logging.
211
211
  *
212
- * @template T - The type of user data returned by the token verification port
212
+ * @template TUser - The type of user data returned by the token verification port
213
+ * @template TBody - The type of the request body payload (preserves type chain)
213
214
  *
214
215
  * @example
215
216
  * Basic JWT authentication:
@@ -292,17 +293,18 @@ export interface AuthenticationOptions {
292
293
  * });
293
294
  * ```
294
295
  */
295
- export declare class AuthenticationMiddleware<T> implements BaseMiddleware {
296
+ export declare class AuthenticationMiddleware<TUser = unknown, TBody = unknown> implements BaseMiddleware<TBody, TUser> {
296
297
  private tokenVerificationPort;
297
298
  private options;
298
- constructor(tokenVerificationPort: CustomTokenVerificationPort<T>, options?: AuthenticationOptions);
299
- before(context: Context): Promise<void>;
299
+ constructor(tokenVerificationPort: CustomTokenVerificationPort<TUser>, options?: AuthenticationOptions);
300
+ before(context: Context<TBody, TUser>): Promise<void>;
300
301
  }
301
302
  /**
302
303
  * Factory function that creates an authentication middleware with token verification.
303
304
  * Provides a functional approach for authentication setup.
304
305
  *
305
- * @template T - The type of user data returned by the token verification port
306
+ * @template TUser - The type of user data returned by the token verification port
307
+ * @template TBody - The type of the request body payload (preserves type chain)
306
308
  * @param tokenVerificationPort - The token verification implementation
307
309
  * @param options - Authentication configuration options
308
310
  * @returns A BaseMiddleware object with authentication logic
@@ -427,5 +429,5 @@ export declare class AuthenticationMiddleware<T> implements BaseMiddleware {
427
429
  * };
428
430
  * ```
429
431
  */
430
- export declare const verifyAuthTokenMiddleware: <T>(tokenVerificationPort: CustomTokenVerificationPort<T>, options?: AuthenticationOptions) => BaseMiddleware;
432
+ export declare const verifyAuthTokenMiddleware: <TUser = unknown, TBody = unknown>(tokenVerificationPort: CustomTokenVerificationPort<TUser>, options?: AuthenticationOptions) => BaseMiddleware<TBody, TUser>;
431
433
  //# sourceMappingURL=authenticationMiddleware.d.ts.map
@@ -173,7 +173,8 @@ async function verifyToken(tokenVerificationPort, context, options = {}) {
173
173
  * Class-based authentication middleware with comprehensive security features.
174
174
  * Provides JWT validation, rate limiting, token blacklisting, and security logging.
175
175
  *
176
- * @template T - The type of user data returned by the token verification port
176
+ * @template TUser - The type of user data returned by the token verification port
177
+ * @template TBody - The type of the request body payload (preserves type chain)
177
178
  *
178
179
  * @example
179
180
  * Basic JWT authentication:
@@ -272,7 +273,8 @@ exports.AuthenticationMiddleware = AuthenticationMiddleware;
272
273
  * Factory function that creates an authentication middleware with token verification.
273
274
  * Provides a functional approach for authentication setup.
274
275
  *
275
- * @template T - The type of user data returned by the token verification port
276
+ * @template TUser - The type of user data returned by the token verification port
277
+ * @template TBody - The type of the request body payload (preserves type chain)
276
278
  * @param tokenVerificationPort - The token verification implementation
277
279
  * @param options - Authentication configuration options
278
280
  * @returns A BaseMiddleware object with authentication logic
@@ -8,7 +8,8 @@ import { BaseMiddleware, Context } from '../core';
8
8
  * - Base64 decoding for Pub/Sub messages
9
9
  * - Non-blocking parsing using setImmediate
10
10
  *
11
- * @template T - The expected type of the parsed body. Defaults to unknown if not specified.
11
+ * @template TBody - The expected type of the parsed body. Defaults to unknown if not specified.
12
+ * @template TUser - The type of the authenticated user (preserves type chain)
12
13
  * @implements {BaseMiddleware}
13
14
  *
14
15
  * @example
@@ -63,10 +64,10 @@ import { BaseMiddleware, Context } from '../core';
63
64
  * });
64
65
  * ```
65
66
  */
66
- export declare class BodyParserMiddleware<T = unknown> implements BaseMiddleware {
67
+ export declare class BodyParserMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
67
68
  private maxSize;
68
69
  constructor(maxSize?: number);
69
- before(context: Context): Promise<void>;
70
+ before(context: Context<TBody, TUser>): Promise<void>;
70
71
  }
71
72
  /**
72
73
  * Enhanced middleware function for parsing the request body in specific HTTP methods.
@@ -76,7 +77,8 @@ export declare class BodyParserMiddleware<T = unknown> implements BaseMiddleware
76
77
  * - Async parsing for large payloads
77
78
  * - Size validation
78
79
  *
79
- * @template T - The expected type of the parsed request body.
80
+ * @template TBody - The expected type of the parsed request body.
81
+ * @template TUser - The type of the authenticated user (preserves type chain)
80
82
  * @param maxSize - Maximum allowed body size in bytes (default: 1MB)
81
83
  * @returns {BaseMiddleware} A middleware object containing a `before` hook.
82
84
  *
@@ -126,5 +128,5 @@ export declare class BodyParserMiddleware<T = unknown> implements BaseMiddleware
126
128
  * });
127
129
  * ```
128
130
  */
129
- export declare const bodyParser: <T = unknown>(maxSize?: number) => BaseMiddleware;
131
+ export declare const bodyParser: <TBody = unknown, TUser = unknown>(maxSize?: number) => BaseMiddleware<TBody, TUser>;
130
132
  //# sourceMappingURL=bodyParserMiddleware.d.ts.map
@@ -157,7 +157,8 @@ const parseBody = async (body) => {
157
157
  * - Base64 decoding for Pub/Sub messages
158
158
  * - Non-blocking parsing using setImmediate
159
159
  *
160
- * @template T - The expected type of the parsed body. Defaults to unknown if not specified.
160
+ * @template TBody - The expected type of the parsed body. Defaults to unknown if not specified.
161
+ * @template TUser - The type of the authenticated user (preserves type chain)
161
162
  * @implements {BaseMiddleware}
162
163
  *
163
164
  * @example
@@ -241,7 +242,8 @@ exports.BodyParserMiddleware = BodyParserMiddleware;
241
242
  * - Async parsing for large payloads
242
243
  * - Size validation
243
244
  *
244
- * @template T - The expected type of the parsed request body.
245
+ * @template TBody - The expected type of the parsed request body.
246
+ * @template TUser - The type of the authenticated user (preserves type chain)
245
247
  * @param maxSize - Maximum allowed body size in bytes (default: 1MB)
246
248
  * @returns {BaseMiddleware} A middleware object containing a `before` hook.
247
249
  *
@@ -4,6 +4,9 @@ import { BaseMiddleware, Context } from '../core';
4
4
  * Implements the `BaseMiddleware` interface and provides an asynchronous
5
5
  * `onError` method that delegates error handling to the `handleError` function.
6
6
  *
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
+ *
7
10
  * @remarks
8
11
  * This middleware should be registered to catch and process errors that occur
9
12
  * during request handling.
@@ -50,12 +53,14 @@ import { BaseMiddleware, Context } from '../core';
50
53
  * });
51
54
  * ```
52
55
  */
53
- export declare class ErrorHandlerMiddleware implements BaseMiddleware {
54
- onError(error: Error, context: Context): Promise<void>;
56
+ export declare class ErrorHandlerMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
57
+ onError(error: Error, context: Context<TBody, TUser>): Promise<void>;
55
58
  }
56
59
  /**
57
60
  * Creates an error handling middleware for processing errors in the application.
58
61
  *
62
+ * @template TBody - The type of the request body payload (preserves type chain)
63
+ * @template TUser - The type of the authenticated user (preserves type chain)
59
64
  * @returns {BaseMiddleware} An object implementing the `onError` method to handle errors.
60
65
  *
61
66
  * @remarks
@@ -96,5 +101,5 @@ export declare class ErrorHandlerMiddleware implements BaseMiddleware {
96
101
  * });
97
102
  * ```
98
103
  */
99
- export declare const errorHandler: () => BaseMiddleware;
104
+ export declare const errorHandler: <TBody = unknown, TUser = unknown>() => BaseMiddleware<TBody, TUser>;
100
105
  //# sourceMappingURL=errorHandlerMiddleware.d.ts.map
@@ -9,6 +9,8 @@ const core_1 = require("../core");
9
9
  * - For `HttpError` instances, responds with the error message, and optionally details and code based on environment and error type.
10
10
  * - For other errors, responds with a generic message in production, and includes stack trace in development.
11
11
  *
12
+ * @template TBody - The type of the request body payload (preserves type chain)
13
+ * @template TUser - The type of the authenticated user (preserves type chain)
12
14
  * @param error - The error object thrown during request processing.
13
15
  * @param context - The request context containing request and response objects.
14
16
  * @returns A promise that resolves when the error response has been sent.
@@ -65,6 +67,9 @@ const handleError = async (error, context) => {
65
67
  * Implements the `BaseMiddleware` interface and provides an asynchronous
66
68
  * `onError` method that delegates error handling to the `handleError` function.
67
69
  *
70
+ * @template TBody - The type of the request body payload (preserves type chain)
71
+ * @template TUser - The type of the authenticated user (preserves type chain)
72
+ *
68
73
  * @remarks
69
74
  * This middleware should be registered to catch and process errors that occur
70
75
  * during request handling.
@@ -120,6 +125,8 @@ exports.ErrorHandlerMiddleware = ErrorHandlerMiddleware;
120
125
  /**
121
126
  * Creates an error handling middleware for processing errors in the application.
122
127
  *
128
+ * @template TBody - The type of the request body payload (preserves type chain)
129
+ * @template TUser - The type of the authenticated user (preserves type chain)
123
130
  * @returns {BaseMiddleware} An object implementing the `onError` method to handle errors.
124
131
  *
125
132
  * @remarks
@@ -3,6 +3,8 @@ import { BaseMiddleware, Context } from '../core';
3
3
  * Middleware class that validates and processes query parameters from the request URL.
4
4
  * Extracts query parameters and validates that required parameters are present.
5
5
  *
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)
6
8
  * @implements {BaseMiddleware}
7
9
  *
8
10
  * @example
@@ -43,15 +45,17 @@ import { BaseMiddleware, Context } from '../core';
43
45
  * });
44
46
  * ```
45
47
  */
46
- export declare class QueryParametersMiddleware implements BaseMiddleware {
48
+ export declare class QueryParametersMiddleware<TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
47
49
  private readonly requiredParams;
48
50
  constructor(requiredParams?: string[]);
49
- before(context: Context): Promise<void>;
51
+ before(context: Context<TBody, TUser>): Promise<void>;
50
52
  }
51
53
  /**
52
54
  * Factory function that creates a query parameter processing middleware.
53
55
  * Extracts and validates query parameters from the request URL.
54
56
  *
57
+ * @template TBody - The type of the request body payload (preserves type chain)
58
+ * @template TUser - The type of the authenticated user (preserves type chain)
55
59
  * @param requiredParams - Array of parameter names that must be present (default: empty array)
56
60
  * @returns BaseMiddleware object with query parameter processing logic
57
61
  *
@@ -109,5 +113,5 @@ export declare class QueryParametersMiddleware implements BaseMiddleware {
109
113
  * });
110
114
  * ```
111
115
  */
112
- export declare const queryParametersMiddleware: (requiredParams?: string[]) => BaseMiddleware;
116
+ export declare const queryParametersMiddleware: <TBody = unknown, TUser = unknown>(requiredParams?: string[]) => BaseMiddleware<TBody, TUser>;
113
117
  //# sourceMappingURL=queryParametersMiddleware.d.ts.map
@@ -24,6 +24,8 @@ const convertQueryToRecord = (query) => {
24
24
  * Middleware class that validates and processes query parameters from the request URL.
25
25
  * Extracts query parameters and validates that required parameters are present.
26
26
  *
27
+ * @template TBody - The type of the request body payload (preserves type chain)
28
+ * @template TUser - The type of the authenticated user (preserves type chain)
27
29
  * @implements {BaseMiddleware}
28
30
  *
29
31
  * @example
@@ -84,6 +86,8 @@ exports.QueryParametersMiddleware = QueryParametersMiddleware;
84
86
  * Factory function that creates a query parameter processing middleware.
85
87
  * Extracts and validates query parameters from the request URL.
86
88
  *
89
+ * @template TBody - The type of the request body payload (preserves type chain)
90
+ * @template TUser - The type of the authenticated user (preserves type chain)
87
91
  * @param requiredParams - Array of parameter names that must be present (default: empty array)
88
92
  * @returns BaseMiddleware object with query parameter processing logic
89
93
  *
@@ -5,6 +5,8 @@ import { Context } from '../core/core';
5
5
  * Automatically wraps the response with success flag, payload, and timestamp.
6
6
  *
7
7
  * @template T - The type of response data being wrapped
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)
8
10
  * @implements {BaseMiddleware}
9
11
  *
10
12
  * @example
@@ -60,14 +62,16 @@ import { Context } from '../core/core';
60
62
  * });
61
63
  * ```
62
64
  */
63
- export declare class ResponseWrapperMiddleware<T> implements BaseMiddleware {
64
- after(context: Context): Promise<void>;
65
+ export declare class ResponseWrapperMiddleware<T = unknown, TBody = unknown, TUser = unknown> implements BaseMiddleware<TBody, TUser> {
66
+ after(context: Context<TBody, TUser>): Promise<void>;
65
67
  }
66
68
  /**
67
69
  * Factory function that creates a response wrapper middleware.
68
70
  * Automatically wraps response data in a standardized format with success flag and timestamp.
69
71
  *
70
72
  * @template T - The type of response data being wrapped
73
+ * @template TBody - The type of the request body payload (preserves type chain)
74
+ * @template TUser - The type of the authenticated user (preserves type chain)
71
75
  * @returns BaseMiddleware object with response wrapping logic
72
76
  *
73
77
  * @example
@@ -118,12 +122,14 @@ export declare class ResponseWrapperMiddleware<T> implements BaseMiddleware {
118
122
  * });
119
123
  * ```
120
124
  */
121
- export declare const responseWrapperMiddleware: <T>() => BaseMiddleware;
125
+ export declare const responseWrapperMiddleware: <T = unknown, TBody = unknown, TUser = unknown>() => BaseMiddleware<TBody, TUser>;
122
126
  /**
123
127
  * Helper function to set response data in context for later wrapping.
124
128
  * This function should be used in handlers when using ResponseWrapperMiddleware.
125
129
  *
126
130
  * @template T - The type of data being set
131
+ * @template TBody - The type of the request body payload (preserves type chain)
132
+ * @template TUser - The type of the authenticated user (preserves type chain)
127
133
  * @param context - The request context
128
134
  * @param data - The data to be included in the response payload
129
135
  *
@@ -176,5 +182,5 @@ export declare const responseWrapperMiddleware: <T>() => BaseMiddleware;
176
182
  * });
177
183
  * ```
178
184
  */
179
- export declare function setResponseData<T>(context: Context, data: T): void;
185
+ export declare function setResponseData<T, TBody = unknown, TUser = unknown>(context: Context<TBody, TUser>, data: T): void;
180
186
  //# sourceMappingURL=responseWrapperMiddleware.d.ts.map
@@ -18,6 +18,8 @@ const wrapResponse = (context) => {
18
18
  * Automatically wraps the response with success flag, payload, and timestamp.
19
19
  *
20
20
  * @template T - The type of response data being wrapped
21
+ * @template TBody - The type of the request body payload (preserves type chain)
22
+ * @template TUser - The type of the authenticated user (preserves type chain)
21
23
  * @implements {BaseMiddleware}
22
24
  *
23
25
  * @example
@@ -84,6 +86,8 @@ exports.ResponseWrapperMiddleware = ResponseWrapperMiddleware;
84
86
  * Automatically wraps response data in a standardized format with success flag and timestamp.
85
87
  *
86
88
  * @template T - The type of response data being wrapped
89
+ * @template TBody - The type of the request body payload (preserves type chain)
90
+ * @template TUser - The type of the authenticated user (preserves type chain)
87
91
  * @returns BaseMiddleware object with response wrapping logic
88
92
  *
89
93
  * @example
@@ -145,6 +149,8 @@ exports.responseWrapperMiddleware = responseWrapperMiddleware;
145
149
  * This function should be used in handlers when using ResponseWrapperMiddleware.
146
150
  *
147
151
  * @template T - The type of data being set
152
+ * @template TBody - The type of the request body payload (preserves type chain)
153
+ * @template TUser - The type of the authenticated user (preserves type chain)
148
154
  * @param context - The request context
149
155
  * @param data - The data to be included in the response payload
150
156
  *
@@ -0,0 +1,28 @@
1
+ import type { Context } from '../core/core';
2
+ /**
3
+ * Get a service from the dependency injection container
4
+ *
5
+ * Type-safe utility to resolve services from the TypeDI container
6
+ * without casting.
7
+ *
8
+ * @template T The service type to resolve
9
+ * @param context - Request context containing the DI container
10
+ * @param serviceClass - Service class constructor
11
+ * @returns Service instance
12
+ * @throws Error if container is not initialized
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { getService } from '@noony-serverless/core';
17
+ * import { UserService } from '../services/user.service';
18
+ *
19
+ * export async function createUserController(context: Context<CreateUserRequest>) {
20
+ * const userService = getService(context, UserService); // Type-safe!
21
+ *
22
+ * const user = await userService.createUser(context.req.parsedBody);
23
+ * context.res.status(201).json({ data: user });
24
+ * }
25
+ * ```
26
+ */
27
+ export declare function getService<T>(context: Context<unknown, unknown>, serviceClass: new (...args: any[]) => T): T;
28
+ //# sourceMappingURL=container.utils.d.ts.map
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getService = getService;
4
+ /**
5
+ * Get a service from the dependency injection container
6
+ *
7
+ * Type-safe utility to resolve services from the TypeDI container
8
+ * without casting.
9
+ *
10
+ * @template T The service type to resolve
11
+ * @param context - Request context containing the DI container
12
+ * @param serviceClass - Service class constructor
13
+ * @returns Service instance
14
+ * @throws Error if container is not initialized
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { getService } from '@noony-serverless/core';
19
+ * import { UserService } from '../services/user.service';
20
+ *
21
+ * export async function createUserController(context: Context<CreateUserRequest>) {
22
+ * const userService = getService(context, UserService); // Type-safe!
23
+ *
24
+ * const user = await userService.createUser(context.req.parsedBody);
25
+ * context.res.status(201).json({ data: user });
26
+ * }
27
+ * ```
28
+ */
29
+ function getService(context, serviceClass) {
30
+ if (!context.container) {
31
+ throw new Error('Container not initialized. Did you forget to add DependencyInjectionMiddleware?');
32
+ }
33
+ return context.container.get(serviceClass);
34
+ }
35
+ //# sourceMappingURL=container.utils.js.map
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Utility functions for Noony Core
3
+ */
4
+ export { getService } from './container.utils';
5
+ export { asString, asStringArray, asNumber, asBoolean, } from './query-param.utils';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ /**
3
+ * Utility functions for Noony Core
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.asBoolean = exports.asNumber = exports.asStringArray = exports.asString = exports.getService = void 0;
7
+ // Container utilities
8
+ var container_utils_1 = require("./container.utils");
9
+ Object.defineProperty(exports, "getService", { enumerable: true, get: function () { return container_utils_1.getService; } });
10
+ // Query parameter utilities
11
+ var query_param_utils_1 = require("./query-param.utils");
12
+ Object.defineProperty(exports, "asString", { enumerable: true, get: function () { return query_param_utils_1.asString; } });
13
+ Object.defineProperty(exports, "asStringArray", { enumerable: true, get: function () { return query_param_utils_1.asStringArray; } });
14
+ Object.defineProperty(exports, "asNumber", { enumerable: true, get: function () { return query_param_utils_1.asNumber; } });
15
+ Object.defineProperty(exports, "asBoolean", { enumerable: true, get: function () { return query_param_utils_1.asBoolean; } });
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Query parameter utility functions for type-safe handling of query parameters
3
+ * that can be string | string[] | undefined
4
+ */
5
+ /**
6
+ * Convert query parameter to single string
7
+ * Takes the first value if array is provided
8
+ *
9
+ * @param value - Query parameter value
10
+ * @returns First string value or undefined
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const search = asString(context.req.query.search);
15
+ * // If query.search = "hello" → "hello"
16
+ * // If query.search = ["hello", "world"] → "hello"
17
+ * // If query.search = undefined → undefined
18
+ * ```
19
+ */
20
+ export declare function asString(value: string | string[] | undefined): string | undefined;
21
+ /**
22
+ * Convert query parameter to string array
23
+ * Returns array if single value is provided
24
+ *
25
+ * @param value - Query parameter value
26
+ * @returns Array of strings or undefined
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const tags = asStringArray(context.req.query.tags);
31
+ * // If query.tags = "javascript" → ["javascript"]
32
+ * // If query.tags = ["javascript", "typescript"] → ["javascript", "typescript"]
33
+ * // If query.tags = undefined → undefined
34
+ * ```
35
+ */
36
+ export declare function asStringArray(value: string | string[] | undefined): string[] | undefined;
37
+ /**
38
+ * Convert query parameter to number
39
+ * Parses the value as integer (base 10)
40
+ *
41
+ * @param value - Query parameter value
42
+ * @returns Parsed number or undefined if invalid
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const page = asNumber(context.req.query.page) || 1;
47
+ * // If query.page = "5" → 5
48
+ * // If query.page = ["10", "20"] → 10 (first value)
49
+ * // If query.page = "invalid" → undefined
50
+ * // If query.page = undefined → undefined
51
+ * ```
52
+ */
53
+ export declare function asNumber(value: string | string[] | undefined): number | undefined;
54
+ /**
55
+ * Convert query parameter to boolean
56
+ * Treats "true" and "1" as true, everything else as false
57
+ *
58
+ * @param value - Query parameter value
59
+ * @returns Boolean value or undefined if not provided
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const isActive = asBoolean(context.req.query.active);
64
+ * // If query.active = "true" → true
65
+ * // If query.active = "1" → true
66
+ * // If query.active = "false" → false
67
+ * // If query.active = "0" → false
68
+ * // If query.active = undefined → undefined
69
+ * ```
70
+ */
71
+ export declare function asBoolean(value: string | string[] | undefined): boolean | undefined;
72
+ //# sourceMappingURL=query-param.utils.d.ts.map
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ /**
3
+ * Query parameter utility functions for type-safe handling of query parameters
4
+ * that can be string | string[] | undefined
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.asString = asString;
8
+ exports.asStringArray = asStringArray;
9
+ exports.asNumber = asNumber;
10
+ exports.asBoolean = asBoolean;
11
+ /**
12
+ * Convert query parameter to single string
13
+ * Takes the first value if array is provided
14
+ *
15
+ * @param value - Query parameter value
16
+ * @returns First string value or undefined
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const search = asString(context.req.query.search);
21
+ * // If query.search = "hello" → "hello"
22
+ * // If query.search = ["hello", "world"] → "hello"
23
+ * // If query.search = undefined → undefined
24
+ * ```
25
+ */
26
+ function asString(value) {
27
+ if (Array.isArray(value)) {
28
+ return value[0];
29
+ }
30
+ return value;
31
+ }
32
+ /**
33
+ * Convert query parameter to string array
34
+ * Returns array if single value is provided
35
+ *
36
+ * @param value - Query parameter value
37
+ * @returns Array of strings or undefined
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const tags = asStringArray(context.req.query.tags);
42
+ * // If query.tags = "javascript" → ["javascript"]
43
+ * // If query.tags = ["javascript", "typescript"] → ["javascript", "typescript"]
44
+ * // If query.tags = undefined → undefined
45
+ * ```
46
+ */
47
+ function asStringArray(value) {
48
+ if (value === undefined) {
49
+ return undefined;
50
+ }
51
+ if (Array.isArray(value)) {
52
+ return value;
53
+ }
54
+ return [value];
55
+ }
56
+ /**
57
+ * Convert query parameter to number
58
+ * Parses the value as integer (base 10)
59
+ *
60
+ * @param value - Query parameter value
61
+ * @returns Parsed number or undefined if invalid
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const page = asNumber(context.req.query.page) || 1;
66
+ * // If query.page = "5" → 5
67
+ * // If query.page = ["10", "20"] → 10 (first value)
68
+ * // If query.page = "invalid" → undefined
69
+ * // If query.page = undefined → undefined
70
+ * ```
71
+ */
72
+ function asNumber(value) {
73
+ const str = asString(value);
74
+ if (!str) {
75
+ return undefined;
76
+ }
77
+ const num = parseInt(str, 10);
78
+ return isNaN(num) ? undefined : num;
79
+ }
80
+ /**
81
+ * Convert query parameter to boolean
82
+ * Treats "true" and "1" as true, everything else as false
83
+ *
84
+ * @param value - Query parameter value
85
+ * @returns Boolean value or undefined if not provided
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const isActive = asBoolean(context.req.query.active);
90
+ * // If query.active = "true" → true
91
+ * // If query.active = "1" → true
92
+ * // If query.active = "false" → false
93
+ * // If query.active = "0" → false
94
+ * // If query.active = undefined → undefined
95
+ * ```
96
+ */
97
+ function asBoolean(value) {
98
+ const str = asString(value);
99
+ if (str === undefined) {
100
+ return undefined;
101
+ }
102
+ return str.toLowerCase() === 'true' || str === '1';
103
+ }
104
+ //# sourceMappingURL=query-param.utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noony-serverless/core",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
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",
@@ -20,6 +20,8 @@
20
20
  "build/core/**/*.d.ts",
21
21
  "build/middlewares/**/*.js",
22
22
  "build/middlewares/**/*.d.ts",
23
+ "build/utils/**/*.js",
24
+ "build/utils/**/*.d.ts",
23
25
  "build/index.js",
24
26
  "build/index.d.ts",
25
27
  "README.md"