Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): add types for NFT metadata
- Loading branch information
1 parent
9b2fa94
commit 913c217
Showing
2 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-disable wrap-regex */ | ||
import { Cardano, InvalidStringError } from '@cardano-sdk/core'; | ||
|
||
export type Uri = Cardano.util.OpaqueString<'Uri'>; | ||
export const Uri = (uri: string) => { | ||
if (/^[a-z]+:\/\/.+/.test(uri)) { | ||
return uri as unknown as Uri; | ||
} | ||
throw new InvalidStringError( | ||
'Expected Uri to start with "[protocol]://", where protocol is usually "https" or "ipfs"' | ||
); | ||
}; | ||
|
||
export type ImageMediaType = Cardano.util.OpaqueString<'ImageMediaType'>; | ||
export const ImageMediaType = (mediaType: string) => { | ||
if (/^image\/.+$/.test(mediaType)) { | ||
return mediaType as unknown as ImageMediaType; | ||
} | ||
throw new InvalidStringError('Expected media type to be "image/*"'); | ||
}; | ||
|
||
export type MediaType = Cardano.util.OpaqueString<'MediaType'>; | ||
export const MediaType = (mediaType: string) => { | ||
if (/^[a-z]+\/.+$/.test(mediaType)) { | ||
return mediaType as unknown as MediaType; | ||
} | ||
throw new InvalidStringError('Expected media type to be "*/*"'); | ||
}; | ||
|
||
/** | ||
* https://cips.cardano.org/cips/cip25/ | ||
*/ | ||
export interface NftMetadataFile { | ||
name: string; | ||
mediaType: MediaType; | ||
src: Uri | Uri[]; | ||
otherProperties?: { | ||
[key: string]: Cardano.Metadatum; | ||
}; | ||
} | ||
|
||
/** | ||
* https://cips.cardano.org/cips/cip25/ | ||
*/ | ||
export interface NftMetadata { | ||
name: string; | ||
image: Uri | Uri[]; | ||
version: string; | ||
mediaType?: ImageMediaType; | ||
files?: NftMetadataFile[]; | ||
description?: string | string[]; | ||
otherProperties?: { | ||
[key: string]: Cardano.Metadatum; | ||
}; | ||
} | ||
|
||
export interface NftMetadataProvider { | ||
(asset: Cardano.Asset): Promise<NftMetadata | undefined>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as NftMetadata from '../../src/NftMetadata'; | ||
import { InvalidStringError } from '@cardano-sdk/core'; | ||
|
||
describe('NftMetadata/types', () => { | ||
describe('Uri', () => { | ||
it('accepts a string starting with protocol://', () => { | ||
expect(() => NftMetadata.Uri('http://some.url')).not.toThrow(); | ||
expect(() => NftMetadata.Uri('ipfs://abc123')).not.toThrow(); | ||
}); | ||
it('throws for string without protocol:// prefix', () => { | ||
expect(() => NftMetadata.Uri('abc123')).toThrowError(InvalidStringError); | ||
}); | ||
}); | ||
|
||
describe('ImageMediaType', () => { | ||
it('accepts a string starting with image/', () => { | ||
expect(() => NftMetadata.ImageMediaType('image/svg+xml')).not.toThrow(); | ||
}); | ||
it('throws for non-image media type', () => { | ||
expect(() => NftMetadata.ImageMediaType('video/webm')).toThrowError(InvalidStringError); | ||
}); | ||
}); | ||
|
||
describe('MediaType', () => { | ||
it('accepts any media type in format "type/subtype"', () => { | ||
expect(() => NftMetadata.MediaType('image/svg+xml')).not.toThrow(); | ||
expect(() => NftMetadata.MediaType('video/mp4')).not.toThrow(); | ||
expect(() => NftMetadata.MediaType('audio/x-wav')).not.toThrow(); | ||
}); | ||
it('throws for incorrectly formatted media type', () => { | ||
expect(() => NftMetadata.MediaType('videomp4')).toThrowError(InvalidStringError); | ||
}); | ||
}); | ||
}); |