@authhero/adapter-interfaces

中文文档
4.26.0发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2344

This package provides a set of interfaces used for creating adapters for AuthHero. Adapters are used to connect AuthHero to various services, such as databases, email services, and more.

原始使用文档
文档目录3 节
  1. Database adapters
  2. Utilities
  3. Passthrough Adapter

@authhero/adapter-interfaces

This package provides a set of interfaces used for creating adapters for AuthHero. Adapters are used to connect AuthHero to various services, such as databases, email services, and more.

Database adapters

The database adapters follow these rules:

  • The created_at and modified_at fields are handled by the adapter when creating or updating a record.
  • The tenant_id field is not part of the entities sent to the adapter.
  • The entity id is passed to the adapter.
  • The adapter can pass objects such as authParams. These objects will typically be flattened as part of the adapter.
  • The types used in the adapters should be inferred from zod schemas to ensure type safety at runtime.
  • The id column and entity should typically start with the entity name, e.g. user_id for the user entity. We follow the naming from auth0 so it might not always be consistent.
  • The adapter should typically expose the following methods.
    • create(tenant_id: string, entity: Entity): Promise<Entity>
    • update(tenant_id: string, entity: Entity): Promise<boolean>
    • remove(tenant_id: string, entity: Entity): Promise<boolean>
    • get(tenant_id: string, [entity_id]: string): Promise<Entity | null>
    • list(tenant_id: string, query: Query): Promise<Entity[]>

Utilities

Passthrough Adapter

The createPassthroughAdapter utility allows you to sync write operations to multiple adapter implementations. This is useful for:

  • Multi-destination logging: Write logs to both a database and an analytics service
  • Cache warming: Sync writes to multiple cache layers
  • Database migration: Gradually migrate data from one database to another

Basic Usage

import {
  createPassthroughAdapter,
  LogsDataAdapter,
} from "@authhero/adapter-interfaces";

// Primary adapter - all reads come from here, writes go here first
const primaryAdapter = createDatabaseLogsAdapter();

// Secondary adapter for analytics
const analyticsAdapter = createAnalyticsLogsAdapter();

// Create passthrough adapter
const logsAdapter = createPassthroughAdapter<LogsDataAdapter>({
  primary: primaryAdapter,
  secondaries: [
    {
      adapter: { create: analyticsAdapter.create },
      onError: (err) => console.error("Analytics sync failed:", err),
    },
  ],
});

// Usage
await logsAdapter.create("tenant-1", log); // Writes to both
const result = await logsAdapter.list("tenant-1"); // Reads from primary only

Configuration Options

interface PassthroughConfig<T> {
  // Primary adapter - all reads come from here, writes go here first
  primary: T;

  // Secondary adapters to sync writes to
  secondaries: {
    // Partial adapter implementation - only implemented methods will be called
    adapter: Partial<T>;

    // If true, wait for the secondary write to complete (default: false)
    blocking?: boolean;

    // Called when a secondary write fails
    onError?: (error: Error, method: string, args: unknown[]) => void;
  }[];

  // Methods to sync to secondaries (default: ["create", "update", "remove", "delete", "set"])
  syncMethods?: string[];
}

Write-Only Adapter Helper

Use createWriteOnlyAdapter to clearly indicate which methods a secondary implements:

import {
  createWriteOnlyAdapter,
  createPassthroughAdapter,
} from "@authhero/adapter-interfaces";

const logsAdapter = createPassthroughAdapter({
  primary: databaseAdapter,
  secondaries: [
    {
      adapter: createWriteOnlyAdapter({
        create: async (tenantId, log) => {
          // Custom write logic (e.g., webhook, analytics)
        },
      }),
    },
  ],
});

Use Cases

Logs to multiple destinations:

const logsAdapter = createPassthroughAdapter({
  primary: databaseLogsAdapter,
  secondaries: [
    { adapter: { create: analyticsEngineAdapter.create } },
    { adapter: { create: r2SqlAdapter.create } },
  ],
});

Cache with fallback sync:

const cacheAdapter = createPassthroughAdapter({
  primary: redisCacheAdapter,
  secondaries: [{ adapter: memcachedAdapter, blocking: true }],
  syncMethods: ["set", "delete"],
});

版本与发布记录

@authhero/adapter-interfaces 共发布 272 个版本,首次发布于 2024/07/17;过去 12 个月发布了 142 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.26.0latest2026/11/052 天前
4.25.12026/11/043 天前
4.25.02026/10/308 天前
4.24.02026/10/299 天前
4.23.02026/10/2612 天前
4.22.02026/10/2315 天前

关于 @authhero/adapter-interfaces 的常见问题

@authhero/adapter-interfaces 是什么?

This package provides a set of interfaces used for creating adapters for AuthHero. Adapters are used to connect AuthHero to various services, such as databases, email services, and more.

如何安装 @authhero/adapter-interfaces?

在项目目录运行 npm install @authhero/adapter-interfaces,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@hono/zod-openapi(^1.4.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@authhero/adapter-interfaces 支持 TypeScript 吗?

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

@authhero/adapter-interfaces 支持 ESM 还是 CommonJS?

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

@authhero/adapter-interfaces 可以商用吗?

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

@authhero/adapter-interfaces 的下载量有多少?

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

@authhero/adapter-interfaces 还在维护吗?

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