I believe tree-shaking is actually a Webpack feature, and only some libraries will support it. axios
is probably not one of them, but axios
is well-tested.
Still, installing node-fetch
(or using newest Node) isn’t really a bad thing, other than, everything will passthrough fetch
even if it is status 404, 429, or 500. Testing is OK with fetch, with some laziness - await fetch(url, { headers }).then(r => r.json())
. It’s possible to test for r.ok
or write a fetch
wrapper, but actually it just makes the code clunkier. (Also opinionated and reflect your coding skills.)
BTW, Deno also supports fetch
, although, just like ESM, many features I used to are lost, like __dirname
.
There are places I need Node 12, for some abandoned projects, like Gitbook - the current Gitbook became a paid web service instead.
As you are getting technical (into the coding mess), anyway; you should learn a proper markdown syntax, in particular, ```.
So a proper answer (albeit not testing for r.ok
) would be,
fetch('https://api.wanikani.com/v2' + apiEndpointPath, {
headers: {
Authorization: 'Bearer ' + apiToken
}
})
.then(response => response.json())
.then(responseBody => console.log(responseBody))
I omitted Content-Type
of application/json
, but it doesn’t appear to matter.
Note that ```js is broken in Discourse (WaniKani Community)
fetch('https://api.wanikani.com/v2' + apiEndpointPath, {
headers: {
Authorization: 'Bearer ' + apiToken
}
})
.then(response => response.json())
.then(responseBody => console.log(responseBody))
Syntax highlighting is now lost.
Though, it (defining the language) is needed for syntax highlighting in Discord, and Github, if you get to use them. (Also, of course, it is a standard markdown syntax.)
About your errors, global objects in Node and web browser, are vastly different, like Headers
is not included (if not having Fetch API, I guess).