mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -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:
@@ -143,15 +143,16 @@ export function isFetchError(e: unknown): e is FetchError {
|
||||
* @public
|
||||
*/
|
||||
export interface BackendSrv {
|
||||
get<T = any>(url: string, params?: any, requestId?: string): Promise<T>;
|
||||
delete<T = any>(url: string, data?: any): Promise<T>;
|
||||
post<T = any>(url: string, data?: any): Promise<T>;
|
||||
patch<T = any>(url: string, data?: any): Promise<T>;
|
||||
put<T = any>(url: string, data?: any): Promise<T>;
|
||||
get<T = any>(url: string, params?: any, requestId?: string, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||
delete<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||
post<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): Promise<T>;
|
||||
patch<T = any>(url: string, data?: any, options?: Partial<BackendSrvRequest>): 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
|
||||
* 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<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 {
|
||||
@@ -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<any> {
|
||||
return getBackendSrv().get(`/api/datasources/${this.id}/resources/${path}`, params);
|
||||
async getResource(
|
||||
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
|
||||
*/
|
||||
async postResource(path: string, body?: any): Promise<any> {
|
||||
return getBackendSrv().post(`/api/datasources/${this.id}/resources/${path}`, { ...body });
|
||||
async postResource(
|
||||
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
|
||||
*/
|
||||
async callHealthCheck(): Promise<HealthCheckResult> {
|
||||
return getBackendSrv()
|
||||
.request({ method: 'GET', url: `/api/datasources/${this.id}/health`, showErrorAlert: false })
|
||||
.then((v) => {
|
||||
return v as HealthCheckResult;
|
||||
return lastValueFrom(
|
||||
getBackendSrv().fetch<HealthCheckResult>({
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user