API Reference
Types

Exposed types

The following types can be imported from fetchtastic/core

HttpMethod

HttpMethod is a type alias that represents a single HTTP method, It includes the following methods:

  • OPTIONS
  • GET
  • HEAD
  • PUT
  • POST
  • DELETE
  • PATCH
import { HttpMethod } from 'fetchtastic';
 
const method: HttpMethod = 'GET';

ResponseParser

ResponseParser is a type alias that represents a single response type, It includes the following parsers:

  • ArrayBuffer
  • Blob
  • FormData
  • JSON
  • Text
import { ResponseParser } from 'fetchtastic';
 
const parser: ResponseParser = 'JSON';

SearchParamInput

SearchParamInput is a type alias that represents the different types of inputs that can be used for a search parameter. It can be one of the following types:

  • string
  • URLSearchParams
  • [string, string | boolean | number][]
  • Record<string, string | boolean | number>
import { SearchParamInput } from 'fetchtastic';
 
const input1: SearchParamInput = 'key=value';
const input2: SearchParamInput = new URLSearchParams('key1=value1&key2=value2');
const input3: SearchParamInput = [
  ['key', 'value'],
  ['key2', true],
];
const input4: SearchParamInput = { key: 'value', key2: 123 };

FetchtasticOptions

FetchtasticOptions is a type alias that represents the options that can be passed to the fetch function. It is defined as:

type FetchtasticOptions = Omit<RequestInit, 'signal' | 'method'>;

That means it includes all of the options from RequestInit except for signal and method

FetchOptions

FetchOptions is a type alias that extends RequestInit and includes the method property, which specifies the HTTP method to use for the request.

import { FetchOptions } from 'fetchtastic';
 
const options: FetchOptions = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ key: 'value' }),
  // ...
};

FetchRequestHeader

FetchRequestHeader is a type alias that represents the different types of headers that are most commonly included in a fetch request. It includes the following headers:

  • Accept
  • Content-Type
  • Authorization
  • User-Agent
  • Referer
  • Cache-Control
  • Accept-Encoding
  • Origin
  • Connection
  • Cookie
  • Pragma
  • If-Modified-Since
import { FetchRequestHeader } from 'fetchtastic';
 
const header: FetchRequestHeader = 'Accept';

DataAssertionFn

A function type that asserts and transforms data. It takes an unknown data parameter and returns a transformed value of type T.

import { DataAssertionFn } from 'fetchtastic';
 
const assertData: DataAssertionFn<number> = data => {
  if (typeof data === 'number') {
    return data * 2;
  }
  return 0;
};

This type can be used to define a function that validates the response data returned by a fetch request, for example by checking that it matches a particular interface or schema.