Options
All
  • Public
  • Public/Protected
  • All
Menu

External module rest-ts-axios

Index

Type aliases

Functions

Type aliases

ApiConsumer

ApiConsumer: object

Thin wrapper around an instance of axios.

You can only obtain an ApiConsumer through createConsumer.

This object has a method defined for each endpoint of the API definition used to create it.

For instance, if your API definition has the following endpoints:

const myCustomAPI = defineAPI({
    listPublications: GET `/publications/${'category'}` .response(Publications),
    addPublication: POST `/publications` .body(Article) .response(PublicationResponse),
    removePublication: DELETE `/publications/${'id'}` .response(RemoveResponse)
});

Then, the ApiConsumer you obtain has the following methods:

consumer.listPublications({ params: { category: string } }) => { data: Publications }
consumer.addPublication({ body: Article }) => { data: PublicationResponse }
consumer.removePublication({ params: { id: string } }) => { data: RemoveResponse }

Note that, in addition to the types shown above, you may pass any option accepted in the method parameter, and the object you get in response is an actual axios response. These have been omitted from the example above for clarity. If you are interested in the exact type definiton, feel free to browse the source code. You will find that there is very little code logic, and that most of the work done by this module happens in the type system, not the runtime.

Type declaration

Functions

createConsumer

  • createConsumer<T>(apiDefinition: T, axios: AxiosInstance): ApiConsumer<T>
  • Bind an API definition to an instance of axios.

    Use this method as a factory to make ApiConsumers from ApiDefinitions.

    Use the returned object to make requests against the chosen API.

    Example

    // Step 1: Import the API definition you created with `rest-ts-core`
    import { myCustomAPI } from 'shared/apis/myCustomAPI';
    
    // Step 2: Create an axios instance with a given base URL.
    // You can also customize global settings such as authentication or custom headers.
    // Refer to the docs for axios to see all of the available settings.
    const driver = axios.instance({
      baseURL: 'http://localhost:3000/api',
      // You can add global settings here such as authentication headers
    });
    
    // Step 3: Bind the API definition to the axios instance
    const consumer = createConsumer(apiDefinition, myCustomAPI);
    
    // Step 4: You can now use this object in your application to make HTTP calls to your backend:
    const res = consumer.listAllPublications({
        // The parameters that you defined in your API will be required here.
        // However, this object is passed directly to your axios instance, and you can add
        // any parameter that axios accepts.
        params: {
            category: 'life sciences'
        }
    });

    Type parameters

    • T: ApiDefinition

    Parameters

    • apiDefinition: T

      The API definition to bind.

    • axios: AxiosInstance

      An axios instance

    Returns ApiConsumer<T>

Generated using TypeDoc