Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
39 changes: 39 additions & 0 deletions lib/api/factory.ts
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 4 additions & 32 deletions lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down