@fluid-app/storage-manager

中文文档
0.3.2发布于 2025/07/18MITTypeScriptESM + CJS近 30 天下载 2.1万

Storage management utilities for Fluid Commerce applications

原始使用文档
文档目录11 节
  1. Installation
  2. Basic Usage
  3. Working with Cookies
  4. Integration with Fluid Ecosystem
  5. Storage Keys Reference
  6. Cookie Keys Reference
  7. API Reference
  8. StorageManager Class
  9. Utility Functions
  10. Server-Side Rendering Compatibility
  11. License

@fluid-app/storage-manager

A utility package for Fluid Commerce applications that provides a consistent interface for working with browser storage (localStorage) and cookies. This package handles storage across browser environments including server-side rendering.

Installation

npm install @fluid-app/storage-manager
# or
yarn add @fluid-app/storage-manager
# or
pnpm add @fluid-app/storage-manager

Basic Usage

import { STORAGE_KEYS, StorageManager } from "@fluid-app/storage-manager";

// Create a storage manager instance
const storageManager = new StorageManager();

// Store string data
storageManager.setItem(STORAGE_KEYS.CART, "cart_abc123");

// Retrieve string data
const cartToken = storageManager.getItem(STORAGE_KEYS.CART); // "cart_abc123"

// Store complex data (automatically serialized to JSON)
storageManager.setObject(STORAGE_KEYS.CART, {
  cart_token: "cart_abc123",
  items: [
    { id: 1, variant_id: 11601, quantity: 2 },
    { id: 2, variant_id: 11602, quantity: 1 },
  ],
  subtotal: "29.99",
});

// Retrieve complex data (automatically parsed from JSON)
const cart = storageManager.getObject(STORAGE_KEYS.CART);
console.log(cart.items.length); // 2

// Remove data
storageManager.removeItem(STORAGE_KEYS.CART);

Working with Cookies

import { COOKIE_KEYS, getCookie } from "@fluid-app/storage-manager";
import { getCountryFromLocale } from "path/to/localeUtils";

// Read a cookie value
const countryCookie = getCookie(COOKIE_KEYS.COUNTRY); // "US"

// The storage manager can also retrieve values from cookies as fallback
const storageManager = new StorageManager();

// Get country code with fallback to cookies
const locale = storageManager.getItem(STORAGE_KEYS.LOCALE);
const countryCode = getCountryFromLocale(locale);

// Get locale with fallback to cookies
const locale = storageManager.getItem(STORAGE_KEYS.LOCALE);

Integration with Fluid Ecosystem

The Storage Manager is used by other Fluid packages to store:

  1. Cart data (@fluid-app/fluid)
  2. Session tokens (@fluid-app/fairshare)
  3. Fingerprint data (@fluid-app/fairshare)
  4. Event queues (@fluid-app/fairshare)

Example integration with Fluid SDK:

import { initializeFluid } from "@fluid-app/fluid";

// Initialize Fluid SDK
initializeFluid({
  fluidShop: "your-shop-id",
  // Attribution is now handled server-side via the settings API
});

// You can still access other stored data if needed
const storageManager = new StorageManager();
const cartData = storageManager.getObject(STORAGE_KEYS.CART);
console.log("Current cart:", cartData);

Storage Keys Reference

The package includes predefined storage keys for consistency:

const STORAGE_KEYS = {
  ATTRIBUTION: "fs_attribution", // Attribution data
  ATTRIBUTION_CACHE: "fs_attribution_cache", // Cached attribution data
  CART: "fs_cart", // Cart data
  CLIENT_SETTINGS: "fs_client_settings",
  CONFIG: "fs_config", // SDK configuration
  COUNTRY: "fs_country", // Country code
  EVENT_QUEUE: "fs_event_queue", // Queued analytics events
  FINGERPRINT: "fs_fingerprint", // Browser fingerprint
  LOCALE: "fs_locale", // Locale
  SERVER_SETTINGS: "fs_server_settings", // Server settings
  SESSION: "fs_session", // Session token
  USER_ID: "fs_user_id", // User ID for identified users
};

The package includes predefined cookie keys:

const COOKIE_KEYS = {
  COUNTRY: "country", // Country code cookie (often set by Cloudflare)
  LOCALE: "locale", // Locale cookie
};

API Reference

StorageManager Class

class StorageManager {
  // Store string data
  setItem(key: string, value: string): void;

  // Retrieve string data
  getItem(key: string): string | null;

  // Remove data
  removeItem(key: string): void;

  // Store complex data (automatically serialized to JSON)
  setObject<T>(key: string, value: T): void;

  // Retrieve complex data (automatically parsed from JSON)
  getObject<T>(key: string): T | null;

  // Get country code from storage or cookies
  getCountryCode(): string;

  // Set country code
  setCountryCode(countryCode: string): void;

  // Get locale from storage or cookies
  getLocale(): string | null;

  // Set locale
  setLocale(locale: string): void;
}

Utility Functions

// Check if code is running in a browser environment
function isBrowser(): boolean;

// Get a cookie value by name
function getCookie(name: string): string | null;

Server-Side Rendering Compatibility

This package safely handles both browser and server environments, making it compatible with frameworks like Next.js:

// In a Next.js component (works in both client and server components)
import { StorageManager } from "@fluid-app/storage-manager";

export default function CartIndicator() {
  // Create a client component that uses localStorage safely
  "use client";

  const [itemCount, setItemCount] = useState(0);

  useEffect(() => {
    const storageManager = new StorageManager();
    const cart = storageManager.getObject("fs_cart");
    setItemCount(cart?.items?.length || 0);
  }, []);

  return <div>Cart Items: {itemCount}</div>;
}

License

MIT

版本与发布记录

@fluid-app/storage-manager 共发布 13 个版本,首次发布于 2025/03/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.2latest2025/07/181.3 年前
0.3.12025/07/161.3 年前
0.3.02025/07/161.3 年前
0.2.42025/07/071.4 年前
0.2.32025/06/111.4 年前
0.2.22025/06/071.4 年前

关于 @fluid-app/storage-manager 的常见问题

@fluid-app/storage-manager 是什么?

Storage management utilities for Fluid Commerce applications

如何安装 @fluid-app/storage-manager?

在项目目录运行 npm install @fluid-app/storage-manager,或使用 pnpm add / yarn add / bun add。

@fluid-app/storage-manager 支持 TypeScript 吗?

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

@fluid-app/storage-manager 支持 ESM 还是 CommonJS?

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

@fluid-app/storage-manager 可以商用吗?

@fluid-app/storage-manager 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fluid-app/storage-manager 的下载量有多少?

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

@fluid-app/storage-manager 还在维护吗?

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