From 030b596a710f5145cfe25430043ad718a105b7e4 Mon Sep 17 00:00:00 2001 From: Wenderson Pires Date: Mon, 29 Jul 2024 18:14:29 -0300 Subject: [PATCH] ensure Naxios runs on the client side only --- package.json | 2 +- src/managers/contract-manager.ts | 2 +- src/managers/wallet-manager.ts | 6 ++++++ src/naxios.ts | 6 ++++++ src/utils/isClient.ts | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/utils/isClient.ts diff --git a/package.json b/package.json index 7bddd38..d0fe6fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wpdas/naxios", - "version": "2.1.0", + "version": "2.1.1", "description": "Promise based NEAR Contract and NEAR Wallet client for browser", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/src/managers/contract-manager.ts b/src/managers/contract-manager.ts index f5ae11b..df999e1 100644 --- a/src/managers/contract-manager.ts +++ b/src/managers/contract-manager.ts @@ -97,7 +97,7 @@ class ContractManager { const outcome = JSON.parse(Buffer.from(res.result).toString()) as R - // If cache is avaiable, store data on it + // If cache is available, store data on it if (config?.useCache && this.cache) { await this.cache.setItem(cacheKey, outcome) } diff --git a/src/managers/wallet-manager.ts b/src/managers/wallet-manager.ts index b60eed3..7cb5b61 100644 --- a/src/managers/wallet-manager.ts +++ b/src/managers/wallet-manager.ts @@ -9,6 +9,7 @@ import { } from '@near-wallet-selector/core' import { setupModal } from '@near-wallet-selector/modal-ui' import { WalletManagerConfig, Network, WalletStatus } from './types' +import { isClient } from '../utils/isClient' const getTestnetConfig = () => ({ networkId: 'testnet', @@ -57,6 +58,11 @@ export class WalletManager { this.walletSelectorModules = config.walletSelectorModules } + // Ensure it is going to run on client side only + if (!isClient()) { + return + } + this.changeWalletStatus('pending') this.initNear().then(() => { diff --git a/src/naxios.ts b/src/naxios.ts index a0d276a..45d91cc 100644 --- a/src/naxios.ts +++ b/src/naxios.ts @@ -3,6 +3,7 @@ import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet' import ContractManager from './managers/contract-manager' import WalletManager from './managers/wallet-manager' import { NaxiosConstructor, Network, ContractApi } from './managers/types' +import { isClient } from './utils/isClient' class naxios { private rpcNodeUrl?: ContractManager['rpcNodeUrl'] @@ -16,6 +17,11 @@ class naxios { this.contractId = config.contractId this.network = config.network + // Ensure it is going to run on client side only + if (!isClient()) { + return + } + if (config.walletSelectorModules) { this.walletSelectorModules = config.walletSelectorModules } diff --git a/src/utils/isClient.ts b/src/utils/isClient.ts new file mode 100644 index 0000000..96a17eb --- /dev/null +++ b/src/utils/isClient.ts @@ -0,0 +1 @@ +export const isClient = () => !(typeof window === 'undefined')