-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[kfetch] TypeScript-ify #20914
[kfetch] TypeScript-ify #20914
Conversation
💚 Build Succeeded |
src/ui/public/chrome/index.d.ts
Outdated
* under the License. | ||
*/ | ||
|
||
import Angular from 'angular'; |
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 don't see Angular used here?
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.
Yep, had another method typed but removed it because it was irrelevant
describe('kfetch', () => { | ||
const matcherName = /my\/path/; | ||
const matcherName: any = /my\/path/; |
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.
Can you use the RegExp
type instead of any
?
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.
.. or actually, i'd just ditch the type. Typescript will infer it since it is static
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.
The reason any
is necessary is that the types for fetch-mock
insist that the matcherName must be a string. By casting the variable to any
we bypass the checks, and since the tests won't pass if the matcher isn't working correctly I feel okay about using any
here.
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.
👍
src/ui/public/kfetch/kfetch.ts
Outdated
// attempt to read the body of the response | ||
return reject(new FetchError(res, await res.json())); | ||
} catch (err) { | ||
// ignore error if we are not be able to get body for response |
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.
Perhaps update/remove this comment, since we no longer ignore the error.
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.
Well, we still ignore the err
variable
@@ -29,7 +31,10 @@ function createAbortable() { | |||
}; | |||
} | |||
|
|||
export function kfetchAbortable(fetchOptions, kibanaOptions) { | |||
export function kfetchAbortable( | |||
fetchOptions?: Omit<KFetchOptions, 'signal'>, |
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'm probably reading this wrong but I don't understand the need for excluding signal
. It's not part of KFetchOptions
.
I'd expect fetchOptions: KFetchOptions
to be fine - but maybe not?
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.
KFetchOptions
extends the default options for the global fetch API, which includes signal
, which is why I've explicitly removed it from the type here (since we overwrite it)
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 see that the RequestInit
interface has signal
of type AbortSignal
. The signal
we are overwriting kibanaOptions
with is also of type AbortSignal
- so should be compatible.
I pulled your branch and removed the Omit
part, and it still compiles. Haven't worked with TS for a while so I might be missing something.
Do you get a compile error if you remove the Omit part?
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.
Nope, the type described in the arguments should be the type that callers must match, it doesn't describe the type of the object we'll be passing to kfetch()
. I'm just trying to remove the signal
option so when a caller is looking at the options through intelisense or something they will see signal
as an option for kfetch()
:
but not for kfetchAbortable()
:
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.
Got it :) Thanks for the explanation.
Unrelated to this: I noticed we have three fetch polyfills in OSS and x-pack: node-fetch, whatwg-fetch and isomorphic-fetch. EDIT: at a closer look isomorphic-fetch just uses the two others under the hood, and the versions are fairly outdated: https://github.com/matthew-andrews/isomorphic-fetch/blob/master/package.json#L22-L23 So isomorphic-fetch should probably go - but that's for another PR. |
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.
A few nits and questions but overall looks really good. Awesome with types!
src/ui/public/kfetch/kfetch.ts
Outdated
prependBasePath?: boolean; | ||
} | ||
|
||
export function kfetch(fetchOptions?: KFetchOptions, kibanaOptions?: KFetchKibanaOptions) { |
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.
fetchOptions
should probably be required. Are there any cases where it makes sense to call kfetch()
(without args)?
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.
Only if you want to do a get request to "/", I'm not sure why I made it optional... I guess because it would work fine if it wasn't specified. I'll update it anyway.
💚 Build Succeeded |
💚 Build Succeeded |
In order to make the awesome new kfetch api easier to consume in purely TypeScript projects, and since it's a pretty small module with very few dependencies, I converted it to TypeScript. Along with kfetch I also started a type definition file for `ui/chrome` that we can extend as we go, but will likely be unnecessary after elastic#19992
In order to make the awesome new kfetch api easier to consume in purely TypeScript projects, and since it's a pretty small module with very few dependencies, I converted it to TypeScript. Along with kfetch I also started a type definition file for `ui/chrome` that we can extend as we go, but will likely be unnecessary after #19992
6.x/6.4: 801e5c0 |
As @sqren mentioned in elastic#20914 (comment) we are using three different fetch polyfills, and it's not totally clear which one is winning out. For some time `import 'whatwg-fetch'` has been included globally in the browser for the last 6 months or so, but wasn't compatible with jest tests running in node.js, which seems to be the reason people have been reaching for `isomorphic-fetch`. Rather than relying on the version of `whatwg-fetch` and `node-fetch` that `isomorphic-fetch` depends on, we can just rely on them directly and stub out the global fetch for jest tests in a jest setup module.
In order to make the awesome new kfetch api easier to consume in purely TypeScript projects, and since it's a pretty small module with very few dependencies, I converted it to TypeScript.
Along with kfetch I also started a type definition file for
ui/chrome
that we can extend as we go, but will likely be unnecessary after #19992