From 4c0825c764ad9cb1ec6a429f865e46529f7c9f9d Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:49:16 +0900 Subject: [PATCH] refactor: get pinata config --- pkgs/frontend/utils/ipfs.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkgs/frontend/utils/ipfs.ts b/pkgs/frontend/utils/ipfs.ts index 690f734..d22d008 100644 --- a/pkgs/frontend/utils/ipfs.ts +++ b/pkgs/frontend/utils/ipfs.ts @@ -1,12 +1,17 @@ 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; @@ -14,10 +19,10 @@ 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;