Skip to content

Commit

Permalink
refactor: get pinata config
Browse files Browse the repository at this point in the history
  • Loading branch information
yawn-c111 committed Dec 3, 2024
1 parent 56da258 commit 4c0825c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkgs/frontend/utils/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { PinataSDK } from "pinata-web3";

const validateEnvVariables = () => {
if (!import.meta.env.VITE_PINATA_JWT) {
const getPinataConfig = () => {
const pinataJwt = import.meta.env.VITE_PINATA_JWT;
const pinataGateway = import.meta.env.VITE_PINATA_GATEWAY;

if (!pinataJwt) {
throw new Error("VITE_PINATA_JWT is not defined");
}
if (!import.meta.env.VITE_PINATA_GATEWAY) {
if (!pinataGateway) {
throw new Error("VITE_PINATA_GATEWAY is not defined");
}

return { pinataJwt, pinataGateway };
};

let ipfsClient: PinataSDK | null = null;

export const createIpfsClient = () => {
if (ipfsClient) return ipfsClient;

validateEnvVariables();
const { pinataJwt, pinataGateway } = getPinataConfig();
ipfsClient = new PinataSDK({
pinataJwt: import.meta.env.VITE_PINATA_JWT as string,
pinataGateway: import.meta.env.VITE_PINATA_GATEWAY as string,
pinataJwt,
pinataGateway,
});

return ipfsClient;
Expand Down

0 comments on commit 4c0825c

Please sign in to comment.