Change the type of the request body.
You must provide a real object whose type will be inferred and used as the response type for this endpoint. Rest.ts offers multiple ways to do this:
Use a regular object.
For instance, if you need a string, use 'string'
, if you need a number, use 123
.
This also works for complex objects like so:
POST `/current-user`
.body({
id: 'string',
kind: 'person' as 'person' | 'robot' | 'cat'
})
Some people like to use classes to define their DTOs. If this is your situation, you may just put the class constructor here, and the instance type will be inferred.
class CurrentUserDTO {
id: string;
kind: 'person' | 'robot' | 'cat';
}
POST `/current-user`
.body(CurrentUserDTO)
(Preferred method) Use a Runtype. runtypes is a library that allows you to create type definitions with runtime type metadata to ensure that input data conforms to an expected type. Rest.ts has first-class support for runtypes:
const CurrentUserDTO = rt.Record({
id: rt.String,
kind: rt.Union(
rt.String('person'),
rt.String('robot'),
rt.String('cat')
)
});
POST `/current-user`
.body(CurrentUserDTO)
rest-ts-express automatically type-checks incoming data when the body of the endpoint definition is a runtype.
Change the HTTP method of this endpoint
Any valid HTTP method. See HttpMethod.
Add query parameters.
Note that query parameters are always optional.
Example:
GET `/users/search`
.query({
'order': 'string',
'filter': 'string'
})
type of the query parameters.
Change the type of the response.
You must provide a real object whose type will be inferred and used as the response type for this endpoint. Rest.ts offers multiple ways to do this:
Use a regular object.
For instance, if you need a string, use 'string'
, if you need a number, use 123
.
This also works for complex objects like so:
GET `/current-user`
.response({
id: 'string',
kind: 'person' as 'person' | 'robot' | 'cat'
})
Some people like to use classes to define their DTOs. If this is your situation, you may just put the class constructor here, and the instance type will be inferred.
class CurrentUserDTO {
id: string;
kind: 'person' | 'robot' | 'cat';
}
GET `/current-user`
.response(CurrentUserDTO)
(Preferred method) Use a Runtype. runtypes is a library that allows you to create type definitions with runtime type metadata to ensure that input data conforms to an expected type. Rest.ts has first-class support for runtypes:
const CurrentUserDTO = rt.Record({
id: rt.String,
kind: rt.Union(
rt.String('person'),
rt.String('robot'),
rt.String('cat')
)
});
GET `/current-user`
.response(CurrentUserDTO)
type of the response data.
Generated using TypeDoc
Builder class to create endpoint definitions.
This class provides a high-level user friendly interface to create a typed definition of an API endpoint.
Endpoint definitions are usually grouped together in API definitions, which can be shared between producers and consumers of the API to create a type-safe communication channel.
You will generally not need to create this class explicitly. Instead, use the helper methods GET, PUT, PATCH, POST or DELETE to create an instance of EndpointBuilder.