2023-07-03 13:05:06 +00:00
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-unsafe-call */
2023-07-04 05:20:28 +00:00
export const checkTokenUri = async ( tokenUri : string , isBaseTokenUri? : boolean ) = > {
if ( isBaseTokenUri ) {
2024-02-03 01:45:54 +00:00
await fetch (
tokenUri
. replace ( 'ipfs://' , 'https://ipfs-gw.stargaze-apis.com/ipfs/' )
. concat ( tokenUri . endsWith ( '/' ) ? '1' : '/1' ) ,
)
2023-07-04 05:20:28 +00:00
. 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 (
2024-02-03 01:45:54 +00:00
tokenUri
. replace ( 'ipfs://' , 'https://ipfs-gw.stargaze-apis.com/ipfs/' )
. concat ( tokenUri . endsWith ( '/' ) ? '1.json' : '/1.json' ) ,
2023-07-04 05:20:28 +00:00
)
. 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 {
2024-02-03 01:45:54 +00:00
await fetch ( tokenUri . replace ( 'ipfs://' , 'https://ipfs-gw.stargaze-apis.com/ipfs/' ) )
2023-07-04 05:20:28 +00:00
. 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 } ` )
} )
2023-07-03 13:05:06 +00:00
}
}