tezx 1.0.44 → 1.0.45

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.
@@ -7,13 +7,15 @@ const context_1 = require("./context");
7
7
  const router_1 = require("./router");
8
8
  const params_1 = require("../utils/params");
9
9
  class TezX extends router_1.Router {
10
- constructor({ basePath = "/", env = {}, debugMode = false, allowDuplicateMw = false, overwriteMethod = true, } = {}) {
10
+ #onPathResolve;
11
+ constructor({ basePath = "/", env = {}, debugMode = false, onPathResolve, allowDuplicateMw = false, overwriteMethod = true, } = {}) {
11
12
  config_1.GlobalConfig.allowDuplicateMw = allowDuplicateMw;
12
13
  config_1.GlobalConfig.overwriteMethod = overwriteMethod;
13
14
  if (debugMode) {
14
15
  config_1.GlobalConfig.debugMode = debugMode;
15
16
  }
16
17
  super({ basePath, env });
18
+ this.#onPathResolve = onPathResolve;
17
19
  this.serve = this.serve.bind(this);
18
20
  }
19
21
  #hashRouter(method, pathname) {
@@ -121,11 +123,19 @@ class TezX extends router_1.Router {
121
123
  let ctx = new context_1.Context(req, connInfo);
122
124
  const urlRef = ctx.req.urlRef;
123
125
  const { pathname } = urlRef;
124
- let middlewares = this.#findMiddleware(pathname);
126
+ let resolvePath = pathname;
127
+ if (this.#onPathResolve) {
128
+ resolvePath = this.#onPathResolve(pathname);
129
+ config_1.GlobalConfig.debugging.warn(`${colors_1.COLORS.white} PATH RESOLVE ${colors_1.COLORS.reset} ${colors_1.COLORS.red}${pathname}${colors_1.COLORS.reset} ➞ ${colors_1.COLORS.cyan}${resolvePath}${colors_1.COLORS.reset}`);
130
+ }
131
+ if (typeof resolvePath !== 'string') {
132
+ throw new Error(`Path resolution failed: expected a string, got ${typeof resolvePath}`);
133
+ }
134
+ let middlewares = this.#findMiddleware(resolvePath);
125
135
  ctx.env = this.env;
126
136
  try {
127
137
  let callback = async (ctx) => {
128
- const find = this.findRoute(ctx.req.method, pathname);
138
+ const find = this.findRoute(ctx.req.method, resolvePath);
129
139
  if (find?.callback) {
130
140
  ctx.params = find.params;
131
141
  const callback = find.callback;
package/cjs/index.js CHANGED
@@ -7,4 +7,4 @@ var server_1 = require("./core/server");
7
7
  Object.defineProperty(exports, "TezX", { enumerable: true, get: function () { return server_1.TezX; } });
8
8
  var params_1 = require("./utils/params");
9
9
  Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return params_1.useParams; } });
10
- exports.version = "1.0.44";
10
+ exports.version = "1.0.45";
package/core/server.d.ts CHANGED
@@ -32,6 +32,25 @@ export type TezXConfig = {
32
32
  * @default true
33
33
  */
34
34
  overwriteMethod?: boolean;
35
+ /**
36
+ * 🔄 Hook to transform or normalize the incoming request pathname before routing.
37
+ *
38
+ * This function allows you to customize how incoming paths are handled.
39
+ * You can use it to:
40
+ * - Remove trailing slashes
41
+ * - Normalize casing
42
+ * - Rewrite certain paths dynamically
43
+ * - Add localization or versioning prefixes
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * onPathResolve: (pathname) => pathname.replace(/\/+$/, "").toLowerCase()
48
+ * ```
49
+ *
50
+ * @param pathname - The raw incoming request path (e.g., `/Api/Users/`)
51
+ * @returns The transformed or resolved path used for routing (e.g., `/api/users`)
52
+ */
53
+ onPathResolve?: (pathname: string) => string;
35
54
  /**
36
55
  * Enables or disables debugging for the middleware.
37
56
  * When set to `true`, detailed debug logs will be output,
@@ -43,7 +62,7 @@ export type TezXConfig = {
43
62
  } & RouterConfig;
44
63
  export declare class TezX<T extends Record<string, any> = {}> extends Router<T> {
45
64
  #private;
46
- constructor({ basePath, env, debugMode, allowDuplicateMw, overwriteMethod, }?: TezXConfig);
65
+ constructor({ basePath, env, debugMode, onPathResolve, allowDuplicateMw, overwriteMethod, }?: TezXConfig);
47
66
  protected findRoute(method: HTTPMethod, pathname: string): {
48
67
  callback: any;
49
68
  middlewares: Middleware<T>[];
package/core/server.js CHANGED
@@ -4,13 +4,15 @@ import { Context, httpStatusMap } from "./context";
4
4
  import { Router } from "./router";
5
5
  import { useParams } from "../utils/params";
6
6
  export class TezX extends Router {
7
- constructor({ basePath = "/", env = {}, debugMode = false, allowDuplicateMw = false, overwriteMethod = true, } = {}) {
7
+ #onPathResolve;
8
+ constructor({ basePath = "/", env = {}, debugMode = false, onPathResolve, allowDuplicateMw = false, overwriteMethod = true, } = {}) {
8
9
  GlobalConfig.allowDuplicateMw = allowDuplicateMw;
9
10
  GlobalConfig.overwriteMethod = overwriteMethod;
10
11
  if (debugMode) {
11
12
  GlobalConfig.debugMode = debugMode;
12
13
  }
13
14
  super({ basePath, env });
15
+ this.#onPathResolve = onPathResolve;
14
16
  this.serve = this.serve.bind(this);
15
17
  }
16
18
  #hashRouter(method, pathname) {
@@ -118,11 +120,19 @@ export class TezX extends Router {
118
120
  let ctx = new Context(req, connInfo);
119
121
  const urlRef = ctx.req.urlRef;
120
122
  const { pathname } = urlRef;
121
- let middlewares = this.#findMiddleware(pathname);
123
+ let resolvePath = pathname;
124
+ if (this.#onPathResolve) {
125
+ resolvePath = this.#onPathResolve(pathname);
126
+ GlobalConfig.debugging.warn(`${COLORS.white} PATH RESOLVE ${COLORS.reset} ${COLORS.red}${pathname}${COLORS.reset} ➞ ${COLORS.cyan}${resolvePath}${COLORS.reset}`);
127
+ }
128
+ if (typeof resolvePath !== 'string') {
129
+ throw new Error(`Path resolution failed: expected a string, got ${typeof resolvePath}`);
130
+ }
131
+ let middlewares = this.#findMiddleware(resolvePath);
122
132
  ctx.env = this.env;
123
133
  try {
124
134
  let callback = async (ctx) => {
125
- const find = this.findRoute(ctx.req.method, pathname);
135
+ const find = this.findRoute(ctx.req.method, resolvePath);
126
136
  if (find?.callback) {
127
137
  ctx.params = find.params;
128
138
  const callback = find.callback;
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Router } from "./core/router";
2
2
  export { TezX } from "./core/server";
3
3
  export { useParams } from "./utils/params";
4
- export let version = "1.0.44";
4
+ export let version = "1.0.45";
@@ -4,7 +4,13 @@ import { CallbackReturn } from "../core/router";
4
4
  /**
5
5
  * Supported authentication method types.
6
6
  */
7
- type AuthMethod = "basic" | "api-key" | "bearer-token";
7
+ export type AuthMethod = "basic" | "api-key" | "bearer-token";
8
+ export type AuthCredential = {
9
+ username?: any;
10
+ password?: any;
11
+ token?: any;
12
+ apiKey?: any;
13
+ };
8
14
  /**
9
15
  * Configuration options for dynamic basic authentication.
10
16
  */
@@ -16,7 +22,7 @@ type DynamicBasicAuthOptions = {
16
22
  * @param ctx - The current request context.
17
23
  * @returns A boolean or Promise resolving to whether the credentials are valid.
18
24
  */
19
- validateCredentials: (method: AuthMethod, credentials: Record<string, any>, ctx: Context) => boolean | Promise<boolean>;
25
+ validateCredentials: (method: AuthMethod, credentials: AuthCredential, ctx: Context) => boolean | Promise<boolean>;
20
26
  /**
21
27
  * 🔒 Function to dynamically determine the realm for authentication prompt.
22
28
  * @param ctx - The current request context.
@@ -65,7 +71,7 @@ type DynamicBasicAuthOptions = {
65
71
  * @param credentials - The validated credentials.
66
72
  * @returns Whether access is allowed.
67
73
  */
68
- checkAccess?: (ctx: Context, credentials: Record<string, any>) => boolean | Promise<boolean>;
74
+ checkAccess?: (ctx: Context, credentials: AuthCredential) => boolean | Promise<boolean>;
69
75
  };
70
76
  /**
71
77
  * 🔐 Middleware for flexible authentication using Basic, API Key, or Bearer Token.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tezx",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
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
  "main": "cjs/index.js",
6
6
  "module": "index.js",