cadl/README.md

165 строки
9.5 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

2023-02-16 01:37:39 +03:00
# TypeSpec
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
[Official Docs](https://typespec.io/) | [Try TypeSpec Online](https://aka.ms/trytypespec) | [Getting Started](https://typespec.io/docs) | [Language Overview](https://typespec.io/docs/language-basics/overview)
2024-01-31 03:18:37 +03:00
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.
2024-01-31 03:18:37 +03:00
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.
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
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.
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
## [Installation](https://typespec.io/docs)
```
2024-01-31 03:18:37 +03:00
npm install -g @typespec/compiler
```
2024-01-31 03:18:37 +03:00
#### Tools
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
The [TypeSpec VS Code extension](https://marketplace.visualstudio.com/items?itemName=typespec.typespec-vscode) can be installed from the VS Code [marketplace](https://marketplace.visualstudio.com/items?itemName=typespec.typespec-vscode) or directly on the command line:
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
```
tsp code install
```
2022-03-21 22:48:20 +03:00
2024-01-31 03:18:37 +03:00
The [TypeSpec VS Extension](https://marketplace.visualstudio.com/items?itemName=typespec.typespecvs) can be installed from the [VS Marketplace](https://marketplace.visualstudio.com/items?itemName=typespec.typespecvs) or directly on the command line:
2022-03-21 22:48:20 +03:00
2024-01-31 03:18:37 +03:00
```
tsp vs install
```
2022-03-21 22:48:20 +03:00
2024-01-31 03:18:37 +03:00
## [Usage](https://typespec.io/docs#create-first-typespec-project)
2022-03-21 22:48:20 +03:00
2024-01-31 03:18:37 +03:00
### TypeSpec to OpenAPI 3.0 Example
2022-03-21 22:48:20 +03:00
2024-01-31 03:18:37 +03:00
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.
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
Run the following command and select "Generic REST API":
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
tsp init
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
Hit enter a few times to confirm the defaults.
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
Copy the contents below into your **main.tsp**:
2023-02-16 01:37:39 +03:00
```typespec
import "@typespec/http";
2024-01-31 03:18:37 +03:00
import "@typespec/rest";
import "@typespec/openapi3";
2023-02-16 01:37:39 +03:00
using TypeSpec.Http;
2024-01-31 03:18:37 +03:00
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[];
}
2021-08-25 22:47:06 +03:00
2024-01-31 03:18:37 +03:00
model Pet {
@minLength(100)
name: string;
2024-01-31 03:18:37 +03:00
@minValue(0)
@maxValue(100)
age: int32;
2024-01-31 03:18:37 +03:00
kind: "dog" | "cat" | "fish";
}
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
Install the dependencies of main.tsp:
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
tsp install
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
Compile it to OpenAPI 3.0:
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
tsp compile main.tsp --emit @typespec/openapi3
2021-08-25 22:47:06 +03:00
```
2024-01-31 03:18:37 +03:00
You can find the emitted OpenAPI output in `./tsp-output/openapi.json`.
2022-05-05 00:33:22 +03:00
2024-01-31 03:18:37 +03:00
## 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](#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
```json5
// Stable setup
"dependencies": {
2023-02-16 01:37:39 +03:00
"@typespec/compiler": "~0.30.0",
"@typespec/http": "~0.14.0",
2023-02-16 01:37:39 +03:00
"@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": {
2023-02-16 01:37:39 +03:00
"@typespec/compiler": "~0.31.0-dev.5",
"@typespec/http": "~0.14.0",
2023-02-16 01:37:39 +03:00
"@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][compiler_src] | [Changelog][compiler_chg] | [![](https://img.shields.io/npm/v/@typespec/compiler)](https://www.npmjs.com/package/@typespec/compiler) | ![](https://img.shields.io/npm/v/@typespec/compiler/next) |
| TypeSpec Libraries | | | |
| [@typespec/http][http_src] | [Changelog][http_chg] | [![](https://img.shields.io/npm/v/@typespec/http)](https://www.npmjs.com/package/@typespec/http) | ![](https://img.shields.io/npm/v/@typespec/http/next) |
| [@typespec/rest][rest_src] | [Changelog][rest_chg] | [![](https://img.shields.io/npm/v/@typespec/rest)](https://www.npmjs.com/package/@typespec/rest) | ![](https://img.shields.io/npm/v/@typespec/rest/next) |
| [@typespec/openapi][openapi_src] | [Changelog][openapi_chg] | [![](https://img.shields.io/npm/v/@typespec/openapi)](https://www.npmjs.com/package/@typespec/openapi) | ![](https://img.shields.io/npm/v/@typespec/openapi/next) |
| [@typespec/openapi3][openapi3_src] | [Changelog][openapi3_chg] | [![](https://img.shields.io/npm/v/@typespec/openapi3)](https://www.npmjs.com/package/@typespec/openapi3) | ![](https://img.shields.io/npm/v/@typespec/openapi3/next) |
| [@typespec/versioning][versioning_src] | [Changelog][versioning_chg] | [![](https://img.shields.io/npm/v/@typespec/versioning)](https://www.npmjs.com/package/@typespec/versioning) | ![](https://img.shields.io/npm/v/@typespec/versioning/next) |
| TypeSpec Tools | | | |
| [@typespec/prettier-plugin-typespec][prettier_src] | [Changelog][prettier_chg] | [![](https://img.shields.io/npm/v/@typespec/prettier-plugin-typespec)](https://www.npmjs.com/package/@typespec/prettier-plugin-typespec) | ![](https://img.shields.io/npm/v/@typespec/prettier-plugin-typespec/next) |
| [typespec-vs][typespec-vs_src] | [Changelog][typespec-vs_chg] | [![](https://img.shields.io/npm/v/typespec-vs)](https://www.npmjs.com/package/typespec-vs) | ![](https://img.shields.io/npm/v/typespec-vs/next) |
| [typespec-vscode][typespec-vscode_src] | [Changelog][typespec-vscode_chg] | [![](https://img.shields.io/npm/v/typespec-vscode)](https://www.npmjs.com/package/typespec-vscode) | ![](https://img.shields.io/npm/v/typespec-vscode/next) |
| [tmlanguage-generator][tmlanguage_src] | [Changelog][tmlanguage_chg] | [![](https://img.shields.io/npm/v/tmlanguage-generator)](https://www.npmjs.com/package/tmlanguage-generator) | ![](https://img.shields.io/npm/v/tmlanguage-generator/next) |
[compiler_src]: packages/compiler
[compiler_chg]: packages/compiler/CHANGELOG.md
[http_src]: packages/http
[http_chg]: packages/http/CHANGELOG.md
[rest_src]: packages/rest
[rest_chg]: packages/rest/CHANGELOG.md
[openapi_src]: packages/openapi
[openapi_chg]: packages/openapi/CHANGELOG.md
[openapi3_src]: packages/openapi3
[openapi3_chg]: packages/openapi3/CHANGELOG.md
[versioning_src]: packages/versioning
[versioning_chg]: packages/versioning/CHANGELOG.md
2023-02-16 01:37:39 +03:00
[prettier_src]: packages/prettier-plugin-typespec
[prettier_chg]: packages/prettier-plugin-typespec/CHANGELOG.md
[typespec-vs_src]: packages/typespec-vs
[typespec-vs_chg]: packages/typespec-vs/CHANGELOG.md
[typespec-vscode_src]: packages/typespec-vscode
[typespec-vscode_chg]: packages/typespec-vscode/CHANGELOG.md
[tmlanguage_src]: packages/tmlanguage-generator
[tmlanguage_chg]: packages/tmlanguage-generator/CHANGELOG.md
`@next` version of the package are the latest versions available on the `main` branch.