从API文档到TypeScript 前端客户端(SDKs)
在 FastAPI系列15:API文档的定制和美化 一节中,我们讲解了如何定制和美化API文档,一个良好的API文档可以过千言万语的沟通,同时也能让你立刻收获一个结构良好的客户端框架。本节我们介绍如何使用openapi-ts为你的API生成一个TypeScript 前端客户端。
我们知道FastAPI是基于OpenAPI规范的,这就意味着我们可以使用一些工具来为不同的编程语言生成客户端(SDKs),如:
- OpenAPI Generator
- openapi-ts
快速入门
我们有一个简单的 FastAPI 应用:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ResponseMessage(BaseModel):
message: str
@app.post("/items/", response_model=ResponseMessage)
async def create_item(item: Item):
return {
"message": "item received"}
@app.get("/items/", response_model=list[Item])
async def get_items():
return [
{
"name": "Plumbus", "price": 3},
{
"name": "Portal Gun", "price": 9001},
]
生成一个TypeScript 客户端
安装 openapi-ts

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



