diff --git a/src/controllers/QController.ts b/src/controllers/QController.ts index 06623f7..2764785 100644 --- a/src/controllers/QController.ts +++ b/src/controllers/QController.ts @@ -933,20 +933,43 @@ export class QController /******************************************************************************* - ** Fetch the data for a specific widget. + * Fetch the data for a specific widget. + * + * Originally, this was a GET request, with params passed on the query string. + * However, that fails if params get too long. So, in 10/2025, qqq-javalin + * is being updated to serve /widget/{widgetName} as a POST. + * + * This method, for some attempted backward compatibility, will first *try* a + * POST. If it fails with a 404 (which an older version (w/o the POST endpoint) + * would return), then, assuming the params are a string (not FormData), then + * this method will automatically re-try the request as a GET. *******************************************************************************/ - async widget(widgetName: string, urlParams?: string): Promise + async widget(widgetName: string, params?: string | FormData): Promise { let url = `/widget/${encodeURIComponent(widgetName)}`; - if (urlParams) + + const paramsAreFormData = params instanceof FormData + let postBody: FormData; + if (paramsAreFormData) { - url += `?${urlParams}`; + postBody = params; + } + else + { + postBody = new FormData(); + if (params) + { + for (let pair of params.split("&")) + { + let [name, value] = pair.split("="); + postBody.append(name, value); + } + } } ///////////////////////////////////////////////////////////////// // see if an abort controller was created for this widget name // ///////////////////////////////////////////////////////////////// - console.log("Looking for controller for widget [" + widgetName + "]"); let controller = QController.widgetAbortControllerMap.get(widgetName); if (controller) { @@ -961,6 +984,66 @@ export class QController const signal = controller.signal; QController.widgetAbortControllerMap.set(widgetName, controller); + return this.axiosInstance + .post(url, postBody, {signal}) + .then((response: AxiosResponse) => + { + /////////////////////////////////////////////////// + // make sure to clear out the request controller // + /////////////////////////////////////////////////// + QController.widgetAbortControllerMap.set(widgetName, null); + return response.data; + }) + .catch(async (e: any) => + { + /////////////////////////////////////////////////////////////////////////////////////////////////////// + // if the server responded with 404, assume this could be due to the backend not supporting POST for // + // the widget endpoint - so if we took in params as a string (not FormData), then try to do a GET // + /////////////////////////////////////////////////////////////////////////////////////////////////////// + if(e?.response?.status == 404) + { + if(!paramsAreFormData) + { + return await this.getWidget(widgetName, params); + } + else + { + console.log("widget POST request failed with 404 - but params are FormData, so we cannot attempt a fallback-GET..."); + } + } + + if (e.code && e.code === "ERR_CANCELED") + { + console.log("Controller request cancellation successful!"); + return; + } + this.handleException(e); + }); + } + + + /******************************************************************************* + * Fetch the data for a specific widget, doing a GET. + * This HTTP method is being deprecated for rendering widgets, due to query + * string being too limited in size, compared to the relative unlimited size of + * POST body - but this method is here for a backward-compatibility later - in + * case hitting a backend server that 404'ed for the POST call. + *******************************************************************************/ + private async getWidget(widgetName: string, urlParams?: string): Promise + { + let url = `/widget/${encodeURIComponent(widgetName)}`; + if(urlParams) + { + url += `?${urlParams}`; + } + + ///////////////////////////////////////// + // keep track of this widget's request // + ///////////////////////////////////////// + const controller = new AbortController(); + const signal = controller.signal; + QController.widgetAbortControllerMap.set(widgetName, controller); + return this.axiosInstance .get(url, {signal}) .then((response: AxiosResponse) => @@ -983,6 +1066,7 @@ export class QController } + /*************************************************************************** * overload 1 of possibleValues function - this is its "original" signature * that took several params, many optional, and kept evolving and getting