Перейти к файлу
Vasil Markoukin d2d397cb67
Operation level authentication and scopes (#2901)
Hi! 🖖🏻 
This PR resolves #2624 by implementing the [design
doc](https://gist.github.com/timotheeguerin/56690786e61a436710dd647de9febc0f),
but in its initial form:
- `@useAuth` can now be applied not only to service namespace, but to
interfaces and operations as well. Its arguments override all
authentication, which was set for enclosing scopes.
- OAuth2 scopes can now be set at operation level (though, the code
doing this in OpenAPI emitter is a bit clunky).
- New `NoAuth` authentication option allows to declare optional
authentication (`NoAuth | AnyOtherAuth`) or override authentication to
none in nested scopes.

This implementation does not introduce new `@authScopes` decorator as
design doc comments suggest, and here's why:

1. It does not compose well with `@useAuth` at operation level. For
example
```
...
@useAuth(BasicAuth)
@authScopes(MyOauth2, ["read"])
op gogo(): void
```
Should that be equivalent to `BasicAuth | MyOauth2`, or to `[BasicAuth,
MyOauth2]`?

2. Introducing new decorator would increase complexity, but (imho) it
would not reduce the amount of boilerplate:
```
alias MyOAuth2 = OAuth2Auth<{ ... }>;

@useAuth(MyOAuth2)
@authAcopes(MyOauth2, ["read"])
@service
namepsace Foo;
```
vs
```
model MyOAuth2Flow<T extends string[]>  {  ...  };
alias MyOauth2<T extends string[]> = Oauth2Auth<[MyOauth2Flow[T]]>

@useAuth(MyOAuth2<["read"]>)
@service
namepsace Foo
```

I would be happy to hear any feedback and apply suggested changes.

And thanks for a convenient development setup and thorough test
coverage!

---------

Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
2024-02-27 07:39:30 -08:00
.chronus Operation level authentication and scopes (#2901) 2024-02-27 07:39:30 -08:00
.github Update with fix (#2880) 2024-02-01 15:54:11 -08:00
.vscode Enable spell check for all files and fix (#2881) 2024-02-08 16:54:23 -08:00
docker Clean up capitalization, parentheses, and phrasing in docs (#2817) 2024-01-25 10:40:16 -08:00
docs Operation level authentication and scopes (#2901) 2024-02-27 07:39:30 -08:00
e2e Add e2e test for the CLI (#2878) 2024-02-09 16:41:52 +00:00
eng Sign VSCode and Visual Studio extensions & pipeline organization (#2913) 2024-02-15 19:24:06 +00:00
grammars Include tmlanguage computed file in repo as we'll need it for github linguist (#2872) 2024-02-09 04:07:46 +00:00
icons/raw Add icon to website and vscode extension (#2829) 2024-01-25 11:25:28 -08:00
packages Operation level authentication and scopes (#2901) 2024-02-27 07:39:30 -08: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 Playground bundle storage (#2587) 2023-10-31 15:04:48 -07:00
.npmrc Get rid of rush and move to pnpm (#2775) 2024-01-24 13:37:34 -08:00
.prettierignore Include tmlanguage computed file in repo as we'll need it for github linguist (#2872) 2024-02-09 04:07:46 +00:00
.prettierrc.json Get rid of rush and move to pnpm (#2775) 2024-01-24 13:37:34 -08:00
CODE_OF_CONDUCT.md Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
CONTRIBUTING.md More rush cleanup - bundler uploader (#2841) 2024-01-26 23:02:45 +00:00
LICENSE Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
README.md Simplify Readme (#2864) 2024-01-30 16:18:37 -08:00
SECURITY.md Add LICENSE, CODE_OF_CONDUCT.md, and SECURITY.md 2021-10-19 14:53:13 -05:00
cspell.yaml Sign VSCode and Visual Studio extensions & pipeline organization (#2913) 2024-02-15 19:24:06 +00:00
package.json Fix: Bumping prerelease count changes from chronus (#2918) 2024-02-14 15:40:39 -08:00
pnpm-lock.yaml Fix: Bumping prerelease count changes from chronus (#2918) 2024-02-14 15:40:39 -08:00
pnpm-workspace.yaml Get rid of rush and move to pnpm (#2775) 2024-01-24 13:37:34 -08:00
troubleshooting.md 1/3 TypeSpec: Find/Replace Content 2023-02-16 13:07:22 -08:00
tsconfig.base.json Migrate test framework to vitest (#2769) 2024-01-02 11:40:29 -08:00
tsconfig.json Migrate test framework to vitest (#2769) 2024-01-02 11:40:29 -08:00
tsconfig.ws.json Migrate test framework to vitest (#2769) 2024-01-02 11:40:29 -08:00
vitest.workspace.ts Clean up capitalization, parentheses, and phrasing in docs (#2817) 2024-01-25 10:40:16 -08:00

README.md

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.