-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublishToGraph.js
More file actions
46 lines (38 loc) · 1.3 KB
/
publishToGraph.js
File metadata and controls
46 lines (38 loc) · 1.3 KB
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
import fetch, { Blob, FormData } from "node-fetch";
/**
* Send file to IPFS network via The Graph hosted IPFS node
* @param data - The raw data from the file to upload.
* @returns ipfs response. Should include the hash and path of the stored item.
*/
export const publishToGraph = async (fileName, data) => {
const url = `${process.env.GRAPH_IPFS_ENDPOINT}/api/v0/add?wrap-with-directory=true`;
const payload = new FormData();
payload.append("file", new Blob([data]), fileName);
console.log("Graph url:", url);
const response = await fetch(url, {
method: "POST",
body: payload,
});
console.log("response body:", await response.text(), response.body);
if (!response.ok) {
throw new Error(
`HTTP error! status: ${response.status}, Failed to pin to graph`
);
}
const result = parseNewlineSeparatedJSON(await response.text());
return result.map(({ Name, Hash }) => ({
hash: Hash,
path: `/${Name}`,
}));
};
/**
* @description parses json from stringified json's seperated by new line
*/
const parseNewlineSeparatedJSON = (text) => {
const lines = text.trim().split("\n");
return lines.map((line) => JSON.parse(line));
};
export const areCidsConsistent = (filebaseCid, graphResult) => {
const graphCid = graphResult[1].hash;
return graphCid === filebaseCid;
};