← 中文资源

简体中文版本

API 版本控制实现工作流

用 URL 路径版本控制实现 API 版本管理,包含向后兼容检查和弃用头。

阅读英文原文

概述

API 版本控制实现工作流是一个中级难度的工作流。用 URL 路径版本控制实现 API 版本管理,包含向后兼容检查和弃用头。该工作流的目标是:Add versioning to an API to enable evolution without breaking existing clients.

环境准备

执行本工作流需要以下环境:An API without versioning or with a single version;A versioning strategy chosen;API documentation tool。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。

执行步骤

本工作流包含5个步骤。步骤1:Choose strategy——Select URL path versioning (/v1, /v2) for simplicity and visibility. 步骤2:Create v1 namespace——Move existing endpoints under the v1 path prefix. 步骤3:Add version to docs——Update API documentation to include the version in all endpoint paths. 步骤4:Add deprecation headers——Include Sunset and Deprecation headers on old versions when new ones are available. 步骤5:Test both versions——Verify that v1 endpoints still work and that v2 can be added without affecting v1.

验证与清理

预期结果:The API has a clear versioning scheme with v1 preserved and deprecation headers ready for future versions.。验证方法:Call an endpoint on v1 and confirm it works; add a v2 version of the same endpoint and confirm v1 is unaffected.。清理操作:No cleanup needed; versioning structure should be committed.。estimatedMinutes 为 45 分钟。

代码示例

# Execute the workflow steps as documented above
echo "Workflow completed successfully"