-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Add ability to abort a kfetch call. #20700
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e3f2dc
Add ability to abort a kfetch call.
cjcenizal d293f87
Merge branch 'master' of github.com:elastic/kibana into kfetch-abort
cjcenizal d01c26f
Add abortcontroller-polyfill.
cjcenizal 94bf026
Split kfetch module into kfetch and kfetchAbortable sub-modules.
cjcenizal 14228d8
Fix bug.
cjcenizal 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
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,77 @@ | ||
/* | ||
* 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 'isomorphic-fetch'; | ||
import url from 'url'; | ||
import chrome from '../chrome'; | ||
import { metadata } from '../metadata'; | ||
import { merge } from 'lodash'; | ||
|
||
class FetchError extends Error { | ||
constructor(res, body) { | ||
super(res.statusText); | ||
this.res = res; | ||
this.body = body; | ||
Error.captureStackTrace(this, FetchError); | ||
} | ||
} | ||
|
||
export function kfetch(fetchOptions, kibanaOptions) { | ||
// fetch specific options with defaults | ||
const { pathname, query, ...combinedFetchOptions } = merge( | ||
{ | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'kbn-version': metadata.version, | ||
}, | ||
}, | ||
fetchOptions, | ||
); | ||
|
||
// kibana specific options with defaults | ||
const combinedKibanaOptions = { | ||
prependBasePath: true, | ||
...kibanaOptions, | ||
}; | ||
|
||
const fullUrl = url.format({ | ||
pathname: combinedKibanaOptions.prependBasePath ? chrome.addBasePath(pathname) : pathname, | ||
query, | ||
}); | ||
|
||
const fetching = new Promise(async (resolve, reject) => { | ||
const res = await fetch(fullUrl, combinedFetchOptions); | ||
|
||
if (!res.ok) { | ||
let body; | ||
try { | ||
body = await res.json(); | ||
} catch (err) { | ||
// ignore error, may not be able to get body for response that is not ok | ||
} | ||
reject(new FetchError(res, body)); | ||
} | ||
|
||
resolve(res.json()); | ||
}); | ||
|
||
return fetching; | ||
} |
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,40 @@ | ||
/* | ||
* 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 { kfetch } from './kfetch'; | ||
|
||
function createAbortable() { | ||
const abortController = new AbortController(); | ||
const { signal, abort } = abortController; | ||
|
||
return { | ||
signal, | ||
abort: abort.bind(abortController), | ||
}; | ||
} | ||
|
||
export function kfetchAbortable(fetchOptions, kibanaOptions) { | ||
const { signal, abort } = createAbortable(); | ||
const fetching = kfetch({ ...fetchOptions, signal }, kibanaOptions); | ||
|
||
return { | ||
fetching, | ||
abort, | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very clean. I like it! |
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,39 @@ | ||
/* | ||
* 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 { kfetchAbortable } from './kfetch_abortable'; | ||
|
||
jest.mock('../chrome', () => ({ | ||
addBasePath: path => `myBase/${path}`, | ||
})); | ||
|
||
jest.mock('../metadata', () => ({ | ||
metadata: { | ||
version: 'my-version', | ||
}, | ||
})); | ||
|
||
describe('kfetchAbortable', () => { | ||
it('should return an object with a fetching promise and an abort callback', () => { | ||
const { fetching, abort } = kfetchAbortable({ pathname: 'my/path' }); | ||
expect(typeof fetching.then).toBe('function'); | ||
expect(typeof fetching.catch).toBe('function'); | ||
expect(typeof abort).toBe('function'); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -549,6 +549,10 @@ [email protected]: | |
version "1.0.9" | ||
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135" | ||
|
||
abortcontroller-polyfill@^1.1.9: | ||
version "1.1.9" | ||
resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.1.9.tgz#9fefe359fda2e9e0932dc85e6106453ac393b2da" | ||
|
||
accept-language-parser@^1.5.0: | ||
version "1.5.0" | ||
resolved "https://registry.yarnpkg.com/accept-language-parser/-/accept-language-parser-1.5.0.tgz#8877c54040a8dcb59e0a07d9c1fde42298334791" | ||
|
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.
We don't need the Promise constructor anymore, do we? Eg. just revert to what we had (unless you prefer this style ofc :) )
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.
If you don't mind, I would like to keep it in this style because it makes it just a bit more explicit that a promise is being returned (at least to me!)