elbywan / wretch-middlewares

Collection of middlewares for the Wretch library. 🎁
MIT License
47 stars 7 forks source link

Retry only on specified error #1

Closed Siemko closed 5 years ago

Siemko commented 6 years ago

Is there a way to make retry middleware work only on specified error (503 with specified json result)?

elbywan commented 6 years ago

Hi @Siemko, sorry that I couldn't reply earlier.

Is there a way to make retry middleware work only on specified error (503 with specified json result)?

I just pushed an update to the RetryMiddleware that allows returning a Promise in the until option.

You can use it like this to retry until you get a specified error & body response that satisfy your condition:

wretch('https://jsonplaceholder.typicode.com/todos/1').middlewares([
    retry({
        until: response => (
            response.status === 503 && 
            response.json().then(body => (
                // body checks here
            ))
        )
    })
]).get().json(body => 
    // ....
)