So you’re using the CLI to build your next Aurelia application and you want to use the Fetch client? You might have noticed by default it does not come configured with the Fetch client. Fortunately, adding it in is a piece of cake.
Firstly, we want to install the Fetch Client as well as a Fetch polyfill:
npm install aurelia-fetch-client whatwg-fetch --save
Now open up aurelia_project/aurelia.json
At the bottom in the bundles section, more specifically vendor-bundle.js
look for the prepend section and add in the polyfill as this will be global and we want to include it before anything else. You might notice I put it above the RequireJS line.
"prepend": [
"node_modules/bluebird/js/browser/bluebird.core.js",
"node_modules/whatwg-fetch/fetch.js",
"scripts/require.js"
],
Now e need to add into the dependencies
section in the vendor bundle the reference to the aurelia-fetch-client
dependency. I like things being in alphabetical order, so I put it above aurelia-framework
.
"aurelia-fetch-client",
Now you can use the Fetch client in your apps import {HttpClient} from 'aurelia-fetch-client';
The URLSearchParams fix doesn’t work for me. Everything else does. Can I put this trivial interface declaration in a local file? If so, where should it go and how do I reference it in the typings.json file?
I ended up creating src/sundry-fixups and putting sundry fixups in it. Everything in the src folder is in scope so it’s not even necessary to declare dependency.
Thanks for the post. Everything worked for me as well except for the URLSearchParams fix.
I fixed it by adding
///
to the typings/index.d.ts file.
Not sure why the last comment didn’t work, possibly because it saw an html tag?
I added another reference to the typings/index.d.ts file where the path was ../node_modules/aurelia-fetch-client/doc/url.d.ts
Worked for me, I did have the same problem with URLSearchParams but by running typings install fixed the problem
Has something been updated? The typings install no longer works, cannot find it. I removed the dt~ and it did install something, but there’s a ton of errors.
The definition file got updated within the last few hours, and has a bunch of problems. I just copy and pasted an older version from July and it’s working. Not sure how to typings install the older version, no idea what version number to use.
I’m going to follow this post and WillT, I would love it if you see that the fix comes back on line
I assume typings is if you are using TypeScript right?
I’m using Babel and I can’t get any of this to work. `au run –watch` fails because it can’t fine `fetch.js`
I tried a lot of things, but I believe this was ultimately the solution:
REINSTALL AURELIA-CLI
`npm install -g npm`
`npm install -g aurelia-cli`
`npm install -g typescript`
`npm install -g typings`
`typings install`
UNINSTALL/REINSTALL aurelia-fetch client
`npm uninstall aurelia-fetch-client –save`
`npm install aurelia-fetch-client –save`
THEN INSTALL THE TYPINGS
`typings install dt~whatwg-fetch –global –save`
THEN YOU WILL WANT THE ReadableStream
`typings install dt~whatwg-streams –global –save`
That cleared all the aurelia-cli errors and does not rely on this (I was able to remove this completely) :
`”url”: “github:aurelia/fetch-client/doc/url.d.ts#bbe0777ef710d889a05759a65fa2c9c3865fc618″`
I installed again and ran into the URLSearchParams issue but found a great solution at: https://github.com/aurelia/fetch-client/issues/71
Contributor RomkeVdMeulen created a typings library add in for it. Use :
“`typings install github:RomkeVdMeulen/URLSearchParams –global –save“`
Worked great for me