tra-typed-fetch
TypeScript icon, indicating that this package has built-in type declarations

1.4.1 • Public • Published

tra-typed-fetch

Table of Contents

About

A package that implements common http requests using fetch, allowing for typed responses with TypeScript.

Installing

npm install tra-typed-fetch

yarn add tra-typed-fetch

Usage

import { createHttpClient, get } from '@tra/typed-fetch';

interface Product {
  name: string;
  id: number;
}

type Products = Product[];

const response = await get<Products>({ url: '/products' });

// Where data will be the typed value bound to the function in the Response
const { data, headers, status } = response;

/* 
Optional exported method to preset headers and baseUrl on each request
Note: currently only supports 1 http client. Existing calls to this function will return cached http client if exists.
*/
interface IRequestConfiguration {
  baseUrl?: string;
  defaultHeaders?: Request['headers'];
}

const { baseConfiguration, get, destroy, patch, post, put } = createHttpClient({
  baseUrl: 'localhost:3000/api/v1/',
  defaultHeaders: new Headers({ 'content-type': 'application/json' }),
});

Package Sidebar

Install

npm i tra-typed-fetch

Weekly Downloads

44

Version

1.4.1

License

MIT

Unpacked Size

41.1 kB

Total Files

48

Last publish

Collaborators

  • andersontr15