openapi-to-typescript by softaworks/agent-toolkit
npx skills add https://github.com/softaworks/agent-toolkit --skill openapi-to-typescript将 OpenAPI 3.0 规范转换为 TypeScript 接口和类型守卫。
输入: OpenAPI 文件(JSON 或 YAML) 输出: 包含接口和类型守卫的 TypeScript 文件
components/schemas 提取模式paths 提取端点(请求/响应类型)types/api.ts)在处理前检查:
- 字段 "openapi" 必须存在且以 "3.0" 开头
- 字段 "paths" 必须存在
- 字段 "components.schemas" 必须存在(如果有类型的话)
如果无效,报告错误并停止。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| OpenAPI | TypeScript |
|---|---|
string | string |
number | number |
integer | number |
boolean | boolean |
null | null |
| 格式 | TypeScript |
|---|---|
uuid | string (注释 UUID) |
date | string (注释 date) |
date-time | string (注释 ISO) |
email | string (注释 email) |
uri | string (注释 URI) |
对象:
// OpenAPI: type: object, properties: {id, name}, required: [id]
interface Example {
id: string; // 必需: 没有 ?
name?: string; // 可选: 有 ?
}
数组:
// OpenAPI: type: array, items: {type: string}
type Names = string[];
枚举:
// OpenAPI: type: string, enum: [active, draft]
type Status = "active" | "draft";
oneOf (联合类型):
// OpenAPI: oneOf: [{$ref: Cat}, {$ref: Dog}]
type Pet = Cat | Dog;
allOf (交叉类型/继承):
// OpenAPI: allOf: [{$ref: Base}, {type: object, properties: ...}]
interface Extended extends Base {
extraField: string;
}
/**
* 自动生成自:{source_file}
* 生成于:{timestamp}
*
* 请勿手动编辑 - 从 OpenAPI 模式重新生成
*/
对于 components/schemas 中的每个模式:
export interface Product {
/** 产品唯一标识符 */
id: string;
/** 产品标题 */
title: string;
/** 产品价格 */
price: number;
/** 创建时间戳 */
created_at?: string;
}
required[] 中的字段没有 ?required[] 之外的字段有 ?对于 paths 中的每个端点:
// GET /products - 查询参数
export interface GetProductsRequest {
page?: number;
limit?: number;
}
// GET /products - 响应 200
export type GetProductsResponse = ProductList;
// POST /products - 请求体
export interface CreateProductRequest {
title: string;
price: number;
}
// POST /products - 响应 201
export type CreateProductResponse = Product;
命名约定:
{Method}{Path}Request 用于参数/请求体{Method}{Path}Response 用于响应为每个主要接口生成一个类型守卫:
export function isProduct(value: unknown): value is Product {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
typeof (value as any).id === 'string' &&
'title' in value &&
typeof (value as any).title === 'string' &&
'price' in value &&
typeof (value as any).price === 'number'
);
}
类型守卫规则:
typeof value === 'object' && value !== null'field' in valuetypeofArray.isArray().includes()export interface ApiError {
status: number;
error: string;
detail?: string;
}
export function isApiError(value: unknown): value is ApiError {
return (
typeof value === 'object' &&
value !== null &&
'status' in value &&
typeof (value as any).status === 'number' &&
'error' in value &&
typeof (value as any).error === 'string'
);
}
当遇到 {"$ref": "#/components/schemas/Product"} 时:
Product)// OpenAPI: items: {$ref: "#/components/schemas/Product"}
// TypeScript:
items: Product[] // 引用,非内联
输入(OpenAPI):
{
"openapi": "3.0.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"email": {"type": "string", "format": "email"},
"role": {"type": "string", "enum": ["admin", "user"]}
},
"required": ["id", "email", "role"]
}
}
},
"paths": {
"/users/{id}": {
"get": {
"parameters": [{"name": "id", "in": "path", "required": true}],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
}
}
}
}
}
}
}
输出(TypeScript):
/**
* 自动生成自:api.openapi.json
* 生成于:2025-01-15T10:30:00Z
*
* 请勿手动编辑 - 从 OpenAPI 模式重新生成
*/
// ============================================================================
// 类型
// ============================================================================
export type UserRole = "admin" | "user";
export interface User {
/** UUID */
id: string;
/** Email */
email: string;
role: UserRole;
}
// ============================================================================
// 请求/响应类型
// ============================================================================
export interface GetUserByIdRequest {
id: string;
}
export type GetUserByIdResponse = User;
// ============================================================================
// 类型守卫
// ============================================================================
export function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
typeof (value as any).id === 'string' &&
'email' in value &&
typeof (value as any).email === 'string' &&
'role' in value &&
['admin', 'user'].includes((value as any).role)
);
}
// ============================================================================
// 错误类型
// ============================================================================
export interface ApiError {
status: number;
error: string;
detail?: string;
}
export function isApiError(value: unknown): value is ApiError {
return (
typeof value === 'object' &&
value !== null &&
'status' in value &&
typeof (value as any).status === 'number' &&
'error' in value &&
typeof (value as any).error === 'string'
);
}
| 错误 | 操作 |
|---|---|
| OpenAPI 版本 != 3.0.x | 报告仅支持 3.0 |
| $ref 未找到 | 列出缺失的引用 |
| 未知类型 | 使用 unknown 并警告 |
| 循环引用 | 使用带有延迟引用的类型别名 |
每周安装量
563
代码仓库
GitHub 星标数
1.2K
首次出现
2026年1月20日
安全审计
安装于
claude-code404
codex403
gemini-cli403
cursor403
opencode399
cline397
Converts OpenAPI 3.0 specifications to TypeScript interfaces and type guards.
Input: OpenAPI file (JSON or YAML) Output: TypeScript file with interfaces and type guards
components/schemaspaths (request/response types)types/api.ts in current directory)Check before processing:
- Field "openapi" must exist and start with "3.0"
- Field "paths" must exist
- Field "components.schemas" must exist (if there are types)
If invalid, report the error and stop.
| OpenAPI | TypeScript |
|---|---|
string | string |
number | number |
integer | number |
boolean | boolean |
| Format | TypeScript |
|---|---|
uuid | string (comment UUID) |
date | string (comment date) |
date-time | string (comment ISO) |
email | string (comment email) |
Object:
// OpenAPI: type: object, properties: {id, name}, required: [id]
interface Example {
id: string; // required: no ?
name?: string; // optional: with ?
}
Array:
// OpenAPI: type: array, items: {type: string}
type Names = string[];
Enum:
// OpenAPI: type: string, enum: [active, draft]
type Status = "active" | "draft";
oneOf (Union):
// OpenAPI: oneOf: [{$ref: Cat}, {$ref: Dog}]
type Pet = Cat | Dog;
allOf (Intersection/Extends):
// OpenAPI: allOf: [{$ref: Base}, {type: object, properties: ...}]
interface Extended extends Base {
extraField: string;
}
/**
* Auto-generated from: {source_file}
* Generated at: {timestamp}
*
* DO NOT EDIT MANUALLY - Regenerate from OpenAPI schema
*/
For each schema in components/schemas:
export interface Product {
/** Product unique identifier */
id: string;
/** Product title */
title: string;
/** Product price */
price: number;
/** Created timestamp */
created_at?: string;
}
required[] have no ?required[] have ?For each endpoint in paths:
// GET /products - query params
export interface GetProductsRequest {
page?: number;
limit?: number;
}
// GET /products - response 200
export type GetProductsResponse = ProductList;
// POST /products - request body
export interface CreateProductRequest {
title: string;
price: number;
}
// POST /products - response 201
export type CreateProductResponse = Product;
Naming convention:
{Method}{Path}Request for params/body{Method}{Path}Response for responseFor each main interface, generate a type guard:
export function isProduct(value: unknown): value is Product {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
typeof (value as any).id === 'string' &&
'title' in value &&
typeof (value as any).title === 'string' &&
'price' in value &&
typeof (value as any).price === 'number'
);
}
Type guard rules:
typeof value === 'object' && value !== null'field' in valuetypeofArray.isArray().includes()export interface ApiError {
status: number;
error: string;
detail?: string;
}
export function isApiError(value: unknown): value is ApiError {
return (
typeof value === 'object' &&
value !== null &&
'status' in value &&
typeof (value as any).status === 'number' &&
'error' in value &&
typeof (value as any).error === 'string'
);
}
When encountering {"$ref": "#/components/schemas/Product"}:
Product)// OpenAPI: items: {$ref: "#/components/schemas/Product"}
// TypeScript:
items: Product[] // reference, not inline
Input (OpenAPI):
{
"openapi": "3.0.0",
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"email": {"type": "string", "format": "email"},
"role": {"type": "string", "enum": ["admin", "user"]}
},
"required": ["id", "email", "role"]
}
}
},
"paths": {
"/users/{id}": {
"get": {
"parameters": [{"name": "id", "in": "path", "required": true}],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/User"}
}
}
}
}
}
}
}
}
Output (TypeScript):
/**
* Auto-generated from: api.openapi.json
* Generated at: 2025-01-15T10:30:00Z
*
* DO NOT EDIT MANUALLY - Regenerate from OpenAPI schema
*/
// ============================================================================
// Types
// ============================================================================
export type UserRole = "admin" | "user";
export interface User {
/** UUID */
id: string;
/** Email */
email: string;
role: UserRole;
}
// ============================================================================
// Request/Response Types
// ============================================================================
export interface GetUserByIdRequest {
id: string;
}
export type GetUserByIdResponse = User;
// ============================================================================
// Type Guards
// ============================================================================
export function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
typeof (value as any).id === 'string' &&
'email' in value &&
typeof (value as any).email === 'string' &&
'role' in value &&
['admin', 'user'].includes((value as any).role)
);
}
// ============================================================================
// Error Types
// ============================================================================
export interface ApiError {
status: number;
error: string;
detail?: string;
}
export function isApiError(value: unknown): value is ApiError {
return (
typeof value === 'object' &&
value !== null &&
'status' in value &&
typeof (value as any).status === 'number' &&
'error' in value &&
typeof (value as any).error === 'string'
);
}
| Error | Action |
|---|---|
| OpenAPI version != 3.0.x | Report that only 3.0 is supported |
| $ref not found | List missing refs |
| Unknown type | Use unknown and warn |
| Circular reference | Use type alias with lazy reference |
Weekly Installs
563
Repository
GitHub Stars
1.2K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code404
codex403
gemini-cli403
cursor403
opencode399
cline397
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
OpenAPI 转 TypeScript 工具 - 自动生成 API 接口与类型守卫
563 周安装
数据库模式设计器 - 内置最佳实践,自动生成生产级SQL/NoSQL数据库架构
564 周安装
Rust Unsafe代码检查器 - 安全使用Unsafe Rust的完整指南与最佳实践
564 周安装
.NET并发编程模式指南:async/await、Channels、Akka.NET选择决策树
565 周安装
韩语语法检查器 - 基于国立国语院标准的拼写、空格、语法、标点错误检测与纠正
565 周安装
技能安全扫描器 - 检测Claude技能安全漏洞,防范提示注入与恶意代码
565 周安装
null | null |
uri | string (comment URI) |