npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marianmeres/http-utils

v1.15.0

Published

Misc DRY http fetch related helpers

Downloads

191

Readme

@marianmeres/http-utils

A few opinionated sweet fetch helpers.

Example

import { HTTP_ERROR, HTTP_STATUS, createHttpApi } from '@marianmeres/http-utils';

// create api helper
const api = createHttpApi(
    // optional base url
    'https://api.example.com',
    // optional lazy evaluated default fetch params (can be overridden per call)
    async () => ({
        token: await getApiTokenFromDb() // example
    })

// EXAMPLE: assuming `/resource` returns json {"some":"data"}
const r = await api.get('/resource');
assert(r.some === 'data');

// EXAMPLE: assuming `/foo` returns 404 header and json {"message":"hey"}
// by default always throws
try {
    const r = await api.get('/foo');
} catch (e) {
    // see HTTP_ERROR for more
    assert(e instanceof HTTP_ERROR.NotFound);
    assert(e.toString() === 'HttpNotFoundError: Not Found');
    assert(e.status === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.CODE);
    assert(e.statusText === HTTP_STATUS.ERROR_CLIENT.NOT_FOUND.TEXT);
    // `body` is a custom prop containing the raw http response body text (JSON.parse-d if available)
    assert(e.body.message === 'hey');
    // `cause` is a standart Error prop, containing here some default debug info
    assert(err.cause.response.headers)
}

// EXAMPLE: assuming `/foo` returns 404 header and json {"message":"hey"}
// will not throw if we pass false flag
const r = await api.get('/foo', { assert: false });
assert(r.message === 'hey');

// EXAMPLE: assuming POST to `/resource` returns OK and json {"message":"created"}
// the provided token below will override the one from the `getApiTokenFromDb()` call above
const r = await api.post('/resource', { some: 'data' }, { token: 'my-api-token' });
assert(r.message === 'created');

// EXAMPLE: raw Response
const r = await api.get('/resource', { raw: true });
assert(r instanceof Response);

// EXAMPLE: access to response headers
let respHeaders = {};
const r = await api.get('/resource', null, respHeaders);
assert(Object.keys(respHeaders).length)

See HTTP_STATUS and HTTP_ERROR for more.