azurajs 2.1.2 → 2.2.0
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/README.md +244 -7
- package/dist/LoggingMiddleware-BQKJUBqT.d.ts +6 -0
- package/dist/LoggingMiddleware-CYNvypha.d.cts +6 -0
- package/dist/Server-Ba-EFdi2.d.ts +53 -0
- package/dist/Server-CY3k1FIL.d.cts +53 -0
- package/dist/common.type-BoV71o_C.d.ts +11 -0
- package/dist/common.type-Ct06XeYQ.d.cts +11 -0
- package/dist/config.cjs +89 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +51 -0
- package/dist/config.d.ts +51 -0
- package/dist/config.js +83 -0
- package/dist/config.js.map +1 -0
- package/dist/cookies.cjs +32 -0
- package/dist/cookies.cjs.map +1 -0
- package/dist/cookies.d.cts +8 -0
- package/dist/cookies.d.ts +8 -0
- package/dist/cookies.js +29 -0
- package/dist/cookies.js.map +1 -0
- package/dist/cors.cjs +29 -0
- package/dist/cors.cjs.map +1 -0
- package/dist/cors.d.cts +14 -0
- package/dist/cors.d.ts +14 -0
- package/dist/cors.js +27 -0
- package/dist/cors.js.map +1 -0
- package/dist/decorators.cjs +141 -0
- package/dist/decorators.cjs.map +1 -0
- package/dist/decorators.d.cts +29 -0
- package/dist/decorators.d.ts +29 -0
- package/dist/decorators.js +122 -0
- package/dist/decorators.js.map +1 -0
- package/dist/http-error.cjs +14 -0
- package/dist/http-error.cjs.map +1 -0
- package/dist/http-error.d.cts +7 -0
- package/dist/http-error.d.ts +7 -0
- package/dist/http-error.js +12 -0
- package/dist/http-error.js.map +1 -0
- package/dist/index.cjs +910 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +899 -0
- package/dist/index.js.map +1 -0
- package/dist/infra.cjs +811 -0
- package/dist/infra.cjs.map +1 -0
- package/dist/infra.d.cts +8 -0
- package/dist/infra.d.ts +8 -0
- package/dist/infra.js +800 -0
- package/dist/infra.js.map +1 -0
- package/dist/logger.cjs +26 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +8 -0
- package/dist/logger.d.ts +8 -0
- package/dist/logger.js +24 -0
- package/dist/logger.js.map +1 -0
- package/dist/middleware.cjs +117 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +10 -0
- package/dist/middleware.d.ts +10 -0
- package/dist/middleware.js +114 -0
- package/dist/middleware.js.map +1 -0
- package/dist/plugins.cjs +52 -0
- package/dist/plugins.cjs.map +1 -0
- package/dist/plugins.d.cts +6 -0
- package/dist/plugins.d.ts +6 -0
- package/dist/plugins.js +49 -0
- package/dist/plugins.js.map +1 -0
- package/dist/rate-limit.cjs +27 -0
- package/dist/rate-limit.cjs.map +1 -0
- package/dist/rate-limit.d.cts +8 -0
- package/dist/rate-limit.d.ts +8 -0
- package/dist/rate-limit.js +25 -0
- package/dist/rate-limit.js.map +1 -0
- package/dist/request.type-CJ-EGGcM.d.cts +22 -0
- package/dist/request.type-CJ-EGGcM.d.ts +22 -0
- package/dist/response.type-d6e6eU9D.d.cts +33 -0
- package/dist/response.type-d6e6eU9D.d.ts +33 -0
- package/dist/router.cjs +111 -0
- package/dist/router.cjs.map +1 -0
- package/dist/router.d.cts +27 -0
- package/dist/router.d.ts +27 -0
- package/dist/router.js +109 -0
- package/dist/router.js.map +1 -0
- package/dist/routes.type-VPROfhnL.d.cts +14 -0
- package/dist/routes.type-VPROfhnL.d.ts +14 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +6 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.cjs +149 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +10 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +141 -0
- package/dist/utils.js.map +1 -0
- package/dist/validations.type-D4ZhF5g6.d.cts +6 -0
- package/dist/validations.type-D4ZhF5g6.d.ts +6 -0
- package/dist/validators.cjs +69 -0
- package/dist/validators.cjs.map +1 -0
- package/dist/validators.d.cts +11 -0
- package/dist/validators.d.ts +11 -0
- package/dist/validators.js +65 -0
- package/dist/validators.js.map +1 -0
- package/package.json +85 -66
- package/src/index.ts +1 -3
- package/src/infra/Router.ts +53 -3
- package/src/infra/Server.ts +37 -10
- package/src/infra/index.ts +1 -1
- package/src/shared/config/ConfigModule.ts +1 -0
package/dist/config.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
5
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
6
|
+
}) : x)(function(x) {
|
|
7
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
8
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
9
|
+
});
|
|
10
|
+
var ConfigModule = class {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.config = {};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Load config files first (azura.config.*)
|
|
16
|
+
* Recivied error if config file not found or invalid format
|
|
17
|
+
* @param configFiles
|
|
18
|
+
*/
|
|
19
|
+
initSync() {
|
|
20
|
+
const cdw = process.cwd();
|
|
21
|
+
const configFiles = [
|
|
22
|
+
"azura.config.ts",
|
|
23
|
+
"azura.config.json",
|
|
24
|
+
"azura.config.yaml",
|
|
25
|
+
"azura.config.yml"
|
|
26
|
+
];
|
|
27
|
+
let loaded = false;
|
|
28
|
+
for (const fileName of configFiles) {
|
|
29
|
+
const filePath = path.join(cdw, fileName);
|
|
30
|
+
if (!existsSync(filePath)) continue;
|
|
31
|
+
const extension = path.extname(fileName);
|
|
32
|
+
const raw = readFileSync(filePath, "utf8");
|
|
33
|
+
try {
|
|
34
|
+
let parsed;
|
|
35
|
+
switch (extension) {
|
|
36
|
+
case ".ts":
|
|
37
|
+
const mod = __require(filePath);
|
|
38
|
+
parsed = mod.default || mod;
|
|
39
|
+
break;
|
|
40
|
+
case ".json":
|
|
41
|
+
parsed = JSON.parse(raw);
|
|
42
|
+
break;
|
|
43
|
+
case ".yaml":
|
|
44
|
+
case ".yml":
|
|
45
|
+
parsed = __require("js-yaml").load(raw);
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
throw new Error(`Invalid config file extension: ${extension}`);
|
|
49
|
+
}
|
|
50
|
+
this.config = { ...this.config, ...parsed };
|
|
51
|
+
loaded = true;
|
|
52
|
+
break;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(`Error loading config file: ${filePath}
|
|
55
|
+
${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!loaded) {
|
|
59
|
+
throw new Error("Nothing config file found in the current directory.");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get all configs from loaded config file
|
|
64
|
+
* @returns ConfigTypes
|
|
65
|
+
*/
|
|
66
|
+
getAll() {
|
|
67
|
+
return this.config;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Return a specific config from loaded config file
|
|
71
|
+
*
|
|
72
|
+
* @template T
|
|
73
|
+
* @param {T} key - key of the config to retrieve
|
|
74
|
+
* @returns {ConfigTypes[T]}
|
|
75
|
+
*/
|
|
76
|
+
get(key) {
|
|
77
|
+
return this.config[key];
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export { ConfigModule };
|
|
82
|
+
//# sourceMappingURL=config.js.map
|
|
83
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/shared/config/ConfigModule.ts"],"names":[],"mappings":";;;;;;;;;AAsCO,IAAM,eAAN,MAAmB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,MAAA,EAAO;AAC1C,QAAA,MAAA,GAAS,IAAA;AACT,QAAA;AAAA,MACF,SAAS,KAAA,EAAoB;AAC3B,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,2BAAA,EAA8B,QAAQ;AAAA,EAAK,KAAA,CAAM,OAAO,CAAA,CAAE,CAAA;AAAA,MAC5E;AAAA,IACF;AAEA,IAAA,IAAI,CAAC,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,MAAM,qDAAqD,CAAA;AAAA,IACvE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAA,GAAsB;AACpB,IAAA,OAAO,IAAA,CAAK,MAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,IAAiC,GAAA,EAAwB;AACvD,IAAA,OAAO,IAAA,CAAK,OAAO,GAAG,CAAA;AAAA,EACxB;AACF","file":"config.js","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Config Files Extensions Supported\n * ex: azura.config.*extension\n */\n\ntype SupportedConfigFile = \".ts\" | \".json\" | \".yaml\" | \".yml\";\n\nexport type ConfigTypes = {\n environment?: \"development\" | \"production\";\n debug?: boolean;\n server?: {\n port?: number;\n cluster?: boolean;\n ipHost?: boolean;\n https?: boolean;\n };\n plugins?: {\n rateLimit?: {\n enabled: boolean;\n limit: number;\n timeframe: number;\n };\n cors?: {\n enabled: boolean;\n origins: string | string[];\n methods: string | string[];\n allowedHeaders: string | string[];\n };\n };\n logging?: {\n enabled?: boolean;\n showDetails?: boolean;\n };\n};\n\nexport class ConfigModule {\n private config: ConfigTypes = {};\n\n /**\n * Load config files first (azura.config.*)\n * Recivied error if config file not found or invalid format\n * @param configFiles\n */\n\n initSync(): void {\n const cdw = process.cwd();\n const configFiles = [\n \"azura.config.ts\",\n \"azura.config.json\",\n \"azura.config.yaml\",\n \"azura.config.yml\",\n ];\n\n let loaded = false;\n for (const fileName of configFiles) {\n const filePath = path.join(cdw, fileName);\n if (!existsSync(filePath)) continue;\n\n const extension = path.extname(fileName) as SupportedConfigFile;\n const raw = readFileSync(filePath, \"utf8\");\n\n try {\n let parsed: ConfigTypes;\n switch (extension) {\n case \".ts\":\n const mod = require(filePath);\n parsed = mod.default || mod;\n break;\n case \".json\":\n parsed = JSON.parse(raw);\n break;\n case \".yaml\":\n case \".yml\":\n parsed = require(\"js-yaml\").load(raw);\n break;\n default:\n throw new Error(`Invalid config file extension: ${extension}`);\n }\n\n this.config = { ...this.config, ...parsed };\n loaded = true;\n break;\n } catch (error: Error | any) {\n throw new Error(`Error loading config file: ${filePath}\\n${error.message}`);\n }\n }\n\n if (!loaded) {\n throw new Error(\"Nothing config file found in the current directory.\");\n }\n }\n\n /**\n * Get all configs from loaded config file\n * @returns ConfigTypes\n */\n\n getAll(): ConfigTypes {\n return this.config;\n }\n\n /**\n * Return a specific config from loaded config file\n *\n * @template T\n * @param {T} key - key of the config to retrieve\n * @returns {ConfigTypes[T]}\n */\n\n get<T extends keyof ConfigTypes>(key: T): ConfigTypes[T] {\n return this.config[key];\n }\n}\n"]}
|
package/dist/cookies.cjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/utils/cookies/ParserCookie.ts
|
|
4
|
+
function parseCookiesHeader(header) {
|
|
5
|
+
if (!header) return {};
|
|
6
|
+
return header.split(";").reduce((acc, pair) => {
|
|
7
|
+
const [k, ...vals] = pair.trim().split("=");
|
|
8
|
+
if (!k) return acc;
|
|
9
|
+
acc[k] = decodeURIComponent(vals.join("="));
|
|
10
|
+
return acc;
|
|
11
|
+
}, {});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/utils/cookies/SerializeCookie.ts
|
|
15
|
+
function serializeCookie(name, val, opts = {}) {
|
|
16
|
+
const encode = opts.encode ?? encodeURIComponent;
|
|
17
|
+
let str = `${name}=${encode(val)}`;
|
|
18
|
+
if (opts.maxAge != null && !Number.isNaN(Number(opts.maxAge)))
|
|
19
|
+
str += `; Max-Age=${Math.floor(Number(opts.maxAge))}`;
|
|
20
|
+
if (opts.domain) str += `; Domain=${opts.domain}`;
|
|
21
|
+
if (opts.path) str += `; Path=${opts.path}`;
|
|
22
|
+
if (opts.expires) str += `; Expires=${opts.expires.toUTCString()}`;
|
|
23
|
+
if (opts.httpOnly) str += `; HttpOnly`;
|
|
24
|
+
if (opts.secure) str += `; Secure`;
|
|
25
|
+
if (opts.sameSite) str += `; SameSite=${opts.sameSite}`;
|
|
26
|
+
return str;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.parseCookiesHeader = parseCookiesHeader;
|
|
30
|
+
exports.serializeCookie = serializeCookie;
|
|
31
|
+
//# sourceMappingURL=cookies.cjs.map
|
|
32
|
+
//# sourceMappingURL=cookies.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/cookies/ParserCookie.ts","../src/utils/cookies/SerializeCookie.ts"],"names":[],"mappings":";;;AAAO,SAAS,mBAAmB,MAAA,EAAoD;AACrF,EAAA,IAAI,CAAC,MAAA,EAAQ,OAAO,EAAC;AACrB,EAAA,OAAO,OAAO,KAAA,CAAM,GAAG,EAAE,MAAA,CAA+B,CAAC,KAAK,IAAA,KAAS;AACrE,IAAA,MAAM,CAAC,GAAG,GAAG,IAAI,IAAI,IAAA,CAAK,IAAA,EAAK,CAAE,KAAA,CAAM,GAAG,CAAA;AAC1C,IAAA,IAAI,CAAC,GAAG,OAAO,GAAA;AACf,IAAA,GAAA,CAAI,CAAC,CAAA,GAAI,kBAAA,CAAmB,IAAA,CAAK,IAAA,CAAK,GAAG,CAAC,CAAA;AAC1C,IAAA,OAAO,GAAA;AAAA,EACT,CAAA,EAAG,EAAE,CAAA;AACP;;;ACNO,SAAS,eAAA,CAAgB,IAAA,EAAc,GAAA,EAAa,IAAA,GAAsB,EAAC,EAAW;AAC3F,EAAA,MAAM,MAAA,GAAS,KAAK,MAAA,IAAU,kBAAA;AAC9B,EAAA,IAAI,MAAM,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,MAAA,CAAO,GAAG,CAAC,CAAA,CAAA;AAChC,EAAA,IAAI,IAAA,CAAK,UAAU,IAAA,IAAQ,CAAC,OAAO,KAAA,CAAM,MAAA,CAAO,IAAA,CAAK,MAAM,CAAC,CAAA;AAC1D,IAAA,GAAA,IAAO,aAAa,IAAA,CAAK,KAAA,CAAM,OAAO,IAAA,CAAK,MAAM,CAAC,CAAC,CAAA,CAAA;AACrD,EAAA,IAAI,IAAA,CAAK,MAAA,EAAQ,GAAA,IAAO,CAAA,SAAA,EAAY,KAAK,MAAM,CAAA,CAAA;AAC/C,EAAA,IAAI,IAAA,CAAK,IAAA,EAAM,GAAA,IAAO,CAAA,OAAA,EAAU,KAAK,IAAI,CAAA,CAAA;AACzC,EAAA,IAAI,KAAK,OAAA,EAAS,GAAA,IAAO,aAAa,IAAA,CAAK,OAAA,CAAQ,aAAa,CAAA,CAAA;AAChE,EAAA,IAAI,IAAA,CAAK,UAAU,GAAA,IAAO,CAAA,UAAA,CAAA;AAC1B,EAAA,IAAI,IAAA,CAAK,QAAQ,GAAA,IAAO,CAAA,QAAA,CAAA;AACxB,EAAA,IAAI,IAAA,CAAK,QAAA,EAAU,GAAA,IAAO,CAAA,WAAA,EAAc,KAAK,QAAQ,CAAA,CAAA;AACrD,EAAA,OAAO,GAAA;AACT","file":"cookies.cjs","sourcesContent":["export function parseCookiesHeader(header: string | undefined): Record<string, string> {\n if (!header) return {};\n return header.split(\";\").reduce<Record<string, string>>((acc, pair) => {\n const [k, ...vals] = pair.trim().split(\"=\");\n if (!k) return acc;\n acc[k] = decodeURIComponent(vals.join(\"=\"));\n return acc;\n }, {});\n}\n","import type { CookieOptions } from \"../../types/http/response.type\";\n\nexport function serializeCookie(name: string, val: string, opts: CookieOptions = {}): string {\n const encode = opts.encode ?? encodeURIComponent;\n let str = `${name}=${encode(val)}`;\n if (opts.maxAge != null && !Number.isNaN(Number(opts.maxAge)))\n str += `; Max-Age=${Math.floor(Number(opts.maxAge))}`;\n if (opts.domain) str += `; Domain=${opts.domain}`;\n if (opts.path) str += `; Path=${opts.path}`;\n if (opts.expires) str += `; Expires=${opts.expires.toUTCString()}`;\n if (opts.httpOnly) str += `; HttpOnly`;\n if (opts.secure) str += `; Secure`;\n if (opts.sameSite) str += `; SameSite=${opts.sameSite}`;\n return str;\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { C as CookieOptions } from './response.type-d6e6eU9D.cjs';
|
|
2
|
+
import 'node:http';
|
|
3
|
+
|
|
4
|
+
declare function parseCookiesHeader(header: string | undefined): Record<string, string>;
|
|
5
|
+
|
|
6
|
+
declare function serializeCookie(name: string, val: string, opts?: CookieOptions): string;
|
|
7
|
+
|
|
8
|
+
export { parseCookiesHeader, serializeCookie };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { C as CookieOptions } from './response.type-d6e6eU9D.js';
|
|
2
|
+
import 'node:http';
|
|
3
|
+
|
|
4
|
+
declare function parseCookiesHeader(header: string | undefined): Record<string, string>;
|
|
5
|
+
|
|
6
|
+
declare function serializeCookie(name: string, val: string, opts?: CookieOptions): string;
|
|
7
|
+
|
|
8
|
+
export { parseCookiesHeader, serializeCookie };
|
package/dist/cookies.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/utils/cookies/ParserCookie.ts
|
|
2
|
+
function parseCookiesHeader(header) {
|
|
3
|
+
if (!header) return {};
|
|
4
|
+
return header.split(";").reduce((acc, pair) => {
|
|
5
|
+
const [k, ...vals] = pair.trim().split("=");
|
|
6
|
+
if (!k) return acc;
|
|
7
|
+
acc[k] = decodeURIComponent(vals.join("="));
|
|
8
|
+
return acc;
|
|
9
|
+
}, {});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/utils/cookies/SerializeCookie.ts
|
|
13
|
+
function serializeCookie(name, val, opts = {}) {
|
|
14
|
+
const encode = opts.encode ?? encodeURIComponent;
|
|
15
|
+
let str = `${name}=${encode(val)}`;
|
|
16
|
+
if (opts.maxAge != null && !Number.isNaN(Number(opts.maxAge)))
|
|
17
|
+
str += `; Max-Age=${Math.floor(Number(opts.maxAge))}`;
|
|
18
|
+
if (opts.domain) str += `; Domain=${opts.domain}`;
|
|
19
|
+
if (opts.path) str += `; Path=${opts.path}`;
|
|
20
|
+
if (opts.expires) str += `; Expires=${opts.expires.toUTCString()}`;
|
|
21
|
+
if (opts.httpOnly) str += `; HttpOnly`;
|
|
22
|
+
if (opts.secure) str += `; Secure`;
|
|
23
|
+
if (opts.sameSite) str += `; SameSite=${opts.sameSite}`;
|
|
24
|
+
return str;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseCookiesHeader, serializeCookie };
|
|
28
|
+
//# sourceMappingURL=cookies.js.map
|
|
29
|
+
//# sourceMappingURL=cookies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils/cookies/ParserCookie.ts","../src/utils/cookies/SerializeCookie.ts"],"names":[],"mappings":";AAAO,SAAS,mBAAmB,MAAA,EAAoD;AACrF,EAAA,IAAI,CAAC,MAAA,EAAQ,OAAO,EAAC;AACrB,EAAA,OAAO,OAAO,KAAA,CAAM,GAAG,EAAE,MAAA,CAA+B,CAAC,KAAK,IAAA,KAAS;AACrE,IAAA,MAAM,CAAC,GAAG,GAAG,IAAI,IAAI,IAAA,CAAK,IAAA,EAAK,CAAE,KAAA,CAAM,GAAG,CAAA;AAC1C,IAAA,IAAI,CAAC,GAAG,OAAO,GAAA;AACf,IAAA,GAAA,CAAI,CAAC,CAAA,GAAI,kBAAA,CAAmB,IAAA,CAAK,IAAA,CAAK,GAAG,CAAC,CAAA;AAC1C,IAAA,OAAO,GAAA;AAAA,EACT,CAAA,EAAG,EAAE,CAAA;AACP;;;ACNO,SAAS,eAAA,CAAgB,IAAA,EAAc,GAAA,EAAa,IAAA,GAAsB,EAAC,EAAW;AAC3F,EAAA,MAAM,MAAA,GAAS,KAAK,MAAA,IAAU,kBAAA;AAC9B,EAAA,IAAI,MAAM,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,MAAA,CAAO,GAAG,CAAC,CAAA,CAAA;AAChC,EAAA,IAAI,IAAA,CAAK,UAAU,IAAA,IAAQ,CAAC,OAAO,KAAA,CAAM,MAAA,CAAO,IAAA,CAAK,MAAM,CAAC,CAAA;AAC1D,IAAA,GAAA,IAAO,aAAa,IAAA,CAAK,KAAA,CAAM,OAAO,IAAA,CAAK,MAAM,CAAC,CAAC,CAAA,CAAA;AACrD,EAAA,IAAI,IAAA,CAAK,MAAA,EAAQ,GAAA,IAAO,CAAA,SAAA,EAAY,KAAK,MAAM,CAAA,CAAA;AAC/C,EAAA,IAAI,IAAA,CAAK,IAAA,EAAM,GAAA,IAAO,CAAA,OAAA,EAAU,KAAK,IAAI,CAAA,CAAA;AACzC,EAAA,IAAI,KAAK,OAAA,EAAS,GAAA,IAAO,aAAa,IAAA,CAAK,OAAA,CAAQ,aAAa,CAAA,CAAA;AAChE,EAAA,IAAI,IAAA,CAAK,UAAU,GAAA,IAAO,CAAA,UAAA,CAAA;AAC1B,EAAA,IAAI,IAAA,CAAK,QAAQ,GAAA,IAAO,CAAA,QAAA,CAAA;AACxB,EAAA,IAAI,IAAA,CAAK,QAAA,EAAU,GAAA,IAAO,CAAA,WAAA,EAAc,KAAK,QAAQ,CAAA,CAAA;AACrD,EAAA,OAAO,GAAA;AACT","file":"cookies.js","sourcesContent":["export function parseCookiesHeader(header: string | undefined): Record<string, string> {\n if (!header) return {};\n return header.split(\";\").reduce<Record<string, string>>((acc, pair) => {\n const [k, ...vals] = pair.trim().split(\"=\");\n if (!k) return acc;\n acc[k] = decodeURIComponent(vals.join(\"=\"));\n return acc;\n }, {});\n}\n","import type { CookieOptions } from \"../../types/http/response.type\";\n\nexport function serializeCookie(name: string, val: string, opts: CookieOptions = {}): string {\n const encode = opts.encode ?? encodeURIComponent;\n let str = `${name}=${encode(val)}`;\n if (opts.maxAge != null && !Number.isNaN(Number(opts.maxAge)))\n str += `; Max-Age=${Math.floor(Number(opts.maxAge))}`;\n if (opts.domain) str += `; Domain=${opts.domain}`;\n if (opts.path) str += `; Path=${opts.path}`;\n if (opts.expires) str += `; Expires=${opts.expires.toUTCString()}`;\n if (opts.httpOnly) str += `; HttpOnly`;\n if (opts.secure) str += `; Secure`;\n if (opts.sameSite) str += `; SameSite=${opts.sameSite}`;\n return str;\n}\n"]}
|
package/dist/cors.cjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/shared/plugins/CORSPlugin.ts
|
|
4
|
+
function cors(opts) {
|
|
5
|
+
const { origin, methods, allowedHeaders } = opts;
|
|
6
|
+
return (ctx, next) => {
|
|
7
|
+
ctx.response.setHeader(
|
|
8
|
+
"Access-Control-Allow-Origin",
|
|
9
|
+
Array.isArray(origin) ? origin.join(",") : origin
|
|
10
|
+
);
|
|
11
|
+
ctx.response.setHeader(
|
|
12
|
+
"Access-Control-Allow-Methods",
|
|
13
|
+
Array.isArray(methods) ? methods.join(",") : methods
|
|
14
|
+
);
|
|
15
|
+
ctx.response.setHeader(
|
|
16
|
+
"Access-Control-Allow-Headers",
|
|
17
|
+
Array.isArray(allowedHeaders) ? allowedHeaders.join(",") : allowedHeaders
|
|
18
|
+
);
|
|
19
|
+
if (ctx.request.method === "OPTIONS") {
|
|
20
|
+
ctx.response.writeHead(204);
|
|
21
|
+
return ctx.response.end();
|
|
22
|
+
}
|
|
23
|
+
return next();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.cors = cors;
|
|
28
|
+
//# sourceMappingURL=cors.cjs.map
|
|
29
|
+
//# sourceMappingURL=cors.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/shared/plugins/CORSPlugin.ts"],"names":[],"mappings":";;;AAGO,SAAS,KAAK,IAAA,EAAmB;AACtC,EAAA,MAAM,EAAE,MAAA,EAAQ,OAAA,EAAS,cAAA,EAAe,GAAI,IAAA;AAE5C,EAAA,OAAO,CAAC,KAAkB,IAAA,KAA8B;AACtD,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,6BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,MAAM,IAAI,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC7C;AACA,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,8BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,OAAO,IAAI,OAAA,CAAQ,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC/C;AACA,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,8BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,cAAc,IAAI,cAAA,CAAe,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC7D;AAEA,IAAA,IAAI,GAAA,CAAI,OAAA,CAAQ,MAAA,KAAW,SAAA,EAAW;AACpC,MAAA,GAAA,CAAI,QAAA,CAAS,UAAU,GAAG,CAAA;AAC1B,MAAA,OAAO,GAAA,CAAI,SAAS,GAAA,EAAI;AAAA,IAC1B;AAEA,IAAA,OAAO,IAAA,EAAK;AAAA,EACd,CAAA;AACF","file":"cors.cjs","sourcesContent":["import type { HttpContext } from \"../../types/common.type\";\nimport type { CorsOptions } from \"../../types/plugins/cors.type\";\n\nexport function cors(opts: CorsOptions) {\n const { origin, methods, allowedHeaders } = opts;\n\n return (ctx: HttpContext, next: () => Promise<void>) => {\n ctx.response.setHeader(\n \"Access-Control-Allow-Origin\",\n Array.isArray(origin) ? origin.join(\",\") : origin\n );\n ctx.response.setHeader(\n \"Access-Control-Allow-Methods\",\n Array.isArray(methods) ? methods.join(\",\") : methods\n );\n ctx.response.setHeader(\n \"Access-Control-Allow-Headers\",\n Array.isArray(allowedHeaders) ? allowedHeaders.join(\",\") : allowedHeaders\n );\n\n if (ctx.request.method === \"OPTIONS\") {\n ctx.response.writeHead(204);\n return ctx.response.end();\n }\n\n return next();\n };\n}\n"]}
|
package/dist/cors.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { R as ResponseServer } from './response.type-d6e6eU9D.cjs';
|
|
2
|
+
import { H as HttpContext } from './common.type-Ct06XeYQ.cjs';
|
|
3
|
+
import 'node:http';
|
|
4
|
+
import './request.type-CJ-EGGcM.cjs';
|
|
5
|
+
|
|
6
|
+
interface CorsOptions {
|
|
7
|
+
origin: string | string[];
|
|
8
|
+
methods: string | string[];
|
|
9
|
+
allowedHeaders: string | string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function cors(opts: CorsOptions): (ctx: HttpContext, next: () => Promise<void>) => ResponseServer | Promise<void>;
|
|
13
|
+
|
|
14
|
+
export { cors };
|
package/dist/cors.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { R as ResponseServer } from './response.type-d6e6eU9D.js';
|
|
2
|
+
import { H as HttpContext } from './common.type-BoV71o_C.js';
|
|
3
|
+
import 'node:http';
|
|
4
|
+
import './request.type-CJ-EGGcM.js';
|
|
5
|
+
|
|
6
|
+
interface CorsOptions {
|
|
7
|
+
origin: string | string[];
|
|
8
|
+
methods: string | string[];
|
|
9
|
+
allowedHeaders: string | string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare function cors(opts: CorsOptions): (ctx: HttpContext, next: () => Promise<void>) => ResponseServer | Promise<void>;
|
|
13
|
+
|
|
14
|
+
export { cors };
|
package/dist/cors.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/shared/plugins/CORSPlugin.ts
|
|
2
|
+
function cors(opts) {
|
|
3
|
+
const { origin, methods, allowedHeaders } = opts;
|
|
4
|
+
return (ctx, next) => {
|
|
5
|
+
ctx.response.setHeader(
|
|
6
|
+
"Access-Control-Allow-Origin",
|
|
7
|
+
Array.isArray(origin) ? origin.join(",") : origin
|
|
8
|
+
);
|
|
9
|
+
ctx.response.setHeader(
|
|
10
|
+
"Access-Control-Allow-Methods",
|
|
11
|
+
Array.isArray(methods) ? methods.join(",") : methods
|
|
12
|
+
);
|
|
13
|
+
ctx.response.setHeader(
|
|
14
|
+
"Access-Control-Allow-Headers",
|
|
15
|
+
Array.isArray(allowedHeaders) ? allowedHeaders.join(",") : allowedHeaders
|
|
16
|
+
);
|
|
17
|
+
if (ctx.request.method === "OPTIONS") {
|
|
18
|
+
ctx.response.writeHead(204);
|
|
19
|
+
return ctx.response.end();
|
|
20
|
+
}
|
|
21
|
+
return next();
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { cors };
|
|
26
|
+
//# sourceMappingURL=cors.js.map
|
|
27
|
+
//# sourceMappingURL=cors.js.map
|
package/dist/cors.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/shared/plugins/CORSPlugin.ts"],"names":[],"mappings":";AAGO,SAAS,KAAK,IAAA,EAAmB;AACtC,EAAA,MAAM,EAAE,MAAA,EAAQ,OAAA,EAAS,cAAA,EAAe,GAAI,IAAA;AAE5C,EAAA,OAAO,CAAC,KAAkB,IAAA,KAA8B;AACtD,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,6BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,MAAM,IAAI,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC7C;AACA,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,8BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,OAAO,IAAI,OAAA,CAAQ,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC/C;AACA,IAAA,GAAA,CAAI,QAAA,CAAS,SAAA;AAAA,MACX,8BAAA;AAAA,MACA,MAAM,OAAA,CAAQ,cAAc,IAAI,cAAA,CAAe,IAAA,CAAK,GAAG,CAAA,GAAI;AAAA,KAC7D;AAEA,IAAA,IAAI,GAAA,CAAI,OAAA,CAAQ,MAAA,KAAW,SAAA,EAAW;AACpC,MAAA,GAAA,CAAI,QAAA,CAAS,UAAU,GAAG,CAAA;AAC1B,MAAA,OAAO,GAAA,CAAI,SAAS,GAAA,EAAI;AAAA,IAC1B;AAEA,IAAA,OAAO,IAAA,EAAK;AAAA,EACd,CAAA;AACF","file":"cors.js","sourcesContent":["import type { HttpContext } from \"../../types/common.type\";\nimport type { CorsOptions } from \"../../types/plugins/cors.type\";\n\nexport function cors(opts: CorsOptions) {\n const { origin, methods, allowedHeaders } = opts;\n\n return (ctx: HttpContext, next: () => Promise<void>) => {\n ctx.response.setHeader(\n \"Access-Control-Allow-Origin\",\n Array.isArray(origin) ? origin.join(\",\") : origin\n );\n ctx.response.setHeader(\n \"Access-Control-Allow-Methods\",\n Array.isArray(methods) ? methods.join(\",\") : methods\n );\n ctx.response.setHeader(\n \"Access-Control-Allow-Headers\",\n Array.isArray(allowedHeaders) ? allowedHeaders.join(\",\") : allowedHeaders\n );\n\n if (ctx.request.method === \"OPTIONS\") {\n ctx.response.writeHead(204);\n return ctx.response.end();\n }\n\n return next();\n };\n}\n"]}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/decorators/Route.ts
|
|
4
|
+
var PREFIX = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
var ROUTES = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
var PARAMS = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
function Controller(prefix = "") {
|
|
8
|
+
return (target) => {
|
|
9
|
+
PREFIX.set(target, prefix);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function createMethodDecorator(method) {
|
|
13
|
+
return function(path = "") {
|
|
14
|
+
return (target, propertyKey) => {
|
|
15
|
+
const ctor = typeof target === "function" ? target : target.constructor;
|
|
16
|
+
const key = String(propertyKey);
|
|
17
|
+
const routes = ROUTES.get(ctor) ?? [];
|
|
18
|
+
const params = PARAMS.get(ctor)?.get(key) ?? [];
|
|
19
|
+
const exists = routes.some(
|
|
20
|
+
(r) => r.method === method && r.path === path && r.propertyKey === key
|
|
21
|
+
);
|
|
22
|
+
if (!exists) {
|
|
23
|
+
routes.push({
|
|
24
|
+
method,
|
|
25
|
+
path,
|
|
26
|
+
propertyKey: key,
|
|
27
|
+
params
|
|
28
|
+
});
|
|
29
|
+
ROUTES.set(ctor, routes);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function createParamDecorator(type) {
|
|
35
|
+
return function(name) {
|
|
36
|
+
return (target, propertyKey, parameterIndex) => {
|
|
37
|
+
const ctor = typeof target === "function" ? target : target.constructor;
|
|
38
|
+
let map = PARAMS.get(ctor);
|
|
39
|
+
if (!map) {
|
|
40
|
+
map = /* @__PURE__ */ new Map();
|
|
41
|
+
PARAMS.set(ctor, map);
|
|
42
|
+
}
|
|
43
|
+
const key = String(propertyKey);
|
|
44
|
+
const list = map.get(key) ?? [];
|
|
45
|
+
const exists = list.some(
|
|
46
|
+
(p) => p.index === parameterIndex && p.type === type && p.name === name
|
|
47
|
+
);
|
|
48
|
+
if (!exists) {
|
|
49
|
+
list.push({
|
|
50
|
+
index: parameterIndex,
|
|
51
|
+
type,
|
|
52
|
+
name
|
|
53
|
+
});
|
|
54
|
+
map.set(key, list);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var Get = createMethodDecorator("GET");
|
|
60
|
+
var Post = createMethodDecorator("POST");
|
|
61
|
+
var Put = createMethodDecorator("PUT");
|
|
62
|
+
var Delete = createMethodDecorator("DELETE");
|
|
63
|
+
var Patch = createMethodDecorator("PATCH");
|
|
64
|
+
var Head = createMethodDecorator("HEAD");
|
|
65
|
+
var Options = createMethodDecorator("OPTIONS");
|
|
66
|
+
var Req = createParamDecorator("req");
|
|
67
|
+
var Res = createParamDecorator("res");
|
|
68
|
+
var Next = createParamDecorator("next");
|
|
69
|
+
var Param = createParamDecorator("param");
|
|
70
|
+
var Query = createParamDecorator("query");
|
|
71
|
+
var Body = createParamDecorator("body");
|
|
72
|
+
var Headers = createParamDecorator("headers");
|
|
73
|
+
var Ip = createParamDecorator("ip");
|
|
74
|
+
var UserAgent = createParamDecorator("useragent");
|
|
75
|
+
function applyDecorators(app, controllers) {
|
|
76
|
+
controllers.forEach((ControllerClass) => {
|
|
77
|
+
const prefix = PREFIX.get(ControllerClass) ?? "";
|
|
78
|
+
const instance = new ControllerClass();
|
|
79
|
+
const routes = ROUTES.get(ControllerClass) ?? [];
|
|
80
|
+
routes.forEach((r) => {
|
|
81
|
+
const handler = async (req, res, next) => {
|
|
82
|
+
try {
|
|
83
|
+
const params = (r.params ?? []).slice().sort((a, b) => a.index - b.index);
|
|
84
|
+
const args = params.map((p) => {
|
|
85
|
+
switch (p.type) {
|
|
86
|
+
case "req":
|
|
87
|
+
return req;
|
|
88
|
+
case "res":
|
|
89
|
+
return res;
|
|
90
|
+
case "next":
|
|
91
|
+
return next;
|
|
92
|
+
case "param":
|
|
93
|
+
return p.name ? req.params[p.name] : req.params;
|
|
94
|
+
case "query":
|
|
95
|
+
return p.name ? req.query[p.name] : req.query;
|
|
96
|
+
case "body": {
|
|
97
|
+
const body = req.body;
|
|
98
|
+
return p.name ? body?.[p.name] : body;
|
|
99
|
+
}
|
|
100
|
+
case "headers":
|
|
101
|
+
return p.name ? req.headers[p.name.toLowerCase()] : req.headers;
|
|
102
|
+
case "ip":
|
|
103
|
+
return req.ip;
|
|
104
|
+
case "useragent":
|
|
105
|
+
return req.headers["user-agent"];
|
|
106
|
+
default:
|
|
107
|
+
return void 0;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const fn = instance[r.propertyKey];
|
|
111
|
+
const result = fn(...args);
|
|
112
|
+
if (result instanceof Promise) await result;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
next(err);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
app.addRoute(r.method, prefix + r.path, handler);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
exports.Body = Body;
|
|
123
|
+
exports.Controller = Controller;
|
|
124
|
+
exports.Delete = Delete;
|
|
125
|
+
exports.Get = Get;
|
|
126
|
+
exports.Head = Head;
|
|
127
|
+
exports.Headers = Headers;
|
|
128
|
+
exports.Ip = Ip;
|
|
129
|
+
exports.Next = Next;
|
|
130
|
+
exports.Options = Options;
|
|
131
|
+
exports.Param = Param;
|
|
132
|
+
exports.Patch = Patch;
|
|
133
|
+
exports.Post = Post;
|
|
134
|
+
exports.Put = Put;
|
|
135
|
+
exports.Query = Query;
|
|
136
|
+
exports.Req = Req;
|
|
137
|
+
exports.Res = Res;
|
|
138
|
+
exports.UserAgent = UserAgent;
|
|
139
|
+
exports.applyDecorators = applyDecorators;
|
|
140
|
+
//# sourceMappingURL=decorators.cjs.map
|
|
141
|
+
//# sourceMappingURL=decorators.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/decorators/Route.ts"],"names":[],"mappings":";;;AAKA,IAAM,MAAA,uBAAa,OAAA,EAA0B;AAC7C,IAAM,MAAA,uBAAa,OAAA,EAAqC;AACxD,IAAM,MAAA,uBAAa,OAAA,EAAkD;AAE9D,SAAS,UAAA,CAAW,SAAS,EAAA,EAAoB;AACtD,EAAA,OAAO,CAAC,MAAA,KAAW;AACjB,IAAA,MAAA,CAAO,GAAA,CAAI,QAAoB,MAAM,CAAA;AAAA,EACvC,CAAA;AACF;AAEA,SAAS,sBAAsB,MAAA,EAAgB;AAC7C,EAAA,OAAO,SAAU,OAAO,EAAA,EAAqB;AAC3C,IAAA,OAAO,CAAC,QAAQ,WAAA,KAAgB;AAC9B,MAAA,MAAM,IAAA,GACJ,OAAO,MAAA,KAAW,UAAA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AA;AAAA,cAC1D,KAAK,IAAA;AACH,gBAAA,OAAO,GAAA,CAAI,EAAA;AAAA,cACb,KAAK,WAAA;AACH,gBAAA,OAAO,GAAA,CAAI,QAAQ,YAAY,CAAA;AAAA,cACjC;AACE,gBAAA,OAAO,KAAA,CAAA;AAAA;AACX,UACF,CAAC,CAAA;AAED,UAAA,MAAM,EAAA,GAAM,QAAA,CAAqC,CAAA,CAAE,WAAW,CAAA;AAG9D,UAAA,MAAM,MAAA,GAAS,EAAA,CAAG,GAAG,IAAI,CAAA;AACzB,UAAA,IAAI,MAAA,YAAkB,SAAS,MAAM,MAAA;AAAA,QACvC,SAAS,GAAA,EAAK;AACZ,UAAA,IAAA,CAAK,GAAG,CAAA;AAAA,QACV;AAAA,MACF,CAAA;AAEA,MAAA,GAAA,CAAI,SAAS,CAAA,CAAE,MAAA,EAAQ,MAAA,GAAS,CAAA,CAAE,MAAM,OAAO,CAAA;AAAA,IACjD,CAAC,CAAA;AAAA,EACH,CAAC,CAAA;AACH","file":"decorators.cjs","sourcesContent":["import type { AzuraClient } from \"../infra/Server\";\nimport type { RequestServer } from \"../types/http/request.type\";\nimport type { ResponseServer } from \"../types/http/response.type\";\nimport type { ParamDefinition, ParamSource, RouteDefinition } from \"../types/routes.type\";\n\nconst PREFIX = new WeakMap<Function, string>();\nconst ROUTES = new WeakMap<Function, RouteDefinition[]>();\nconst PARAMS = new WeakMap<Function, Map<string, ParamDefinition[]>>();\n\nexport function Controller(prefix = \"\"): ClassDecorator {\n return (target) => {\n PREFIX.set(target as Function, prefix);\n };\n}\n\nfunction createMethodDecorator(method: string) {\n return function (path = \"\"): MethodDecorator {\n return (target, propertyKey) => {\n const ctor =\n typeof target === \"function\" ? (target as Function) : (target as any).constructor;\n const key = String(propertyKey);\n const routes = ROUTES.get(ctor) ?? [];\n const params = PARAMS.get(ctor)?.get(key) ?? [];\n\n const exists = routes.some(\n (r) => r.method === method && r.path === path && r.propertyKey === key\n );\n if (!exists) {\n routes.push({\n method,\n path,\n propertyKey: key,\n params,\n });\n ROUTES.set(ctor, routes);\n }\n };\n };\n}\n\nfunction createParamDecorator(type: ParamSource) {\n return function (name?: string): ParameterDecorator {\n return (target, propertyKey, parameterIndex) => {\n const ctor =\n typeof target === \"function\" ? (target as Function) : (target as any).constructor;\n let map = PARAMS.get(ctor);\n if (!map) {\n map = new Map<string, ParamDefinition[]>();\n PARAMS.set(ctor, map);\n }\n\n const key = String(propertyKey);\n const list = map.get(key) ?? [];\n const exists = list.some(\n (p) => p.index === parameterIndex && p.type === type && p.name === name\n );\n if (!exists) {\n list.push({\n index: parameterIndex,\n type,\n name,\n });\n map.set(key, list);\n }\n };\n };\n}\n\nexport const Get = createMethodDecorator(\"GET\");\nexport const Post = createMethodDecorator(\"POST\");\nexport const Put = createMethodDecorator(\"PUT\");\nexport const Delete = createMethodDecorator(\"DELETE\");\nexport const Patch = createMethodDecorator(\"PATCH\");\nexport const Head = createMethodDecorator(\"HEAD\");\nexport const Options = createMethodDecorator(\"OPTIONS\");\n\nexport const Req = createParamDecorator(\"req\");\nexport const Res = createParamDecorator(\"res\");\nexport const Next = createParamDecorator(\"next\");\nexport const Param = createParamDecorator(\"param\");\nexport const Query = createParamDecorator(\"query\");\nexport const Body = createParamDecorator(\"body\");\nexport const Headers = createParamDecorator(\"headers\");\nexport const Ip = createParamDecorator(\"ip\");\nexport const UserAgent = createParamDecorator(\"useragent\");\n\nexport function applyDecorators(app: AzuraClient, controllers: Array<new () => any>) {\n controllers.forEach((ControllerClass) => {\n const prefix = PREFIX.get(ControllerClass) ?? \"\";\n const instance = new ControllerClass();\n const routes = ROUTES.get(ControllerClass) ?? [];\n\n routes.forEach((r) => {\n const handler = async (\n req: RequestServer,\n res: ResponseServer,\n next: (err?: unknown) => void\n ) => {\n try {\n const params = (r.params ?? []).slice().sort((a, b) => a.index - b.index);\n const args = params.map((p) => {\n switch (p.type) {\n case \"req\":\n return req;\n case \"res\":\n return res;\n case \"next\":\n return next;\n case \"param\":\n return p.name ? req.params[p.name] : req.params;\n case \"query\":\n return p.name ? req.query[p.name] : req.query;\n case \"body\": {\n const body = req.body as Record<string, unknown> | undefined;\n return p.name ? body?.[p.name] : body;\n }\n case \"headers\":\n return p.name ? req.headers[p.name.toLowerCase()] : req.headers;\n case \"ip\":\n return req.ip;\n case \"useragent\":\n return req.headers[\"user-agent\"];\n default:\n return undefined;\n }\n });\n\n const fn = (instance as Record<string, unknown>)[r.propertyKey] as (\n ...args: unknown[]\n ) => unknown;\n const result = fn(...args);\n if (result instanceof Promise) await result;\n } catch (err) {\n next(err);\n }\n };\n\n app.addRoute(r.method, prefix + r.path, handler);\n });\n });\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { A as AzuraClient } from './Server-CY3k1FIL.cjs';
|
|
2
|
+
export { P as ParamDefinition, a as ParamSource, R as RouteDefinition } from './routes.type-VPROfhnL.cjs';
|
|
3
|
+
export { R as RequestServer } from './request.type-CJ-EGGcM.cjs';
|
|
4
|
+
export { R as ResponseServer } from './response.type-d6e6eU9D.cjs';
|
|
5
|
+
import './config.cjs';
|
|
6
|
+
import 'node:http';
|
|
7
|
+
import './router.cjs';
|
|
8
|
+
import './common.type-Ct06XeYQ.cjs';
|
|
9
|
+
|
|
10
|
+
declare function Controller(prefix?: string): ClassDecorator;
|
|
11
|
+
declare const Get: (path?: string) => MethodDecorator;
|
|
12
|
+
declare const Post: (path?: string) => MethodDecorator;
|
|
13
|
+
declare const Put: (path?: string) => MethodDecorator;
|
|
14
|
+
declare const Delete: (path?: string) => MethodDecorator;
|
|
15
|
+
declare const Patch: (path?: string) => MethodDecorator;
|
|
16
|
+
declare const Head: (path?: string) => MethodDecorator;
|
|
17
|
+
declare const Options: (path?: string) => MethodDecorator;
|
|
18
|
+
declare const Req: (name?: string) => ParameterDecorator;
|
|
19
|
+
declare const Res: (name?: string) => ParameterDecorator;
|
|
20
|
+
declare const Next: (name?: string) => ParameterDecorator;
|
|
21
|
+
declare const Param: (name?: string) => ParameterDecorator;
|
|
22
|
+
declare const Query: (name?: string) => ParameterDecorator;
|
|
23
|
+
declare const Body: (name?: string) => ParameterDecorator;
|
|
24
|
+
declare const Headers: (name?: string) => ParameterDecorator;
|
|
25
|
+
declare const Ip: (name?: string) => ParameterDecorator;
|
|
26
|
+
declare const UserAgent: (name?: string) => ParameterDecorator;
|
|
27
|
+
declare function applyDecorators(app: AzuraClient, controllers: Array<new () => any>): void;
|
|
28
|
+
|
|
29
|
+
export { Body, Controller, Delete, Get, Head, Headers, Ip, Next, Options, Param, Patch, Post, Put, Query, Req, Res, UserAgent, applyDecorators };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { A as AzuraClient } from './Server-Ba-EFdi2.js';
|
|
2
|
+
export { P as ParamDefinition, a as ParamSource, R as RouteDefinition } from './routes.type-VPROfhnL.js';
|
|
3
|
+
export { R as RequestServer } from './request.type-CJ-EGGcM.js';
|
|
4
|
+
export { R as ResponseServer } from './response.type-d6e6eU9D.js';
|
|
5
|
+
import './config.js';
|
|
6
|
+
import 'node:http';
|
|
7
|
+
import './router.js';
|
|
8
|
+
import './common.type-BoV71o_C.js';
|
|
9
|
+
|
|
10
|
+
declare function Controller(prefix?: string): ClassDecorator;
|
|
11
|
+
declare const Get: (path?: string) => MethodDecorator;
|
|
12
|
+
declare const Post: (path?: string) => MethodDecorator;
|
|
13
|
+
declare const Put: (path?: string) => MethodDecorator;
|
|
14
|
+
declare const Delete: (path?: string) => MethodDecorator;
|
|
15
|
+
declare const Patch: (path?: string) => MethodDecorator;
|
|
16
|
+
declare const Head: (path?: string) => MethodDecorator;
|
|
17
|
+
declare const Options: (path?: string) => MethodDecorator;
|
|
18
|
+
declare const Req: (name?: string) => ParameterDecorator;
|
|
19
|
+
declare const Res: (name?: string) => ParameterDecorator;
|
|
20
|
+
declare const Next: (name?: string) => ParameterDecorator;
|
|
21
|
+
declare const Param: (name?: string) => ParameterDecorator;
|
|
22
|
+
declare const Query: (name?: string) => ParameterDecorator;
|
|
23
|
+
declare const Body: (name?: string) => ParameterDecorator;
|
|
24
|
+
declare const Headers: (name?: string) => ParameterDecorator;
|
|
25
|
+
declare const Ip: (name?: string) => ParameterDecorator;
|
|
26
|
+
declare const UserAgent: (name?: string) => ParameterDecorator;
|
|
27
|
+
declare function applyDecorators(app: AzuraClient, controllers: Array<new () => any>): void;
|
|
28
|
+
|
|
29
|
+
export { Body, Controller, Delete, Get, Head, Headers, Ip, Next, Options, Param, Patch, Post, Put, Query, Req, Res, UserAgent, applyDecorators };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// src/decorators/Route.ts
|
|
2
|
+
var PREFIX = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
var ROUTES = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
var PARAMS = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
function Controller(prefix = "") {
|
|
6
|
+
return (target) => {
|
|
7
|
+
PREFIX.set(target, prefix);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function createMethodDecorator(method) {
|
|
11
|
+
return function(path = "") {
|
|
12
|
+
return (target, propertyKey) => {
|
|
13
|
+
const ctor = typeof target === "function" ? target : target.constructor;
|
|
14
|
+
const key = String(propertyKey);
|
|
15
|
+
const routes = ROUTES.get(ctor) ?? [];
|
|
16
|
+
const params = PARAMS.get(ctor)?.get(key) ?? [];
|
|
17
|
+
const exists = routes.some(
|
|
18
|
+
(r) => r.method === method && r.path === path && r.propertyKey === key
|
|
19
|
+
);
|
|
20
|
+
if (!exists) {
|
|
21
|
+
routes.push({
|
|
22
|
+
method,
|
|
23
|
+
path,
|
|
24
|
+
propertyKey: key,
|
|
25
|
+
params
|
|
26
|
+
});
|
|
27
|
+
ROUTES.set(ctor, routes);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function createParamDecorator(type) {
|
|
33
|
+
return function(name) {
|
|
34
|
+
return (target, propertyKey, parameterIndex) => {
|
|
35
|
+
const ctor = typeof target === "function" ? target : target.constructor;
|
|
36
|
+
let map = PARAMS.get(ctor);
|
|
37
|
+
if (!map) {
|
|
38
|
+
map = /* @__PURE__ */ new Map();
|
|
39
|
+
PARAMS.set(ctor, map);
|
|
40
|
+
}
|
|
41
|
+
const key = String(propertyKey);
|
|
42
|
+
const list = map.get(key) ?? [];
|
|
43
|
+
const exists = list.some(
|
|
44
|
+
(p) => p.index === parameterIndex && p.type === type && p.name === name
|
|
45
|
+
);
|
|
46
|
+
if (!exists) {
|
|
47
|
+
list.push({
|
|
48
|
+
index: parameterIndex,
|
|
49
|
+
type,
|
|
50
|
+
name
|
|
51
|
+
});
|
|
52
|
+
map.set(key, list);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
var Get = createMethodDecorator("GET");
|
|
58
|
+
var Post = createMethodDecorator("POST");
|
|
59
|
+
var Put = createMethodDecorator("PUT");
|
|
60
|
+
var Delete = createMethodDecorator("DELETE");
|
|
61
|
+
var Patch = createMethodDecorator("PATCH");
|
|
62
|
+
var Head = createMethodDecorator("HEAD");
|
|
63
|
+
var Options = createMethodDecorator("OPTIONS");
|
|
64
|
+
var Req = createParamDecorator("req");
|
|
65
|
+
var Res = createParamDecorator("res");
|
|
66
|
+
var Next = createParamDecorator("next");
|
|
67
|
+
var Param = createParamDecorator("param");
|
|
68
|
+
var Query = createParamDecorator("query");
|
|
69
|
+
var Body = createParamDecorator("body");
|
|
70
|
+
var Headers = createParamDecorator("headers");
|
|
71
|
+
var Ip = createParamDecorator("ip");
|
|
72
|
+
var UserAgent = createParamDecorator("useragent");
|
|
73
|
+
function applyDecorators(app, controllers) {
|
|
74
|
+
controllers.forEach((ControllerClass) => {
|
|
75
|
+
const prefix = PREFIX.get(ControllerClass) ?? "";
|
|
76
|
+
const instance = new ControllerClass();
|
|
77
|
+
const routes = ROUTES.get(ControllerClass) ?? [];
|
|
78
|
+
routes.forEach((r) => {
|
|
79
|
+
const handler = async (req, res, next) => {
|
|
80
|
+
try {
|
|
81
|
+
const params = (r.params ?? []).slice().sort((a, b) => a.index - b.index);
|
|
82
|
+
const args = params.map((p) => {
|
|
83
|
+
switch (p.type) {
|
|
84
|
+
case "req":
|
|
85
|
+
return req;
|
|
86
|
+
case "res":
|
|
87
|
+
return res;
|
|
88
|
+
case "next":
|
|
89
|
+
return next;
|
|
90
|
+
case "param":
|
|
91
|
+
return p.name ? req.params[p.name] : req.params;
|
|
92
|
+
case "query":
|
|
93
|
+
return p.name ? req.query[p.name] : req.query;
|
|
94
|
+
case "body": {
|
|
95
|
+
const body = req.body;
|
|
96
|
+
return p.name ? body?.[p.name] : body;
|
|
97
|
+
}
|
|
98
|
+
case "headers":
|
|
99
|
+
return p.name ? req.headers[p.name.toLowerCase()] : req.headers;
|
|
100
|
+
case "ip":
|
|
101
|
+
return req.ip;
|
|
102
|
+
case "useragent":
|
|
103
|
+
return req.headers["user-agent"];
|
|
104
|
+
default:
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const fn = instance[r.propertyKey];
|
|
109
|
+
const result = fn(...args);
|
|
110
|
+
if (result instanceof Promise) await result;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
next(err);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
app.addRoute(r.method, prefix + r.path, handler);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { Body, Controller, Delete, Get, Head, Headers, Ip, Next, Options, Param, Patch, Post, Put, Query, Req, Res, UserAgent, applyDecorators };
|
|
121
|
+
//# sourceMappingURL=decorators.js.map
|
|
122
|
+
//# sourceMappingURL=decorators.js.map
|