-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fetchRetry from "fetch-retry"; | ||
|
||
interface MakeSolidFetchOptions { | ||
/** | ||
* return false if the response should be retried | ||
*/ | ||
verifyResponse: (response: unknown) => Promise<boolean>; | ||
} | ||
|
||
// @note: returns already parsed response | ||
type SolidFetch = (input: string, init?: RequestInit) => Promise<unknown>; | ||
|
||
export function makeSolidFetch(opts: MakeSolidFetchOptions): SolidFetch { | ||
const solidFetch = fetchRetry(self.fetch, { | ||
retries: 3, | ||
async retryOn(_attempt, error, response) { | ||
const retry = error !== null || !response?.ok; | ||
if (retry) { | ||
// eslint-disable-next-line no-console | ||
console.log("Retrying failed fetch", { | ||
error, | ||
status: response?.status, | ||
}); | ||
} | ||
// if we have a response, we verify it and decide if we should retry | ||
if (!retry) { | ||
const responseJson = await response.clone().json(); | ||
const verified = await opts.verifyResponse(responseJson); | ||
if (!verified) { | ||
console.log("Response verification failed, retrying..."); | ||
} | ||
|
||
return !verified; | ||
} | ||
|
||
return true; | ||
}, | ||
retryDelay: function (attempt) { | ||
return Math.pow(2, attempt) * 1000; | ||
}, | ||
}); | ||
|
||
return (input: string, init?: RequestInit) => | ||
solidFetch(input, init).then((response) => response.json()); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.