Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Migrate redux/downloads off of `metabase/api/legacy-client` and onto RTK-based hooks (#74330)

Romeo Van Snicke2ce2b2d5bc9
diff --git a/frontend/src/metabase/api/dataset.ts b/frontend/src/metabase/api/dataset.ts
index 4786f6268f2..a4aa8326368 100644
--- a/frontend/src/metabase/api/dataset.ts
+++ b/frontend/src/metabase/api/dataset.ts
@@ -29,8 +29,42 @@ interface IgnorableError {
ignore_error?: boolean;
}
+export type DownloadDatasetArgs = {
+ method: "GET" | "POST";
+ url: string;
+ body?: Record<string, unknown>;
+};
+
export const datasetApi = Api.injectEndpoints({
endpoints: (builder) => ({
+ downloadDataset: builder.mutation<Response, DownloadDatasetArgs>({
+ query: ({ method, url, body }) => {
+ if (method === "POST") {
+ // BE expects the body to be form-encoded :(
+ const formData = new URLSearchParams();
+ if (body != null) {
+ for (const key in body) {
+ formData.append(key, JSON.stringify(body[key]));
+ }
+ }
+ return {
+ method: "POST",
+ url,
+ body: { formData },
+ formData: true,
+ fetch: true,
+ transformResponse: ({ response }: { response: Response }) =>
+ response,
+ };
+ }
+ return {
+ method: "GET",
+ url,
+ fetch: true,
+ transformResponse: ({ response }: { response: Response }) => response,
+ };
+ },
+ }),
getAdhocQuery: builder.query<
Dataset,
DatasetQuery & RefetchDeps & IgnorableError
@@ -97,6 +131,7 @@ export const datasetApi = Api.injectEndpoints({
});
export const {
+ useDownloadDatasetMutation,
useGetAdhocQueryQuery,
useLazyGetAdhocQueryQuery,
useGetAdhocPivotQueryQuery,
diff --git a/frontend/src/metabase/api/query.ts b/frontend/src/metabase/api/query.ts
index e415d126505..e35f03d6f5d 100644
--- a/frontend/src/metabase/api/query.ts
+++ b/frontend/src/metabase/api/query.ts
@@ -17,7 +17,7 @@ const isAllowedHTTPMethod = (method: any): method is AllowedHTTPMethods => {
export const apiQuery: BaseQueryFn = async (args, ctx, extraOptions) => {
const method = typeof args === "string" ? "GET" : (args?.method ?? "GET");
const url = typeof args === "string" ? args : args.url;
- const { bodyParamName, noEvent, formData, fetch } = args;
+ const { bodyParamName, noEvent, formData, fetch, transformResponse } = args;
if (!isAllowedHTTPMethod(method)) {
return { error: "Invalid HTTP method" };
@@ -34,6 +34,7 @@ export const apiQuery: BaseQueryFn = async (args, ctx, extraOptions) => {
noEvent,
formData,
fetch,
+ transformResponse,
...extraOptions,
},
);
diff --git a/frontend/src/metabase/redux/downloads.ts b/frontend/src/metabase/redux/downloads.ts
index eef1f5e56be..45294714a07 100644
--- a/frontend/src/metabase/redux/downloads.ts
+++ b/frontend/src/metabase/redux/downloads.ts
@@ -8,7 +8,8 @@ import {
import { t } from "ttag";
import _ from "underscore";
-import api, { GET, POST } from "metabase/api/legacy-client";
+import { datasetApi } from "metabase/api/dataset";
+import api from "metabase/api/legacy-client";
import { exportFormatPng } from "metabase/common/types/export";
import { waitUntilNextFramePainted } from "metabase/common/utils/wait-until-next-frame-paints";
import { trackExportDashboardToPDF } from "metabase/dashboard/analytics";
@@ -58,7 +59,7 @@ export interface DownloadQueryResultsOpts {
}
interface DownloadQueryResultsParams {
- method: string;
+ method: "GET" | "POST";
url: string;
body?: Record<string, unknown>;
params?: URLSearchParams | string;
@@ -242,14 +243,28 @@ export const downloadQueryResults = createAsyncThunk(
export const downloadDataset = createAsyncThunk(
"metabase/downloads/downloadDataset",
- async ({ opts, id }: { opts: DownloadQueryResultsOpts; id: number }) => {
+ async (
+ { opts, id }: { opts: DownloadQueryResultsOpts; id: number },
+ { dispatch },
+ ) => {
const params = getDatasetParams(opts);
- const response = await getDatasetResponse(params);
- const fileName = getDatasetFileName(response.headers, opts.type);
- const fileContent = await response.blob();
- openSaveDialog(fileName, fileContent);
-
- return { id, fileName };
+ const promise = dispatch(
+ datasetApi.endpoints.downloadDataset.initiate({
+ method: params.method,
+ url: getDatasetDownloadUrl(params.url, params.params),
+ body: params.body,
+ }),
+ );
+ try {
+ const response = await promise.unwrap();
+ const fileName = getDatasetFileName(response.headers, opts.type);
+ const fileContent = await response.blob();
+ openSaveDialog(fileName, fileContent);
+
+ return { id, fileName };
+ } finally {
+ promise.reset();
+ }
},
);
@@ -552,43 +567,6 @@ export function getDatasetDownloadUrl(
return url;
}
-interface TransformResponseProps {
- response?: Response;
-}
-
-const getDatasetResponse = ({
- url,
- method,
- body,
- params,
-}: DownloadQueryResultsParams) => {
- const requestUrl = getDatasetDownloadUrl(url, params);
-
- if (method === "POST") {
- // BE expects the body to be form-encoded :(
- const formattedBody = new URLSearchParams();
- if (body != null) {
- for (const key in body) {
- formattedBody.append(key, JSON.stringify(body[key]));
- }
- }
- return POST(requestUrl, {
- formData: true,
- fetch: true,
- transformResponse: ({ response }: TransformResponseProps) =>
- checkNotNull(response),
- })({
- formData: formattedBody,
- });
- } else {
- return GET(requestUrl, {
- fetch: true,
- transformResponse: ({ response }: TransformResponseProps) =>
- checkNotNull(response),
- })();
- }
-};
-
const getDatasetFileName = (headers: Headers, type: string) => {
const header = headers.get("Content-Disposition") ?? "";
const headerContent = decodeURIComponent(header);