-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose an HTTP-request browser client #35486
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3517293
Expose an HTTP-request browser client
eliperelman 941db30
Fix failing tests from kfetch refactor
eliperelman ab4aa06
Make abort() non-enumerable, fix review issues
eliperelman 233ae67
Move kfetch test setup to build-excluded location
eliperelman d9ba9ee
Add ndjson tests to browser http service
eliperelman 7bd98c9
Lint fixes
eliperelman 568b6d2
Fix missing update of del to delete in http mock
eliperelman 4dd708e
Fix problems with merging headers with undefined Content-Type
eliperelman 1626c45
Delete correct property from updated options
eliperelman a6b30b8
Linting fix
eliperelman 42cc816
Fix reference to kfetch_test_setup due to moving test file
eliperelman e9ae8ed
Add tests and fix implementation of abortables
eliperelman dc98cd9
Add missing http start mock contract, fix test in CI
eliperelman a10236f
Remove abortable promise functionality
eliperelman e3db5be
Fix DELETE method handler, remove unnecessary promise wrapper
eliperelman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"attributes":{"description":"","kibanaSavedObjectMeta":{"searchSourceJSON":"{\"filter\":[],\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"},"title":"Log Agents","uiStateJSON":"{}","visState":"{\"title\":\"Log Agents\",\"type\":\"area\",\"params\":{\"type\":\"area\",\"grid\":{\"categoryLines\":false,\"style\":{\"color\":\"#eee\"}},\"categoryAxes\":[{\"id\":\"CategoryAxis-1\",\"type\":\"category\",\"position\":\"bottom\",\"show\":true,\"style\":{},\"scale\":{\"type\":\"linear\"},\"labels\":{\"show\":true,\"truncate\":100},\"title\":{\"text\":\"agent.raw: Descending\"}}],\"valueAxes\":[{\"id\":\"ValueAxis-1\",\"name\":\"LeftAxis-1\",\"type\":\"value\",\"position\":\"left\",\"show\":true,\"style\":{},\"scale\":{\"type\":\"linear\",\"mode\":\"normal\"},\"labels\":{\"show\":true,\"rotate\":0,\"filter\":false,\"truncate\":100},\"title\":{\"text\":\"Count\"}}],\"seriesParams\":[{\"show\":\"true\",\"type\":\"area\",\"mode\":\"stacked\",\"data\":{\"label\":\"Count\",\"id\":\"1\"},\"drawLinesBetweenPoints\":true,\"showCircles\":true,\"interpolate\":\"linear\",\"valueAxis\":\"ValueAxis-1\"}],\"addTooltip\":true,\"addLegend\":true,\"legendPosition\":\"right\",\"times\":[],\"addTimeMarker\":false},\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"schema\":\"metric\",\"params\":{}},{\"id\":\"2\",\"enabled\":true,\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"agent.raw\",\"size\":5,\"order\":\"desc\",\"orderBy\":\"1\"}}]}"},"id":"082f1d60-a2e7-11e7-bb30-233be9be6a15","migrationVersion":{"visualization":"7.0.0"},"references":[{"id":"f1e4c910-a2e6-11e7-bb30-233be9be6a15","name":"kibanaSavedObjectMeta.searchSourceJSON.index","type":"index-pattern"}],"type":"visualization","version":1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { merge } from 'lodash'; | ||
import { format } from 'url'; | ||
|
||
import { HttpFetchOptions, HttpBody, Deps } from './types'; | ||
import { HttpFetchError } from './http_fetch_error'; | ||
|
||
const JSON_CONTENT = /^(application\/(json|x-javascript)|text\/(x-)?javascript|x-json)(;.*)?$/; | ||
const NDJSON_CONTENT = /^(application\/ndjson)(;.*)?$/; | ||
|
||
export const setup = ({ basePath, injectedMetadata }: Deps) => { | ||
async function fetch(path: string, options: HttpFetchOptions = {}): Promise<HttpBody> { | ||
const { query, prependBasePath, ...fetchOptions } = merge( | ||
{ | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
prependBasePath: true, | ||
headers: { | ||
'kbn-version': injectedMetadata.getKibanaVersion(), | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
options | ||
); | ||
const url = format({ | ||
pathname: prependBasePath ? basePath.addToPath(path) : path, | ||
query, | ||
}); | ||
|
||
if ( | ||
options.headers && | ||
'Content-Type' in options.headers && | ||
options.headers['Content-Type'] === undefined | ||
) { | ||
delete fetchOptions.headers['Content-Type']; | ||
} | ||
|
||
let response; | ||
let body = null; | ||
|
||
try { | ||
response = await window.fetch(url, fetchOptions as RequestInit); | ||
} catch (err) { | ||
throw new HttpFetchError(err.message); | ||
} | ||
|
||
const contentType = response.headers.get('Content-Type') || ''; | ||
|
||
try { | ||
if (NDJSON_CONTENT.test(contentType)) { | ||
body = await response.blob(); | ||
} else if (JSON_CONTENT.test(contentType)) { | ||
body = await response.json(); | ||
} else { | ||
body = await response.text(); | ||
} | ||
} catch (err) { | ||
throw new HttpFetchError(err.message, response, body); | ||
} | ||
|
||
if (!response.ok) { | ||
throw new HttpFetchError(response.statusText, response, body); | ||
} | ||
|
||
return body; | ||
} | ||
|
||
function shorthand(method: string) { | ||
return (path: string, options: HttpFetchOptions = {}) => fetch(path, { ...options, method }); | ||
} | ||
|
||
return { fetch, shorthand }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't use it because when you call
.mockReturnValue(createSetupContractMock()),
typescript doesn't infer type toHttpService.setup
yet.you can pass
.mockReturnValue({}),
and it won't complain.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on what you mean by this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's why I prefer next form:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently inconsistent with the way the rest of the client-side mocks work, so I'm going to leave the current form. We can re-evaluate this again in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, now I know why you did it this way, @restrry. I think having the type-safety is preferable and we should probably change the ones I had changed back.