npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@anker-in/lib

v1.1.3

Published

Unified Anker headless library with shared context, Shopify integration, and cart utilities

Downloads

228

Readme

@anker-in/lib

便利导出包,整合所有 Anker headless 功能。用户只需安装和导入这一个包即可使用所有功能。

安装

pnpm add @anker-in/lib

就这样! 所有依赖(@anker-in/shared@anker-in/cart@anker-in/shopify)会自动安装。

快速开始

1. 设置 HeadlessProvider

import { HeadlessProvider } from '@anker-in/lib'

const headlessConfig = {
  storefrontToken: 'your-token',
  storeDomain: 'your-store.myshopify.com',
  locale: 'en-US',
  comboMetafieldsNamespace: 'custom',
  cartIdCookieName: 'cart_id',
  brand: 'Anker',
  appName: 'My App'
}

function App() {
  return (
    <HeadlessProvider headlessConfig={headlessConfig}>
      <YourApp />
    </HeadlessProvider>
  )
}

2. 使用所有功能(从一个包导入!)

import {
  // Context
  useHeadlessContext,
  
  // Shopify 功能
  useProduct,
  createCart,
  
  // Cart 功能
  useBuyNow,
  useAddToCart,
  useUpdateCartLines
} from '@anker-in/lib'

function ProductPage() {
  const { storefrontToken, storeDomain } = useHeadlessContext()
  const product = useProduct(productId)
  const { trigger: buyNow } = useBuyNow()
  
  const handleBuyNow = () => {
    buyNow({
      lineItems: [
        {
          variant: { id: product.variants[0].id },
          quantity: 1
        }
      ]
    })
  }
  
  return (
    <div>
      <h1>{product?.title}</h1>
      <button onClick={handleBuyNow}>立即购买</button>
    </div>
  )
}

架构设计

为了避免循环依赖,采用了分层架构:

          ┌──────────────┐
          │   shared     │ (基础包:Context + Types)
          └──────┬───────┘
                 │
         ┌───────┼────────┐
         │       │        │
         ↓       ↓        ↓
    ┌────────┬──────┬─────────┐
    │  lib   │ cart │ shopify │
    └────┬───┴──────┴────┬────┘
         │               │
         └───────┬───────┘
                 ↓
         (lib 重新导出所有)

包职责

  • @anker-in/shared: 基础包,提供 HeadlessProviderHeadlessConfig
  • @anker-in/shopify: Shopify API 集成和 hooks
  • @anker-in/cart: 购物车功能和 hooks
  • @anker-in/lib: 便利导出包,重新导出所有功能 + 自己的工具函数

为什么这样设计?

问题: 如果 lib 包含 Context,而 cart 使用 lib 的 Context,那么 lib 就不能导出 cart(会形成循环)。

解决: 将 Context 提取到独立的 shared 包,这样:

  • cart 依赖 shared(获取 Context)
  • lib 依赖 shared(获取 Context)
  • lib 依赖 cart(重新导出)
  • ✅ 完全消除循环依赖!

API

HeadlessProvider

提供全局配置的 Context Provider。

<HeadlessProvider headlessConfig={config}>
  {children}
</HeadlessProvider>

useHeadlessContext

获取 Headless 配置的 Hook。

const { storefrontToken, storeDomain, locale, ... } = useHeadlessContext()

HeadlessConfig

type HeadlessConfig = {
  storefrontToken: string        // Shopify Storefront API token
  storeDomain: string            // 店铺域名
  locale: string                 // 语言环境
  comboMetafieldsNamespace: string
  cartIdCookieName: string       // 购物车 ID 的 cookie 名称
  brand: string                  // 品牌名称
  appName: string                // 应用名称
}

导出内容

@anker-in/lib 可以导入:

  • Shared: HeadlessProvider, useHeadlessContext, HeadlessConfig
  • Shopify: 所有 Shopify 功能和 hooks
  • Cart: 所有购物车功能和 hooks
  • Lib 自己的工具: math 等工具函数

简单来说,你只需要从 @anker-in/lib 导入所有内容!