AgentSkillsCN

openapi-integration

在 Next.js 后端与 Swift iOS 客户端之间搭建并维护 OpenAPI 3.0 集成。适用于用户需要:(1) 使用 next-openapi-gen 从 Zod 模式生成 OpenAPI 规范;(2) 为 iOS 包配置 Swift OpenAPI 生成器;(3) 使用 JSDoc 注释创建或更新 API 路由;(4) 为 API 合约创建 Zod 模式;(5) 在后端与 iOS 之间同步 OpenAPI 规范;(6) 创建封装生成 API 客户端的 Swift 服务层;或 (7) 排查 OpenAPI 生成或 Swift 类型相关的问题时使用。

SKILL.md
--- frontmatter
name: openapi-integration
description: Set up and maintain OpenAPI 3.0 integration between a Next.js backend and a Swift iOS client. Use when the user needs to (1) set up OpenAPI spec generation from Zod schemas using next-openapi-gen, (2) configure Swift OpenAPI generator for an iOS package, (3) create or update API routes with JSDoc annotations, (4) create Zod schemas for API contracts, (5) sync the OpenAPI spec between backend and iOS, (6) create Swift service layers wrapping generated API clients, or (7) troubleshoot OpenAPI generation or Swift type issues.

OpenAPI Integration

Type-safe API communication between a Next.js backend and a Swift iOS client via OpenAPI 3.0.

Architecture

code
Backend (Next.js)                              iOS Client (Swift)
┌──────────────┐   ┌──────────────┐           ┌───────────────────────┐
│ Zod Schemas  │──▶│next-openapi  │──▶ JSON ──▶│swift-openapi-generator│
│              │   │   -gen       │           │ (SPM build plugin)    │
└──────────────┘   └──────────────┘           └───────────┬───────────┘
                                                          ▼
┌──────────────┐                              Types.swift + Client.swift
│ API Routes   │◀── JSDoc @openapi tags               (generated)
│ (app/api/v1) │                                          ▼
└──────────────┘                              Service Layer (manual)

Placeholders

All scripts accept these as arguments — replace with project-specific values:

ArgumentDescriptionExample
<backend-dir>Backend directoryadmin, backend, web
<ios-app>iOS app root directoryMyApp, RxStorage
<package-name>Swift package nameMyAppCore, RxStorageCore

Workflow

Initial setup

  1. Install next-openapi-gen in backend: ./scripts/admin-install-openapi.sh <backend-dir>
  2. Add "openapi:generate": "next-openapi-gen generate" to <backend-dir>/package.json scripts
  3. Create Zod schemas in <backend-dir>/lib/schemas/ → See backend-setup.md
  4. Annotate API routes with JSDoc @openapi tags → See jsdoc-reference.md
  5. Create the /api/openapi serving route → See backend-setup.md
  6. Set up Swift package dependencies and generator config → See ios-setup.md
  7. Create API client, auth middleware, and service layer → See ios-setup.md

After API changes (ongoing workflow)

  1. Edit routes in <backend-dir>/app/api/v1/ or schemas in <backend-dir>/lib/schemas/
  2. Start server: bun run dev in backend directory
  3. Regenerate and sync spec: ./scripts/ios-update-openapi.sh <backend-dir> <ios-app> <package-name>
  4. Build iOS package: ./scripts/ios-build-package.sh <ios-app> <package-name>
  5. Update Swift service layer to use new/changed generated types

Scripts

ScriptArgsPurpose
admin-install-openapi.sh<backend-dir>Install next-openapi-gen
admin-openapi-generate.sh<backend-dir>Generate spec from Zod schemas
ios-update-openapi.sh<backend-dir> <ios-app> <package-name>Generate + download spec to iOS
ios-download-openapi.sh<ios-app> <package-name>Download spec from running server
ios-build-package.sh<ios-app> <package-name>Build Swift package (regenerates types)
ios-clean-package.sh<ios-app> <package-name>Clean Swift build folder

Override the server endpoint via env var: OPENAPI_DOCUMENTATION_ENDPOINT=https://api.example.com/api/openapi

References

Packages

ComponentPackageVersion
Backendnext-openapi-gen^0.9.4
iOS Generatorswift-openapi-generator^1.4.0
iOS Runtimeswift-openapi-runtime^1.6.0
iOS Transportswift-openapi-urlsession^1.0.0