All files / tests/helpers ipfs.ts

75% Statements 9/12
0% Branches 0/1
100% Functions 1/1
75% Lines 9/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 423x           4x   4x 4x                     4x                 4x       4x 4x 4x            
export async function pinJSON(params: {
  data: string | object;
  name: string;
  keyvalues?: object;
  auth: string;
}): Promise<string | undefined> {
  const { data, name, keyvalues, auth } = params;
 
  try {
    const pinataData = {
      pinataOptions: {
        cidVersion: 1,
      },
      pinataMetadata: {
        name,
        keyvalues,
      },
      pinataContent: data,
    };
 
    const res = await fetch("https://api.pinata.cloud/pinning/pinJSONToIPFS", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + auth,
      },
      body: JSON.stringify(pinataData),
    });
 
    Iif (!res.ok) {
      throw new Error(`HTTP error! status: ${res.status}`);
    }
 
    const responseData = await res.json();
    console.log("saveToIPFS result:", responseData);
    return responseData.IpfsHash;
  } catch (error: any) {
    console.error("saveToIPFS error:", error?.message);
    return undefined;
  }
}