# Decorator Status

Decorator swagger schema response operation
Module
import { Status } from "@tsed/schema"
Source/packages/specs/schema/src/types/decorators/operations/status.ts

# Overview

function Status(status: string | number, model?: Type<any> | any): ReturnsChainedDecorators;

# Description

Add responses documentation for a specific status code.

# Usage

Ts.ED v6 API introducing the chaining decorator concept. Now a decorator like Returns can be used with another decorators like Description.

import {Returns} from "@tsed/schema";

@Controller("/")
class MyController {
  @Status(404, String).Description("Not Found")
  @Status(200, Model).Description("Success")
  async myMethod(): Promise<Model> {}
}
1
2
3
4
5
6
7
8

TIP

TypeScript and your IDE will discover automatically the chained decorators. But for more details you can look on ReturnsChainedDecorators interface, to know what chained decorators are available under Returns decorator.

This example will produce this documentation in swagger:

{
  "responses": {
    "404": {
      "description": "Description",
      "schema": {"type": "string"}
    },
    "2OO": {
      "description": "Description",
      "schema": {"$ref": "..."}
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12

# Declaring a generic model

Sometime, it might be useful to use generic models. TypeScript doesn't store the generic type in the metadata. This is why we need to explicitly the generic models with the decorators.

One of the generic's usage, can be a paginated list. With Returns decorator it's now possible to generic type and generate the appropriate OpenSpec documentation.

Starting with the pagination model, by using Generics and CollectionOf :

@Generics("T")
class Pagination<T> {
 @CollectionOf("T")
 data: T[];

 @Property()
 totalCount: number;
}
1
2
3
4
5
6
7
8

Now, we need a model to be used with the generic Pagination model:

class Product {
 @Property()
 id: string;

 @Property()
 title: string;
}
1
2
3
4
5
6
7

Finally, we can use our models on a method as following:

class Controller {
  @OperationPath("POST", "/")
  @Status(200, Pagination).Of(Product).Description("description")
  async method(): Promise<Pagination<Product> | null> {
    return null;
  }
}
1
2
3
4
5
6
7

# Declaring a nested generics models 6+

It's also possible to a nested generics models in order to have this type Pagination<Submission<Product>>:

import {Generics, Property, Returns} from "@tsed/schema";
import {Post} from "@tsed/common";

class Controller {
  @Post("/")
  @Status(200, Pagination).Of(Submission).Nested(Product).Description("description")
  async method(): Promise<Pagination<Submission<Product>> | null> {
    return null;
  }
}
1
2
3
4
5
6
7
8
9
10

And here is the Submission model:

import {Generics, Property} from "@tsed/schema";

@Generics("T")
class Submission<T> {
  @Property()
  _id: string;
  @Property("T")
  data: T;
}
1
2
3
4
5
6
7
8
9