From 2cddea362c1041b9b812ee7f17e267c879f6339f Mon Sep 17 00:00:00 2001 From: MarcusDavidG Date: Sat, 22 Aug 2026 16:33:26 +0100 Subject: [PATCH] refactor: narrow nav.tsx API import to lib/api/factory (#387) Introduce lib/api/factory.ts as a dedicated narrow entry-point that exports only getApi (and the version helpers it re-exports). Move the getApi implementation there from lib/api/index.ts. Update components/nav.tsx to import getApi from @/lib/api/factory instead of the full @/lib/api barrel. The barrel previously pulled in every mock-utility re-export (resetMockData, applyMockScenario, etc.) via mock-boundary, meaning a syntax error in mock.ts could prevent the navigation component from compiling. lib/api/index.ts is updated to re-export getApi from ./factory so all existing consumers of @/lib/api are unaffected. Closes #387 --- components/nav.tsx | 2 +- lib/api/factory.ts | 39 +++++++++++++++++++++++++++++++++++++++ lib/api/index.ts | 36 ++++-------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 lib/api/factory.ts diff --git a/components/nav.tsx b/components/nav.tsx index 4c985ea..df4ea28 100644 --- a/components/nav.tsx +++ b/components/nav.tsx @@ -3,7 +3,7 @@ import Link from "next/link"; import type { Route } from "next"; import { usePathname, useRouter, useParams } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; -import { getApi } from "@/lib/api"; +import { getApi } from "@/lib/api/factory"; import { useAccount } from "wagmi"; import { cn } from "@/lib/utils"; import { ConnectButton } from "./wallet/connect-button"; diff --git a/lib/api/factory.ts b/lib/api/factory.ts new file mode 100644 index 0000000..303a6a5 --- /dev/null +++ b/lib/api/factory.ts @@ -0,0 +1,39 @@ +/** + * lib/api/factory.ts — narrow entry-point for `getApi`. + * + * This module exposes only the `getApi` factory so that components that + * need to call API methods do not transitively import the full barrel + * (`lib/api/index.ts`) with its mock-utility re-exports + * (`resetMockData`, `applyMockScenario`, etc.). + * + * Usage: + * import { getApi } from '@/lib/api/factory' + * + * The full barrel (`@/lib/api`) re-exports `getApi` from here, so + * existing consumers are unaffected. + */ + +import { config } from '../config' +import { LiveAccessApi } from './live' +import { createMockAccessApi } from './mock-boundary' +import type { AccessApi } from './types' + +export { checkVersionCompatibility } from './version' +export type { VersionCompatibility } from './version' + +/** + * Returns the appropriate API client based on the environment. + * + * @param address Connected wallet address (used for session/membership queries) + * @param token SIWE session token — pass this to authenticate admin mutations. + * Ignored by the mock client (mutations succeed unconditionally in mock mode). + * @param communityId Scoped community ID or slug + */ +export function getApi(address?: string, token?: string, communityId?: string): AccessApi { + if (config.apiMode === 'mock') return createMockAccessApi(address, communityId) + const api = new LiveAccessApi(address, token, communityId) + // Kick off the startup version compatibility check. It resolves in the + // background; callers can await api.checkVersion() for the result. + api.checkVersion() + return api +} diff --git a/lib/api/index.ts b/lib/api/index.ts index 7cdfa34..cef1ccf 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -1,35 +1,7 @@ -import { config } from '../config' -import { LiveAccessApi } from './live' -import { - createMockAccessApi, - resetMockData, - applyMockScenario, - replayMockEvent, - setMockRoleMutationFailure, - setMockMetaVersion, -} from './mock-boundary' -import { AccessApi } from './types' -import type { VersionCompatibility } from './version' - -export { checkVersionCompatibility } from './version' -export type { VersionCompatibility } - -/** - * Returns the appropriate API client based on the environment. - * - * @param address Connected wallet address (used for session/membership queries) - * @param token SIWE session token — pass this to authenticate admin mutations. - * Ignored by the mock client (mutations succeed unconditionally in mock mode). - * @param communityId Scoped community ID or slug - */ -export function getApi(address?: string, token?: string, communityId?: string): AccessApi { - if (config.apiMode === 'mock') return createMockAccessApi(address, communityId) - const api = new LiveAccessApi(address, token, communityId) - // Kick off the startup version compatibility check. It resolves in the - // background; callers can await api.checkVersion() for the result. - api.checkVersion() - return api -} +// Re-export getApi and version helpers from the narrow factory module so that +// existing consumers of `@/lib/api` continue to work unchanged. +export { getApi, checkVersionCompatibility } from './factory' +export type { VersionCompatibility } from './factory' export * from './types' export * from './mappers'