divaprotocol / diva-monorepo

Diva Monorepo
https://diva.finance
4 stars 4 forks source link

Extend json content handling for referenceAsset #815

Open Walodja1987 opened 1 year ago

Walodja1987 commented 1 year ago

Currently, only URLs that end with .json are handled as json. However, there are also URLs, that do not end with .json but still have json content. Here an example: https://nftstorage.link/ipfs/bafkreic5xs3jg3m4poqqikwyslatiyknkevtwyqqzlouokwlslgkiu5nga

Extend the current check by checking whether the content is of json format by parsing the the response data via JSON.parse.

Dummy code:

async function isJson(url: string): Promise<boolean> {
try {
const response = ...

// Try to parse the content as JSON
try {
JSON.parse(response.data);
return true;
} catch (error) {
return false;
}
}
}