Skip to content

Commit

Permalink
fix: only processing the first response success handler
Browse files Browse the repository at this point in the history
Fixes an issue with response success handlers where only the first handler was processed.
  • Loading branch information
kleinfreund committed Nov 25, 2023
1 parent 7567bc5 commit f4a1e06
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/retrieve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,22 @@ describe('retrieve', () => {
} satisfies RetrieveConfig,
'test',
],
[
{
url: 'http://example.org',
responseSuccessHandlers: [
(retrieveResponse) => {
retrieveResponse.data = 'test'
return Promise.resolve(retrieveResponse)
},
(retrieveResponse) => {
retrieveResponse.data = 'overridden data'
return Promise.resolve(retrieveResponse)
},
],
} satisfies RetrieveConfig,
'overridden data',
],
])('onResponseSuccess handlers produce response', async (config: RetrieveConfig, expectedData: unknown) => {
vi.spyOn(global, 'fetch').mockImplementation(() => Promise.resolve(new Response('OK')))

Expand Down
6 changes: 3 additions & 3 deletions src/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ export async function retrieve(config: RetrieveConfig): Promise<RetrieveResponse
let retrieveResponse = await createRetrieveResponse(response)

for (const responseSuccessHandler of config.responseSuccessHandlers ?? []) {
if (retrieveResponse.response.ok) {
retrieveResponse = await responseSuccessHandler(retrieveResponse)
}
retrieveResponse = await responseSuccessHandler(retrieveResponse)
}

if (retrieveResponse.response.ok) {
return retrieveResponse
}

Expand Down

0 comments on commit f4a1e06

Please sign in to comment.