@ confessed/ sanity-types (已关闭)
用于联邦学院Sanity CMS后端的TypeScript类型包. 此软件包为所有的Sanity schema类型提供了自动生成的TypeScript定义,确保类型安全贯穿于您前端应用程序和API集成.
快速启动
安装
npm install @confed/sanity-types
基本用途
import type {Program, Event, Person} from '@confed/sanity-types'
// Use types in your components or API functions
const program: Program = {
_id: 'program-123',
_type: 'program',
_createdAt: '2024-01-01T00:00:00Z',
_updatedAt: '2024-01-01T00:00:00Z',
_rev: 'rev-123',
name: 'Computer Programming',
slug: {_type: 'slug', current: 'computer-programming'},
// ... other required fields
}
// Type-safe event handling
const handleEvent = (event: Event) => {
console.log(event.title) // Full IntelliSense support
console.log(event.startDate) // Type-safe date access
}
高级使用
// Import multiple types
import type {
Program,
School,
Campus,
Person,
SanityImageAsset,
PtBasic
} from '@confed/sanity-types'
// Use with React components
interface ProgramCardProps {
program: Program
school?: School
}
const ProgramCard: React.FC<ProgramCardProps> = ({ program, school }) => {
return (
<div>
<h2>{program.name}</h2>
{school && <p>School: {school.name}</p>}
{program.description && (
<div>{/* Portable text content */}</div>
)}
</div>
)
}
// Use with API functions
async function fetchProgram(id: string): Promise<Program | null> {
const response = await fetch(`/api/programs/${id}`)
if (!response.ok) return null
return response.json() as Promise<Program>
}
可用类型
内容类型
Program- 详细、要求和元数据的学术方案School- 学院内的学术学校/部门Campus- 自然校园地点Area- 研究或感兴趣的领域Person- 学院、工作人员和其他个人Event- 学院活动、讲习班和活动NewsArticle- 新闻和公告Webpage- 一般网页和内容Testimonial- 学生和利益相关者的证词Credential- 学术证书和证书ProgramIntake- 方案接收期和执行方法AdmissionRequirements- 方案录取标准Duration- 方案期限和时间要求ProgramFile- 与程序有关的文件和文件
导航结构( R)
Menu- 导航菜单(标题、页脚、侧边栏、移动)MenuItem- 附有链接和分项目的个人菜单项目Link- 与元数据的内部和外部联系Redirect- 用于内容迁移的 URL 重定向
内容块与媒体
PtBasic- 有丰富格式的便携式文本内容Figure- 有无障碍功能的图像AccessibleImage- 可访问图像组件Button- 呼叫行动按钮YoutubeVideo- 嵌入的YouTube视频
SEO 元数据( M)
Seo- 搜索引擎优化元数据Slug- 方便URL识别器
危险系统类型
SanityImageAsset- 有元数据的图像资产SanityFileAsset- 文件资产SanityImageHotspot- 图像热点坐标SanityImageCrop- 图像裁剪数据SanityImageMetadata- 图像元数据,包括尺寸和调色板Geopoint- 地理坐标MediaTag- 媒体标记系统
如何运作
此软件包使用 卫生类型Gen 从您的 Sanity 方案自动生成 TypeScript 定义。 这些类型来自:
- Schema 定义 (单位:千美元)
schema.json) - 包含所有内容类型计划 - GROQ 查询 - 任何类型脚本/Java脚本文件
src/包含 GROQ 查询的目录
完整工作流程 :
- Schema 变化 您修改了 Sanity 计划
- 本地生成 + 前承诺钩会自动生成类型
- 提交 · 共同承担的类型和计划变化
- CI/CD 触发器 - GitHub 动作工作流程在推上运行
- 核查 · 重新生成的用于核查的CI类型
- 出版 + 如果类型改变, 版本会撞出并发布到 npm
- 安全部署 · 单独工作流程部署 Sanity Studio(由
.github/workflows/deploy-sanity.yml(中文(简体) ) - 无通知提交 ——CI从不承诺回到你的仓库
生成的类型提供:
- 所有内容类型的完整类型安全
- 智能和自动完成您的 IDE
- 编辑时间错误检查内容结构
- 交叉引用文档的参考类型安全性
不断更新类型
此软件包中的类型是自动生成的,并且应当 从未手动编辑。此工程在每次任务上都配置自动类型生成,所以类型自动保持当前状态.
当前状况 : 类型通过Husky预承诺钩在每次承诺上被再生成,当检测到变化时通过CI/CD自动发布到npm上.
工作流量 :
- 地方发展: 在每次犯罪前自动生成的类型
- 中心/CD: 如果检测到变化, 类型已核实并公布为 npm
- 未提交通知 : CI 从不返回您的仓库
为了保持它们的历史:
1. 在 Schema 更改后生成类型
每当修改您的 Sanity 计划时, 都会重新生成类型 :
# From the root directory
npm run gen:types
此命令运行 :
npm run schema:extract- 提取最新计划npm run typegen- 生成类型脚本定义
2. 自动生成(建议)
在您的开发工作流程中设置自动类型生成 :
# Watch for schema changes and regenerate types
npm run typegen -- --watch
3. 承诺前胡克 (当前积极
这个工程有Husky预承诺钩已经配置和工作. 这些类型在每次任务时自动重生:
# The hook is already set up at .husky/pre-commit
# It runs: npm run gen:types
# To verify it's working, make a schema change and commit:
git add .
git commit -m "your message"
# Types will be automatically regenerated before commit
说明: 承诺前钩已经在这个计划中活动,所以不需要手动设置.
4. CI/CD一体化(自动出版)
这个项目具有自动的CI/CD工作流程,处理不同的关切:
类型 出版 (单位:千美元).github/workflows/publish-types.yml:
# Automatically runs on schema changes:
# - Generates types in CI for verification
# - Bumps version and publishes to npm if types changed
# - Does NOT commit back to repository (no circular commits)
卫生工作室部署 (单位:千美元).github/workflows/deploy-sanity.yml:
# Automatically deploys Sanity Studio on schema/config changes:
# - Builds the Sanity Studio
# - Deploys to Sanity hosting
# - Separate from types publishing workflow
说明: CI工作流程因关注而分离. 类型生成和承诺通过承诺前的钩子在当地处理.
配置
类型生成配置于 sanity-typegen.json 项目根 :
{
"schema": "./schema.json",
"path": ["./src/**/*.{ts,tsx,js,jsx}"],
"generates": "./packages/sanity-types/sanity.types.ts",
"overloadClientMethods": true
}
解决问题
未更新类型
- 确保你运行
npm run gen:types从项目根 - 检查您的方案更改是否已被保存
- 校验
sanity-typegen.json配置
类型出错
- 方案改变后的再生类型
- 请检查access-date=中的日期值 (帮助)
- 确保提供所有所需字段
构建问题
- 清除您的类型脚本缓存
- 重新启动您的开发服务器
- 校验软件包在您的工作空间中正确链接
捐款
- 永不编辑
sanity.types.ts直接 - 这是自动生成的 - 在相应的 schema 文件中做更改
- 运行
npm run gen:types以更新类型 - 执行计划更改和生成类型
依赖关系
sanity- 环境卫生管理框架@sanity/typegen- 类型生成工具typescript- 类型脚本编译器
许可证
这个包是联邦学院后端项目的一部分,并遵循同样的许可条件.