Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EndpointBuilder<T>

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.

Type parameters

Hierarchy

  • EndpointBuilder

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

def

def: Readonly<T>

Methods

body

  • 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:

    1. 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'
           })
    2. 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)
    3. (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.

    Type parameters

    • U

    Parameters

    • body: U

    Returns EndpointBuilder<RemoveKey<T, "body"> & object>

method

query

  • Add query parameters.

    Note that query parameters are always optional.

    Example:

        GET `/users/search`
            .query({
                'order': 'string',
                'filter': 'string'
            })

    Type parameters

    Parameters

    • query: U

      type of the query parameters.

    Returns EndpointBuilder<RemoveKey<T, "query"> & object>

response

  • response<U>(response: U): EndpointBuilder<RemoveKey<T, "response"> & object>
  • 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:

    1. 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'
           })
    2. 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)
    3. (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 parameters

    • U

    Parameters

    • response: U

      type of the response data.

    Returns EndpointBuilder<RemoveKey<T, "response"> & object>

Generated using TypeDoc