/
metadata.ts
47 lines (41 loc) · 1.6 KB
/
metadata.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
import path from 'path'
import https from 'https'
import axios from 'axios'
import { Attribute } from './model'
import { Context } from './processor'
export const IPFS_GATEWAY = 'https://ipfs.io/ipfs/'
export interface TokenMetadata {
image: string
attributes: Attribute[]
}
export async function fetchTokenMetadata(ctx: Context, uri: string): Promise<TokenMetadata | undefined> {
try {
if (uri.startsWith('ipfs://')) {
const gatewayURL = path.posix.join(IPFS_GATEWAY, ipfsRegExp.exec(uri)![1])
let res = await client.get(gatewayURL)
ctx.log.info(`Successfully fetched metadata from ${gatewayURL}`)
return res.data
} else if (uri.startsWith('http://') || uri.startsWith('https://')) {
let res = await client.get(uri)
ctx.log.info(`Successfully fetched metadata from ${uri}`)
return res.data
} else {
ctx.log.warn(`Unexpected metadata URL protocol: ${uri}`)
return undefined
}
} catch (e) {
throw new Error(`Failed to fetch metadata at ${uri}. Error: ${e}`)
}
}
const ipfsRegExp = /^ipfs:\/\/(.+)$/
const client = axios.create({
headers: {'Content-Type': 'application/json'},
httpsAgent: new https.Agent({keepAlive: true}),
transformResponse(res: string): TokenMetadata {
let data: {image: string; attributes: {trait_type: string; value: string}[]} = JSON.parse(res)
return {
image: data.image,
attributes: data.attributes.map((a) => new Attribute({traitType: a.trait_type, value: a.value})),
}
},
})