@bool-ts/core 2.3.6 → 2.3.8
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.
package/package.json
CHANGED
|
@@ -12,40 +12,53 @@ import {
|
|
|
12
12
|
responseHeadersArgsKey,
|
|
13
13
|
routeModelArgsKey
|
|
14
14
|
} from "../constants/keys";
|
|
15
|
+
import type { IContext } from "../interfaces";
|
|
15
16
|
|
|
16
17
|
export type TArgumentsMetadata<TValidationSchema = unknown> =
|
|
17
18
|
| {
|
|
18
19
|
index: number;
|
|
19
20
|
type: typeof requestHeadersArgsKey;
|
|
20
|
-
validationSchema?:
|
|
21
|
+
validationSchema?:
|
|
22
|
+
| TValidationSchema
|
|
23
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
21
24
|
}
|
|
22
25
|
| {
|
|
23
26
|
index: number;
|
|
24
27
|
type: typeof requestHeaderArgsKey;
|
|
25
28
|
key: string;
|
|
26
|
-
validationSchema?:
|
|
29
|
+
validationSchema?:
|
|
30
|
+
| TValidationSchema
|
|
31
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
27
32
|
}
|
|
28
33
|
| {
|
|
29
34
|
index: number;
|
|
30
35
|
type: typeof requestBodyArgsKey;
|
|
31
|
-
validationSchema?:
|
|
36
|
+
validationSchema?:
|
|
37
|
+
| TValidationSchema
|
|
38
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
32
39
|
parser?: "arrayBuffer" | "blob" | "formData" | "json" | "text";
|
|
33
40
|
}
|
|
34
41
|
| {
|
|
35
42
|
index: number;
|
|
36
43
|
type: typeof paramsArgsKey;
|
|
37
|
-
validationSchema?:
|
|
44
|
+
validationSchema?:
|
|
45
|
+
| TValidationSchema
|
|
46
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
38
47
|
}
|
|
39
48
|
| {
|
|
40
49
|
index: number;
|
|
41
50
|
type: typeof paramArgsKey;
|
|
42
51
|
key: string;
|
|
43
|
-
validationSchema?:
|
|
52
|
+
validationSchema?:
|
|
53
|
+
| TValidationSchema
|
|
54
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
44
55
|
}
|
|
45
56
|
| {
|
|
46
57
|
index: number;
|
|
47
58
|
type: typeof queryArgsKey;
|
|
48
|
-
validationSchema?:
|
|
59
|
+
validationSchema?:
|
|
60
|
+
| TValidationSchema
|
|
61
|
+
| ((context: IContext) => Promise<TValidationSchema> | TValidationSchema);
|
|
49
62
|
}
|
|
50
63
|
| {
|
|
51
64
|
index: number;
|
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
} from "../decorators";
|
|
11
11
|
import type { THttpMethods } from "../http";
|
|
12
12
|
import type {
|
|
13
|
+
IContext,
|
|
13
14
|
IController,
|
|
14
15
|
ICustomValidator,
|
|
15
16
|
IGuard,
|
|
@@ -75,7 +76,7 @@ import {
|
|
|
75
76
|
HttpServerError,
|
|
76
77
|
jsonErrorInfer
|
|
77
78
|
} from "../http";
|
|
78
|
-
import { ansiText, inferStatusText, isWebSocketUpgrade } from "../utils";
|
|
79
|
+
import { ansiText, hasCallSignature, inferStatusText, isWebSocketUpgrade } from "../utils";
|
|
79
80
|
import { Context } from "./context";
|
|
80
81
|
import { HttpRouter } from "./httpRouter";
|
|
81
82
|
import { HttpRouterGroup } from "./httpRouterGroup";
|
|
@@ -394,15 +395,31 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
394
395
|
color: "yellow"
|
|
395
396
|
}
|
|
396
397
|
);
|
|
398
|
+
const timeInMs = Math.round((end - start + Number.EPSILON) * 10 ** 2) / 10 ** 2;
|
|
397
399
|
const convertedTime = ansiText(
|
|
398
|
-
` ${
|
|
399
|
-
{
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
400
|
+
` ${timeInMs}ms `.padStart(10),
|
|
401
|
+
(() => {
|
|
402
|
+
if (timeInMs >= 500) {
|
|
403
|
+
return {
|
|
404
|
+
color: "white",
|
|
405
|
+
backgroundColor: "red"
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
if (timeInMs >= 300) {
|
|
409
|
+
return {
|
|
410
|
+
color: "black",
|
|
411
|
+
backgroundColor: "yellow"
|
|
412
|
+
};
|
|
413
|
+
} else {
|
|
414
|
+
return {
|
|
415
|
+
color: "white",
|
|
416
|
+
backgroundColor: "blue"
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
})()
|
|
403
420
|
);
|
|
404
421
|
const convertedResponseStatus = ansiText(
|
|
405
|
-
` ${inferedResponseStatus}
|
|
422
|
+
` ${inferedResponseStatus} `.padStart(5),
|
|
406
423
|
(() => {
|
|
407
424
|
if (inferedResponseStatus >= 100 && inferedResponseStatus < 200)
|
|
408
425
|
return {
|
|
@@ -443,11 +460,11 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
443
460
|
console.info(
|
|
444
461
|
[
|
|
445
462
|
`PID: ${convertedPID}`,
|
|
446
|
-
`Method: ${convertedMethod
|
|
447
|
-
`Time: ${convertedTime
|
|
463
|
+
`Method: ${convertedMethod}`,
|
|
464
|
+
`Time: ${convertedTime}`,
|
|
448
465
|
typeof responseStatus !== "number" || !responseStatus
|
|
449
466
|
? undefined
|
|
450
|
-
: convertedResponseStatus
|
|
467
|
+
: `Status: ${convertedResponseStatus}`,
|
|
451
468
|
`IP: ${convertedReqIp}`,
|
|
452
469
|
pathname
|
|
453
470
|
]
|
|
@@ -1643,24 +1660,37 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1643
1660
|
* @returns
|
|
1644
1661
|
*/
|
|
1645
1662
|
async #argumentsResolver<TValidationSchema = unknown>({
|
|
1663
|
+
context,
|
|
1646
1664
|
data,
|
|
1647
1665
|
validationSchema,
|
|
1648
1666
|
argumentIndex,
|
|
1649
1667
|
funcName
|
|
1650
1668
|
}: {
|
|
1669
|
+
context: IContext;
|
|
1651
1670
|
data: unknown;
|
|
1652
|
-
validationSchema:
|
|
1671
|
+
validationSchema:
|
|
1672
|
+
| TValidationSchema
|
|
1673
|
+
| ((context: IContext) => TValidationSchema | Promise<TValidationSchema>);
|
|
1653
1674
|
argumentIndex: number;
|
|
1654
1675
|
funcName: string | symbol;
|
|
1655
1676
|
}) {
|
|
1656
|
-
if (
|
|
1677
|
+
if (
|
|
1678
|
+
!this.#customValidator ||
|
|
1679
|
+
!validationSchema ||
|
|
1680
|
+
Object.getOwnPropertyDescriptor(validationSchema, "prototype")
|
|
1681
|
+
) {
|
|
1657
1682
|
return data;
|
|
1658
1683
|
}
|
|
1659
1684
|
|
|
1660
1685
|
try {
|
|
1686
|
+
const inferValidationSchema =
|
|
1687
|
+
typeof validationSchema === "function" && hasCallSignature(validationSchema)
|
|
1688
|
+
? await validationSchema(context)
|
|
1689
|
+
: validationSchema;
|
|
1690
|
+
|
|
1661
1691
|
const validation = await this.#customValidator.validate(
|
|
1662
1692
|
data,
|
|
1663
|
-
|
|
1693
|
+
inferValidationSchema,
|
|
1664
1694
|
argumentIndex,
|
|
1665
1695
|
funcName
|
|
1666
1696
|
);
|
|
@@ -1897,6 +1927,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1897
1927
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1898
1928
|
? await request?.[argMetadata.parser || "json"]()
|
|
1899
1929
|
: await this.#argumentsResolver({
|
|
1930
|
+
context: context,
|
|
1900
1931
|
data: await request?.[argMetadata.parser || "json"](),
|
|
1901
1932
|
validationSchema: argMetadata.validationSchema,
|
|
1902
1933
|
argumentIndex: argMetadata.index,
|
|
@@ -1907,6 +1938,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1907
1938
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1908
1939
|
? requestHeaders
|
|
1909
1940
|
: await this.#argumentsResolver({
|
|
1941
|
+
context: context,
|
|
1910
1942
|
data: requestHeaders?.toJSON(),
|
|
1911
1943
|
validationSchema: argMetadata.validationSchema,
|
|
1912
1944
|
argumentIndex: argMetadata.index,
|
|
@@ -1917,6 +1949,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1917
1949
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1918
1950
|
? requestHeaders?.get(argMetadata.key) || undefined
|
|
1919
1951
|
: await this.#argumentsResolver({
|
|
1952
|
+
context: context,
|
|
1920
1953
|
data: requestHeaders?.get(argMetadata.key) || undefined,
|
|
1921
1954
|
validationSchema: argMetadata.validationSchema,
|
|
1922
1955
|
argumentIndex: argMetadata.index,
|
|
@@ -1927,6 +1960,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1927
1960
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1928
1961
|
? parameters?.[argMetadata.key] || undefined
|
|
1929
1962
|
: await this.#argumentsResolver({
|
|
1963
|
+
context: context,
|
|
1930
1964
|
data: parameters?.[argMetadata.key] || undefined,
|
|
1931
1965
|
validationSchema: argMetadata.validationSchema,
|
|
1932
1966
|
argumentIndex: argMetadata.index,
|
|
@@ -1946,6 +1980,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1946
1980
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1947
1981
|
? context.get(argMetadata.type, contextOptions)
|
|
1948
1982
|
: await this.#argumentsResolver({
|
|
1983
|
+
context: context,
|
|
1949
1984
|
data: context.get(argMetadata.type, contextOptions),
|
|
1950
1985
|
validationSchema: argMetadata.validationSchema,
|
|
1951
1986
|
argumentIndex: argMetadata.index,
|
|
@@ -1975,6 +2010,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1975
2010
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1976
2011
|
? await request?.[argMetadata.parser || "json"]()
|
|
1977
2012
|
: await this.#argumentsResolver({
|
|
2013
|
+
context: context,
|
|
1978
2014
|
data: await request?.[argMetadata.parser || "json"](),
|
|
1979
2015
|
validationSchema: argMetadata.validationSchema,
|
|
1980
2016
|
argumentIndex: argMetadata.index,
|
|
@@ -1990,6 +2026,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
1990
2026
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
1991
2027
|
? requestHeaders
|
|
1992
2028
|
: await this.#argumentsResolver({
|
|
2029
|
+
context: context,
|
|
1993
2030
|
data: requestHeaders?.toJSON(),
|
|
1994
2031
|
validationSchema: argMetadata.validationSchema,
|
|
1995
2032
|
argumentIndex: argMetadata.index,
|
|
@@ -2003,6 +2040,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2003
2040
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2004
2041
|
? requestHeaders?.get(argMetadata.key) || undefined
|
|
2005
2042
|
: await this.#argumentsResolver({
|
|
2043
|
+
context: context,
|
|
2006
2044
|
data: requestHeaders?.get(argMetadata.key) || undefined,
|
|
2007
2045
|
validationSchema: argMetadata.validationSchema,
|
|
2008
2046
|
argumentIndex: argMetadata.index,
|
|
@@ -2013,6 +2051,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2013
2051
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2014
2052
|
? parameters?.[argMetadata.key] || undefined
|
|
2015
2053
|
: await this.#argumentsResolver({
|
|
2054
|
+
context: context,
|
|
2016
2055
|
data: parameters?.[argMetadata.key],
|
|
2017
2056
|
validationSchema: argMetadata.validationSchema,
|
|
2018
2057
|
argumentIndex: argMetadata.index,
|
|
@@ -2029,6 +2068,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2029
2068
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2030
2069
|
? context.get(argMetadata.type)
|
|
2031
2070
|
: await this.#argumentsResolver({
|
|
2071
|
+
context: context,
|
|
2032
2072
|
data: context.get(argMetadata.type),
|
|
2033
2073
|
validationSchema: argMetadata.validationSchema,
|
|
2034
2074
|
argumentIndex: argMetadata.index,
|
|
@@ -2073,6 +2113,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2073
2113
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2074
2114
|
? await request?.[argMetadata.parser || "json"]()
|
|
2075
2115
|
: await this.#argumentsResolver({
|
|
2116
|
+
context: context,
|
|
2076
2117
|
data: await request?.[argMetadata.parser || "json"](),
|
|
2077
2118
|
validationSchema: argMetadata.validationSchema,
|
|
2078
2119
|
argumentIndex: argMetadata.index,
|
|
@@ -2088,6 +2129,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2088
2129
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2089
2130
|
? requestHeaders
|
|
2090
2131
|
: await this.#argumentsResolver({
|
|
2132
|
+
context: context,
|
|
2091
2133
|
data: requestHeaders?.toJSON(),
|
|
2092
2134
|
validationSchema: argMetadata.validationSchema,
|
|
2093
2135
|
argumentIndex: argMetadata.index,
|
|
@@ -2098,6 +2140,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2098
2140
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2099
2141
|
? requestHeaders?.get(argMetadata.key) || undefined
|
|
2100
2142
|
: await this.#argumentsResolver({
|
|
2143
|
+
context: context,
|
|
2101
2144
|
data: requestHeaders?.get(argMetadata.key) || undefined,
|
|
2102
2145
|
validationSchema: argMetadata.validationSchema,
|
|
2103
2146
|
argumentIndex: argMetadata.index,
|
|
@@ -2111,6 +2154,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2111
2154
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2112
2155
|
? parameters?.[argMetadata.key] || undefined
|
|
2113
2156
|
: await this.#argumentsResolver({
|
|
2157
|
+
context: context,
|
|
2114
2158
|
data: parameters?.[argMetadata.key] || undefined,
|
|
2115
2159
|
validationSchema: argMetadata.validationSchema,
|
|
2116
2160
|
argumentIndex: argMetadata.index,
|
|
@@ -2127,6 +2171,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2127
2171
|
args[argMetadata.index] = !argMetadata.validationSchema
|
|
2128
2172
|
? context.get(argMetadata.type)
|
|
2129
2173
|
: await this.#argumentsResolver({
|
|
2174
|
+
context: context,
|
|
2130
2175
|
data: context.get(argMetadata.type),
|
|
2131
2176
|
validationSchema: argMetadata.validationSchema,
|
|
2132
2177
|
argumentIndex: argMetadata.index,
|
|
@@ -2164,6 +2209,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2164
2209
|
controllerActionArguments[argMetadata.index] = !argMetadata.validationSchema
|
|
2165
2210
|
? await request?.[argMetadata.parser || "json"]()
|
|
2166
2211
|
: await this.#argumentsResolver({
|
|
2212
|
+
context: context,
|
|
2167
2213
|
data: await request?.[argMetadata.parser || "json"](),
|
|
2168
2214
|
validationSchema: argMetadata.validationSchema,
|
|
2169
2215
|
argumentIndex: argMetadata.index,
|
|
@@ -2179,6 +2225,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2179
2225
|
controllerActionArguments[argMetadata.index] = !argMetadata.validationSchema
|
|
2180
2226
|
? requestHeaders
|
|
2181
2227
|
: await this.#argumentsResolver({
|
|
2228
|
+
context: context,
|
|
2182
2229
|
data: requestHeaders?.toJSON(),
|
|
2183
2230
|
validationSchema: argMetadata.validationSchema,
|
|
2184
2231
|
argumentIndex: argMetadata.index,
|
|
@@ -2189,6 +2236,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2189
2236
|
controllerActionArguments[argMetadata.index] = !argMetadata.validationSchema
|
|
2190
2237
|
? requestHeaders?.get(argMetadata.key) || undefined
|
|
2191
2238
|
: await this.#argumentsResolver({
|
|
2239
|
+
context: context,
|
|
2192
2240
|
data: requestHeaders?.get(argMetadata.key) || undefined,
|
|
2193
2241
|
validationSchema: argMetadata.validationSchema,
|
|
2194
2242
|
argumentIndex: argMetadata.index,
|
|
@@ -2202,6 +2250,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2202
2250
|
controllerActionArguments[argMetadata.index] = !argMetadata.validationSchema
|
|
2203
2251
|
? parameters?.[argMetadata.key] || undefined
|
|
2204
2252
|
: await this.#argumentsResolver({
|
|
2253
|
+
context: context,
|
|
2205
2254
|
data: parameters?.[argMetadata.key] || undefined,
|
|
2206
2255
|
validationSchema: argMetadata.validationSchema,
|
|
2207
2256
|
argumentIndex: argMetadata.index,
|
|
@@ -2218,6 +2267,7 @@ export class Application<TRootClass extends Object = Object> {
|
|
|
2218
2267
|
controllerActionArguments[argMetadata.index] = !argMetadata.validationSchema
|
|
2219
2268
|
? context.get(argMetadata.type)
|
|
2220
2269
|
: await this.#argumentsResolver({
|
|
2270
|
+
context: context,
|
|
2221
2271
|
data: context.get(argMetadata.type),
|
|
2222
2272
|
validationSchema: argMetadata.validationSchema,
|
|
2223
2273
|
argumentIndex: argMetadata.index,
|
package/src/utils/functions.ts
CHANGED
|
@@ -11,3 +11,15 @@ export const inferStatusText = (httpCode: number): string => {
|
|
|
11
11
|
|
|
12
12
|
return "Unknown error";
|
|
13
13
|
};
|
|
14
|
+
|
|
15
|
+
type TCallable = <T>(...args: T[]) => unknown;
|
|
16
|
+
|
|
17
|
+
export const hasCallSignature = (value: any): value is TCallable => {
|
|
18
|
+
return (
|
|
19
|
+
typeof value === "function" &&
|
|
20
|
+
!(
|
|
21
|
+
value.prototype &&
|
|
22
|
+
Object.getOwnPropertyDescriptor(value, "prototype")?.writable === false
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
};
|