mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 03:34:15 -06:00
BackendSrv: Make it possible to pass options
to .get|post|patch...
methods (#51316)
* feat: make it possible to pass in `options` to `.get()`, `.post()`, etc methods * feat(Datasource): make it possible to specify `optinos` for `getResource()` and `postResource()` * fix(BackendSrv): use partial `BackendSrvRequest` * feat(Datasource): make it possible to set `options` for resource requests * refactor(BackendSrv): remove internal usage of `.request()` * fix(GettingStarted): remove unnecessary import * fix: fix type issues by typing the delete dashboard response * refactor: use more strict types for `params` By reusing the type of `BackendSrvRequest.params` * refactor: make the options paramater partial * fix: extract `data` from the fetch response * docs: update the deprecation notice or `BackendSrv.request()` * refactor: use `.request()` as if it was private * refactor: remove unnecessary change
This commit is contained in:
parent
489b302c03
commit
cc6fae18db
@ -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.", "6"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "7"],
|
[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.", "8"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "9"],
|
[0, 0, 0, "Do not use any type assertions.", "9"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "10"],
|
[0, 0, 0, "Do not use any type assertions.", "10"],
|
||||||
[0, 0, 0, "Do not use any type assertions.", "11"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "11"],
|
||||||
[0, 0, 0, "Do not use any type assertions.", "12"],
|
[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"]
|
|
||||||
],
|
],
|
||||||
"packages/grafana-runtime/src/utils/analytics.ts:5381": [
|
"packages/grafana-runtime/src/utils/analytics.ts:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
|
[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.", "14"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "15"],
|
[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.", "16"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "17"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "17"]
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "18"]
|
|
||||||
],
|
],
|
||||||
"public/app/core/services/context_srv.ts:5381": [
|
"public/app/core/services/context_srv.ts:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
||||||
|
@ -143,15 +143,16 @@ export function isFetchError(e: unknown): e is FetchError {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export interface BackendSrv {
|
export interface BackendSrv {
|
||||||
get<T = any>(url: string, params?: any, requestId?: string): Promise<T>;
|
get<T = any>(url: string, params?: any, requestId?: string, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||||
delete<T = any>(url: string, data?: any): Promise<T>;
|
delete<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||||
post<T = any>(url: string, data?: any): Promise<T>;
|
post<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||||
patch<T = any>(url: string, data?: any): Promise<T>;
|
patch<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||||
put<T = any>(url: string, data?: any): Promise<T>;
|
put<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use the fetch function instead. If you prefer to work with a 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.
|
* 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<T = any>(options: BackendSrvRequest): Promise<T>;
|
request<T = any>(options: BackendSrvRequest): Promise<T>;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { merge, Observable, of } from 'rxjs';
|
import { lastValueFrom, merge, Observable, of } from 'rxjs';
|
||||||
import { catchError, switchMap } from 'rxjs/operators';
|
import { catchError, switchMap } from 'rxjs/operators';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -24,6 +24,8 @@ import {
|
|||||||
getGrafanaLiveSrv,
|
getGrafanaLiveSrv,
|
||||||
StreamingFrameOptions,
|
StreamingFrameOptions,
|
||||||
StreamingFrameAction,
|
StreamingFrameAction,
|
||||||
|
BackendSrvRequest,
|
||||||
|
FetchResponse,
|
||||||
} from '../services';
|
} from '../services';
|
||||||
|
|
||||||
import { BackendDataSourceResponse, toDataQueryResponse } from './queryResponse';
|
import { BackendDataSourceResponse, toDataQueryResponse } from './queryResponse';
|
||||||
@ -219,29 +221,38 @@ class DataSourceWithBackend<
|
|||||||
/**
|
/**
|
||||||
* Make a GET request to the datasource resource path
|
* Make a GET request to the datasource resource path
|
||||||
*/
|
*/
|
||||||
async getResource(path: string, params?: any): Promise<any> {
|
async getResource(
|
||||||
return getBackendSrv().get(`/api/datasources/${this.id}/resources/${path}`, params);
|
path: string,
|
||||||
|
params?: BackendSrvRequest['params'],
|
||||||
|
options?: Partial<BackendSrvRequest>
|
||||||
|
): Promise<any> {
|
||||||
|
return getBackendSrv().get(`/api/datasources/${this.id}/resources/${path}`, params, options?.requestId, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a POST request to the datasource resource path
|
* Send a POST request to the datasource resource path
|
||||||
*/
|
*/
|
||||||
async postResource(path: string, body?: any): Promise<any> {
|
async postResource(
|
||||||
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body });
|
path: string,
|
||||||
|
data?: BackendSrvRequest['data'],
|
||||||
|
options?: Partial<BackendSrvRequest>
|
||||||
|
): Promise<any> {
|
||||||
|
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...data }, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the datasource healthcheck
|
* Run the datasource healthcheck
|
||||||
*/
|
*/
|
||||||
async callHealthCheck(): Promise<HealthCheckResult> {
|
async callHealthCheck(): Promise<HealthCheckResult> {
|
||||||
return getBackendSrv()
|
return lastValueFrom(
|
||||||
.request({ method: 'GET', url: `/api/datasources/${this.id}/health`, showErrorAlert: false })
|
getBackendSrv().fetch<HealthCheckResult>({
|
||||||
.then((v) => {
|
method: 'GET',
|
||||||
return v as HealthCheckResult;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -407,24 +407,29 @@ export class BackendSrv implements BackendService {
|
|||||||
return this.inspectorStream;
|
return this.inspectorStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get<T = any>(url: string, params?: any, requestId?: string): Promise<T> {
|
async get<T = any>(
|
||||||
return await this.request({ method: 'GET', url, params, requestId });
|
url: string,
|
||||||
|
params?: BackendSrvRequest['params'],
|
||||||
|
requestId?: BackendSrvRequest['requestId'],
|
||||||
|
options?: Partial<BackendSrvRequest>
|
||||||
|
) {
|
||||||
|
return this.request<T>({ ...options, method: 'GET', url, params, requestId });
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete<T = any>(url: string, data?: any): Promise<T> {
|
async delete<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>) {
|
||||||
return await this.request({ method: 'DELETE', url, data });
|
return this.request<T>({ ...options, method: 'DELETE', url, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async post<T = any>(url: string, data?: any): Promise<T> {
|
async post<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>) {
|
||||||
return await this.request({ method: 'POST', url, data });
|
return this.request<T>({ ...options, method: 'POST', url, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async patch<T = any>(url: string, data: any): Promise<T> {
|
async patch<T = any>(url: string, data: any, options?: Partial<BackendSrvRequest>) {
|
||||||
return await this.request({ method: 'PATCH', url, data });
|
return this.request<T>({ ...options, method: 'PATCH', url, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
async put<T = any>(url: string, data: any): Promise<T> {
|
async put<T = any>(url: string, data: any, options?: Partial<BackendSrvRequest>): Promise<T> {
|
||||||
return await this.request({ method: 'PUT', url, data });
|
return this.request<T>({ ...options, method: 'PUT', url, data });
|
||||||
}
|
}
|
||||||
|
|
||||||
withNoBackendCache(callback: any) {
|
withNoBackendCache(callback: any) {
|
||||||
|
@ -10,6 +10,7 @@ import { LibraryElementExport } from '../../dashboard/components/DashExportModal
|
|||||||
import { getLibraryPanel } from '../../library-panels/state/api';
|
import { getLibraryPanel } from '../../library-panels/state/api';
|
||||||
import { LibraryElementDTO, LibraryElementKind } from '../../library-panels/types';
|
import { LibraryElementDTO, LibraryElementKind } from '../../library-panels/types';
|
||||||
import { DashboardSearchHit } from '../../search/types';
|
import { DashboardSearchHit } from '../../search/types';
|
||||||
|
import { DeleteDashboardResponse } from '../types';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
clearDashboard,
|
clearDashboard,
|
||||||
@ -275,11 +276,7 @@ export function saveDashboard(options: SaveDashboardCommand) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function deleteFolder(uid: string, showSuccessAlert: boolean) {
|
function deleteFolder(uid: string, showSuccessAlert: boolean) {
|
||||||
return getBackendSrv().request({
|
return getBackendSrv().delete(`/api/folders/${uid}?forceDeleteRules=false`, undefined, { showSuccessAlert });
|
||||||
method: 'DELETE',
|
|
||||||
url: `/api/folders/${uid}?forceDeleteRules=false`,
|
|
||||||
showSuccessAlert: showSuccessAlert,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createFolder(payload: any) {
|
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) {
|
export function deleteDashboard(uid: string, showSuccessAlert: boolean) {
|
||||||
return getBackendSrv().request({
|
return getBackendSrv().delete<DeleteDashboardResponse>(`/api/dashboards/uid/${uid}`, { showSuccessAlert });
|
||||||
method: 'DELETE',
|
|
||||||
url: `/api/dashboards/uid/${uid}`,
|
|
||||||
showSuccessAlert: showSuccessAlert,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeInOrder(tasks: any[]) {
|
function executeInOrder(tasks: any[]) {
|
||||||
|
@ -11,3 +11,9 @@ export interface Snapshot {
|
|||||||
url?: string;
|
url?: string;
|
||||||
userId: number;
|
userId: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type DeleteDashboardResponse = {
|
||||||
|
id: number;
|
||||||
|
message: string;
|
||||||
|
title: string;
|
||||||
|
};
|
||||||
|
@ -71,13 +71,7 @@ export class GettingStarted extends PureComponent<PanelProps, State> {
|
|||||||
|
|
||||||
dashboard?.removePanel(panel!);
|
dashboard?.removePanel(panel!);
|
||||||
|
|
||||||
backendSrv
|
backendSrv.put('/api/user/helpflags/1', undefined, { showSuccessAlert: false }).then((res: any) => {
|
||||||
.request({
|
|
||||||
method: 'PUT',
|
|
||||||
url: '/api/user/helpflags/1',
|
|
||||||
showSuccessAlert: false,
|
|
||||||
})
|
|
||||||
.then((res: any) => {
|
|
||||||
contextSrv.user.helpFlags1 = res.helpFlags1;
|
contextSrv.user.helpFlags1 = res.helpFlags1;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user