https://cypress.io logo
hi there! I'm having some issues handling multiple...
# best-practices
b
hi there! I'm having some issues handling multiple API calls, does somebody knows which is the best way of handling a case like this one?:
Copy code
typescript
Cypress.Commands.add('myCommand', (sumCriteria: Function, anotherCriteria: Function) => {
    // I only retrieve fathers with certain criteria
    return cy.request('GET', fathersUrl).its('body').then(fatherObjects => {
        return fatherObjects.filter(father => father.childs.length && father.childs.find(sumCriteria))
    }).then(filteredFathers => {
        filteredFathers.forEach(father => {
            // For each father I retrieve a single child
            const targetChildId = father.childs.find(sumCriteria).id;
            // For each single child I retrieve its data and evaluate if it has the needed criteria
            cy.request('GET', `${childsUrl}/${targetChildId}`)
                .its('body')
                .then(property => anotherCriteria(property))
        })
    });
})
5 Views