A clean, dependency-free toolkit for NUS students to parse, validate, and analyze OpenAPI specifications. This package provides everything you need to work with OpenAPI specs without complex external dependencies.
- π Smart OpenAPI Parser - Parse, validate, and analyze YAML/JSON specifications
- π Comprehensive Examples - Learn from complete, working implementations
- π― Student-Friendly - Designed specifically for NUS project requirements
- π« Zero Complex Dependencies - No X-venture packages, clean and simple
# Install the core package
npm install yaml
# Install peer dependencies
npm install ts-node@^10.9.1 typescript@^5.0.3
# Optional: Install TypeScript globally if not already installed
npm install -g typescript# Clone or download this repository
git clone <repository-url>
cd nus-openapi-core
# Install dependencies
npm install
# Build the project
npm run build
# Run the comprehensive parser example
npm run example:parser
# Run the simple API example using simple-api.yaml
npm run example:simple
# Or run in development mode (with TypeScript directly)
npm run example:parser-dev
npm run example:simple-dev- Multi-format Support: Parse both YAML and JSON OpenAPI specifications
- Smart Analysis: Extract components, paths, operations, and references
- Validation Engine: Comprehensive syntax and schema validation
- Component Discovery: Automatically identify schemas, parameters, responses
- Reference Tracking: Find and validate all $ref references
import { SimpleOpenAPIParser } from '@x-venture/nus-openapi-core';
const parser = new SimpleOpenAPIParser();
// Parse from string
const result = await parser.parse(yamlContent);
if (result.isValid) {
console.log('β
Valid OpenAPI specification!');
// Get API information
const info = parser.getInfo(result.document);
console.log(`API: ${info.title} v${info.version}`);
// Get components
const components = parser.getComponents(result.document);
console.log(`Components: ${components.length}`);
// Get paths
const paths = parser.getPaths(result.document);
console.log(`Endpoints: ${paths.length}`);
} else {
console.log('β Invalid specification');
result.errors?.forEach(error => console.log(error.message));
}import { OpenAPIParser } from '@x-venture/nus-openapi-core';
const parser = new OpenAPIParser();
// Parse with enhanced validation
const result = await parser.parse(yamlContent);
if (result.isValid) {
console.log('β
Valid OpenAPI specification!');
// Get detailed diagnostics
const diagnostics = result.diagnostics;
console.log(`Validation diagnostics: ${diagnostics?.length || 0}`);
} else {
console.log('β Invalid specification');
result.diagnostics?.forEach(diag => console.log(diag.message));
}Run with: npm run example:simple
Features demonstrated:
- π Loading OpenAPI specs from files (simple-api.yaml)
- π Basic parsing and validation
- π Component analysis
- π€οΈ Path and operation breakdown
- π Reference tracking
- π Statistics and insights
Run with: npm run example:parser
Features demonstrated:
- π Complex OpenAPI specification parsing
- π§© Advanced component analysis
- π Server configuration analysis
- π Detailed operation breakdown
- π Learning tips for NUS students
The included examples/simple-api.yaml file is perfect for learning:
openapi: 3.1.0
info:
title: Simple NUS API Example
description: A basic API example perfect for beginners
version: 1.0.0
paths:
/hello:
get:
summary: Say hello
responses:
'200':
description: Hello message
content:
application/json:
schema:
$ref: '#/components/schemas/Message'
/students/{studentId}:
get:
summary: Get student information
parameters:
- name: studentId
in: path
required: true
schema:
type: string
pattern: '^A\\d{7}[A-Z]$'
responses:
'200':
description: Student information
content:
application/json:
schema:
$ref: '#/components/schemas/Student'
components:
schemas:
Message:
type: object
required: [text]
properties:
text:
type: string
timestamp:
type: string
format: date-time
Student:
type: object
required: [id, name, email, faculty]
properties:
id:
type: string
pattern: '^A\\d{7}[A-Z]$'
name:
type: string
email:
type: string
format: email
faculty:
type: string
enum: [Engineering, Science, Arts, Business]# Build and run the simple example
npm run example:simple
# Or run in development mode
npm run example:simple-devExpected Output:
- β Validation results
- π API information (title, version, description)
- π§© Component analysis (Message, Student schemas)
- π€οΈ Path analysis (/hello, /students/{studentId})
- π Reference tracking ($ref usage)
- π Statistics and insights
# 1. Clone the repository
git clone <repository-url>
cd nus-openapi-core
# 2. Install all dependencies
npm install
# 3. Build the project
npm run build
# 4. Test everything works
npm run example:simple-dev
# 5. Start development mode (auto-rebuild on changes)
npm run dev| Script | Purpose | When to Use |
|---|---|---|
npm run build |
Compile TypeScript to JavaScript | Before running examples |
npm run dev |
Watch mode - rebuilds on changes | During development |
npm run test |
Run test suite | Before committing changes |
npm run example:parser |
Run comprehensive parser example | To see advanced features |
npm run example:parser-dev |
Run parser example (dev) | During development |
npm run example:simple |
Run simple API example | To see basic parsing |
npm run example:simple-dev |
Run simple example (dev) | During development |
nus-openapi-core/
βββ src/
β βββ π parser/ # Core OpenAPI parsing logic
β β βββ SimpleOpenAPIParser.ts # Simple, dependency-free parser
β β βββ OpenAPIParser.ts # Enhanced parser with diagnostics
β β βββ DiagnosticCollector.ts # Error collection and validation
β β
β βββ π― examples/ # Complete working examples
β β βββ parser-usage.ts # Simple API demo with simple-api.yaml
β β βββ run-example.ts # Comprehensive parser demo
β β
β βββ π types.ts # TypeScript definitions
β βββ π¦ index.ts # Main export file
β
βββ examples/ # Example OpenAPI specifications
β βββ simple-api.yaml # Basic example for learning
β βββ student-api.yaml # More complex example
β
βββ π package.json # Dependencies and scripts
βββ βοΈ tsconfig.json # TypeScript configuration
βββ π README.md # This file
# Solution: Install all dependencies
npm install
# If still failing, clear cache and reinstall
rm -rf node_modules package-lock.json
npm install# Make sure to build first
npm run build
# Then run the example
npm run example:simple
# For development (no build needed)
npm run example:simple-dev- Make sure you're running from the project root directory
- Verify the
examples/simple-api.yamlfile exists - Check file permissions
- Start Simple: Run
npm run example:simple-devto see basic parsing - Understand Structure: Examine the
simple-api.yamlfile - Explore Advanced: Try
npm run example:parser-devfor complex features - Modify Examples: Edit the YAML files and see how parsing changes
- Build Your Own: Use the parser in your own projects
class SimpleOpenAPIParser {
// Parse OpenAPI specification
async parse(content: string): Promise<ParserResult>
// Get API information
getInfo(document: any): OpenAPIInfo
getVersion(document: any): string
getServers(document: any): Array<{url: string, description?: string}>
// Get components and structure
getComponents(document: any): OpenAPIComponent[]
getPaths(document: any): OpenAPIPath[]
getReferences(document: any): string[]
// Additional utilities
getTags(document: any): string[]
getSecuritySchemes(document: any): Record<string, any>
getParametersByPath(document: any, path: string): any[]
getResponsesByPath(document: any, path: string, method: string): Record<string, any>
}class OpenAPIParser {
// Parse with enhanced validation
async parse(content: string): Promise<ParserResult>
// Same methods as SimpleOpenAPIParser
// Plus enhanced diagnostic collection
}- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built for NUS students with β€οΈ
- Clean, dependency-free implementation
- OpenAPI 3.1 specification support