Перейти к файлу
Timothee Guerin c344444f7b
Add management of stale issues (#2279)
Add new policies for Microsoft Github bot to auto close issues that
needs-author-feedback and don't get reply.

- 7 Days without author feedback will send a reminder
- 14 days without author feedback will close the issue
- Response from the author will remove the `needs-author-feedback`
2023-10-30 19:17:21 +00:00
.github Add management of stale issues (#2279) 2023-10-30 19:17:21 +00:00
.vscode Fix playground usage from external package (#2540) 2023-10-05 12:59:06 -07:00
common Fix: Augment decorators might not run if alias caused the type to be resolved too early (#2603) 2023-10-30 18:46:48 +00:00
docker Fix dockerfile and docs (#1746) 2023-03-28 20:31:23 +00:00
docs Add unixtimestamp32 to standard lib (#2579) 2023-10-18 19:03:41 +00:00
e2e Prepare typespec publish for July release (#2175) 2023-07-11 16:27:59 -07:00
eng Drop support for node 16 (#2605) 2023-10-26 11:41:54 -07:00
packages Fix: Augment decorators might not run if alias caused the type to be resolved too early (#2603) 2023-10-30 18:46:48 +00:00
.dockerignore Feature: docker image (#57) 2021-11-12 11:17:46 -08:00
.editorconfig Format CSharp files: add `dotnet format` to `rush format` (#490) 2022-04-29 09:46:23 -07:00
.gitattributes Rush pnpm workspaces (#2080) 2023-06-19 09:55:18 -07:00
.gitignore Rush pnpm workspaces (#2080) 2023-06-19 09:55:18 -07:00
.prettierignore Core documentation - generate data type docs programmatically (#2467) 2023-09-25 09:50:32 -07:00
.prettierrc.json Update to support prettier 3.0 (#2157) 2023-08-03 17:11:54 -07:00
CODE_OF_CONDUCT.md Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
CONTRIBUTING.md Samples as tests (#2215) 2023-08-04 10:19:02 -07:00
LICENSE Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
README.md Use canonical github microsoft/typespec org/repo casing (all lowercase) (#2038) 2023-06-08 15:50:57 -05:00
SECURITY.md Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
cspell.yaml Use "c8" to merge coverage files (#2432) 2023-09-19 18:38:42 +00:00
rush.json Stop publishing `@typespec/lint` (#2578) 2023-10-18 06:19:35 -07:00
troubleshooting.md 1/3 TypeSpec: Find/Replace Content 2023-02-16 13:07:22 -08:00
tsconfig.json Rename `ref-doc` package to `tspd` and provide a cli that can be used by each package (#2225) 2023-08-01 22:34:18 +00:00

README.md

TypeSpec

Try TypeSpec Online

TypeSpec is a language for describing cloud service APIs and generating other API description languages, client and service code, documentation, and other assets. TypeSpec provides highly extensible core language primitives that can describe API shapes common among REST, OpenAPI, GraphQL, gRPC, and other protocols.

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.

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 issues for any issues you encounter while using the preview.

Try TypeSpec without installing anything

You can try TypeSpec on the web without installing anything.

Getting Started

For documentation for TypeSpec language, see https://microsoft.github.io/typespec.

Using Docker

See docker documentation

Using Node & Npm

One-time Setup

  1. Install Node.js 16 LTS and ensure you are able to run the npm command in a command prompt:

    npm --version
    

    It is recommended to have npm 7+. To update npm run npm install -g npm

  2. Install TypeSpec compiler and libraries:

   npm init -y
   npm install -g @typespec/compiler

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

    npx tsp init
    npx tsp compile
  1. Install the TypeSpec extension for your editor of choice:

Creating TypeSpec project

  1. Create a folder for your new TypeSpec project

  2. Initialize a TypeSpec project.

    • Run tsp init > Select Generic Rest API template with @typespec/rest and @typespec/openapi3 libraries checked.
    • Run tsp install to install node package dependencies.
  3. Open the folder in your editor and edit main.tsp

  4. Follow our documentation to get started writing TypeSpec!

  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:

    tsp compile .
    

    This will compile the TypeSpec files in the project folder into one output file: .\tsp-output\openapi.json.

Troubleshooting

See common issues here

Usage

See full usage documentation by typing:

tsp --help

Compiling TypeSpec source to an OpenAPI 3.0 specification

Here is a very small TypeSpec example that uses the @typespec/openapi3 library to generate OpenAPI 3.0 from TypeSpec.

sample.tsp

import "@typespec/http";

using TypeSpec.Http;

@server("https://example.com", "Single server endpoint")
@route("/example")
namespace Example {
  @get
  @route("/message")
  op getMessage(): string;
}

You can compile it to OpenAPI 3.0 by using the following command:

tsp compile sample.tsp --emit @typespec/openapi3

Once it compiles, you can find the emitted OpenAPI document in `./tsp-output/openapi.json.

You can also pass in a directory instead of a file to tsp compile. That's equivalent to passing main.tsp in that directory.

Formatting TypeSpec files

TypeSpec provides an auto-formatter to keep your specs clean and organized. node_modules folders are automatically excluded by default

tsp format <patterns...>

# 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.
tsp format **/*.tsp "!my-test-folder/**/*"
tsp format **/*.tsp --exclude "my-test-folder/**/*"

Installing VS Code Extension

tsp code install

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.

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)

You can also configure a project to use a local npm install of @typespec/compiler. See local-typespec sample.

Installing Visual Studio Extension

tsp vs install

This will download and install the latest Visual Studio extension. Use tsp vs uninstall to remove it.

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 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.