@cerios/openapi-core

中文文档
1.1.1发布于 2026/03/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.3万

Core utilities for parsing and processing OpenAPI specifications. Shared foundation for OpenAPI code generators.

原始使用文档
文档目录20 节
  1. Installation
  2. Features
  3. Usage
  4. API Reference
  5. Types
  6. Spec Parsing
  7. Reference Resolution
  8. Operation Filtering
  9. Name Utilities
  10. Method Naming
  11. Schema Traversal
  12. Header Filters
  13. Pattern Utilities
  14. String Utilities
  15. Content Type Utilities
  16. Config Loader
  17. CLI Utilities
  18. Classes
  19. Batch Execution
  20. License

@cerios/openapi-core

Core utilities for parsing and processing OpenAPI specifications. This package provides the shared foundation for OpenAPI code generators.

Installation

npm install @cerios/openapi-core

Features

  • 🔍 OpenAPI Parsing - Parse OpenAPI 3.0/3.1 YAML and JSON specifications
  • 🔗 Reference Resolution - Resolve $ref references throughout the spec
  • 🏷️ Operation Filtering - Filter operations by tags, paths, and methods
  • 🛠️ Utility Functions - Common utilities for schema processing
  • 📦 Batch Execution - Parallel and sequential execution of generators
  • 🧪 Test Fixtures - Shared OpenAPI fixtures for testing

Usage

import {
    shouldIncludeOperation,
    toPascalCase,
    toCamelCase,
    resolveRefName,
    LRUCache,
    loadOpenAPISpec,
} from "@cerios/openapi-core";

// Load and parse an OpenAPI spec
const spec = await loadOpenAPISpec("openapi.yaml");

// Extract schema name from $ref
const schemaName = resolveRefName("#/components/schemas/User"); // "User"

// Filter operations
const include = shouldIncludeOperation(operation, path, method, {
    includeTags: ["users"],
    excludePaths: ["/internal/*"],
});

// Name utilities
const className = toPascalCase("user-profile"); // "UserProfile"
const methodName = toCamelCase("get-user-by-id"); // "getUserById"

API Reference

Types

  • OpenAPISpec - OpenAPI specification structure
  • OpenAPISchema - Schema definition structure
  • OpenAPIParameter - Parameter definition
  • OpenAPIRequestBody - Request body definition
  • OpenAPIResponse - Response definition
  • OperationFilters - Operation filtering options
  • ExecutionMode - Parallel/sequential execution mode
  • BaseGeneratorOptions - Base options for all generators

Spec Parsing

  • loadOpenAPISpec() - Load and parse an OpenAPI spec file
  • loadOpenAPISpecCached() - Load spec with LRU caching

Reference Resolution

  • resolveRefName() - Extract name from $ref path
  • resolveRequestBodyRef() - Resolve request body references
  • resolveResponseRef() - Resolve response references
  • mergeParameters() - Merge path and operation parameters

Operation Filtering

  • shouldIncludeOperation() - Check if operation matches filters
  • validateFilters() - Validate filter configuration
  • createFilterStatistics() - Create filter statistics tracker
  • formatFilterStatistics() - Format statistics for display

Name Utilities

  • toPascalCase() - Convert string to PascalCase
  • toCamelCase() - Convert string to camelCase
  • resolveRefName() - Extract name from $ref path
  • getOperationName() - Get operation name from operationId or path
  • generateMethodNameFromPath() - Generate method name from path
  • capitalize() - Capitalize first letter

Method Naming

  • pathToPascalCase() - Convert path to PascalCase (e.g., /users/{userId} → UsersByUserId)
  • generateHttpMethodName() - Generate method name from HTTP method + path
  • extractPathParams() - Extract parameter names from path template
  • sanitizeOperationId() - Sanitize operationId for TypeScript
  • sanitizeParamName() - Sanitize parameter name for TypeScript

Schema Traversal

  • extractSchemaRefs() - Extract all $ref names from schema tree
  • expandTransitiveReferences() - Expand to include transitively referenced schemas
  • detectCircularReferences() - Detect circular reference chains
  • topologicalSortSchemas() - Sort schemas by dependencies
  • analyzeSchemaUsage() - Analyze request/response context usage
  • classifyEnumType() - Classify enum values as string/number/boolean/mixed

Header Filters

  • shouldIgnoreHeader() - Check if header matches ignore patterns
  • filterHeaders() - Filter headers excluding ignored ones
  • validateIgnorePatterns() - Validate and warn about unmatched patterns

Pattern Utilities

  • stripPrefix() - Strip prefix from schema names
  • stripSuffix() - Strip suffix from strings
  • stripAffixes() - Strip both prefix and suffix
  • stripPathPrefix() - Strip prefix from paths (supports glob)
  • isGlobPattern() - Check if string is a glob pattern

String Utilities

  • escapeJSDoc() - Escape JSDoc content
  • escapeDescription() - Escape description text
  • escapePattern() - Escape regex pattern
  • getPrimaryType() - Get primary type from schema
  • hasMultipleTypes() - Check if schema has multiple types
  • isNullable() - Check if schema is nullable

Content Type Utilities

  • getResponseParseMethod() - Determine response parsing method (json, text, blob, etc.)

Config Loader

  • createConfigLoader() - Factory for type-safe config loaders with cosmiconfig + Zod validation
  • mergeCliWithConfig() - Merge CLI options with config values (CLI takes precedence)

CLI Utilities

  • findSpecFiles() - Find OpenAPI spec files in directory
  • getRandomCeriosMessage() - Get random CLI greeting message

Classes

  • LRUCache - Least Recently Used cache implementation
  • GeneratorError - Base error class for generators
  • SpecValidationError - OpenAPI spec validation errors
  • FileOperationError - File I/O errors
  • ConfigValidationError - Configuration errors
  • CircularReferenceError - Circular reference detection errors
  • CliOptionsError - CLI options validation errors
  • SchemaGenerationError - Schema generation errors

Batch Execution

  • executeBatch() - Execute multiple generators
  • getBatchExitCode() - Calculate exit code from results
  • Generator - Interface for generator implementations

License

MIT

版本与发布记录

@cerios/openapi-core 共发布 5 个版本,首次发布于 2026/02/20;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.1latest2026/03/068 个月前
1.1.02026/02/278 个月前
1.0.12026/02/218 个月前
1.0.02026/02/208 个月前
0.0.12026/02/208 个月前

关于 @cerios/openapi-core 的常见问题

@cerios/openapi-core 是什么?

Core utilities for parsing and processing OpenAPI specifications. Shared foundation for OpenAPI code generators.

如何安装 @cerios/openapi-core?

在项目目录运行 npm install @cerios/openapi-core,或使用 pnpm add / yarn add / bun add。

@cerios/openapi-core 支持 TypeScript 吗?

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

@cerios/openapi-core 支持 ESM 还是 CommonJS?

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

@cerios/openapi-core 可以商用吗?

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

@cerios/openapi-core 的下载量有多少?

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

@cerios/openapi-core 还在维护吗?

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