You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@eis/fetchutils

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eis/fetchutils

A typescript wrapper for the fetch API

0.1.9
latest
Source
npm
Version published
Weekly downloads
6
-14.29%
Maintainers
1
Weekly downloads
 
Created
Source

What is this?

fetchutils is a wrapper around Javascript's fetch API that attempts to solve a few common issues:

  • fetch does not throw for non-200 status codes
  • fetch requires a separate async call to .json() or .text() to read the body

Example Usage

interface Book {
    id: string;
    title: string;
    author: string;
}

const client = new HttpClient('http://example.com', {
    headers: { authorization: "Bearer <token>" }
})

async function doStuff() {
    try {

        // Pass the expected type of the return value.
        // No need to call response.json() or check response.ok here.
        const books = await client.get<Book[]>('/books');

        // Access the result
        books.forEach(book=> {
            console.log('id', book.id);
            console.log('author', book.author);
            console.log('title', book.title);
        });

    } catch (e: unknown) {
        // get() will wrap any non-2xx status codes and throw an instance
        // of HttpError. If the response body was a json object, the message
        // will be the serialized json value. Otherwise, the body text or
        // the http status text is returned.

        if (e instanceof HttpError){
            console.log(`Error ${e.code}: ${e.message}`);
        }

        throw e;
    }
}

POST

interface Book {
    id: string;
    title: string;
    author: string;
}

const client = new HttpClient('http://example.com', {
    headers: { authorization: "Bearer <token>" }
})

async function doStuff() {
    try {

        // you can call post() like this:
        // const books = await client.post<Book>('/books', {
        //     body: JSON.stringify({
        //         title: 'my new book',
        //         author: 'myself',
        //     })
        // });

        // or you can call the postJson() shortcut:
        const book = await client.postJson<Book>('/books', {
            title: 'my new book',
            author: 'myself',
        });

        // you can also postJson() without a response:
        // await client.postJson('/books', {
        //     title: 'my new book',
        //     author: 'myself',
        // });

        console.log(`Created book ${book.id}`)

    } catch (e: unknown) {
        // ...
    }
}

There are similar APIs for PUT and DELETE requests.

Keywords

fetch

FAQs

Package last updated on 20 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts