tezx 3.0.8-beta โ†’ 3.0.10-beta

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.
@@ -74,9 +74,6 @@ class Context {
74
74
  }
75
75
  return this;
76
76
  }
77
- get params() {
78
- return this.#params;
79
- }
80
77
  set params(params) {
81
78
  this.#params = params;
82
79
  }
@@ -135,7 +132,7 @@ class Context {
135
132
  redirect(url, status = 302) {
136
133
  return new Response(null, {
137
134
  status: status,
138
- headers: { Location: url },
135
+ headers: { ...this.#headers, Location: url },
139
136
  });
140
137
  }
141
138
  async download(filePath, filename) {
@@ -129,11 +129,9 @@ class Router {
129
129
  }
130
130
  return this;
131
131
  }
132
- #addRoute(method, path, handlers, skip = false) {
132
+ #addRoute(method, path, handlers) {
133
133
  let pattern = `/${(0, low_level_js_1.sanitizePathSplitBasePath)(this.basePath, path).join("/")}`;
134
- if (!skip) {
135
- this.router.addRoute(method, pattern, handlers);
136
- }
134
+ this.router.addRoute(method, pattern, handlers);
137
135
  this.route.push({
138
136
  method: method,
139
137
  pattern: pattern,
@@ -174,18 +172,9 @@ class Router {
174
172
  if (!(router instanceof Router)) {
175
173
  throw new Error("Router instance is required.");
176
174
  }
177
- if (this.router?.mergeRouter) {
178
- const parts = (0, low_level_js_1.sanitizePathSplitBasePath)(this.basePath, path);
179
- router.route.forEach((r) => {
180
- this.#addRoute(r?.method, `/${(0, low_level_js_1.sanitizePathSplitBasePath)(path, r?.pattern).join("/")}`, r?.handlers, true);
181
- });
182
- this.router.mergeRouter(`/${parts.join("/")}`, router.router);
183
- }
184
- else {
185
- router.route.forEach((r) => {
186
- this.#addRoute(r?.method, `/${(0, low_level_js_1.sanitizePathSplitBasePath)(path, r?.pattern).join("/")}`, r?.handlers);
187
- });
188
- }
175
+ router.route.forEach((r) => {
176
+ this.#addRoute(r?.method, `/${(0, low_level_js_1.sanitizePathSplitBasePath)(path, r?.pattern).join("/")}`, r?.handlers);
177
+ });
189
178
  Object.assign(this.staticFile, router.staticFile);
190
179
  }
191
180
  }
package/cjs/index.js CHANGED
@@ -5,7 +5,7 @@ const router_js_1 = require("./core/router.js");
5
5
  Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return router_js_1.Router; } });
6
6
  const server_js_1 = require("./core/server.js");
7
7
  Object.defineProperty(exports, "TezX", { enumerable: true, get: function () { return server_js_1.TezX; } });
8
- exports.version = "3.0.8-beta";
8
+ exports.version = "3.0.10-beta";
9
9
  exports.default = {
10
10
  Router: router_js_1.Router,
11
11
  TezX: server_js_1.TezX,
@@ -36,7 +36,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.default = exports.cacheControl = void 0;
37
37
  const config_js_1 = require("../core/config.js");
38
38
  const cacheControl = (options) => {
39
- const { defaultSettings, useWeakETag = false, rules = [], logEvent = (event, ctx, error) => {
39
+ const { defaultSettings, useWeakETag = false, rules = [], onError = (error, ctx) => {
40
+ ctx.setStatus = 500;
41
+ ctx.body = { error: "Failed to set cache headers." };
42
+ }, logEvent = (event, ctx, error) => {
40
43
  if (event === "error") {
41
44
  config_js_1.GlobalConfig.debugging.error(`[CACHE] ${event.toUpperCase()}: ${error?.message}`);
42
45
  }
@@ -77,8 +80,7 @@ const cacheControl = (options) => {
77
80
  }
78
81
  catch (error) {
79
82
  logEvent("error", ctx, error);
80
- ctx.setStatus = 500;
81
- ctx.body = { error: "Failed to set cache headers." };
83
+ return onError?.(error, ctx);
82
84
  }
83
85
  };
84
86
  };
@@ -102,23 +102,6 @@ class RadixRouter {
102
102
  }
103
103
  return { success: false, node };
104
104
  }
105
- mergeRouter(basePath, childRouter) {
106
- const segments = (0, index_js_1.sanitizePathSplit)(basePath);
107
- let node = this.root;
108
- for (const segment of segments) {
109
- node.children[segment] ??= { children: {} };
110
- node = node.children[segment];
111
- }
112
- if (childRouter.root.handlers) {
113
- node.isEndpoint = true;
114
- node.handlers ??= {};
115
- for (const method in childRouter.root.handlers) {
116
- node.handlers[method] ??= [];
117
- node.handlers[method].push(...(childRouter.root.handlers?.[method] || []));
118
- }
119
- }
120
- Object.assign(node.children, childRouter.root.children);
121
- }
122
105
  parsePattern(pattern) {
123
106
  const segments = (0, index_js_1.sanitizePathSplit)(pattern);
124
107
  const result = [];
@@ -38,7 +38,7 @@ function determineContentTypeBody(body) {
38
38
  if (typeof Blob !== "undefined" && body instanceof Blob) {
39
39
  return { type: body.type || "application/octet-stream", body };
40
40
  }
41
- if (typeof body === "object" && typeof body.pipe === "function") {
41
+ if (typeof body === "object" && typeof body?.pipe === "function") {
42
42
  return { type: "application/octet-stream", body };
43
43
  }
44
44
  if (typeof body === "object") {
package/core/context.d.ts CHANGED
@@ -102,15 +102,6 @@ export declare class Context<T extends Record<string, any> = {}, Path extends st
102
102
  * @returns {this} Returns the current instance for method chaining.
103
103
  */
104
104
  deleteHeader(key: ResHeaderKey): this;
105
- /**
106
- * Gets the route parameters extracted from the URL.
107
- *
108
- * @returns {Record<string, any>} An object containing key-value pairs of route parameters.
109
- *
110
- * @example
111
- * // For route `/user/:id` and URL `/user/123`, it returns: { id: "123" }
112
- */
113
- get params(): Record<string, any>;
114
105
  protected set params(params: Record<string, any>);
115
106
  /**
116
107
  * Gets the wrapped request object (`TezXRequest`).
package/core/context.js CHANGED
@@ -71,9 +71,6 @@ export class Context {
71
71
  }
72
72
  return this;
73
73
  }
74
- get params() {
75
- return this.#params;
76
- }
77
74
  set params(params) {
78
75
  this.#params = params;
79
76
  }
@@ -132,7 +129,7 @@ export class Context {
132
129
  redirect(url, status = 302) {
133
130
  return new Response(null, {
134
131
  status: status,
135
- headers: { Location: url },
132
+ headers: { ...this.#headers, Location: url },
136
133
  });
137
134
  }
138
135
  async download(filePath, filename) {
package/core/router.js CHANGED
@@ -126,11 +126,9 @@ export class Router {
126
126
  }
127
127
  return this;
128
128
  }
129
- #addRoute(method, path, handlers, skip = false) {
129
+ #addRoute(method, path, handlers) {
130
130
  let pattern = `/${sanitizePathSplitBasePath(this.basePath, path).join("/")}`;
131
- if (!skip) {
132
- this.router.addRoute(method, pattern, handlers);
133
- }
131
+ this.router.addRoute(method, pattern, handlers);
134
132
  this.route.push({
135
133
  method: method,
136
134
  pattern: pattern,
@@ -171,18 +169,9 @@ export class Router {
171
169
  if (!(router instanceof Router)) {
172
170
  throw new Error("Router instance is required.");
173
171
  }
174
- if (this.router?.mergeRouter) {
175
- const parts = sanitizePathSplitBasePath(this.basePath, path);
176
- router.route.forEach((r) => {
177
- this.#addRoute(r?.method, `/${sanitizePathSplitBasePath(path, r?.pattern).join("/")}`, r?.handlers, true);
178
- });
179
- this.router.mergeRouter(`/${parts.join("/")}`, router.router);
180
- }
181
- else {
182
- router.route.forEach((r) => {
183
- this.#addRoute(r?.method, `/${sanitizePathSplitBasePath(path, r?.pattern).join("/")}`, r?.handlers);
184
- });
185
- }
172
+ router.route.forEach((r) => {
173
+ this.#addRoute(r?.method, `/${sanitizePathSplitBasePath(path, r?.pattern).join("/")}`, r?.handlers);
174
+ });
186
175
  Object.assign(this.staticFile, router.staticFile);
187
176
  }
188
177
  }
package/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { Router } from "./core/router.js";
2
2
  import { TezX } from "./core/server.js";
3
3
  export type { Context as BaseContext } from "./core/context.js";
4
- export type { NetAddr as AddressType, Callback, Ctx as Context, CookieOptions, ErrorHandler, FormDataOptions, HandlerType, HttpBaseResponse, HTTPMethod, Middleware, StaticFileArray, NextCallback, RequestHeaders, ResponseHeaders, ResponseInit, RouteMatchResult, RouteRegistry, Runtime, StaticServeOption, WebSocketCallback, WebSocketEvent, WebSocketOptions, } from "./types/index.js";
4
+ export type { NetAddr as AddressType, Callback, Ctx as Context, CookieOptions, ErrorHandler, FormDataOptions, HandlerType, HttpBaseResponse, HTTPMethod, Middleware, NextCallback, RequestHeaders, ResponseHeaders, ResponseInit, RouteMatchResult, RouteRegistry, Runtime, StaticFileArray, StaticServeOption, WebSocketCallback, WebSocketEvent, WebSocketOptions } from "./types/index.js";
5
5
  export type { TezXConfig } from "./core/server.js";
6
- export type { RouterConfig } from "./core/router.js";
7
6
  export type { TezXRequest } from "./core/request.js";
7
+ export type { RouterConfig } from "./core/router.js";
8
8
  export { Router, TezX };
9
9
  export declare let version: string;
10
10
  declare const _default: {
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Router } from "./core/router.js";
2
2
  import { TezX } from "./core/server.js";
3
3
  export { Router, TezX };
4
- export let version = "3.0.8-beta";
4
+ export let version = "3.0.10-beta";
5
5
  export default {
6
6
  Router,
7
7
  TezX,
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { Middleware } from "../types/index.js";
2
+ import { HttpBaseResponse, Middleware } from "../types/index.js";
3
3
  export type CacheRule = {
4
4
  /**
5
5
  * ๐ŸŽฏ Condition to determine if this rule applies.
@@ -28,6 +28,13 @@ export type CacheOptions = {
28
28
  * ๐Ÿงช Weak ETag generation (optional).
29
29
  */
30
30
  useWeakETag?: boolean;
31
+ /**
32
+ * Error handler for cache middleware.
33
+ * @param error - The error that occurred.
34
+ * @param ctx - The current request context.
35
+ * @returns An HTTP response to send when an error occurs.
36
+ */
37
+ onError?: (error: Error, ctx: Context) => HttpBaseResponse;
31
38
  /**
32
39
  * ๐Ÿ“ Logging function for cache events.
33
40
  */
@@ -1,6 +1,9 @@
1
1
  import { GlobalConfig } from "../core/config.js";
2
2
  const cacheControl = (options) => {
3
- const { defaultSettings, useWeakETag = false, rules = [], logEvent = (event, ctx, error) => {
3
+ const { defaultSettings, useWeakETag = false, rules = [], onError = (error, ctx) => {
4
+ ctx.setStatus = 500;
5
+ ctx.body = { error: "Failed to set cache headers." };
6
+ }, logEvent = (event, ctx, error) => {
4
7
  if (event === "error") {
5
8
  GlobalConfig.debugging.error(`[CACHE] ${event.toUpperCase()}: ${error?.message}`);
6
9
  }
@@ -41,8 +44,7 @@ const cacheControl = (options) => {
41
44
  }
42
45
  catch (error) {
43
46
  logEvent("error", ctx, error);
44
- ctx.setStatus = 500;
45
- ctx.body = { error: "Failed to set cache headers." };
47
+ return onError?.(error, ctx);
46
48
  }
47
49
  };
48
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tezx",
3
- "version": "3.0.8-beta",
3
+ "version": "3.0.10-beta",
4
4
  "description": "TezX is a high-performance, lightweight JavaScript framework designed for speed, scalability, and flexibility. It enables efficient routing, middleware management, and static file serving with minimal configuration. Fully compatible with Node.js, Deno, and Bun.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",
@@ -30,13 +30,6 @@ export declare class RadixRouter implements RouteRegistry {
30
30
  * @private
31
31
  */
32
32
  private _match;
33
- /**
34
- * Merges a child router under a given base path.
35
- *
36
- * @param basePath - The base route prefix to mount the child router
37
- * @param childRouter - The child router to merge into this one
38
- */
39
- mergeRouter(basePath: string, childRouter: this): void;
40
33
  /**
41
34
  * Parses a route pattern into structured segments.
42
35
  *
@@ -99,23 +99,6 @@ export class RadixRouter {
99
99
  }
100
100
  return { success: false, node };
101
101
  }
102
- mergeRouter(basePath, childRouter) {
103
- const segments = sanitizePathSplit(basePath);
104
- let node = this.root;
105
- for (const segment of segments) {
106
- node.children[segment] ??= { children: {} };
107
- node = node.children[segment];
108
- }
109
- if (childRouter.root.handlers) {
110
- node.isEndpoint = true;
111
- node.handlers ??= {};
112
- for (const method in childRouter.root.handlers) {
113
- node.handlers[method] ??= [];
114
- node.handlers[method].push(...(childRouter.root.handlers?.[method] || []));
115
- }
116
- }
117
- Object.assign(node.children, childRouter.root.children);
118
- }
119
102
  parsePattern(pattern) {
120
103
  const segments = sanitizePathSplit(pattern);
121
104
  const result = [];
package/types/index.d.ts CHANGED
@@ -346,7 +346,6 @@ export interface RouteRegistry {
346
346
  * @param handlers - Array of middleware or callback handlers
347
347
  */
348
348
  addRoute<T extends Record<string, any> = any>(method: HTTPMethod, path: string, handler: HandlerType<T>): void;
349
- mergeRouter?(path: string, router: this): void;
350
349
  /**
351
350
  * Find a route based on the given method and path.
352
351
  *
package/utils/response.js CHANGED
@@ -31,7 +31,7 @@ export function determineContentTypeBody(body) {
31
31
  if (typeof Blob !== "undefined" && body instanceof Blob) {
32
32
  return { type: body.type || "application/octet-stream", body };
33
33
  }
34
- if (typeof body === "object" && typeof body.pipe === "function") {
34
+ if (typeof body === "object" && typeof body?.pipe === "function") {
35
35
  return { type: "application/octet-stream", body };
36
36
  }
37
37
  if (typeof body === "object") {