@noony-serverless/core 0.3.1 → 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.
|
@@ -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,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.
|
|
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"
|