Is it possible to consume external REST api in components? With the new breaking changes, I couldn't find an updated answer.
Appreciate any help.
Sure!
You can do a native fetch
for any CRUD
operations inside components.
Something similar like this:
let data;
const getData = async () => {
const response = await fetch('https://your.endpoint/api/foo');
if (response.headers.get('content-type')?.includes('application/json')) {
const json = await response.json();
return { response, json };
} else {
return { response };
}
};
Then you can initialize whenever you want. E.g:
onMount(async ()=> {
await getData();
})