tezx 1.0.13 → 1.0.15
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 +8 -3
- package/{dist/cjs → cjs}/index.js +1 -1
- package/{dist/index.js → index.js} +1 -1
- package/package.json +18 -19
- package/dist/MiddlewareConfigure.d.ts +0 -17
- package/dist/MiddlewareConfigure.js +0 -63
- package/dist/adapter.d.ts +0 -10
- package/dist/adapter.js +0 -162
- package/dist/cjs/MiddlewareConfigure.js +0 -68
- package/dist/cjs/adapter.js +0 -166
- package/dist/cjs/common.js +0 -15
- package/dist/cjs/config/config.js +0 -35
- package/dist/cjs/context.js +0 -428
- package/dist/cjs/environment.js +0 -34
- package/dist/cjs/header.js +0 -85
- package/dist/cjs/helper/common.js +0 -11
- package/dist/cjs/helper/env-parser.js +0 -69
- package/dist/cjs/helper/index.js +0 -20
- package/dist/cjs/middleware/cors.js +0 -49
- package/dist/cjs/middleware/index.js +0 -26
- package/dist/cjs/middleware/logger.js +0 -21
- package/dist/cjs/middleware/powered-by.js +0 -10
- package/dist/cjs/middleware/rateLimiter.js +0 -40
- package/dist/cjs/middleware/request-id.js +0 -15
- package/dist/cjs/middleware/sanitizeHeader.js +0 -51
- package/dist/cjs/middleware/secureHeaders.js +0 -42
- package/dist/cjs/middleware/xssProtection.js +0 -26
- package/dist/cjs/request.js +0 -80
- package/dist/cjs/router.js +0 -377
- package/dist/cjs/server.js +0 -170
- package/dist/cjs/utils/colors.js +0 -24
- package/dist/cjs/utils/debugging.js +0 -17
- package/dist/cjs/utils/formData.js +0 -219
- package/dist/cjs/utils/params.js +0 -94
- package/dist/cjs/utils/state.js +0 -34
- package/dist/cjs/utils/staticFile.js +0 -158
- package/dist/cjs/utils/url.js +0 -59
- package/dist/common.d.ts +0 -21
- package/dist/common.js +0 -11
- package/dist/config/config.d.ts +0 -17
- package/dist/config/config.js +0 -31
- package/dist/context.d.ts +0 -195
- package/dist/context.js +0 -424
- package/dist/environment.d.ts +0 -6
- package/dist/environment.js +0 -30
- package/dist/header.d.ts +0 -71
- package/dist/header.js +0 -81
- package/dist/helper/common.d.ts +0 -5
- package/dist/helper/common.js +0 -8
- package/dist/helper/env-parser.d.ts +0 -5
- package/dist/helper/env-parser.js +0 -66
- package/dist/helper/index.d.ts +0 -2
- package/dist/helper/index.js +0 -2
- package/dist/middleware/cors.d.ts +0 -10
- package/dist/middleware/cors.js +0 -46
- package/dist/middleware/index.d.ts +0 -9
- package/dist/middleware/index.js +0 -8
- package/dist/middleware/logger.d.ts +0 -15
- package/dist/middleware/logger.js +0 -18
- package/dist/middleware/powered-by.d.ts +0 -16
- package/dist/middleware/powered-by.js +0 -6
- package/dist/middleware/rateLimiter.d.ts +0 -61
- package/dist/middleware/rateLimiter.js +0 -36
- package/dist/middleware/request-id.d.ts +0 -16
- package/dist/middleware/request-id.js +0 -11
- package/dist/middleware/sanitizeHeader.d.ts +0 -53
- package/dist/middleware/sanitizeHeader.js +0 -47
- package/dist/middleware/secureHeaders.d.ts +0 -78
- package/dist/middleware/secureHeaders.js +0 -38
- package/dist/middleware/xssProtection.d.ts +0 -43
- package/dist/middleware/xssProtection.js +0 -22
- package/dist/request.d.ts +0 -82
- package/dist/request.js +0 -76
- package/dist/router.d.ts +0 -191
- package/dist/router.js +0 -373
- package/dist/server.d.ts +0 -54
- package/dist/server.js +0 -166
- package/dist/utils/colors.d.ts +0 -21
- package/dist/utils/colors.js +0 -21
- package/dist/utils/debugging.d.ts +0 -7
- package/dist/utils/debugging.js +0 -13
- package/dist/utils/formData.d.ts +0 -5
- package/dist/utils/formData.js +0 -213
- package/dist/utils/params.d.ts +0 -7
- package/dist/utils/params.js +0 -91
- package/dist/utils/state.d.ts +0 -50
- package/dist/utils/state.js +0 -30
- package/dist/utils/staticFile.d.ts +0 -9
- package/dist/utils/staticFile.js +0 -154
- package/dist/utils/url.d.ts +0 -16
- package/dist/utils/url.js +0 -55
- /package/{dist/index.d.ts → index.d.ts} +0 -0
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { Context } from "../context";
|
|
2
|
-
import { Middleware } from "../router";
|
|
3
|
-
export type DynamicHeaderValue = string | ((ctx: Context) => string | undefined);
|
|
4
|
-
export type SecurityHeaderOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* 🔵 Content Security Policy header value
|
|
7
|
-
* @default "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"
|
|
8
|
-
* @example
|
|
9
|
-
* contentSecurityPolicy: "default-src 'none'; script-src 'self' https://trusted.cdn.com"
|
|
10
|
-
* contentSecurityPolicy: (ctx) => ctx.isAdmin ? "default-src 'self'" : "default-src 'none'"
|
|
11
|
-
*/
|
|
12
|
-
contentSecurityPolicy?: DynamicHeaderValue;
|
|
13
|
-
/**
|
|
14
|
-
* 🟢 Whether to set X-Frame-Options header (clickjacking protection)
|
|
15
|
-
* @default true
|
|
16
|
-
* @example
|
|
17
|
-
* frameGuard: true // Always enable
|
|
18
|
-
* frameGuard: (ctx) => !ctx.path.startsWith('/embed/') // Disable for embed routes
|
|
19
|
-
*/
|
|
20
|
-
frameGuard?: boolean | ((ctx: Context) => boolean);
|
|
21
|
-
/**
|
|
22
|
-
* 🟣 HTTP Strict Transport Security (HSTS) header
|
|
23
|
-
* @default true
|
|
24
|
-
* @example
|
|
25
|
-
* hsts: true // Enable with 2 year duration
|
|
26
|
-
* hsts: (ctx) => ctx.secure // Enable only for HTTPS
|
|
27
|
-
*/
|
|
28
|
-
hsts?: boolean | ((ctx: Context) => boolean);
|
|
29
|
-
/**
|
|
30
|
-
* 🟠 XSS Protection header
|
|
31
|
-
* @default true
|
|
32
|
-
* @example
|
|
33
|
-
* xssProtection: false // Disable legacy XSS filter
|
|
34
|
-
*/
|
|
35
|
-
xssProtection?: boolean | ((ctx: Context) => boolean);
|
|
36
|
-
/**
|
|
37
|
-
* 🟡 X-Content-Type-Options header (MIME sniffing prevention)
|
|
38
|
-
* @default true
|
|
39
|
-
* @example
|
|
40
|
-
* noSniff: false // Only disable if you have specific needs
|
|
41
|
-
*/
|
|
42
|
-
noSniff?: boolean | ((ctx: Context) => boolean);
|
|
43
|
-
/**
|
|
44
|
-
* 🔴 Referrer Policy header
|
|
45
|
-
* @default "no-referrer"
|
|
46
|
-
* @example
|
|
47
|
-
* referrerPolicy: "strict-origin-when-cross-origin"
|
|
48
|
-
*/
|
|
49
|
-
referrerPolicy?: DynamicHeaderValue;
|
|
50
|
-
/**
|
|
51
|
-
* 🟤 Permissions Policy header (formerly Feature Policy)
|
|
52
|
-
* @default "geolocation=(), microphone=(), camera=()"
|
|
53
|
-
* @example
|
|
54
|
-
* permissionsPolicy: "geolocation=(self 'https://example.com')"
|
|
55
|
-
*/
|
|
56
|
-
permissionsPolicy?: DynamicHeaderValue;
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* 🛡️ Comprehensive security headers middleware
|
|
60
|
-
*
|
|
61
|
-
* Sets multiple security-related HTTP headers with dynamic configuration.
|
|
62
|
-
* All headers can be configured per-request using functions.
|
|
63
|
-
*
|
|
64
|
-
* @param {SecurityHeaderOptions} [options={}] - Configuration options
|
|
65
|
-
* @returns {Middleware} Middleware function
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* // Basic usage with defaults
|
|
69
|
-
* app.use(secureHeaders());
|
|
70
|
-
*
|
|
71
|
-
* // Custom configuration
|
|
72
|
-
* app.use(secureHeaders({
|
|
73
|
-
* contentSecurityPolicy: "default-src 'self'",
|
|
74
|
-
* frameGuard: (ctx) => !ctx.isEmbedded,
|
|
75
|
-
* referrerPolicy: "strict-origin-when-cross-origin"
|
|
76
|
-
* }));
|
|
77
|
-
*/
|
|
78
|
-
export declare const secureHeaders: (options?: SecurityHeaderOptions) => Middleware;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export const secureHeaders = (options = {}) => {
|
|
2
|
-
return async (ctx, next) => {
|
|
3
|
-
const resolveValue = (value) => {
|
|
4
|
-
return typeof value === "function" ? value(ctx) : value;
|
|
5
|
-
};
|
|
6
|
-
const contentSecurityPolicy = resolveValue(options.contentSecurityPolicy) ||
|
|
7
|
-
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';";
|
|
8
|
-
const frameGuard = resolveValue(options.frameGuard) ?? true;
|
|
9
|
-
const hsts = resolveValue(options.hsts) ?? true;
|
|
10
|
-
const xssProtection = resolveValue(options.xssProtection) ?? true;
|
|
11
|
-
const noSniff = resolveValue(options.noSniff) ?? true;
|
|
12
|
-
const referrerPolicy = resolveValue(options.referrerPolicy) || "no-referrer";
|
|
13
|
-
const permissionsPolicy = resolveValue(options.permissionsPolicy) ||
|
|
14
|
-
"geolocation=(), microphone=(), camera=()";
|
|
15
|
-
if (contentSecurityPolicy) {
|
|
16
|
-
ctx.headers.set("Content-Security-Policy", contentSecurityPolicy);
|
|
17
|
-
}
|
|
18
|
-
if (frameGuard) {
|
|
19
|
-
ctx.headers.set("X-Frame-Options", "DENY");
|
|
20
|
-
}
|
|
21
|
-
if (hsts) {
|
|
22
|
-
ctx.headers.set("Strict-Transport-Security", "max-age=63072000; includeSubDomains");
|
|
23
|
-
}
|
|
24
|
-
if (xssProtection) {
|
|
25
|
-
ctx.headers.set("X-XSS-Protection", "1; mode=block");
|
|
26
|
-
}
|
|
27
|
-
if (noSniff) {
|
|
28
|
-
ctx.headers.set("X-Content-Type-Options", "nosniff");
|
|
29
|
-
}
|
|
30
|
-
if (referrerPolicy) {
|
|
31
|
-
ctx.headers.set("Referrer-Policy", referrerPolicy);
|
|
32
|
-
}
|
|
33
|
-
if (permissionsPolicy) {
|
|
34
|
-
ctx.headers.set("Permissions-Policy", permissionsPolicy);
|
|
35
|
-
}
|
|
36
|
-
return await next();
|
|
37
|
-
};
|
|
38
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Context } from "../context";
|
|
2
|
-
import { NextCallback } from "../router";
|
|
3
|
-
export type XSSProtectionOptions = {
|
|
4
|
-
/**
|
|
5
|
-
* 🟢 Whether to enable XSS protection
|
|
6
|
-
* @default true
|
|
7
|
-
* @example
|
|
8
|
-
* enabled: true // Always enable
|
|
9
|
-
* enabled: (ctx) => !ctx.isAdmin // Disable for admin routes
|
|
10
|
-
*/
|
|
11
|
-
enabled?: boolean | ((ctx: Context) => boolean);
|
|
12
|
-
/**
|
|
13
|
-
* 🔵 Protection mode to use
|
|
14
|
-
* @default "block"
|
|
15
|
-
* @example
|
|
16
|
-
* mode: "block" // Block the page if XSS detected
|
|
17
|
-
* mode: "filter" // Sanitize the page if XSS detected
|
|
18
|
-
*/
|
|
19
|
-
mode?: "block" | "filter";
|
|
20
|
-
/**
|
|
21
|
-
* 🟣 Fallback CSP for browsers without XSS protection
|
|
22
|
-
* @default "default-src 'self'; script-src 'self';"
|
|
23
|
-
* @example
|
|
24
|
-
* fallbackCSP: "default-src 'none'; script-src 'self' https://trusted.cdn.com"
|
|
25
|
-
*/
|
|
26
|
-
fallbackCSP?: string;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* 🛡️ Middleware to set the X-XSS-Protection header and provide enhanced XSS mitigation
|
|
30
|
-
* @param options - Configuration options for XSS protection
|
|
31
|
-
* @returns Middleware function
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* // Basic usage
|
|
35
|
-
* app.use(xssProtection());
|
|
36
|
-
*
|
|
37
|
-
* // Custom configuration
|
|
38
|
-
* app.use(xssProtection({
|
|
39
|
-
* mode: "filter",
|
|
40
|
-
* fallbackCSP: "default-src 'self'"
|
|
41
|
-
* }));
|
|
42
|
-
*/
|
|
43
|
-
export declare const xssProtection: (options?: XSSProtectionOptions) => (ctx: Context, next: NextCallback) => Promise<any>;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { GlobalConfig } from "../config/config";
|
|
2
|
-
export const xssProtection = (options = {}) => {
|
|
3
|
-
const { enabled = true, mode = "block", fallbackCSP = "default-src 'self'; script-src 'self';", } = options;
|
|
4
|
-
return async (ctx, next) => {
|
|
5
|
-
const isEnabled = typeof enabled === "function" ? enabled(ctx) : enabled;
|
|
6
|
-
if (!isEnabled) {
|
|
7
|
-
GlobalConfig.debugging.warn("🟠 XSS protection is disabled.");
|
|
8
|
-
return await next();
|
|
9
|
-
}
|
|
10
|
-
const xssHeaderValue = mode === "block" ? "1; mode=block" : "1";
|
|
11
|
-
ctx.headers.set("X-XSS-Protection", xssHeaderValue);
|
|
12
|
-
GlobalConfig.debugging.warn(`🟢 X-XSS-Protection set to: ${xssHeaderValue}`);
|
|
13
|
-
if (fallbackCSP) {
|
|
14
|
-
const existingCSP = ctx.headers.get("Content-Security-Policy");
|
|
15
|
-
if (!existingCSP) {
|
|
16
|
-
ctx.headers.set("Content-Security-Policy", fallbackCSP);
|
|
17
|
-
GlobalConfig.debugging.warn(`🟣 Fallback CSP set to: ${fallbackCSP}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return await next();
|
|
21
|
-
};
|
|
22
|
-
};
|
package/dist/request.d.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { HeadersParser } from "./header";
|
|
2
|
-
import { UrlRef } from "./utils/url";
|
|
3
|
-
export type FormDataOptions = {
|
|
4
|
-
maxSize?: number;
|
|
5
|
-
allowedTypes?: string[];
|
|
6
|
-
sanitized?: boolean;
|
|
7
|
-
maxFiles?: number;
|
|
8
|
-
};
|
|
9
|
-
type TransportType = "tcp" | "udp" | "unix" | "pipe";
|
|
10
|
-
export type AddressType = {
|
|
11
|
-
transport?: TransportType;
|
|
12
|
-
family?: "IPv4" | "IPv6" | "Unix";
|
|
13
|
-
address?: string;
|
|
14
|
-
port?: number;
|
|
15
|
-
};
|
|
16
|
-
export type ConnAddress = {
|
|
17
|
-
remoteAddr: AddressType;
|
|
18
|
-
localAddr: AddressType;
|
|
19
|
-
};
|
|
20
|
-
export type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" | "HEAD" | "ALL" | "TRACE" | "CONNECT" | string;
|
|
21
|
-
export declare class Request {
|
|
22
|
-
headers: HeadersParser;
|
|
23
|
-
/**
|
|
24
|
-
* Full request URL including protocol and query string
|
|
25
|
-
* @type {string}
|
|
26
|
-
*/
|
|
27
|
-
readonly url: string;
|
|
28
|
-
/**
|
|
29
|
-
* HTTP request method (GET, POST, PUT, DELETE, etc.)
|
|
30
|
-
* @type {HTTPMethod}
|
|
31
|
-
*/
|
|
32
|
-
readonly method: HTTPMethod;
|
|
33
|
-
/** Parsed URL reference containing components like query parameters, pathname, etc. */
|
|
34
|
-
readonly urlRef: UrlRef;
|
|
35
|
-
/** Query parameters extracted from the URL */
|
|
36
|
-
readonly query: Record<string, any>;
|
|
37
|
-
protected rawRequest: any;
|
|
38
|
-
/**
|
|
39
|
-
* Retrieve a parameter by name.
|
|
40
|
-
* @param name - The parameter name.
|
|
41
|
-
* @returns The parameter value if found, or undefined.
|
|
42
|
-
*/
|
|
43
|
-
readonly params: Record<string, any>;
|
|
44
|
-
/**
|
|
45
|
-
* Represents the remote address details of a connected client.
|
|
46
|
-
*
|
|
47
|
-
* @property {TransportType} [transport] - The transport protocol used (e.g., `"tcp"`, `"udp"`).
|
|
48
|
-
* @property {"IPv4" | "IPv6" | "Unix"} [family] - The address family, indicating whether the connection is over IPv4, IPv6, or a Unix socket.
|
|
49
|
-
* @property {string} [hostname] - The hostname or IP address of the remote client.
|
|
50
|
-
* @property {number} [port] - The remote client's port number.
|
|
51
|
-
* @default {{}}
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* ctx.req.remoteAddress
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
remoteAddress: AddressType;
|
|
59
|
-
constructor(req: any, params: Record<string, any>, remoteAddress: AddressType);
|
|
60
|
-
/**
|
|
61
|
-
* Parses the request body as plain text.
|
|
62
|
-
* @returns {Promise<string>} The text content of the request body.
|
|
63
|
-
*/
|
|
64
|
-
text(): Promise<string>;
|
|
65
|
-
/**
|
|
66
|
-
* Parses the request body as JSON.
|
|
67
|
-
* @returns {Promise<Record<string, any>>} The parsed JSON object.
|
|
68
|
-
* If the Content-Type is not 'application/json', it returns an empty object.
|
|
69
|
-
*/
|
|
70
|
-
json(): Promise<Record<string, any>>;
|
|
71
|
-
/**
|
|
72
|
-
* Parses the request body based on Content-Type.
|
|
73
|
-
* Supports:
|
|
74
|
-
* - application/json → JSON parsing
|
|
75
|
-
* - application/x-www-form-urlencoded → URL-encoded form parsing
|
|
76
|
-
* - multipart/form-data → Multipart form-data parsing (for file uploads)
|
|
77
|
-
* @returns {Promise<Record<string, any>>} The parsed form data as an object.
|
|
78
|
-
* @throws {Error} If the Content-Type is missing or invalid.
|
|
79
|
-
*/
|
|
80
|
-
formData(options?: FormDataOptions): Promise<Record<string, any>>;
|
|
81
|
-
}
|
|
82
|
-
export {};
|
package/dist/request.js
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { EnvironmentDetector } from "./environment";
|
|
2
|
-
import { HeadersParser } from "./header";
|
|
3
|
-
import { parseJsonBody, parseMultipartBody, parseTextBody, parseUrlEncodedBody, } from "./utils/formData";
|
|
4
|
-
import { urlParse } from "./utils/url";
|
|
5
|
-
export class Request {
|
|
6
|
-
headers = new HeadersParser();
|
|
7
|
-
url;
|
|
8
|
-
method;
|
|
9
|
-
urlRef = {
|
|
10
|
-
hash: undefined,
|
|
11
|
-
protocol: undefined,
|
|
12
|
-
origin: undefined,
|
|
13
|
-
username: undefined,
|
|
14
|
-
password: undefined,
|
|
15
|
-
hostname: undefined,
|
|
16
|
-
port: undefined,
|
|
17
|
-
href: undefined,
|
|
18
|
-
query: {},
|
|
19
|
-
pathname: "/",
|
|
20
|
-
};
|
|
21
|
-
query;
|
|
22
|
-
rawRequest;
|
|
23
|
-
params = {};
|
|
24
|
-
remoteAddress = {};
|
|
25
|
-
constructor(req, params, remoteAddress) {
|
|
26
|
-
this.remoteAddress = remoteAddress;
|
|
27
|
-
this.headers = new HeadersParser(req?.headers);
|
|
28
|
-
this.method = req?.method?.toUpperCase();
|
|
29
|
-
this.params = params;
|
|
30
|
-
this.rawRequest = req;
|
|
31
|
-
if (EnvironmentDetector.getEnvironment == "node") {
|
|
32
|
-
const protocol = EnvironmentDetector.detectProtocol(req);
|
|
33
|
-
const host = EnvironmentDetector.getHost(this.headers);
|
|
34
|
-
this.url = `${protocol}://${host}${req.url}`;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.url = req.url;
|
|
38
|
-
}
|
|
39
|
-
this.urlRef = urlParse(this.url);
|
|
40
|
-
this.query = this.urlRef.query;
|
|
41
|
-
}
|
|
42
|
-
async text() {
|
|
43
|
-
return await parseTextBody(this.rawRequest);
|
|
44
|
-
}
|
|
45
|
-
async json() {
|
|
46
|
-
const contentType = this.headers.get("content-type") || "";
|
|
47
|
-
if (contentType.includes("application/json")) {
|
|
48
|
-
return await parseJsonBody(this.rawRequest);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
return {};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async formData(options) {
|
|
55
|
-
const contentType = this.headers.get("content-type") || "";
|
|
56
|
-
if (!contentType) {
|
|
57
|
-
throw Error("Invalid Content-Type");
|
|
58
|
-
}
|
|
59
|
-
if (contentType.includes("application/json")) {
|
|
60
|
-
return await parseJsonBody(this.rawRequest);
|
|
61
|
-
}
|
|
62
|
-
else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
63
|
-
return parseUrlEncodedBody(this.rawRequest);
|
|
64
|
-
}
|
|
65
|
-
else if (contentType.includes("multipart/form-data")) {
|
|
66
|
-
const boundary = contentType?.split("; ")?.[1]?.split("=")?.[1];
|
|
67
|
-
if (!boundary) {
|
|
68
|
-
throw Error("Boundary not found");
|
|
69
|
-
}
|
|
70
|
-
return await parseMultipartBody(this.rawRequest, boundary, options);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
return {};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
package/dist/router.d.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { Context, ResponseHeaders } from "./context";
|
|
2
|
-
import MiddlewareConfigure, { DuplicateMiddlewares, UniqueMiddlewares } from "./MiddlewareConfigure";
|
|
3
|
-
import { HTTPMethod } from "./request";
|
|
4
|
-
export type NextCallback = () => Promise<any>;
|
|
5
|
-
export type ctx<T extends Record<string, any> = {}> = Context<T> & T;
|
|
6
|
-
export type Callback<T extends Record<string, any> = {}> = (ctx: ctx<T>) => Promise<Response> | Response;
|
|
7
|
-
export type Middleware<T extends Record<string, any> = {}> = (ctx: ctx<T>, next: NextCallback) => NextCallback | Promise<NextCallback | Response> | Response;
|
|
8
|
-
export type RouterConfig = {
|
|
9
|
-
/**
|
|
10
|
-
* `env` allows you to define environment variables for the router.
|
|
11
|
-
* It is a record of key-value pairs where the key is the variable name
|
|
12
|
-
* and the value can be either a string or a number.
|
|
13
|
-
*/
|
|
14
|
-
env?: Record<string, string | number>;
|
|
15
|
-
/**
|
|
16
|
-
* `basePath` sets the base path for the router. This is useful for grouping
|
|
17
|
-
* routes under a specific path prefix.
|
|
18
|
-
*/
|
|
19
|
-
basePath?: string;
|
|
20
|
-
};
|
|
21
|
-
declare class TrieRouter {
|
|
22
|
-
children: Map<string, TrieRouter>;
|
|
23
|
-
handlers: Map<HTTPMethod, {
|
|
24
|
-
callback: Callback<any>;
|
|
25
|
-
middlewares: UniqueMiddlewares | DuplicateMiddlewares;
|
|
26
|
-
}>;
|
|
27
|
-
pathname: string;
|
|
28
|
-
paramName: any;
|
|
29
|
-
isParam: boolean;
|
|
30
|
-
constructor(pathname?: string);
|
|
31
|
-
}
|
|
32
|
-
export type StaticServeOption = {
|
|
33
|
-
cacheControl?: string;
|
|
34
|
-
headers?: ResponseHeaders;
|
|
35
|
-
};
|
|
36
|
-
export declare class Router<T extends Record<string, any> = {}> extends MiddlewareConfigure<T> {
|
|
37
|
-
#private;
|
|
38
|
-
protected routers: Map<string, Map<HTTPMethod, {
|
|
39
|
-
callback: Callback<T>;
|
|
40
|
-
middlewares: UniqueMiddlewares | DuplicateMiddlewares;
|
|
41
|
-
}>>;
|
|
42
|
-
protected env: Record<string, string | number>;
|
|
43
|
-
protected triRouter: TrieRouter;
|
|
44
|
-
constructor({ basePath, env }?: RouterConfig);
|
|
45
|
-
/**
|
|
46
|
-
* Serves static files from a specified directory.
|
|
47
|
-
*
|
|
48
|
-
* This method provides two overloads:
|
|
49
|
-
* 1. `static(route: string, folder: string, option?: StaticServeOption): this;`
|
|
50
|
-
* - Serves static files from `folder` at the specified `route`.
|
|
51
|
-
* 2. `static(folder: string, option?: StaticServeOption): this;`
|
|
52
|
-
* - Serves static files from `folder` at the root (`/`).
|
|
53
|
-
*
|
|
54
|
-
* @param {string} route - The base route to serve static files from (optional in overload).
|
|
55
|
-
* @param {string} folder - The folder containing the static files.
|
|
56
|
-
* @param {StaticServeOption} [option] - Optional settings for static file serving.
|
|
57
|
-
* @returns {this} Returns the current instance to allow method chaining.
|
|
58
|
-
*/
|
|
59
|
-
static(route: string, folder: string, option?: StaticServeOption): this;
|
|
60
|
-
static(folder: string, Option?: StaticServeOption): this;
|
|
61
|
-
/**
|
|
62
|
-
* Registers a GET route with optional middleware(s)
|
|
63
|
-
* @param path - URL path pattern (supports route parameters)
|
|
64
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
65
|
-
* @returns Current instance for chaining
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* // Simple GET route
|
|
69
|
-
* app.get('/users', (ctx) => { ... });
|
|
70
|
-
*
|
|
71
|
-
* // With middleware
|
|
72
|
-
* app.get('/secure', authMiddleware, (ctx) => { ... });
|
|
73
|
-
*
|
|
74
|
-
* // With multiple middlewares
|
|
75
|
-
* app.get('/admin', [authMiddleware, adminMiddleware], (ctx) => { ... });
|
|
76
|
-
*/
|
|
77
|
-
get(path: string, callback: Callback<T>): this;
|
|
78
|
-
get(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
79
|
-
get(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
80
|
-
/**
|
|
81
|
-
* Registers a POST route with optional middleware(s)
|
|
82
|
-
* @param path - URL path pattern
|
|
83
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
84
|
-
*/
|
|
85
|
-
post(path: string, callback: Callback<T>): this;
|
|
86
|
-
post(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
87
|
-
post(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
88
|
-
/**
|
|
89
|
-
* Registers a PUT route with optional middleware(s)
|
|
90
|
-
* @param path - URL path pattern
|
|
91
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
92
|
-
*/
|
|
93
|
-
put(path: string, callback: Callback<T>): this;
|
|
94
|
-
put(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
95
|
-
put(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
96
|
-
/**
|
|
97
|
-
* Registers a PATCH route with optional middleware(s)
|
|
98
|
-
* @param path - URL path pattern
|
|
99
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
100
|
-
*/
|
|
101
|
-
patch(path: string, callback: Callback<T>): this;
|
|
102
|
-
patch(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
103
|
-
patch(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
104
|
-
/**
|
|
105
|
-
* Registers a DELETE route with optional middleware(s)
|
|
106
|
-
* @param path - URL path pattern
|
|
107
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
108
|
-
*/
|
|
109
|
-
delete(path: string, callback: Callback<T>): this;
|
|
110
|
-
delete(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
111
|
-
delete(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
112
|
-
/**
|
|
113
|
-
* Registers an OPTIONS route (primarily for CORS preflight requests)
|
|
114
|
-
* @param path - URL path pattern
|
|
115
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
116
|
-
*/
|
|
117
|
-
options(path: string, callback: Callback<T>): this;
|
|
118
|
-
options(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
119
|
-
options(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
120
|
-
/**
|
|
121
|
-
* Registers a HEAD route (returns headers only)
|
|
122
|
-
* @param path - URL path pattern
|
|
123
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
124
|
-
*/
|
|
125
|
-
head(path: string, callback: Callback<T>): this;
|
|
126
|
-
head(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
127
|
-
head(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
128
|
-
/**
|
|
129
|
-
* Registers a route that responds to all HTTP methods
|
|
130
|
-
* @param path - URL path pattern
|
|
131
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
132
|
-
*/
|
|
133
|
-
all(path: string, callback: Callback<T>): this;
|
|
134
|
-
all(path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
135
|
-
all(path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
136
|
-
/**
|
|
137
|
-
* Generic method registration for custom HTTP methods
|
|
138
|
-
* @param method - HTTP method name (e.g., 'PURGE')
|
|
139
|
-
* @param path - URL path pattern
|
|
140
|
-
* @param args - Handler callback or middleware(s) + handler
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* // Register custom method
|
|
144
|
-
* server.addRoute('PURGE', '/cache', purgeHandler);
|
|
145
|
-
*/
|
|
146
|
-
addRoute(method: HTTPMethod, path: string, callback: Callback<T>): this;
|
|
147
|
-
addRoute(method: HTTPMethod, path: string, middlewares: Middleware<T>[], callback: Callback<T>): this;
|
|
148
|
-
addRoute(method: HTTPMethod, path: string, middlewares: Middleware<T>, callback: Callback<T>): this;
|
|
149
|
-
/**
|
|
150
|
-
* Mount a sub-router at specific path prefix
|
|
151
|
-
* @param path - Base path for the sub-router
|
|
152
|
-
* @param router - Router instance to mount
|
|
153
|
-
* @returns Current instance for chaining
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* const apiRouter = new Router();
|
|
157
|
-
* apiRouter.get('/users', () => { ... });
|
|
158
|
-
* server.addRouter('/api', apiRouter);
|
|
159
|
-
*/
|
|
160
|
-
addRouter(path: string, router: Router<T | any>): void;
|
|
161
|
-
/**
|
|
162
|
-
* Create route group with shared path prefix
|
|
163
|
-
* @param prefix - Path prefix for the group
|
|
164
|
-
* @param callback - Function that receives group-specific router
|
|
165
|
-
* @returns Current router instance for chaining
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* app.group('/v1', (group) => {
|
|
169
|
-
* group.get('/users', v1UserHandler);
|
|
170
|
-
* });
|
|
171
|
-
*/
|
|
172
|
-
group(prefix: string, callback: (group: Router<T>) => void): this;
|
|
173
|
-
/**
|
|
174
|
-
* Register middleware with flexible signature
|
|
175
|
-
* @overload
|
|
176
|
-
* @param path - Optional path to scope middleware
|
|
177
|
-
* @param middlewares - Middleware(s) to register
|
|
178
|
-
* @param [callback] - Optional sub-router or handler
|
|
179
|
-
*/
|
|
180
|
-
use(path: string, middlewares: Middleware<T>[], callback: Callback<T> | Router<T | any>): this;
|
|
181
|
-
use(path: string, middleware: Middleware<T>, callback: Callback<T> | Router<T | any>): this;
|
|
182
|
-
use(path: string, middlewares: Middleware<T>[]): this;
|
|
183
|
-
use(path: string, middleware: Middleware<T>): this;
|
|
184
|
-
use(path: string, callback: Callback<T> | Router<T | any>): this;
|
|
185
|
-
use(middlewares: Middleware<T>[], callback: Callback<T> | Router<T | any>): this;
|
|
186
|
-
use(middleware: Middleware<T>, callback: Callback<T> | Router<T | any>): this;
|
|
187
|
-
use(middlewares: Middleware<T>[]): this;
|
|
188
|
-
use(middleware: Middleware<T>): this;
|
|
189
|
-
use(callback: Callback<T> | Router<T | any>): this;
|
|
190
|
-
}
|
|
191
|
-
export {};
|