aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/intercept-jquery-ajax.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/intercept-jquery-ajax.ts')
-rw-r--r--src/shared/intercept-jquery-ajax.ts99
1 files changed, 72 insertions, 27 deletions
diff --git a/src/shared/intercept-jquery-ajax.ts b/src/shared/intercept-jquery-ajax.ts
index a77dd44..b627d0e 100644
--- a/src/shared/intercept-jquery-ajax.ts
+++ b/src/shared/intercept-jquery-ajax.ts
@@ -1,41 +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;
-document.addEventListener("DOMContentLoaded", () => {
- const proxiedPost = $.post;
- const proxiedGet = $.get;
+/**
+ * On DOMContentLoaded, proxies jQuery's `$.post` and `$.get` so that
+ * registered interceptors are called before the real request.
+ */
+document.addEventListener("DOMContentLoaded", function() {
+ // 1. Keep the original methods bound to the jQuery ($) object
+ const originalPost = $.post.bind($);
+ const originalGet = $.get.bind($);
- $.post = function() {
- for (const { url, callback } of postIntercepts) {
- if (arguments[0].endsWith(url)) {
- callback(arguments);
+ /** Proxied `$.post` that invokes registered interceptors before the real call. */
+ $.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);
- };
- $.get = function() {
- for (const { url, callback } of getIntercepts) {
- if (arguments[0].endsWith(url)) {
- callback(arguments);
+ // 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(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;
});
-export function interceptPost(url: string, callback: InterceptJqueryAjaxCallback) {
- postIntercepts.push({ url, callback });
-};
+/**
+ * Registers a callback that fires whenever a jQuery POST request matches
+ * the given URL suffix.
+ *
+ * @param urlSuffix - The URL suffix to match (checked via `endsWith`).
+ * @param interceptor - Receives the original `arguments` from `$.post`.
+ */
+export function addPostInterceptor(urlSuffix: string, interceptor: JQueryAjaxInterceptor): void {
+ postInterceptors.push({ urlSuffix, interceptor });
+}
-export function interceptGet(url: string, callback: InterceptJqueryAjaxCallback) {
- getIntercepts.push({ url, callback });
-};
+/**
+ * Registers a callback that fires whenever a jQuery GET request matches
+ * the given URL suffix.
+ *
+ * @param urlSuffix - The URL suffix to match (checked via `endsWith`).
+ * @param interceptor - Receives the original `arguments` from `$.get`.
+ */
+export function addGetInterceptor(urlSuffix: string, interceptor: JQueryAjaxInterceptor): void {
+ getInterceptors.push({ urlSuffix, interceptor });
+}