aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/intercept-jquery-ajax.ts
diff options
context:
space:
mode:
authorArpit Chakladar <arpitchakladar+git@gmail.com>2026-07-13 23:17:25 +0530
committerArpit Chakladar <arpitchakladar+git@gmail.com>2026-07-13 23:17:25 +0530
commit68777ec421d41be0da27013afd4165ae7d658d26 (patch)
tree4c1cc6af7acfeac54b2e54372944344d3f5b9a26 /src/shared/intercept-jquery-ajax.ts
parent20c3b38f24d79df8843f17985a7d13533d4e0d10 (diff)
downloadbanglar-bhumi-utils-68777ec421d41be0da27013afd4165ae7d658d26.tar.gz
banglar-bhumi-utils-68777ec421d41be0da27013afd4165ae7d658d26.zip
refactor: added linting with esling and also resolved warnings for it
Diffstat (limited to 'src/shared/intercept-jquery-ajax.ts')
-rw-r--r--src/shared/intercept-jquery-ajax.ts87
1 files changed, 56 insertions, 31 deletions
diff --git a/src/shared/intercept-jquery-ajax.ts b/src/shared/intercept-jquery-ajax.ts
index 7e1f54e..b627d0e 100644
--- a/src/shared/intercept-jquery-ajax.ts
+++ b/src/shared/intercept-jquery-ajax.ts
@@ -1,61 +1,86 @@
-type InterceptJqueryAjaxCallback = (args: any) => void;
+export type JQueryAjaxArgs = [
+ url: string,
+ data: string | Record<string, unknown>,
+ callback: (data: unknown, textStatus: string, jqXHR: JQuery.jqXHR) => void,
+ dataType?: string
+];
-interface InterceptJqueryEntry {
- url: string;
- callback: InterceptJqueryAjaxCallback;
+type JQueryAjaxInterceptor = (args: JQueryAjaxArgs) => void;
+
+type InterceptorRegistration = {
+ urlSuffix: string;
+ interceptor: JQueryAjaxInterceptor
};
-const postIntercepts: InterceptJqueryEntry[] = [];
-const getIntercepts: InterceptJqueryEntry[] = [];
+const postInterceptors: InterceptorRegistration[] = [];
+const getInterceptors: InterceptorRegistration[] = [];
+
+// Define a strict signature to replace the unsafe `Function` type
+type OriginalJQueryAjaxMethod = (...args: unknown[]) => JQuery.jqXHR;
/**
* On DOMContentLoaded, proxies jQuery's `$.post` and `$.get` so that
* registered interceptors are called before the real request.
*/
-document.addEventListener("DOMContentLoaded", () => {
- const proxiedPost = $.post;
- const proxiedGet = $.get;
+document.addEventListener("DOMContentLoaded", function() {
+ // 1. Keep the original methods bound to the jQuery ($) object
+ const originalPost = $.post.bind($);
+ const originalGet = $.get.bind($);
/** Proxied `$.post` that invokes registered interceptors before the real call. */
- $.post = function() {
- for (const { url, callback } of postIntercepts) {
- if (arguments[0].endsWith(url)) {
- callback(arguments);
+ $.post = function(this: typeof $, ...args: unknown[]): JQuery.jqXHR {
+ for (const { urlSuffix, interceptor } of postInterceptors) {
+ // Safely inspect jQuery settings objects without using "any"
+ const firstArg = args[0];
+ const requestUrl = typeof firstArg === "string"
+ ? firstArg
+ : (firstArg && typeof firstArg === "object" && "url" in firstArg && typeof firstArg.url === "string" ? firstArg.url : null);
+
+ if (typeof requestUrl === "string" && requestUrl.endsWith(urlSuffix)) {
+ // Cast the unknown array to the expected tuple type
+ interceptor(args as unknown as JQueryAjaxArgs);
}
}
- return proxiedPost.apply(this, Array.from(arguments) as any);
- };
+ // Use our strict signature instead of `Function` to satisfy ESLint
+ return (originalPost as unknown as OriginalJQueryAjaxMethod)(...args);
+ } as typeof $.post; // Reassert the original type so external consumers see the normal signature
+
/** Proxied `$.get` that invokes registered interceptors before the real call. */
- $.get = function() {
- for (const { url, callback } of getIntercepts) {
- if (arguments[0].endsWith(url)) {
- callback(arguments);
+ $.get = function(this: typeof $, ...args: unknown[]): JQuery.jqXHR {
+ for (const { urlSuffix, interceptor } of getInterceptors) {
+ const firstArg = args[0];
+ const requestUrl = typeof firstArg === "string"
+ ? firstArg
+ : (firstArg && typeof firstArg === "object" && "url" in firstArg && typeof firstArg.url === "string" ? firstArg.url : null);
+
+ if (typeof requestUrl === "string" && requestUrl.endsWith(urlSuffix)) {
+ interceptor(args as unknown as JQueryAjaxArgs);
}
}
- return proxiedGet.apply(this, Array.from(arguments) as any);
- };
+ return (originalGet as unknown as OriginalJQueryAjaxMethod)(...args);
+ } as typeof $.get;
});
/**
* Registers a callback that fires whenever a jQuery POST request matches
* the given URL suffix.
*
- * @param url - The URL suffix to match (checked via `endsWith`).
- * @param callback - Receives the original `arguments` from `$.post`.
+ * @param urlSuffix - The URL suffix to match (checked via `endsWith`).
+ * @param interceptor - Receives the original `arguments` from `$.post`.
*/
-export function interceptPost(url: string, callback: InterceptJqueryAjaxCallback) {
- postIntercepts.push({ url, callback });
-};
+export function addPostInterceptor(urlSuffix: string, interceptor: JQueryAjaxInterceptor): void {
+ postInterceptors.push({ urlSuffix, interceptor });
+}
/**
* Registers a callback that fires whenever a jQuery GET request matches
* the given URL suffix.
*
- * @param url - The URL suffix to match (checked via `endsWith`).
- * @param callback - Receives the original `arguments` from `$.get`.
+ * @param urlSuffix - The URL suffix to match (checked via `endsWith`).
+ * @param interceptor - Receives the original `arguments` from `$.get`.
*/
-export function interceptGet(url: string, callback: InterceptJqueryAjaxCallback) {
- getIntercepts.push({ url, callback });
-};
+export function addGetInterceptor(urlSuffix: string, interceptor: JQueryAjaxInterceptor): void {
+ getInterceptors.push({ urlSuffix, interceptor });
+}