Update token URI validation logic to cover standard collections as well
This commit is contained in:
+67
-17
@@ -1,21 +1,71 @@
|
||||
/* eslint-disable eslint-comments/disable-enable-pair */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
export const checkTokenUri = async (tokenUri: string) => {
|
||||
const file = await fetch(tokenUri.replace('ipfs://', 'https://ipfs.io/ipfs/'))
|
||||
.then((res) =>
|
||||
res.json().catch((err: any) => {
|
||||
throw Error(`Metadata file could not be parsed. Please check that it is valid JSON.`)
|
||||
}),
|
||||
)
|
||||
.catch((err: any) => {
|
||||
throw Error(`Unable to fetch metadata from ${tokenUri}`)
|
||||
})
|
||||
|
||||
if (!file.image) {
|
||||
throw Error('Token URI must contain an image URL.')
|
||||
}
|
||||
if (file.image && !file.image.startsWith('ipfs://')) {
|
||||
throw Error('Metadata file: The corresponding value for image must be an IPFS URL.')
|
||||
export const checkTokenUri = async (tokenUri: string, isBaseTokenUri?: boolean) => {
|
||||
if (isBaseTokenUri) {
|
||||
await fetch(tokenUri.replace('ipfs://', 'https://ipfs.io/ipfs/').concat(tokenUri.endsWith('/') ? '1' : '/1'))
|
||||
.then((res) =>
|
||||
res
|
||||
.json()
|
||||
.then((data) => {
|
||||
if (!data.image) {
|
||||
throw Error('Metadata validation failed. The metadata files must contain an image URL.')
|
||||
}
|
||||
if (!data.image.startsWith('ipfs://')) {
|
||||
throw Error('Metadata file validation failed: The corresponding value for image must be an IPFS URL.')
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
throw Error(
|
||||
`Metadata validation failed. Please check that the metadata files in the IPFS folder are valid JSON.`,
|
||||
)
|
||||
}),
|
||||
)
|
||||
.catch(async () => {
|
||||
await fetch(
|
||||
tokenUri.replace('ipfs://', 'https://ipfs.io/ipfs/').concat(tokenUri.endsWith('/') ? '1.json' : '/1.json'),
|
||||
)
|
||||
.then((response) =>
|
||||
response
|
||||
.json()
|
||||
.then((file) => {
|
||||
if (!file.image) {
|
||||
throw Error('Metadata validation failed. The metadata files must contain an image URL.')
|
||||
}
|
||||
if (!file.image.startsWith('ipfs://')) {
|
||||
throw Error('Metadata file validation failed: The corresponding value for image must be an IPFS URL.')
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
throw Error(
|
||||
`Metadata validation failed. Please check that the metadata files in the IPFS folder are valid JSON.`,
|
||||
)
|
||||
}),
|
||||
)
|
||||
.catch(() => {
|
||||
throw Error(
|
||||
`Unable to fetch metadata from ${tokenUri}. Metadata validation failed. Please check that the base token URI points to an IPFS folder with metadata files in it.`,
|
||||
)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
await fetch(tokenUri.replace('ipfs://', 'https://ipfs.io/ipfs/'))
|
||||
.then((res) =>
|
||||
res
|
||||
.json()
|
||||
.then((file) => {
|
||||
if (!file.image) {
|
||||
throw Error('Token URI must contain an image URL.')
|
||||
}
|
||||
if (!file.image.startsWith('ipfs://')) {
|
||||
throw Error('Metadata file: The corresponding value for image must be an IPFS URL.')
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
throw Error(`Metadata file could not be parsed. Please check that it is valid JSON.`)
|
||||
}),
|
||||
)
|
||||
.catch(() => {
|
||||
throw Error(`Unable to fetch metadata from ${tokenUri}`)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user