Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Manejo de varias llamadas a la API de una sola llamada a la API

Con mi equipo, estamos tratando de implementar un comando para una operación muy común para la lógica comercial, pero tengo problemas para manejar su implementación. Básicamente:

  1. Tenemos que recuperar una matriz de objetos (GET).

  2. Para cada uno de esos objetos tenemos que recuperar (GET) otro objeto dentro de su padre .

  3. Para cada uno de esos subobjetos (hijos) tenemos que verificar una condición y si es la condición deseada recuperamos el hijo, de lo contrario pasamos nulo.

P: ¿Cómo manejo varias llamadas a la API que dependen de una única llamada a la API sin salirme de la cadena CY?

Esta es mi implementación actual (no funciona pero explica un poco la lógica deseada)

 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)) }) }); })

¡Gracias por adelantado!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Casi tiene el patrón correcto, pero en lugar de devolver resultados, póngalos en la cola.

Cypress hace dos cosas para que esto funcione

  • en un comando personalizado, espera a que se resuelvan los comandos asincrónicos
  • devuelve lo que está en la cola en la última evaluación
 Cypress.Commands.add('myCommand', (sumCriteria, anotherCriteria) => { cy.request('GET', fathersUrl) .its('body') .then(fatherObjects => { const filteredFathers = fatherObjects.filter(father => { return father.childs.find(sumCriteria) }); const results = [] filteredFathers.forEach(father => { cy.request('GET', father) // waits for all these to resove .its('body') .then(property => anotherCriteria(property)) }) cy.then(() => results) // returns this last queued command }) })

Un ejemplo reproducible:

 Cypress.Commands.add('myCommand', (sumCriteria, anotherCriteria) => { const fathersUrl = 'https://jsonplaceholder.typicode.com/todos/1' cy.request('GET', fathersUrl) .then(() => { // simulated url extraction const filteredFathers = [ 'https://jsonplaceholder.typicode.com/todos/2', 'https://jsonplaceholder.typicode.com/todos/3' ] const results = [] filteredFathers.forEach(father => { cy.request('GET', father) .then(res => { results.push(res.body.id) }) }); cy.then(() => results) }); }) cy.myCommand() .should('deep.eq', [2,3]) // ✅ passes
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!