I'm trying to extract all names from an API that returns a JSON with the following structure:
{
"data" : [
{
"attributes" : {
"name" : "Prog1"
...
},
"id" : "12345",
},
{
"attributes" : {
"name" : "Prog2"
...
},
"id" : "67890",
},
...
],
"links" : {
"next" : "https://api.company.com/v1/programs?page%5Bnumber%5D=3",
"self" : "https://api.company.com/v1/programs?page%5Bnumber%5D=2",
"prev" : "https://api.company.com/v1/programs?page%5Bnumber%5D=1",
}
}
I want to keep sending requests, and logging the names as long as there is a "next" link, after which I'll stop sending requests.
Here's the function I have so far:
async function getPrograms(url: string, user: string, pass: string) {
try {
const response = await fetch(url, {
method: 'GET',
headers: {Authorization: 'Basic ' + Buffer.from(`${user}:${pass}`).toString('base64')},
});
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
console.log(response);
const result = (await response.json()) as GetProgramsResponse;
result.data.forEach((program) => {
// Later will add other stuff here
console.log(program.attributes.name);
})
console.log(result.links.next);
if (result.links.next) {
getPrograms(result.links.next, user, pass)
}
} catch (error) {
if (error instanceof Error) {
console.log('error message: ', error.message);
return error.message;
} else {
console.log('unexpected error: ', error);
return 'An unexpected error occured';
}
}
};
getPrograms(url, u, p);
The program hangs after around 6 iterations, not finishing but not continuing either. I'm assuming it has to do with the requests being sent asynchronously, but I'm relatively new to TypeScript and can't seem to figure out how to wait for all iterations to finish.
Would love for someone to point me to the right direction, and get some feedback regarding the proposed solution as a whole.
Thanks!