typespec 简单试用

typespec 是一个强大的api 描述框架,以下是一个简单的试用

安装typespec

可以安装为全局cli

  • 命令
npm install -g @typespec/compiler

使用

  • 创建项目
tsp init // 后续按照提示操作,可以选择http
  • 安装依赖
tsp install 
  • 项目结构
├── main.tsp
├── package-lock.json
├── package.json
└── tspconfig.yaml
  • 代码简单说明

tspconfig.yaml

emit:
  - "@typespec/openapi3"

package.json

{
  "name": "demo",
  "version": "0.1.0",
  "type": "module",
  "dependencies": {
    "@typespec/compiler": "latest",
    "@typespec/http": "latest",
    "@typespec/openapi3": "latest"
  },
  "private": true
}

main.tsp

import "@typespec/http";
 
using TypeSpec.Http;
 
model Pet {
  name: string;
  age: int32;
}
 
model Store {
  name: string;
  address: Address;
}
 
model Address {
  street: string;
  city: string;
}
 
@route("/pets")
interface Pets {
  list(@query filter: string): Pet[];
  create(@body pet: Pet): Pet;
  read(@path id: string): Pet;
}
 
@route("/stores")
interface Stores {
  list(@query filter: string): Store[];
  read(@path id: Store): Store;
}
  • 编译
tsp compile .
  • 效果

说明

以上是一个简单的试用,typespec 还是很强大的,值得学习试用下,对于开发api first 的架构很值得参考

参考资料

https://typespec.io/
https://github.com/microsoft/typespec

posted on   荣锋亮  阅读(150)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-01-28 通过DACModule 模块学习dremio 服务模块依赖关系之 bootstrap
2020-01-28 edgedb 1.0 Alpha 2 了
2020-01-28 aceql-http docker 试用
2019-01-28 keycloak docker-compose 运行
2019-01-28 使用 pkg 打包分发 nodejs 应用
2018-01-28 gradle multiproject && docker build
2015-01-28 expression encoder 4 安装 出现“已经安排重启您的计算机

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示