Versioning
Introduction
In this section, we’ll focus on implementing versioning in your REST API. Versioning allows you to manage changes to your API over time without breaking existing clients. We’ll introduce the @versioned
decorator, show how to define versions with enums, and demonstrate how to use the @added
decorator to specify version-specific models and operations.
Adding the Versioning Library
Before we can use the versioning decorators, we need to add the @typespec/versioning
library to our project. This involves updating the package.json
file and installing the necessary dependencies.
Step 1: Update package.json
Add the @typespec/versioning
library to your package.json
file, in both the peerDependencies
and devDependencies
sections. Your updated package.json
should look like this:
Step 2: Install Dependencies
Run the following command to install the new dependencies:
Introduction to the @versioned
Decorator
The @versioned
decorator is used to define different versions of your API. This decorator allows you to specify the versions that your API supports and manage changes across these versions.
Example: Defining API Versions
Let’s define two versions of our API, v1
and v2
:
In this example:
- We’re importing and using a new module,
@typespec/versioning
, which provides versioning support. - The
@versioned
decorator is used to define the versions supported by the API, defined in theVersions
enum. - The
Versions
enum specifies two versions:v1
(1.0) andv2
(2.0).
Generating OpenAPI Specifications for Different Versions
Once we start adding versions, the TypeSpec compiler will generate individual OpenAPI specifications for each version. In our case, it will generate two OpenAPI specs, one for each version of our Pet Store service API. Our file structure will now look like this:
- main.tsp
- tspconfig.yaml
- package.json
Directorynode_modules/
- …
Directorytsp-output/
Directory@typespec/
Directoryopenapi3
- openapi.1.0.yaml
- openapi.2.0.yaml
Generating separate specs for each version ensures backward compatibility, provides clear documentation for developers to understand differences between versions, and simplifies maintenance by allowing independent updates to each version’s specifications.
By encapsulating different versions of the API within the context of the same TypeSpec project, we can manage all versions in a unified manner. This approach makes it easier to maintain consistency, apply updates, and ensure that all versions are properly documented and aligned with the overall API strategy.
Using the @added
Decorator
The @added
decorator is used to indicate that a model or operation was added in a specific version of the API. This allows you to manage changes and additions to your API over time.
Example: Adding a New Model in a Specific Version
Let’s add a Toy
model that is only available in version 2 of the API:
In this example:
- The
Toy
model is defined with the@added(Versions.v2)
decorator to indicate that it was added in version 2 of the API.
Version-Specific Operations
Let’s define version-specific operations to manage toys for pets. These operations will only be available in version 2 of the API.
Example: Adding Version-Specific Operations
In this example:
- The
Toys
namespace is defined under thePets
namespace. - The
@added(Versions.v2)
decorator is applied to the operations within theToys
namespace to indicate that they were added in version 2 of the API. - The
Toys
namespace includes operations to list, create, update, and delete toys for a specific pet. These operations are only available in version 2 of the API.
Conclusion
In the next section, we’ll dive into creating custom response models for your REST API.