-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
http_service.ts
71 lines (62 loc) · 2.55 KB
/
http_service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { HttpSetup, HttpStart } from './types';
import { InjectedMetadataSetup } from '../injected_metadata';
import { FatalErrorsSetup } from '../fatal_errors';
import { BasePath } from './base_path';
import { AnonymousPathsService } from './anonymous_paths_service';
import { LoadingCountService } from './loading_count_service';
import { Fetch } from './fetch';
import { CoreService } from '../../types';
import { ExternalUrlService } from './external_url_service';
interface HttpDeps {
injectedMetadata: InjectedMetadataSetup;
fatalErrors: FatalErrorsSetup;
}
/** @internal */
export class HttpService implements CoreService<HttpSetup, HttpStart> {
private readonly anonymousPaths = new AnonymousPathsService();
private readonly loadingCount = new LoadingCountService();
private service?: HttpSetup;
public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup {
const kibanaVersion = injectedMetadata.getKibanaVersion();
const basePath = new BasePath(
injectedMetadata.getBasePath(),
injectedMetadata.getServerBasePath(),
injectedMetadata.getPublicBaseUrl()
);
const fetchService = new Fetch({ basePath, kibanaVersion });
const loadingCount = this.loadingCount.setup({ fatalErrors });
loadingCount.addLoadingCountSource(fetchService.getRequestCount$());
this.service = {
basePath,
anonymousPaths: this.anonymousPaths.setup({ basePath }),
externalUrl: new ExternalUrlService().setup({ injectedMetadata, location: window.location }),
intercept: fetchService.intercept.bind(fetchService),
fetch: fetchService.fetch.bind(fetchService),
delete: fetchService.delete.bind(fetchService),
get: fetchService.get.bind(fetchService),
head: fetchService.head.bind(fetchService),
options: fetchService.options.bind(fetchService),
patch: fetchService.patch.bind(fetchService),
post: fetchService.post.bind(fetchService),
put: fetchService.put.bind(fetchService),
...loadingCount,
};
return this.service;
}
public start() {
if (!this.service) {
throw new Error(`HttpService#setup() must be called first!`);
}
return this.service;
}
public stop() {
this.loadingCount.stop();
}
}