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

@h-ai/api-contract

v0.1.0-alpha.25

Published

Hai Framework public API contracts powered by oRPC and Zod.

Downloads

1,977

Readme

@h-ai/api-contract

hai-framework 的公共 HTTP API 契约包,使用 oRPC contract + Zod 作为唯一真相源。

能力概览

  • apiContract.iamapiContract.storageapiContract.aiapiContract.payment:领域级 contract。
  • apiContract.create():按应用场景组合启用的领域 contract。
  • apiContract.route():定义自定义 HTTP 路由元数据,供应用级 contract 复用。
  • apiContract.haiResultSchema() / apiContract.voidResultSchema / apiContract.paginatedSchema():公共 DTO Schema 工厂。
  • 公共 DTO Schema:所有 HTTP 输出统一包装为 HaiResult<T>
  • IAM token DTO 兼容 httpOnly cookie 模式:响应体中 refreshToken 可能不存在,由服务端 Set-Cookie 管理。

快速开始

import { apiContract } from '@h-ai/api-contract'

export const contract = apiContract.create({
  iam: apiContract.iam,
  storage: apiContract.storage,
  ai: false,
})

自定义应用 contract 也从本包定义路由,不依赖 @h-ai/serv

import { apiContract } from '@h-ai/api-contract'
import { z } from 'zod'

const PingOutput = apiContract.haiResultSchema(z.object({ pong: z.boolean() }))

export const appContract = {
  ping: apiContract
    .route({ method: 'POST', path: '/app/ping', operationId: 'app.ping', tags: ['app'] })
    .output(PingOutput),
}

API 契约

import { apiContract } from '@h-ai/api-contract'

const myContract = apiContract.create({ iam: apiContract.iam, storage: apiContract.storage, ai: apiContract.ai })

myContract.iam.auth.login
myContract.storage.presignedUrls.createUpload
myContract.ai.chats.createCompletion

客户端通过 @h-ai/api-client 直接调用嵌套方法:

import { apiClient } from '@h-ai/api-client'

await apiClient.init({ baseUrl: 'https://api.example.com/api/v1' })
const login = await apiClient.iam.auth.login({ identifier: 'alice', password: 'secret' })
await apiClient.close()

服务端通过 @h-ai/serv 挂载:

import { apiContract } from '@h-ai/api-contract'
import { serv } from '@h-ai/serv'

const contract = apiContract.create({ iam: apiContract.iam, storage: apiContract.storage, ai: apiContract.ai })

const app = serv.createApp({
  contract,
  procedures,
  http: { apiPrefix: '/api/v1' },
})

API 概览

  • apiContract.create(options):过滤 false / undefined 领域并组合应用级 contract。
  • apiContract.route(routeConfig):定义自定义 endpoint 的 method/path/operationId/tags 等路由元数据。
  • apiContract.haiResultSchema(dataSchema) / apiContract.voidResultSchema / apiContract.paginatedSchema(itemSchema):公共 DTO Schema 工厂。
  • Iam*Schema / Storage*Schema / Ai*Schema / Payment*Schema:领域输入 / 输出 DTO Schema。

配置

本包无运行时配置,不读取环境变量。

错误处理

Contract 只描述输入输出结构。业务错误由 procedure 返回 HaiResult<T> 表达,客户端不需要捕获业务异常。

测试

pnpm --filter @h-ai/api-contract test
pnpm --filter @h-ai/api-contract typecheck

License

Apache-2.0