# Introduction

# Route

This is Pulse's integrated fetch API. It's a wrapper around Fetch, which is native to the browser environment.

# Setup

The route function returns a Route which can be configured and used to make HTTP requests. The basic config is just the baseurl and any fetch config options. There is also a value for timeout, which will set a timeout for the request. This function will return another function to use to make requests in your actions or anywhere else in your app!

const MyAPI = route({
  baseURL: 'http://localhost:3000', // default: 'https://localhost'
  options: {
    headers: {
      'Content-Type': 'application/json'
    }
  },
  timeout: 20000, // default: infinite
})

# Use

The returned function accepts a path, and another opportunity to modify the fetch config.

const data = await MyAPI('get-data', {
  method: 'POST', // default: 'GET'
  body: {
    data: 'Hello World!'
  },
  params: {}, // url params
  query: {}, // url query
  options: {
    headers: {
      'Content-Type': 'application/json, text/plain' // this is not necessary
    }
  },
});

# Route Config Parameters

  • options (RequestOptions) optional - This has all the same options as the js fetch function. Nothing is required, but it is recommended depending on your setup
  • baseURL (string) optional - The base url for the endpoint which defaults to your local system.
  • timeout (number) optional - A timeout for the fetch. By default it does not have a timeout so if you are unsure if the request will complete, you should set this.

# Request Options

Note: Refer to Fetch Documentation for More Info.

For more information on the options available, please refer to the Fetch API (opens new window) page, as we use this under the hood.

# Response

Every API method returns a pulse response which allows for easy interpretation and manipulation of the incoming data.

interface PulseResponse<DataType = any> {
  data: Object;
  timedout?: boolean;
  status: number;
  raw?: Response;
  type?: string;
}