This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
forked from kodadot/nft-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinata.ts
72 lines (62 loc) · 1.74 KB
/
pinata.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Axios from 'axios';
import { extractCid, justHash } from './utils/ipfs';
export const BASE_URL = 'https://api.pinata.cloud/pinning/';
export type APIKeys = {
pinata_api_key: string;
pinata_api_secret: string;
}
const api = Axios.create({
baseURL: BASE_URL,
headers: {
'Content-Type': 'application/json',
pinata_api_key: process.env.VUE_APP_PINATA_API_KEY,
pinata_secret_api_key: process.env.VUE_APP_PINATA_SECRET_API_KEY
},
withCredentials: false
});
export const pinJson = async (object: any) => {
try {
const { status, data } = await api.post('pinJSONToIPFS', object);
console.log('[PINATA] Pin Image', status, data);
if (status < 400) {
return data.IpfsHash;
}
} catch (e) {
throw e;
}
};
export const pinFile = async (file: Blob, keys: APIKeys): Promise<string> => {
const formData = new FormData();
formData.append('file', file);
try {
const { status, data } = await api.post('pinFileToIPFS', formData, {
headers: {
'Content-Type': `multipart/form-data;`,
pinata_api_key: keys.pinata_api_key,
pinata_secret_api_key: keys.pinata_api_secret
}
});
console.log('[PINATA] Pin Image', status, data);
if (status < 400) {
return data.IpfsHash;
} else {
throw new Error('Unable to PIN for reasons');
}
} catch (e) {
throw e;
}
};
export const unpin = async (ipfsLink: string) => {
const hash = justHash(ipfsLink) ? ipfsLink : extractCid(ipfsLink)
try {
const { status, data } = await api.delete(`unpin/${hash}`);
console.log('[PINATA] Pin Image', status, data);
if (status < 400) {
return data;
}
} catch (e) {
throw e;
}
};
export default api;
// QmYt2FydonvVMsEqe2q3hvm38WDq21xM8Z5ZSHZw19PwjF;