@mersocarlin/fetch
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

@mersocarlin/fetch

fetch wrapper on top of isomorphic-fetch.

Motivation

I needed a consistent fetch wrapper that always returns the same structure (body and statusCode) for all API calls throughout my projects.

Basic error handling was also a "must-have" and I ended up duplicating the same code in every new project/repository I was working on.

I've also written this code a long time ago but never had the chance to give it the attention it deservs + open source it.

It works seamless with @mersocarlin/mersocarlin/api-error so I get named API errors for free.

Install

yarn add @mersocarlin/fetch
npm i @mersocarlin/fetch

Usage

import fetch from '@mersocarlin/fetch'

const login = async (username: string, password: string) => {
  try {
    const { body, statusCode } = await fetch('https://example.com/api/signin', {
      body: {
        username,
        password,
      },
      method: 'post',
    })

    return body
  } catch(error) {
    throw error
  }
}

const fetchPrivateUsers = async () => {
  try {
    const { body, statusCode } = await fetch('https://example.com/api/users', {
      headers: {
        'Authorization': 'Bearer TOKEN',
      }
    })

    return body
  } catch(error) {
    throw error
  }
}

const fetchPublicUsers = async () => {
  try {
    const { body, statusCode } = await fetch('https://example.com/users')

    return body
  } catch(error) {
    throw error
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @mersocarlin/fetch

Weekly Downloads

0

Version

0.0.5

License

MIT

Unpacked Size

7.76 kB

Total Files

5

Last publish

Collaborators

  • mersocarlin