AgentSkillsCN

Swift Api Generator

Swift API 生成器

SKILL.md

Swift API Integration Generator Skill

Purpose

Generate complete API integration layers from specifications. This skill creates service classes, Codable models, error handling, authentication, and matching unit tests following iOS best practices.

Capabilities

  • Parse OpenAPI/JSON specifications to generate Swift code
  • Create service classes with async/await and Combine support
  • Generate Codable models matching API DTOs
  • Implement authentication with token refresh
  • Add offline support and caching strategies
  • Generate comprehensive unit tests with mocks
  • Create documentation comments

Usage

When implementing API integrations, I will:

  1. Analyze the API specification (OpenAPI, Postman, or custom JSON)
  2. Generate models that match the API response/request structure
  3. Create service classes with proper error handling
  4. Implement authentication including token management
  5. Add caching and offline support where appropriate
  6. Generate unit tests with mock responses

Common Tasks

Generate from OpenAPI

python
python service_generator.py --spec api-spec.yaml --output Services/

Generate models from JSON

python
python model_generator.py --json response.json --name UserResponse --output Models/API/

Generate complete integration

bash
./generate_api_integration.sh --api-name Orders --base-url https://api.example.com

Resources

  • service_generator.py - Generate service classes from specs
  • model_generator.py - Create Codable models from JSON
  • test_generator.py - Generate unit tests with mocks
  • templates/ - Swift code templates
  • examples/ - Example API specifications

Templates Used

  • Service class with async/await
  • Codable models with custom decoding
  • Network request builders
  • Error handling patterns
  • Mock service protocols
  • Unit test structures

Best Practices

  1. Use async/await for modern Swift
  2. Include proper error types
  3. Add retry logic for network failures
  4. Implement token refresh transparently
  5. Cache responses appropriately
  6. Generate comprehensive documentation