forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfs.ts
35 lines (32 loc) · 883 Bytes
/
ipfs.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
/**
* Upload JSON to IPFS and return the CID.
*/
export const uploadJsonToIpfs = async (
data: Record<string, unknown>
): Promise<string> => {
// Next.js API route.
const response = await fetch('/api/uploadJson', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
})
if (response.ok) {
const { cid } = await response.json()
if (!cid) {
throw new Error('Failed to get CID from response.')
}
return cid
} else {
// Vercel limits file size to 4.5MB and responds with 413 if exceeded. Add
// some buffer to make room for the other fields.
if (response.status === 413) {
throw new Error('Data too large to upload. Max 4MB.')
}
const { error } = await response
.json()
.catch(() => ({ error: 'Unknown error' }))
throw new Error(error)
}
}