microsoft/typespec
Fork: 226 Star: 4624 (更新于 2024-12-21 11:01:32)
license: MIT
Language: Java .
最后发布版本: @typespec/compiler@0.59.1 ( 2024-08-14 02:51:18)
TypeSpec
Official Docs | Try TypeSpec Online | Getting Started | Language Overview
TypeSpec is a language for defining cloud service APIs and shapes. TypeSpec is a highly extensible language with primitives that can describe API shapes common among REST, OpenAPI, gRPC, and other protocols.
TypeSpec is excellent for generating many different API description formats, client and service code, documentation, and many other assets. All this while keeping your TypeSpec definition as a single source of truth.
Using TypeSpec, you can create reusable patterns for all aspects of an API and package those reusable patterns into libraries. These patterns establish "guardrails" for API designers and makes it easier to follow best practices than to deviate from them. TypeSpec also has a rich linter framework with the ability to flag anti-patterns as well as an emitter framework that lets you control of the output to ensure it follows the patterns you want.
Installation
npm install -g @typespec/compiler
Tools
The TypeSpec VS Code extension can be installed from the VS Code marketplace or directly on the command line:
tsp code install
The TypeSpec VS Extension can be installed from the VS Marketplace or directly on the command line:
tsp vs install
Usage
TypeSpec to OpenAPI 3.0 Example
This example uses the @typespec/http
, @typespec/rest
, and @typespec/openapi3
libraries to define a basic REST service and generate an OpenAPI 3.0 document from it.
Run the following command and select "Generic REST API":
tsp init
Hit enter a few times to confirm the defaults.
Copy the contents below into your main.tsp:
import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi3";
using TypeSpec.Http;
using TypeSpec.Rest;
/** This is a pet store service. */
@service({
title: "Pet Store Service",
})
@server("https://example.com", "The service endpoint")
namespace PetStore;
@route("/pets")
interface Pets {
list(): Pet[];
}
model Pet {
@minLength(100)
name: string;
@minValue(0)
@maxValue(100)
age: int32;
kind: "dog" | "cat" | "fish";
}
Install the dependencies of main.tsp:
tsp install
Compile it to OpenAPI 3.0:
tsp compile main.tsp --emit @typespec/openapi3
You can find the emitted OpenAPI output in ./tsp-output/openapi.json
.
Advanced Scenarios
Installing nightly version
On every commit to the main branch, packages with changes are automatically published to npm with the @next
tag.
The packages section shows which version corresponds to the next
tag for each package.
To use a nightly
version of the packages, go over each one of the packages in the package.json
file and update it to either the latest published @next
version or @latest
, whichever is the newest. You can also use the tag latest
or next
instead of an explicit version.
After updating the package.json file you can run npm update --force
. Force is required as there might be some incompatible version requirement.
Example
// Stable setup
"dependencies": {
"@typespec/compiler": "~0.30.0",
"@typespec/http": "~0.14.0",
"@typespec/rest": "~0.14.0",
"@typespec/openapi": "~0.9.0",
}
// Consume next version
// In this example: compiler and openapi have changes but rest library has none
"dependencies": {
"@typespec/compiler": "~0.31.0-dev.5",
"@typespec/http": "~0.14.0",
"@typespec/rest": "~0.14.0", // No changes to @typespec/rest library so need to stay the latest.
"@typespec/openapi": "~0.10.0-dev.2",
}
Packages
Name | Changelog | Latest | Next |
---|---|---|---|
Core functionality | |||
@typespec/compiler | Changelog | ||
TypeSpec Libraries | |||
@typespec/http | Changelog | ||
@typespec/rest | Changelog | ||
@typespec/openapi | Changelog | ||
@typespec/openapi3 | Changelog | ||
@typespec/versioning | Changelog | ||
TypeSpec Tools | |||
@typespec/prettier-plugin-typespec | Changelog | ||
typespec-vs | Changelog | ||
typespec-vscode | Changelog | ||
tmlanguage-generator | Changelog |
@next
version of the package are the latest versions available on the main
branch.
最近版本更新:(数据更新于 2024-09-07 15:42:24)
2024-08-14 02:51:18 @typespec/compiler@0.59.1
2024-08-14 01:15:33 @typespec/openapi3@0.59.1
2024-08-13 22:50:27 @typespec/rest@0.59.1
2024-08-13 22:50:24 @typespec/http@0.59.1
2024-08-08 13:41:08 typespec@0.59.0
2024-08-08 13:41:04 @typespec/xml@0.59.0
2024-08-08 13:41:02 @typespec/playground@0.4.1
2024-08-08 13:40:58 @typespec/http-server-javascript@0.58.0-alpha.2
2024-08-08 13:40:55 @typespec/http-server-csharp@0.58.0-alpha.2
2024-08-08 13:40:53 @typespec/bundler@0.1.6
主题(topics):
json-schema, openapi3, protobuf, typespec
microsoft/typespec同语言 Java最近更新仓库
2024-12-20 19:54:15 mybatis-flex/mybatis-flex
2024-12-19 21:53:46 spring-projects/spring-boot
2024-12-19 21:25:16 Stirling-Tools/Stirling-PDF
2024-12-19 16:28:14 Tornaco/Thanox
2024-12-19 10:42:01 alibaba/nacos
2024-12-18 20:58:34 dromara/forest