UNPKG

@grafana/runtime

Version:
1 lines 6.87 kB
{"version":3,"file":"backendSrv.mjs","sources":["../../../src/services/backendSrv.ts"],"sourcesContent":["import { Observable } from 'rxjs';\n\n/**\n * Used to initiate a remote call via the {@link BackendSrv}\n *\n * @public\n */\nexport type BackendSrvRequest = {\n /**\n * Request URL\n */\n url: string;\n\n /**\n * Number of times to retry the remote call if it fails.\n */\n retry?: number;\n\n /**\n * HTTP headers that should be passed along with the remote call.\n * Please have a look at {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | Fetch API}\n * for supported headers.\n */\n headers?: Record<string, any>;\n\n /**\n * HTTP verb to perform in the remote call GET, POST, PUT etc.\n */\n method?: string;\n\n /**\n * Set to false an success application alert box will not be shown for successful PUT, DELETE, POST requests\n */\n showSuccessAlert?: boolean;\n\n /**\n * Set to false to not show an application alert box for request errors\n */\n showErrorAlert?: boolean;\n\n /**\n * Provided by the initiator to identify a particular remote call. An example\n * of this is when a datasource plugin triggers a query. If the request id already\n * exist the backendSrv will try to cancel and replace the previous call with the\n * new one.\n */\n requestId?: string;\n\n /**\n * Set to to true to not include call in query inspector\n */\n hideFromInspector?: boolean;\n\n /**\n * The data to send\n */\n data?: any;\n\n /**\n * Query params\n */\n params?: Record<string, any>;\n\n /**\n * Define how the response object should be parsed. See:\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data\n *\n * By default values are json parsed from text\n */\n responseType?: 'json' | 'text' | 'arraybuffer' | 'blob';\n\n /**\n * Used to cancel an open connection\n * https://developer.mozilla.org/en-US/docs/Web/API/AbortController\n */\n abortSignal?: AbortSignal;\n\n /**\n * The credentials read-only property of the Request interface indicates whether the user agent should send cookies from the other domain in the case of cross-origin requests.\n */\n credentials?: RequestCredentials;\n\n /**\n * @deprecated withCredentials is deprecated in favor of credentials\n */\n withCredentials?: boolean;\n};\n\n/**\n * Response for fetch function in {@link BackendSrv}\n *\n * @public\n */\nexport interface FetchResponse<T = any> {\n data: T;\n readonly status: number;\n readonly statusText: string;\n readonly ok: boolean;\n readonly headers: Headers;\n readonly redirected: boolean;\n readonly type: ResponseType;\n readonly url: string;\n readonly config: BackendSrvRequest;\n readonly traceId?: string;\n}\n\n/**\n * Error type for fetch function in {@link BackendSrv}\n *\n * @public\n */\nexport interface FetchErrorDataProps {\n message?: string;\n status?: string;\n error?: string | any;\n}\n\n/**\n * Error type for fetch function in {@link BackendSrv}\n *\n * @public\n */\nexport interface FetchError<T = any> {\n status: number;\n statusText?: string;\n data: T;\n message?: string;\n cancelled?: boolean;\n isHandled?: boolean;\n config: BackendSrvRequest;\n traceId?: string;\n}\n\nexport function isFetchError<T = any>(e: unknown): e is FetchError<T> {\n return typeof e === 'object' && e !== null && 'status' in e && 'data' in e;\n}\n\n/**\n * Used to communicate via http(s) to a remote backend such as the Grafana backend,\n * a datasource etc. The BackendSrv is using the {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | Fetch API}\n * under the hood to handle all the communication.\n *\n * The request function can be used to perform a remote call by specifying a {@link BackendSrvRequest}.\n * To make the BackendSrv a bit easier to use we have added a couple of shorthand functions that will\n * use default values executing the request.\n *\n * @remarks\n * By default, Grafana displays an error message alert if the remote call fails. To prevent this from\n * happening `showErrorAlert = true` on the options object.\n *\n * @public\n */\nexport interface BackendSrv {\n get<T = any>(url: string, params?: any, requestId?: string, options?: Partial<BackendSrvRequest>): Promise<T>;\n delete<T = unknown>(url: string, data?: unknown, options?: Partial<BackendSrvRequest>): Promise<T>;\n post<T = any>(url: string, data?: unknown, options?: Partial<BackendSrvRequest>): Promise<T>;\n patch<T = any>(url: string, data?: unknown, options?: Partial<BackendSrvRequest>): Promise<T>;\n put<T = any>(url: string, data?: unknown, options?: Partial<BackendSrvRequest>): Promise<T>;\n\n /**\n * @deprecated Use the `.fetch()` function instead. If you prefer to work with a promise\n * wrap the Observable returned by fetch with the lastValueFrom function, or use the get|delete|post|patch|put methods.\n * This method is going to be private from Grafana 10.\n */\n request<T = unknown>(options: BackendSrvRequest): Promise<T>;\n\n /**\n * Special function used to communicate with datasources that will emit core\n * events that the Grafana QueryInspector and QueryEditor is listening for to be able\n * to display datasource query information. Can be skipped by adding `option.silent`\n * when initializing the request.\n *\n * @deprecated Use the fetch function instead\n */\n datasourceRequest<T = unknown>(options: BackendSrvRequest): Promise<FetchResponse<T>>;\n\n /**\n * Observable http request interface\n */\n fetch<T>(options: BackendSrvRequest): Observable<FetchResponse<T>>;\n\n /**\n * Observe each raw chunk in the response. This is useful when reading values from\n * a long living HTTP connection like the kubernetes WATCH command.\n *\n * Each chunk includes the full response headers and the `data` property is filled with the chunk.\n */\n chunked(options: BackendSrvRequest): Observable<FetchResponse<Uint8Array | undefined>>;\n}\n\nlet singletonInstance: BackendSrv;\n\n/**\n * Used during startup by Grafana to set the BackendSrv so it is available\n * via the {@link getBackendSrv} to the rest of the application.\n *\n * @internal\n */\nexport const setBackendSrv = (instance: BackendSrv) => {\n singletonInstance = instance;\n};\n\n/**\n * Used to retrieve the {@link BackendSrv} that can be used to communicate\n * via http(s) to a remote backend such as the Grafana backend, a datasource etc.\n *\n * @public\n */\nexport const getBackendSrv = (): BackendSrv => singletonInstance;\n"],"names":[],"mappings":"AAsIO,SAAS,aAAsB,CAAgC,EAAA;AACpE,EAAA,OAAO,OAAO,CAAM,KAAA,QAAA,IAAY,MAAM,IAAQ,IAAA,QAAA,IAAY,KAAK,MAAU,IAAA,CAAA;AAC3E;AAuDA,IAAI,iBAAA;AAQS,MAAA,aAAA,GAAgB,CAAC,QAAyB,KAAA;AACrD,EAAoB,iBAAA,GAAA,QAAA;AACtB;AAQO,MAAM,gBAAgB,MAAkB;;;;"}