Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poll-api-return-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@relayprotocol/relay-sdk': patch
---

Fix `pollUntilHasData` and `pollUntilOk` resolving to `undefined` when the data or ok response arrives after the first attempt
32 changes: 32 additions & 0 deletions packages/sdk/src/utils/pollApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { axios } from './axios'
import { pollUntilHasData, pollUntilOk } from './pollApi'

const request = { url: 'https://api.relay.link/requests/v2' }

describe('pollApi', () => {
afterEach(() => {
vi.restoreAllMocks()
vi.useRealTimers()
})

it('pollUntilHasData should resolve to the data found on a later attempt', async () => {
vi.useFakeTimers()
vi.spyOn(axios, 'request')
.mockResolvedValueOnce({ data: { requests: [] } })
.mockResolvedValueOnce({ data: { requests: [{ id: '0x1' }] } })

const result = pollUntilHasData(request, (json) => json.requests.length > 0)
await vi.advanceTimersByTimeAsync(5000)

await expect(result).resolves.toEqual({ requests: [{ id: '0x1' }] })
})

it('pollUntilOk should resolve to true when a later attempt is ok', async () => {
vi.spyOn(axios, 'request')
.mockResolvedValueOnce({ status: 202 })
.mockResolvedValueOnce({ status: 200 })

await expect(pollUntilOk(request, undefined, 15, 0, 0)).resolves.toBe(true)
})
})
4 changes: 2 additions & 2 deletions packages/sdk/src/utils/pollApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function pollUntilHasData(
// The response is still unchanged. Check again in five seconds
await new Promise((resolve) => setTimeout(resolve, 5000))
attemptCount++
await pollUntilHasData(request, dataParser, maximumAttempts, attemptCount)
return pollUntilHasData(request, dataParser, maximumAttempts, attemptCount)
}

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ export async function pollUntilOk(
// The response is still unchanged
await new Promise((resolve) => setTimeout(resolve, pollingInterval))
attemptCount++
await pollUntilOk(
return pollUntilOk(
request,
validate,
maximumAttempts,
Expand Down