-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mx): nftData uri issue. add getTransactionStatus, fix fetchHttpOr…
…Ipfs get image issue
- Loading branch information
1 parent
9e60ebd
commit 9fa31ff
Showing
3 changed files
with
37 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
import axios from "axios"; | ||
|
||
export async function fetchHttpOrIpfs(uri: string) { | ||
async function fetchWithFallback(uri: string, fallbackUri: string) { | ||
const http = axios.create(); | ||
try { | ||
const response = await http.get(uri, { timeout: 10000 }); | ||
return response.data; | ||
} catch (ex) { | ||
try { | ||
const response = await http.get(fallbackUri, { timeout: 10000 }); | ||
return response.data; | ||
} catch (ex) { | ||
return ""; | ||
} | ||
} | ||
} | ||
|
||
export async function fetchHttpOrIpfs(uri: string) { | ||
const url = new URL(uri); | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
const response = await http.get(uri); | ||
return response.data; | ||
return fetchWithFallback( | ||
uri, | ||
`${uri.replace("ipfs.io", "xpnetwork.infura-ipfs.io")}`, | ||
); | ||
} | ||
if (url.protocol === "ipfs:") { | ||
const response = await http.get( | ||
`https://ipfs.io/ipfs/${uri.replace("ipfs://", "")}`, | ||
); | ||
return response.data; | ||
const ipfsUri = `https://ipfs.io/ipfs/${uri.replace("ipfs://", "")}`; | ||
const fallbackIpfsUri = `https://xpnetwork.infura-ipfs.io/ipfs/${uri.replace( | ||
"ipfs://", | ||
"", | ||
)}`; | ||
return fetchWithFallback(ipfsUri, fallbackIpfsUri); | ||
} | ||
throw new Error("Unsupported protocol"); | ||
} |