Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,14 @@ A REST service in TypeSpec is defined using the [`@service`](../../../standard-l

Let's start by defining a simple REST service for a Pet Store:

```tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;
// highlight-start
@service(#{
title: "Pet Store",
})
@server("https://example.com", "Single server endpoint")
// highlight-end
```diff lang=tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;
+@service(#{
+ title: "Pet Store",
+})
+@server("https://example.com", "Single server endpoint")
```

In this example:
Expand All @@ -121,16 +119,15 @@ In this example:

Let's create a namespace for our Pet Store service:

```tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")

// highlight-next-line
namespace PetStore;
```diff lang=tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")

+namespace PetStore;
```

In this example:
Expand All @@ -150,31 +147,29 @@ In TypeSpec, a [model](../../language-basics/models.md) is a fundamental buildin

Let's define a simple model for a `Pet`:

```tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")
namespace PetStore;

// highlight-start
model Pet {
id: int32;
name: string;
age: int32;
kind: petType;
}

enum petType {
dog: "dog",
cat: "cat",
fish: "fish",
bird: "bird",
reptile: "reptile",
}
// highlight-end
```diff lang=tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")
namespace PetStore;

+model Pet {
+ id: int32;
+ name: string;
+ age: int32;
+ kind: petType;
+}
+
+enum petType {
+ dog: "dog",
+ cat: "cat",
+ fish: "fish",
+ bird: "bird",
+ reptile: "reptile",
+}
```

In this example:
Expand All @@ -189,38 +184,35 @@ In this example:

We can add [validation](../../../language-basics/values/#validation) annotations to our model properties to enforce certain constraints:

```tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")
namespace PetStore;

model Pet {
id: int32;

// highlight-next-line
@minLength(1)
name: string;

// highlight-next-line
@minValue(0)
// highlight-next-line
@maxValue(100)
age: int32;

kind: petType;
}

enum petType {
dog: "dog",
cat: "cat",
fish: "fish",
bird: "bird",
reptile: "reptile",
}
```diff lang=tsp title=main.tsp tryit="{"emit": ["@typespec/openapi3"]}"
import "@typespec/http";

using Http;

@service(#{ title: "Pet Store" })
@server("https://example.com", "Single server endpoint")
namespace PetStore;

model Pet {
id: int32;

+ @minLength(1)
name: string;

+ @minValue(0)
+ @maxValue(100)
age: int32;

kind: petType;
}

enum petType {
dog: "dog",
cat: "cat",
fish: "fish",
bird: "bird",
reptile: "reptile",
}
```

In this example:
Expand Down
Loading