diff --git a/.betterer.results b/.betterer.results index 4ba3eb8a341..e57b20976b5 100644 --- a/.betterer.results +++ b/.betterer.results @@ -1072,12 +1072,10 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "6"], [0, 0, 0, "Unexpected any. Specify a different type.", "7"], [0, 0, 0, "Unexpected any. Specify a different type.", "8"], - [0, 0, 0, "Unexpected any. Specify a different type.", "9"], - [0, 0, 0, "Unexpected any. Specify a different type.", "10"], - [0, 0, 0, "Do not use any type assertions.", "11"], - [0, 0, 0, "Do not use any type assertions.", "12"], - [0, 0, 0, "Unexpected any. Specify a different type.", "13"], - [0, 0, 0, "Do not use any type assertions.", "14"] + [0, 0, 0, "Do not use any type assertions.", "9"], + [0, 0, 0, "Do not use any type assertions.", "10"], + [0, 0, 0, "Unexpected any. Specify a different type.", "11"], + [0, 0, 0, "Do not use any type assertions.", "12"] ], "packages/grafana-runtime/src/utils/analytics.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] @@ -2870,8 +2868,7 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "14"], [0, 0, 0, "Unexpected any. Specify a different type.", "15"], [0, 0, 0, "Unexpected any. Specify a different type.", "16"], - [0, 0, 0, "Unexpected any. Specify a different type.", "17"], - [0, 0, 0, "Unexpected any. Specify a different type.", "18"] + [0, 0, 0, "Unexpected any. Specify a different type.", "17"] ], "public/app/core/services/context_srv.ts:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/packages/grafana-runtime/src/services/backendSrv.ts b/packages/grafana-runtime/src/services/backendSrv.ts index 27e743447dc..90df2ad263f 100644 --- a/packages/grafana-runtime/src/services/backendSrv.ts +++ b/packages/grafana-runtime/src/services/backendSrv.ts @@ -143,15 +143,16 @@ export function isFetchError(e: unknown): e is FetchError { * @public */ export interface BackendSrv { - get(url: string, params?: any, requestId?: string): Promise; - delete(url: string, data?: any): Promise; - post(url: string, data?: any): Promise; - patch(url: string, data?: any): Promise; - put(url: string, data?: any): Promise; + get(url: string, params?: any, requestId?: string, options?: Partial): Promise; + delete(url: string, data?: any, options?: Partial): Promise; + post(url: string, data?: any, options?: Partial): Promise; + patch(url: string, data?: any, options?: Partial): Promise; + put(url: string, data?: any, options?: Partial): Promise; /** - * @deprecated Use the fetch function instead. If you prefer to work with a promise - * wrap the Observable returned by fetch with the lastValueFrom function. + * @deprecated Use the `.fetch()` function instead. If you prefer to work with a promise + * wrap the Observable returned by fetch with the lastValueFrom function, or use the get|delete|post|patch|put methods. + * This method is going to be private from Grafana 10. */ request(options: BackendSrvRequest): Promise; diff --git a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts index bb9b75d1282..3c8fe6800a2 100644 --- a/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts +++ b/packages/grafana-runtime/src/utils/DataSourceWithBackend.ts @@ -1,4 +1,4 @@ -import { merge, Observable, of } from 'rxjs'; +import { lastValueFrom, merge, Observable, of } from 'rxjs'; import { catchError, switchMap } from 'rxjs/operators'; import { @@ -24,6 +24,8 @@ import { getGrafanaLiveSrv, StreamingFrameOptions, StreamingFrameAction, + BackendSrvRequest, + FetchResponse, } from '../services'; import { BackendDataSourceResponse, toDataQueryResponse } from './queryResponse'; @@ -219,29 +221,38 @@ class DataSourceWithBackend< /** * Make a GET request to the datasource resource path */ - async getResource(path: string, params?: any): Promise { - return getBackendSrv().get(`/api/datasources/${this.id}/resources/${path}`, params); + async getResource( + path: string, + params?: BackendSrvRequest['params'], + options?: Partial + ): Promise { + return getBackendSrv().get(`/api/datasources/${this.id}/resources/${path}`, params, options?.requestId, options); } /** * Send a POST request to the datasource resource path */ - async postResource(path: string, body?: any): Promise { - return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body }); + async postResource( + path: string, + data?: BackendSrvRequest['data'], + options?: Partial + ): Promise { + return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...data }, options); } /** * Run the datasource healthcheck */ async callHealthCheck(): Promise { - return getBackendSrv() - .request({ method: 'GET', url: `/api/datasources/${this.id}/health`, showErrorAlert: false }) - .then((v) => { - return v as HealthCheckResult; + return lastValueFrom( + getBackendSrv().fetch({ + method: 'GET', + url: `/api/datasources/${this.id}/health`, + showErrorAlert: false, }) - .catch((err) => { - return err.data as HealthCheckResult; - }); + ) + .then((v: FetchResponse) => v.data as HealthCheckResult) + .catch((err) => err.data as HealthCheckResult); } /** diff --git a/public/app/core/services/backend_srv.ts b/public/app/core/services/backend_srv.ts index 99cf0a1da70..b1526f90c73 100644 --- a/public/app/core/services/backend_srv.ts +++ b/public/app/core/services/backend_srv.ts @@ -407,24 +407,29 @@ export class BackendSrv implements BackendService { return this.inspectorStream; } - async get(url: string, params?: any, requestId?: string): Promise { - return await this.request({ method: 'GET', url, params, requestId }); + async get( + url: string, + params?: BackendSrvRequest['params'], + requestId?: BackendSrvRequest['requestId'], + options?: Partial + ) { + return this.request({ ...options, method: 'GET', url, params, requestId }); } - async delete(url: string, data?: any): Promise { - return await this.request({ method: 'DELETE', url, data }); + async delete(url: string, data?: any, options?: Partial) { + return this.request({ ...options, method: 'DELETE', url, data }); } - async post(url: string, data?: any): Promise { - return await this.request({ method: 'POST', url, data }); + async post(url: string, data?: any, options?: Partial) { + return this.request({ ...options, method: 'POST', url, data }); } - async patch(url: string, data: any): Promise { - return await this.request({ method: 'PATCH', url, data }); + async patch(url: string, data: any, options?: Partial) { + return this.request({ ...options, method: 'PATCH', url, data }); } - async put(url: string, data: any): Promise { - return await this.request({ method: 'PUT', url, data }); + async put(url: string, data: any, options?: Partial): Promise { + return this.request({ ...options, method: 'PUT', url, data }); } withNoBackendCache(callback: any) { diff --git a/public/app/features/manage-dashboards/state/actions.ts b/public/app/features/manage-dashboards/state/actions.ts index d83f3cfe58d..91a832143fe 100644 --- a/public/app/features/manage-dashboards/state/actions.ts +++ b/public/app/features/manage-dashboards/state/actions.ts @@ -10,6 +10,7 @@ import { LibraryElementExport } from '../../dashboard/components/DashExportModal import { getLibraryPanel } from '../../library-panels/state/api'; import { LibraryElementDTO, LibraryElementKind } from '../../library-panels/types'; import { DashboardSearchHit } from '../../search/types'; +import { DeleteDashboardResponse } from '../types'; import { clearDashboard, @@ -275,11 +276,7 @@ export function saveDashboard(options: SaveDashboardCommand) { } function deleteFolder(uid: string, showSuccessAlert: boolean) { - return getBackendSrv().request({ - method: 'DELETE', - url: `/api/folders/${uid}?forceDeleteRules=false`, - showSuccessAlert: showSuccessAlert, - }); + return getBackendSrv().delete(`/api/folders/${uid}?forceDeleteRules=false`, undefined, { showSuccessAlert }); } export function createFolder(payload: any) { @@ -304,11 +301,7 @@ export function getFolderById(id: number): Promise<{ id: number; title: string } } export function deleteDashboard(uid: string, showSuccessAlert: boolean) { - return getBackendSrv().request({ - method: 'DELETE', - url: `/api/dashboards/uid/${uid}`, - showSuccessAlert: showSuccessAlert, - }); + return getBackendSrv().delete(`/api/dashboards/uid/${uid}`, { showSuccessAlert }); } function executeInOrder(tasks: any[]) { diff --git a/public/app/features/manage-dashboards/types.ts b/public/app/features/manage-dashboards/types.ts index e3b8e490d54..41a746e254e 100644 --- a/public/app/features/manage-dashboards/types.ts +++ b/public/app/features/manage-dashboards/types.ts @@ -11,3 +11,9 @@ export interface Snapshot { url?: string; userId: number; } + +export type DeleteDashboardResponse = { + id: number; + message: string; + title: string; +}; diff --git a/public/app/plugins/panel/gettingstarted/GettingStarted.tsx b/public/app/plugins/panel/gettingstarted/GettingStarted.tsx index b432ecd31ff..d32a1a7a34f 100644 --- a/public/app/plugins/panel/gettingstarted/GettingStarted.tsx +++ b/public/app/plugins/panel/gettingstarted/GettingStarted.tsx @@ -71,15 +71,9 @@ export class GettingStarted extends PureComponent { dashboard?.removePanel(panel!); - backendSrv - .request({ - method: 'PUT', - url: '/api/user/helpflags/1', - showSuccessAlert: false, - }) - .then((res: any) => { - contextSrv.user.helpFlags1 = res.helpFlags1; - }); + backendSrv.put('/api/user/helpflags/1', undefined, { showSuccessAlert: false }).then((res: any) => { + contextSrv.user.helpFlags1 = res.helpFlags1; + }); }; render() {