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

@akshay-nm/confetch

v0.8.4

Published

Configur-ed/able fetch

Downloads

60

Readme

Confetch

Configur-ed/able fetch

code style: prettier codecov

tldr;

To get started:

npm install @akshay-nm/confetch

or

yarn add @akshay-nm/confetch

This is a wrapper over window.fetch. Go through https://github.com/akshay-nm/confetch-example and Confetch manual for more details.

This package also has a default responseHandler which you can use. The responseHandler just resolves the res.json() promise if possible, otherwise throws the errors based on statusCodes. You can pass the errors as an object.

For the bravehearts

VOLATILE PACKAGE (RAPIDLY CHANGING API so please bear with me till we reach v1.0.0) v0.x.0 to v0.y.0 means some features/methods have been deprecated v0.x.y to v0.x.z means a new feature/method has been added without changing existing ones

const { configureConfetch } = require('@akshay-nm/confetch')
const { configureStatusCodeBasedErrors } = require('@akshay-nm/confetch').handlers

// The new object is merged (it does not replace the default status codes configuration).
configureStatusCodeBasedErrors({
  400: 'CUSTOM ERROR MESSAGE TO BE THROWN IF THE RESPONSE CODE IS 400',
}) // This will make sure that all requestHandlers throw error with the custom message whenever the resposne status code is 400

configureConfetch({
  baseUrl: 'YOUR_BASE_URL', // this is the only parameter you NEED to pass (otherwise confetch will throw)
})

...

const { buildResponseHandler } = require('@akshay-nm/confetch').handlers
const { getUrlFromPath } = require('@akshay-nm/confetch').utils

const responseHandler1 = buildResponseHandler() // This will throw errors preconfigured message
const responseHandler2 = buildResponseHandler({ 400: 'OTHER CUSTOM MESSAGE' }) // This will throw error with 'OTHER CUSTOM MESSAGE' on 400

confetch({
  url: getUrlFromPath('YOUR_PATH?YOUR_QUERY'),
  ... // rest of parameters for fetch
}).send().then(responseHander1).catch(console.log) // this will log the global message in case of a 400 (configured using configureStatusCodeBasedErrors)

confetch({
  url: getUrlFromPath('YOUR_PATH?YOUR_QUERY'),
  ... // rest of parameters for fetch
}).send().then(responseHander1).catch(console.log) // this will log 'OTHER CUSTOM MESSAGE' in case of 400 (configured via buildResponseHandler)

Since the response handler just tries to convert the response into JSON, it either returns a promise or just throws the errors.

Following configuration options are supported:

  • baseUrl: You can specify a base url and use the getUrlFromPath method to get complete urls for your requests.
  • timeoutDuration: This is the duration after which your requests will be aborted.
  • headers: This is an object (just like what you would pass to a simple fetch call).

All the configuration parameters are merged with info object passed to a confetch call. So you can override the default paramaters while creating configured fetch requests, if that's what you want.

The package also exposes confetch method which basically returns an object with 3 properties:

  • request
  • send
  • abort
  • controller
  • signal
  • timeout

The ones which you should be interested in are:

  • send
  • abort

You call send to send the configured requests and abort to manually abort the request. Both of them return a promise.

So this is how it would look like:

const { configureConfetch } = require('@akshay-nm/confetch')
confetchConfiguration = {
  baseUrl: 'https://some.org',
  timeoutDuration = 3000,
  headers = {
    'Content-Type': 'application/json'
  }
}
configureConfetch(confetchConfiguration)

...
const { confetch } = require('@akshay-nm/confetch')
const info = {
  url: getUrlFromPath(`/some-path?some=query`),
  method: 'GET',
  headers: {
    'Some-Request-Specific-Header': 'ITS_VALUE'
  }
}

// initialise, send, abort
const request = confetch(info)
request.send()
  .then(res => {
    /*do something with res/*
  })
  .catch(error => {
    /*handle error*/
  })
request.abort()

// fire and forget
confetch(info).send()
  .then(res => {
    /*do something with res/*
  })
  .catch(error => {
    /*handle error*/
  })

What's next?

  • Some pre-configured fetches