-
Notifications
You must be signed in to change notification settings - Fork 54
/
types.ts
54 lines (49 loc) · 1.6 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-disable wrap-regex */
import { InvalidStringError, OpaqueString } from '@cardano-sdk/util';
import { Metadatum } from '../../Cardano';
export type Uri = OpaqueString<'Uri'>;
export const Uri = (uri: string) => {
if (/^[a-z]+:\/\/.+/.test(uri)) {
return uri as unknown as Uri;
}
if (uri.startsWith('data:')) {
return uri as unknown as Uri;
}
if (uri.startsWith('Qm') && uri.length === 46) {
return `ipfs://${uri}` as unknown as Uri;
}
throw new InvalidStringError(
'Expected Uri to start with "[protocol]:", where protocol is usually "https", "ipfs" or "data"'
);
};
export type ImageMediaType = 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 = 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;
otherProperties?: Map<string, Metadatum>;
}
/** https://cips.cardano.org/cips/cip25/ https://cips.cardano.org/cips/cip68/ (label 222) */
export interface NftMetadata {
name: string;
image: Uri;
version: string;
mediaType?: ImageMediaType;
files?: NftMetadataFile[];
description?: string;
otherProperties?: Map<string, Metadatum>;
}