cadl/README.md

236 строки
12 KiB
Markdown
Исходник Обычный вид История

2023-02-16 01:37:39 +03:00
# TypeSpec
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
[Try TypeSpec Online](https://aka.ms/trytypespec)
2023-02-16 01:37:39 +03:00
TypeSpec is a language for describing cloud service APIs and generating other API
2021-08-25 22:47:06 +03:00
description languages, client and service code, documentation, and other assets.
2023-02-16 01:37:39 +03:00
TypeSpec provides highly extensible core language primitives that can describe API
shapes common among REST, OpenAPI, GraphQL, gRPC, and other protocols.
2023-02-16 01:37:39 +03:00
Using TypeSpec, you can create reusable patterns for all aspects of an API, along with the ability to check for and flag known anti-patterns. These patterns establish "guardrails" for API designers and make it easier to follow best practices than deviate from them. TypeSpec promotes highly regular API designs that adhere to best practices by construction.
2021-08-25 22:47:06 +03:00
You can try a work-in-progress build of the compiler by following the steps in
the Getting Started section below. Please feel free to [file
2023-02-16 01:37:39 +03:00
issues](https://github.com/Microsoft/typespec/issues) for any issues you encounter while
2021-08-25 22:47:06 +03:00
using the preview.
2023-02-16 01:37:39 +03:00
## Try TypeSpec without installing anything
2023-02-16 01:37:39 +03:00
You can try TypeSpec on the web without installing anything.
2023-02-16 01:37:39 +03:00
- [TypeSpec playground](https://typespecplayground.z22.web.core.windows.net)
- [TypeSpec playground for Azure services](https://typespecplayground.z22.web.core.windows.net/typespec-azure/)
2021-08-25 22:47:06 +03:00
## Getting Started
2023-02-16 01:37:39 +03:00
For documentation for TypeSpec language, see https://microsoft.github.io/typespec.
2021-11-12 22:17:46 +03:00
### Using Docker
[See docker documentation](./docker)
2021-11-12 22:17:46 +03:00
### Using Node & Npm
#### One-time Setup
2022-03-21 22:48:20 +03:00
1. Install [Node.js 16 LTS](https://nodejs.org/en/download/) and ensure you are able to run the `npm` command in a command prompt:
2021-08-25 22:47:06 +03:00
2022-03-21 22:48:20 +03:00
```bash
2021-08-25 22:47:06 +03:00
npm --version
```
2022-03-21 22:48:20 +03:00
It is recommended to have npm 7+. To update npm run `npm install -g npm`
2023-02-16 01:37:39 +03:00
2. Install TypeSpec compiler and libraries:
2022-03-21 22:48:20 +03:00
```bash
2021-08-25 22:47:06 +03:00
npm init -y
2023-02-16 01:37:39 +03:00
npm install -g @typespec/compiler
```
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
If you do not wish to install the compiler globally with `-g` flag, you will need to install it locally once in every TypeSpec project folder. You would also need to prefix every TypeSpec run command with `npx`. See [npx documentation](https://docs.npmjs.com/cli/v7/commands/npx)
2021-08-25 22:47:06 +03:00
```bash
2023-02-16 01:37:39 +03:00
npx tsp init
npx tsp compile
```
2023-02-16 01:37:39 +03:00
3. Install the TypeSpec extension for your editor of choice:
2021-08-25 22:47:06 +03:00
- [Instructions for Visual Studio](#installing-visual-studio-extension)
- [Instructions for Visual Studio Code](#installing-vs-code-extension)
2023-02-16 01:37:39 +03:00
### Creating TypeSpec project
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
1. Create a folder for your new TypeSpec project
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
2. Initialize a TypeSpec project.
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
- Run `tsp init` > Select `Generic Rest API` template with `@typespec/rest` and `@typespec/openapi3` libraries checked.
- Run `tsp install` to install node package dependencies.
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
3. Open the folder in your editor and edit `main.tsp`
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
4. Follow our [documentation](https://microsoft.github.io/typespec) to get started writing TypeSpec!
2022-03-21 22:48:20 +03:00
2023-02-16 01:37:39 +03:00
5. Once you're ready to compile your TypeSpec to Swagger, save the file and type this at the command prompt in your project folder:
2022-03-21 22:48:20 +03:00
```bash
2023-02-16 01:37:39 +03:00
tsp compile .
```
2022-03-21 22:48:20 +03:00
2023-02-16 01:37:39 +03:00
This will compile the TypeSpec files in the project folder into one output file: `.\typespec-output\openapi.json`.
2022-03-21 22:48:20 +03:00
## Troubleshooting
[See common issues here](./troubleshooting.md)
2021-08-25 22:47:06 +03:00
## Usage
See full usage documentation by typing:
```
2023-02-16 01:37:39 +03:00
tsp --help
2021-08-25 22:47:06 +03:00
```
2023-02-16 01:37:39 +03:00
### Compiling TypeSpec source to an OpenAPI 3.0 specification
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
Here is a very small TypeSpec example that uses the `@typespec/openapi3` library to generate OpenAPI 3.0 from TypeSpec.
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
#### sample.tsp
2023-02-16 01:37:39 +03:00
```typespec
import "@typespec/rest";
2023-02-16 01:37:39 +03:00
using TypeSpec.Http;
2021-11-17 21:08:07 +03:00
@server("https://example.com", "Single server endpoint")
2021-11-17 21:08:07 +03:00
@route("/example")
namespace Example {
@get
@route("/message")
2021-11-17 21:08:07 +03:00
op getMessage(): string;
}
```
2021-08-25 22:47:06 +03:00
You can compile it to OpenAPI 3.0 by using the following command:
2021-08-25 22:47:06 +03:00
```
2023-02-16 01:37:39 +03:00
tsp compile sample.tsp --emit @typespec/openapi3
2021-08-25 22:47:06 +03:00
```
2023-02-16 01:37:39 +03:00
Once it compiles, you can find the emitted OpenAPI document in `./typespec-output/openapi.json.
2023-02-16 01:37:39 +03:00
You can also pass in a directory instead of a file to `tsp compile`. That's
equivalent to passing `main.tsp` in that directory.
2023-02-16 01:37:39 +03:00
### Formatting TypeSpec files
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
TypeSpec provides an auto-formatter to keep your specs clean and organized.
`node_modules` folders are automatically excluded by default
2021-08-25 22:47:06 +03:00
```bash
2023-02-16 01:37:39 +03:00
tsp format <patterns...>
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
# Format all the files in the current directory with the typespec extension.
tsp format **/*.tsp
# Exclude certain patterns. Either use `!` prefix or pass it via the `--exclude` or `-x` option.
2023-02-16 01:37:39 +03:00
tsp format **/*.tsp "!my-test-folder/**/*"
tsp format **/*.tsp --exclude "my-test-folder/**/*"
2021-08-25 22:47:06 +03:00
```
### Installing VS Code Extension
```
2023-02-16 01:37:39 +03:00
tsp code install
2021-08-25 22:47:06 +03:00
```
2023-02-16 01:37:39 +03:00
This will download and install the latest VS Code extension. Use `tsp code uninstall` to remove it. Pass `--insiders` if you use VS Code Insiders edition.
2021-08-25 22:47:06 +03:00
2023-02-16 01:37:39 +03:00
If `tsp-server` cannot be found on PATH by VS Code in your setup, you can
configure its location in VS Code settings. Search for "TypeSpec" in File ->
Preferences -> Settings, and adjust `typespec.tsp-server.path` accordingly. You may
need to restart VS Code after changing this. This should be the path to the `@typespec/compiler` package. (e.g. `./node_modules/@typespec/compiler`)
2021-08-25 22:47:06 +03:00
You can also configure a project to use a local npm install of
2023-02-16 01:37:39 +03:00
`@typespec/compiler`. See [local-typespec sample](packages/samples/local-typespec).
2021-08-25 22:47:06 +03:00
### Installing Visual Studio Extension
```
2023-02-16 01:37:39 +03:00
tsp vs install
2021-08-25 22:47:06 +03:00
```
2023-02-16 01:37:39 +03:00
This will download and install the latest Visual Studio extension. Use `tsp vs uninstall` to remove it.
2022-05-05 00:33:22 +03:00
2023-02-16 01:37:39 +03:00
If `tsp-server` cannot be found on PATH by Visual Studio in your setup, you can
configure its location by setting up the `typespec.tsp-server.path` entry in `.vs/VSWorkspaceSettings.json`. You may need to restart Visual Studio after changing this.
This should be the path to the `@typespec/compiler` package. (e.g. `./node_modules/@typespec/compiler`)
### 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/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/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 | | | |
2023-02-16 01:37:39 +03:00
| [@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/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
[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.