@gadgetinc/core

中文文档
0.15.1发布于 2025/12/17MITTypeScriptESM + CJS零依赖近 30 天下载 4万

This package contains core types and interfaces that all Gadget client implementations must conform to. @gadgetinc/core defines the contracts for API clients, connections, model managers, records, and operations that are shared across all generated Gadg

原始使用文档
文档目录13 节
  1. Overview
  2. Key Exports
  3. Client & Connection Types
  4. Model & Record Types
  5. Action & Function Types
  6. Query & Filter Types
  7. Utility Types
  8. Architecture
  9. Symbols for Internal State
  10. Change Tracking
  11. Pagination
  12. For Gadget Developers
  13. Publishing

Core

This package contains core types and interfaces that all Gadget client implementations must conform to. @gadgetinc/core defines the contracts for API clients, connections, model managers, records, and operations that are shared across all generated Gadget clients.

Note that developers shouldn't need to use this package directly and should instead use their generated client plus framework-specific binding packages like @gadgetinc/react.

Overview

@gadgetinc/core provides the type system foundation for the Gadget framework. It exports TypeScript types and interfaces that:

  • Define what a Gadget API client looks like (AnyClient)
  • Specify how connections to Gadget backends work (AnyConnection)
  • Describe model managers and their operations (AnyModelManager, AnyInternalModelManager)
  • Define record shapes and behavior (GadgetRecord, GadgetRecordList)
  • Provide filter, sort, and selection types for querying data
  • Declare action and function metadata structures

Key Exports

Client & Connection Types

  • AnyClient - Interface for any Gadget app's API client instance. Defines methods like query(), mutate(), transaction(), and properties like connection and internal.
  • AnyConnection - Interface for managing the connection to a Gadget backend, including authentication, GraphQL client access, and transaction support.
  • AnyCoreImplementation - Interface for core utility functions like GadgetRecord construction, variable disambiguation, and error handling.

Model & Record Types

  • AnyModelManager - Generic interface representing one model's API (e.g., api.widget). Provides methods like findOne(), findMany(), findFirst().
  • AnyInternalModelManager - Interface for internal API model managers used within Gadget backend code.
  • GadgetRecord - Represents one record returned from a Gadget API call. Includes helper methods for change tracking, field access, and serialization.
  • GadgetRecordList - An array of GadgetRecord instances with built-in pagination support (hasNextPage, nextPage(), etc.).

Action & Function Types

  • ActionFunction - Metadata and types for model actions (create, update, delete, custom actions).
  • BulkActionFunction - Metadata for bulk operations on multiple records.
  • GlobalActionFunction - Metadata for global actions not tied to a specific model.
  • ViewFunction - Metadata for backend view queries.

Query & Filter Types

The package exports comprehensive type definitions for querying Gadget data:

  • Filter types: IDFilter, StringFilter, IntFilter, FloatFilter, DateTimeFilter, JSONFilter, BooleanFilter, StateFilter, etc.
  • AnyFilter - Union type for all filter types, supporting AND, OR, and NOT combinators.
  • AnySort - Type for sort orders, including field-based sorting and vector similarity sorting.
  • FieldSelection - Type for selecting specific fields from GraphQL responses.
  • Options types: FindManyOptions, BaseFindOptions, InternalFindOptions, etc.

Utility Types

  • Select<Schema, Selection> - Type utility for extracting a subset of a schema based on a selection.
  • DefaultSelection<Available, Options, Defaults> - Type utility for defaulting selections when none are provided.
  • AvailableSelection<Schema> - Converts a schema type into valid selection options.
  • Scalar types: GadgetID, DateTime, EmailAddress, URLString, JSONBlob, CurrencyAmount, etc.

Architecture

The @gadgetinc/core package uses TypeScript's structural typing to define contracts without imposing implementation details. Generated Gadget clients implement these interfaces, ensuring consistent APIs across all apps.

Symbols for Internal State

The package exports symbols used to attach internal state to client objects:

export const $modelRelationships = Symbol.for("gadget/modelRelationships");
export const $coreImplementation = Symbol.for("gadget/coreImplementation");

These symbols allow generated clients to store metadata and implementation details without polluting the public API surface.

Change Tracking

GadgetRecord includes built-in change tracking to detect modifications:

record.changed(); // Returns true if any field has changed
record.changes(); // Returns { field: { current, previous } } for changed fields
record.flushChanges(); // Marks current state as "saved"
record.revertChanges(); // Reverts to last saved state

This enables optimistic UI updates and efficient data synchronization.

Pagination

GadgetRecordList extends Array<GadgetRecord> with pagination helpers:

list.hasNextPage; // boolean
list.endCursor; // string cursor for next page
await list.nextPage(); // Fetches next page of results

This implements Relay-style cursor pagination across all Gadget APIs.

For Gadget Developers

This package is the single source of truth for type definitions across the Gadget ecosystem. When adding new features:

  1. Define types here first in @gadgetinc/core
  2. Have generated clients implement the new types
  3. Update framework packages (@gadgetinc/react, etc.) to leverage the new types
  4. Ensure backward compatibility or coordinate breaking changes

The testing/ subdirectory exports test utilities like mockUrqlClient for use in other packages' test suites.

Publishing

After releasing a new version of @gadgetinc/core, ensure that:

  1. Generated clients are regenerated to use the new version
  2. Framework packages like @gadgetinc/react and @gadgetinc/client-hooks are updated
  3. The PlatformDependencyVersions variable in Gadget mainline is updated

版本与发布记录

@gadgetinc/core 共发布 16 个版本,首次发布于 2025/10/08;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.15.1latest2025/12/1710 个月前
0.15.02025/12/1211 个月前
0.14.02025/11/131 年前
0.13.02025/10/301 年前
0.12.02025/10/291 年前
0.11.02025/10/281 年前

关于 @gadgetinc/core 的常见问题

@gadgetinc/core 是什么?

This package contains core types and interfaces that all Gadget client implementations must conform to. @gadgetinc/core defines the contracts for API clients, connections, model managers, records, and operations that are shared across all generated Gadg

如何安装 @gadgetinc/core?

在项目目录运行 npm install @gadgetinc/core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@urql/core(>=4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gadgetinc/core 支持 TypeScript 吗?

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

@gadgetinc/core 支持 ESM 还是 CommonJS?

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

@gadgetinc/core 可以商用吗?

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

@gadgetinc/core 的下载量有多少?

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

@gadgetinc/core 还在维护吗?

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