@fluid-app/portal-sdk

中文文档
0.1.1433发布于 2026/11/15TypeScriptESM + CJS近 30 天下载 14.8万

SDK for building custom Fluid portals

原始使用文档
文档目录16 节
  1. Installation
  2. Peer Dependencies
  3. Quick Start
  4. Remote widget architecture
  5. Public facades
  6. Glossary
  7. Hooks
  8. useFluidProfile
  9. useFluidTheme
  10. Providers
  11. FluidProvider
  12. FluidThemeProvider
  13. Types
  14. Query Keys
  15. Error Handling
  16. License

@fluid-app/portal-sdk

SDK for building custom Fluid portals. Provides React hooks, providers, and an API client for integrating with the Fluid Commerce platform.

Installation

npm install @fluid-app/portal-sdk
# or
pnpm add @fluid-app/portal-sdk
# or
bun add @fluid-app/portal-sdk

Peer Dependencies

This package requires the following peer dependencies:

{
  "react": ">=18.0.0",
  "@tanstack/react-query": ">=5.0.0"
}

Quick Start

Wrap your application with FluidProvider:

import { FluidProvider } from "@fluid-app/portal-sdk";

function App() {
  return (
    <FluidProvider
      config={{
        baseUrl: "https://api.fluid.app/api",
        getAuthToken: () => localStorage.getItem("fluid_token"),
        onAuthError: () => {
          // Handle 401 errors (e.g., redirect to login)
          window.location.href = "/login";
        },
      }}
    >
      <YourApp />
    </FluidProvider>
  );
}

Remote widget architecture

Remote widgets cross a runtime boundary: widget code runs in a worker while the portal SDK runs in the browser host. The implementation is split by ownership:

  • src/widgets/remote/contract/ owns the shared, JSON-safe protocol: capability declarations, portal-function metadata, serializable types, and custom-element event contracts.
  • src/widgets/remote/worker/ owns worker authoring and execution: defineWidget, defineWidgetPackage, startWidgetPackage, and worker-side element wrappers.
  • src/widgets/remote/host/ owns portal integration: loading remote packages, capability handlers, registry providers, host element registration, and fullscreen coordination.

The dependency direction is worker → contract ← host. Worker code must never import host code or host-only dependencies such as portal providers, DOM renderers, or host capability adapters.

Public facades

  • @fluid-app/portal-sdk/widgets/worker is the stable worker-safe authoring facade. Widget packages should import worker APIs only from this subpath.
  • @fluid-app/portal-sdk is the host application facade. It exposes portal setup and the shared contract types needed to configure the host.
  • Existing exported host utilities, such as @fluid-app/portal-sdk/utils/build-widget-registry, remain compatibility facades over the host implementation. Paths below src/widgets/remote/ are internal and are not package entry points.

Glossary

  • Contract — Shared serializable vocabulary understood by both runtimes; it contains no host implementation.
  • Worker — The isolated runtime that renders a remote widget and calls declared portal functions.
  • Host — The portal browser runtime that loads workers and implements their declared capabilities.
  • Capability — A versioned group of portal functions a widget declares and the host may implement.
  • Facade — A stable package entry that re-exports an internal implementation without exposing its directory layout.

Hooks

useFluidProfile

Fetch the portal profile (themes, navigation, screens):

import { useFluidProfile } from "@fluid-app/portal-sdk";

function Navigation() {
  const { data: profile, isLoading } = useFluidProfile();

  if (isLoading) return <Spinner />;

  return (
    <nav>
      {profile?.navigation.navigation_items.map((item) => (
        <NavItem key={item.id} item={item} />
      ))}
    </nav>
  );
}

useFluidTheme

Control theme settings:

import { useFluidTheme } from "@fluid-app/portal-sdk";
import type { Theme } from "@fluid-app/portal-sdk";

function ThemeSwitcher({ themes }: { themes: Theme[] }) {
  const { currentTheme, setTheme, setThemeMode, mode } = useFluidTheme();

  return (
    <div>
      <select
        value={currentTheme?.name}
        onChange={(e) => {
          const theme = themes.find((t) => t.name === e.target.value);
          if (theme) setTheme(theme);
        }}
      >
        {themes.map((theme) => (
          <option key={theme.name} value={theme.name}>
            {theme.name}
          </option>
        ))}
      </select>

      <button onClick={() => setThemeMode(mode === "dark" ? "light" : "dark")}>
        Toggle {mode === "dark" ? "Light" : "Dark"} Mode
      </button>
    </div>
  );
}

Providers

FluidProvider

The main provider that sets up the SDK. It wraps:

  • QueryClientProvider (TanStack Query)
  • FluidThemeProvider (theme management)
import { FluidProvider } from "@fluid-app/portal-sdk";
import { QueryClient } from "@tanstack/react-query";

// Optional: provide your own QueryClient
const queryClient = new QueryClient({
  defaultOptions: {
    queries: { staleTime: 5 * 60 * 1000 },
  },
});

function App() {
  return (
    <FluidProvider
      config={{
        baseUrl: "https://api.fluid.app/api",
        getAuthToken: () => getToken(),
      }}
      queryClient={queryClient}
      initialTheme={defaultTheme}
    >
      <YourApp />
    </FluidProvider>
  );
}

FluidThemeProvider

Can be used standalone if you need theme management without the full SDK:

import { FluidThemeProvider, useFluidTheme } from "@fluid-app/portal-sdk";

function App() {
  return (
    <FluidThemeProvider initialTheme={myTheme}>
      <ThemedContent />
    </FluidThemeProvider>
  );
}

Theme CSS variables are applied to the document root (or a custom container) with the --fluid- prefix.

Types

All types are exported for use in your application:

import type {
  // Core types
  Profile,
  Navigation,
  NavigationItem,
  ScreenDefinition,

  // Client types
  FluidSDKConfig,
  RequestOptions,
} from "@fluid-app/portal-sdk";

Query Keys

Query keys are exported for cache invalidation and prefetching:

import { PROFILE_QUERY_KEY } from "@fluid-app/portal-sdk";
import { useQueryClient } from "@tanstack/react-query";

function RefreshButton() {
  const queryClient = useQueryClient();

  const handleRefresh = () => {
    queryClient.invalidateQueries({ queryKey: PROFILE_QUERY_KEY });
  };

  return <button onClick={handleRefresh}>Refresh Profile</button>;
}

Error Handling

The SDK provides an ApiError class for structured error handling:

import { ApiError, isApiError } from "@fluid-app/portal-sdk";

try {
  await someApiCall();
} catch (error) {
  if (isApiError(error)) {
    console.log("Status:", error.status);
    console.log("Message:", error.message);
    console.log("Data:", error.data); // Server error details
  }
}

License

Private - Fluid Commerce

版本与发布记录

@fluid-app/portal-sdk 共发布 1,423 个版本,首次发布于 2026/03/07;过去 12 个月发布了 1423 个版本。

版本发布日期距今
0.1.1433latest2026/11/15今天
0.1.14322026/11/15今天
0.1.14312026/11/15今天
0.1.14302026/11/15今天
0.1.14292026/11/15今天
0.1.14282026/11/15今天

关于 @fluid-app/portal-sdk 的常见问题

@fluid-app/portal-sdk 是什么?

SDK for building custom Fluid portals

如何安装 @fluid-app/portal-sdk?

在项目目录运行 npm install @fluid-app/portal-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(4.6.5)、react(^19.2.4)、react-dom(^19.2.4)、lucide-react(~0.575.0)、react-hook-form(^7.64.0)、@hookform/resolvers(^5.2.2)、@tanstack/react-query(^5.90.11)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fluid-app/portal-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluid-app/portal-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fluid-app/portal-sdk 可以商用吗?

@fluid-app/portal-sdk 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@fluid-app/portal-sdk 的下载量有多少?

根据 npm 官方下载统计,@fluid-app/portal-sdk 在 2026/10/15 – 2026/11/13 这 30 天内被下载 147,785 次,日均约 4,926 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluid-app/portal-sdk 还在维护吗?

最新版本 0.1.1433 发布于 2026/11/15(今天);至今共发布 1423 个版本,其中过去 12 个月发布了 1423 个;首次发布于 2026/03/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。