-
Notifications
You must be signed in to change notification settings - Fork 146
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
1 parent
e67b61d
commit 227f6ee
Showing
9 changed files
with
68 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
import axios from 'axios'; | ||
|
||
import { analytics } from '@shared/utils/analytics'; | ||
|
||
const leatherHeaders: HeadersInit = { | ||
'x-leather-version': VERSION, | ||
}; | ||
|
||
function isErrorCode(statusCode: number) { | ||
return statusCode >= 400; | ||
} | ||
|
||
function trackApiError(url: string, statusCode: number) { | ||
void analytics.track('api_error', { origin: new URL(url).origin, statusCode, url }); | ||
} | ||
|
||
/** | ||
* @deprecated Use `axios` directly instead | ||
* @deprecated Use `axios` directly instead. Fetch only needed for interation | ||
* with generated stacks blockchain api library | ||
*/ | ||
export function wrappedFetch(input: RequestInfo, init: RequestInit = {}) { | ||
export async function wrappedFetch(input: RequestInfo, init: RequestInit = {}) { | ||
const initHeaders = init.headers || {}; | ||
// eslint-disable-next-line no-restricted-globals | ||
return fetch(input, { | ||
credentials: 'omit', | ||
const resp = await fetch(input, { | ||
...init, | ||
credentials: 'omit', | ||
headers: { ...initHeaders, ...leatherHeaders }, | ||
}); | ||
if (isErrorCode(resp.status)) trackApiError(resp.url, resp.status); | ||
return resp; | ||
} | ||
|
||
export async function fetchWithTimeout( | ||
input: RequestInfo, | ||
init: RequestInit & { timeout?: number } = {} | ||
) { | ||
const { timeout = 8000, ...options } = init; | ||
|
||
const controller = new AbortController(); | ||
const id = setTimeout(() => controller.abort(), timeout); | ||
|
||
const response = await wrappedFetch(input, { | ||
...options, | ||
signal: controller.signal, | ||
}); | ||
clearTimeout(id); | ||
|
||
axios.interceptors.response.use(response => { | ||
if (isErrorCode(response.status)) trackApiError(response.config.url ?? '', response.status); | ||
return response; | ||
} | ||
}); |
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
This file was deleted.
Oops, something went wrong.
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