azurajs 3.0.1 → 3.0.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.
- package/dist/config/index.js +128 -6
- package/dist/config/index.js.map +1 -1
- package/dist/config/index.mjs +130 -1
- package/dist/config/index.mjs.map +1 -1
- package/dist/core/index.js +1100 -11
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +1102 -3
- package/dist/core/index.mjs.map +1 -1
- package/dist/decorators/index.js +117 -87
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/index.mjs +98 -1
- package/dist/decorators/index.mjs.map +1 -1
- package/dist/index.js +2592 -236
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2537 -9
- package/dist/index.mjs.map +1 -1
- package/dist/middleware/index.js +16 -7
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/index.mjs +17 -1
- package/dist/middleware/index.mjs.map +1 -1
- package/dist/plugins/index.js +1056 -73
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/index.mjs +1042 -1
- package/dist/plugins/index.mjs.map +1 -1
- package/dist/types/index.js +49 -12
- package/dist/types/index.js.map +1 -1
- package/dist/types/index.mjs +49 -2
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/index.js +551 -50
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +541 -3
- package/dist/utils/index.mjs.map +1 -1
- package/package.json +35 -17
- package/{dist/chunk-DR254CWJ.mjs → src/config/ConfigModule.ts} +169 -132
- package/src/config/index.ts +1 -0
- package/src/core/index.ts +2 -0
- package/src/core/router.ts +284 -0
- package/{dist/chunk-EYAHUNC7.mjs → src/core/server.ts} +590 -699
- package/src/decorators/Route.ts +110 -0
- package/src/decorators/index.ts +23 -0
- package/src/index.ts +12 -0
- package/src/middleware/LoggingMiddleware.ts +20 -0
- package/src/middleware/index.ts +1 -0
- package/src/plugins/CORSPlugin.ts +56 -0
- package/src/plugins/CircuitBreakerPlugin.ts +84 -0
- package/src/plugins/CompressionPlugin.ts +80 -0
- package/src/plugins/ETagPlugin.ts +31 -0
- package/src/plugins/HealthCheckPlugin.ts +57 -0
- package/src/plugins/HelmetPlugin.ts +89 -0
- package/src/plugins/JWTPlugin.ts +132 -0
- package/src/plugins/MultipartPlugin.ts +168 -0
- package/src/plugins/ProxyPlugin.ts +89 -0
- package/src/plugins/RateLimitPlugin.ts +96 -0
- package/src/plugins/RequestIdPlugin.ts +21 -0
- package/src/plugins/SSEPlugin.ts +114 -0
- package/src/plugins/SessionPlugin.ts +98 -0
- package/src/plugins/StaticPlugin.ts +152 -0
- package/src/plugins/TimeoutPlugin.ts +33 -0
- package/src/plugins/index.ts +18 -0
- package/src/types/common.type.ts +82 -0
- package/src/types/config.type.ts +57 -0
- package/{dist/chunk-OWUGAI5V.mjs → src/types/http/status.ts} +49 -51
- package/src/types/index.ts +55 -0
- package/src/types/plugins/plugin.type.ts +170 -0
- package/src/types/reflect.d.ts +14 -0
- package/src/types/routes.type.ts +70 -0
- package/src/utils/HttpError.ts +62 -0
- package/src/utils/IpResolver.ts +30 -0
- package/src/utils/Logger.ts +144 -0
- package/src/utils/Parser.ts +182 -0
- package/src/utils/cookies/CookieManager.ts +48 -0
- package/src/utils/index.ts +9 -0
- package/{dist/chunk-UWIFSGSQ.mjs → src/utils/validators/DTOValidator.ts} +145 -141
- package/src/utils/validators/SchemaValidator.ts +45 -0
- package/dist/chunk-3UFAWS2V.js +0 -392
- package/dist/chunk-3UFAWS2V.js.map +0 -1
- package/dist/chunk-4LSFAAZW.js +0 -4
- package/dist/chunk-4LSFAAZW.js.map +0 -1
- package/dist/chunk-7NSRIVZM.js +0 -54
- package/dist/chunk-7NSRIVZM.js.map +0 -1
- package/dist/chunk-AOG6NYAM.js +0 -144
- package/dist/chunk-AOG6NYAM.js.map +0 -1
- package/dist/chunk-DR254CWJ.mjs.map +0 -1
- package/dist/chunk-EYAHUNC7.mjs.map +0 -1
- package/dist/chunk-HHDQPIJN.mjs +0 -19
- package/dist/chunk-HHDQPIJN.mjs.map +0 -1
- package/dist/chunk-HHZNAGGI.js +0 -702
- package/dist/chunk-HHZNAGGI.js.map +0 -1
- package/dist/chunk-KJM5XCAY.js +0 -21
- package/dist/chunk-KJM5XCAY.js.map +0 -1
- package/dist/chunk-NLSZKAPA.mjs +0 -1044
- package/dist/chunk-NLSZKAPA.mjs.map +0 -1
- package/dist/chunk-OWUGAI5V.mjs.map +0 -1
- package/dist/chunk-POPNQEOK.js +0 -1063
- package/dist/chunk-POPNQEOK.js.map +0 -1
- package/dist/chunk-QPRW4YU4.js +0 -134
- package/dist/chunk-QPRW4YU4.js.map +0 -1
- package/dist/chunk-REJDZUZ5.mjs +0 -382
- package/dist/chunk-REJDZUZ5.mjs.map +0 -1
- package/dist/chunk-TC6N6TJZ.mjs +0 -100
- package/dist/chunk-TC6N6TJZ.mjs.map +0 -1
- package/dist/chunk-TEUXKMXP.js +0 -122
- package/dist/chunk-TEUXKMXP.js.map +0 -1
- package/dist/chunk-UWIFSGSQ.mjs.map +0 -1
- package/dist/chunk-YPBKY4KY.mjs +0 -3
- package/dist/chunk-YPBKY4KY.mjs.map +0 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CookieOptions } from "../../types/common.type.js";
|
|
2
|
+
|
|
3
|
+
export function serializeCookie(
|
|
4
|
+
name: string,
|
|
5
|
+
value: string,
|
|
6
|
+
options: CookieOptions = {},
|
|
7
|
+
): string {
|
|
8
|
+
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
9
|
+
|
|
10
|
+
if (options.maxAge != null) {
|
|
11
|
+
cookie += `; Max-Age=${Math.floor(options.maxAge)}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (options.expires) {
|
|
15
|
+
cookie += `; Expires=${options.expires.toUTCString()}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (options.domain) {
|
|
19
|
+
cookie += `; Domain=${options.domain}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
cookie += `; Path=${options.path ?? "/"}`;
|
|
23
|
+
|
|
24
|
+
if (options.secure) {
|
|
25
|
+
cookie += "; Secure";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (options.httpOnly !== false) {
|
|
29
|
+
cookie += "; HttpOnly";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (options.sameSite) {
|
|
33
|
+
cookie += `; SameSite=${options.sameSite}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return cookie;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function clearCookieHeader(
|
|
40
|
+
name: string,
|
|
41
|
+
options: CookieOptions = {},
|
|
42
|
+
): string {
|
|
43
|
+
return serializeCookie(name, "", {
|
|
44
|
+
...options,
|
|
45
|
+
maxAge: 0,
|
|
46
|
+
expires: new Date(0),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Logger } from "./Logger.js";
|
|
2
|
+
export { HttpError } from "./HttpError.js";
|
|
3
|
+
export { resolveIp } from "./IpResolver.js";
|
|
4
|
+
export { parseQueryString, parseCookies, parseBody, parseUrl } from "./Parser.js";
|
|
5
|
+
export { serializeCookie, clearCookieHeader } from "./cookies/CookieManager.js";
|
|
6
|
+
export { DTOValidator } from "./validators/DTOValidator.js";
|
|
7
|
+
export type { Schema, ValidationResult, ValidationError } from "./validators/DTOValidator.js";
|
|
8
|
+
export { SchemaValidator } from "./validators/SchemaValidator.js";
|
|
9
|
+
export type { ExternalSchema, SchemaValidationResult } from "./validators/SchemaValidator.js";
|
|
@@ -1,141 +1,145 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (rule.
|
|
74
|
-
errors.push({
|
|
75
|
-
field,
|
|
76
|
-
message: rule.message ?? `${field} must
|
|
77
|
-
value
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (rule.
|
|
81
|
-
errors.push({
|
|
82
|
-
field,
|
|
83
|
-
message: rule.message ?? `${field}
|
|
84
|
-
value
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
errors.push({
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
1
|
+
export interface ValidationError {
|
|
2
|
+
field: string;
|
|
3
|
+
message: string;
|
|
4
|
+
value?: any;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ValidationResult {
|
|
8
|
+
valid: boolean;
|
|
9
|
+
errors: ValidationError[];
|
|
10
|
+
data?: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type RuleType = "string" | "number" | "boolean" | "object" | "array";
|
|
14
|
+
|
|
15
|
+
interface FieldRule {
|
|
16
|
+
type?: RuleType;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
min?: number;
|
|
19
|
+
max?: number;
|
|
20
|
+
pattern?: RegExp;
|
|
21
|
+
enum?: any[];
|
|
22
|
+
custom?: (value: any) => string | null;
|
|
23
|
+
message?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type Schema = Record<string, FieldRule>;
|
|
27
|
+
|
|
28
|
+
export class DTOValidator {
|
|
29
|
+
static validate(data: any, schema: Schema): ValidationResult {
|
|
30
|
+
const errors: ValidationError[] = [];
|
|
31
|
+
|
|
32
|
+
if (data == null || typeof data !== "object") {
|
|
33
|
+
return {
|
|
34
|
+
valid: false,
|
|
35
|
+
errors: [{ field: "_root", message: "Request body must be an object" }],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const [field, rule] of Object.entries(schema)) {
|
|
40
|
+
const value = data[field];
|
|
41
|
+
|
|
42
|
+
if (rule.required && (value === undefined || value === null || value === "")) {
|
|
43
|
+
errors.push({
|
|
44
|
+
field,
|
|
45
|
+
message: rule.message ?? `${field} is required`,
|
|
46
|
+
value,
|
|
47
|
+
});
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (value === undefined || value === null) continue;
|
|
52
|
+
|
|
53
|
+
if (rule.type) {
|
|
54
|
+
const actualType = Array.isArray(value) ? "array" : typeof value;
|
|
55
|
+
if (actualType !== rule.type) {
|
|
56
|
+
errors.push({
|
|
57
|
+
field,
|
|
58
|
+
message: rule.message ?? `${field} must be of type ${rule.type}`,
|
|
59
|
+
value,
|
|
60
|
+
});
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (rule.type === "string" || typeof value === "string") {
|
|
66
|
+
if (rule.min !== undefined && value.length < rule.min) {
|
|
67
|
+
errors.push({
|
|
68
|
+
field,
|
|
69
|
+
message: rule.message ?? `${field} must be at least ${rule.min} characters`,
|
|
70
|
+
value,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
if (rule.max !== undefined && value.length > rule.max) {
|
|
74
|
+
errors.push({
|
|
75
|
+
field,
|
|
76
|
+
message: rule.message ?? `${field} must be at most ${rule.max} characters`,
|
|
77
|
+
value,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (rule.pattern && !rule.pattern.test(value)) {
|
|
81
|
+
errors.push({
|
|
82
|
+
field,
|
|
83
|
+
message: rule.message ?? `${field} has invalid format`,
|
|
84
|
+
value,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (rule.type === "number" || typeof value === "number") {
|
|
90
|
+
if (rule.min !== undefined && value < rule.min) {
|
|
91
|
+
errors.push({
|
|
92
|
+
field,
|
|
93
|
+
message: rule.message ?? `${field} must be at least ${rule.min}`,
|
|
94
|
+
value,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (rule.max !== undefined && value > rule.max) {
|
|
98
|
+
errors.push({
|
|
99
|
+
field,
|
|
100
|
+
message: rule.message ?? `${field} must be at most ${rule.max}`,
|
|
101
|
+
value,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (rule.type === "array" && Array.isArray(value)) {
|
|
107
|
+
if (rule.min !== undefined && value.length < rule.min) {
|
|
108
|
+
errors.push({
|
|
109
|
+
field,
|
|
110
|
+
message: rule.message ?? `${field} must have at least ${rule.min} items`,
|
|
111
|
+
value,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (rule.max !== undefined && value.length > rule.max) {
|
|
115
|
+
errors.push({
|
|
116
|
+
field,
|
|
117
|
+
message: rule.message ?? `${field} must have at most ${rule.max} items`,
|
|
118
|
+
value,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (rule.enum && !rule.enum.includes(value)) {
|
|
124
|
+
errors.push({
|
|
125
|
+
field,
|
|
126
|
+
message: rule.message ?? `${field} must be one of: ${rule.enum.join(", ")}`,
|
|
127
|
+
value,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (rule.custom) {
|
|
132
|
+
const customError = rule.custom(value);
|
|
133
|
+
if (customError) {
|
|
134
|
+
errors.push({ field, message: customError, value });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
valid: errors.length === 0,
|
|
141
|
+
errors,
|
|
142
|
+
data: errors.length === 0 ? data : undefined,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface ExternalSchema {
|
|
2
|
+
parse?: (data: any) => any;
|
|
3
|
+
safeParse?: (data: any) => { success: boolean; data?: any; error?: any };
|
|
4
|
+
validate?: (data: any) => { value?: any; error?: any };
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface SchemaValidationResult {
|
|
8
|
+
valid: boolean;
|
|
9
|
+
data?: any;
|
|
10
|
+
errors?: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class SchemaValidator {
|
|
14
|
+
static validate(data: any, schema: ExternalSchema): SchemaValidationResult {
|
|
15
|
+
// Zod-style schema
|
|
16
|
+
if (typeof schema.safeParse === "function") {
|
|
17
|
+
const result = schema.safeParse(data);
|
|
18
|
+
if (result.success) {
|
|
19
|
+
return { valid: true, data: result.data };
|
|
20
|
+
}
|
|
21
|
+
return { valid: false, errors: result.error };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Zod parse (throws on error)
|
|
25
|
+
if (typeof schema.parse === "function") {
|
|
26
|
+
try {
|
|
27
|
+
const parsed = schema.parse(data);
|
|
28
|
+
return { valid: true, data: parsed };
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return { valid: false, errors: err };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Joi-style schema
|
|
35
|
+
if (typeof schema.validate === "function") {
|
|
36
|
+
const result = schema.validate(data);
|
|
37
|
+
if (result.error) {
|
|
38
|
+
return { valid: false, errors: result.error };
|
|
39
|
+
}
|
|
40
|
+
return { valid: true, data: result.value };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { valid: true, data };
|
|
44
|
+
}
|
|
45
|
+
}
|