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

snag-query

v1.0.3

Published

Get the data you need with less code

Downloads

7

Readme

GitHub Repository

Install

npm:

npm i snag-query

pnpm:

pnpm i snag-query

HOW FETCH

Create a new object Snag and write the general config for that object.

const snag = new Snag({
	URL: 'https://pokeapi.co/api/v2/',
	header: 'JSON'
})

NOTE: Currently, the header property only accepts 'JSON' value. The URL property can be undefined.

Without config

const snag = new Snag({})

The Snag object will return 3 methods to make the fetch you want.

getSnag()

This makes just one fetch with GET method and returns an object with data value and method to refetch the same data if you need.

Params

path: Write the path of the URL configured in object Snag or URL to make a fetch (URL has priority over path).

format: Receive a callback to return the shape of the object.

const { data, refetch } = snag.getSnag<ValueWillReturnType>({
path: 'pokemon/ditto'
format: (pokemon: JSONFormAPIType) => {
	return {
		name: pokemon.name,
		id: pokemon.id
	}
} 
})

This will return

{ 
	name: 'ditto',
	id: 132 
}

If you want the whole JSON, keep the format param empty.

NOTE: This method have a property header too.

getSnags()

The difference with getSnag() method is that you can make much fetch at once.

Params

paths: Receive an Array with paths of the base URL of the object, it's works like path param from getSnag() method.

format: Receive a callback to return the shape of the objects that will be within an Array.

createPathsFn: if you don't have an Array with paths within, you can create it in this param.

const { data, refetch } = snag.getSnags<ValueWillReturnType>({
paths: ['/pokemon/1','/pokemon/4','/pokemon/7'],
format: (pokemon: JSONFormAPIType) => {
	return {
		name: pokemon.name,
		id: pokemon.id
	}
}
})

This will return

[
	{
		name: 'bulbasaur',
		id: 1
	},
	{
		name: 'charmander',
		id: 4
	},
	{
		name: 'squirtle',
		id: 7
	}
]

Or You can try it too


const { data, refetch } = snag.getSnags<ValueWillReturnType>({
	format: (pokemon: JSONFormAPIType) => {
		return {
			name: pokemon.name,
			id: pokemon.id
		}
	}
	createPathsFn: () => {
		return Array.from({lenght: 3}, (_,i) => {
			return `/pokemon/${i}`
		})
	}
})

This will return

[
	{
		name: 'bulbasaur',
		id: 1
	},
	{
		name: 'ivysaur',
		id: 2
	},
	{
		name: 'venusaur',
		id: 3
	}
]

If you want the whole JSON, keep the format param empty.

NOTE: This method have a property header too.

mutateSnag()

The method mutateSnag() can fetch with others methods (POST, PATCH, PUT, DELETE) and can receive a body, but works a bit different, It return an object that contain another method. With this you can make the fetch.

mutateSnag() params

path: Write the path of the URL configured in object Snag or URL to make a fetch (URL has priority over path).

method: The method of fetching you have to do (POST, PATCH, PUT, DELETE), the default value is POST.

NOTE: This method have a property header too.

mutate() Params

format: This receive a callback to return the shape of the object.

body: Body of query, there's no need to use JSON.stringify()

const btn = document.querySelector('.btn')

const snag = new Snag({
	URL: 'https://reqres.in/'
	header: 'JSON'
})
const snagMutation = snag.mutateSnag<ValueWillReturnType>({
	path: '/api/users'
	})
	
btn.addEventListener('click', () => {
	const queryResult = snagMutation.mutate({
		body: {
		    name: "midudev",
		    job: "web developer"
		}
		format: (obj: JSONFormAPIType) => {
			name: obj.name
		}
	})
})

This will return

{
	name: 'midudev'
}

If you want the whole JSON, keep the format param empty.

Snag-query is in beta version