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

svelte-fetch

v0.2.0

Published

A `fetch` client with Svelte store integration for request states.

Downloads

4

Readme

SVELTE-FETCH

A very thin wrapper around fetch with Svelte store integration for request states.

Why

When making a web app, I always end up adding ongoing requests to some central state array so that I can compute when my application is "loading". That's basically what this is: a thin wrapper around the native fetch API, with importable Svelte readables that can be used to show spinners and blocking modals for various request states.

Install

npm i -S svelte-fetch or yarn add svelte-fetch

Usage

<script>

    import Fetch, {
        hasAny,
        hasBlocking,
        hasBackground
    } from "svelte-fetch"

    const fetch = new Fetch()

    const [res1, res2, res3] = await Promise.all([
        // Makes a regular request (typically shows a spinner in the UI)
        fetch.get("http://localhost:3000/dev/ping?delay=1000"),
        //Makes a blocking request (typically block UI interaction)
        fetch.blocking.get("http://localhost:3000/dev/ping?delay=1000"),
        //Makes a background request (typically invisible to the UI)
        fetch
            .background
            .expect(JSON)
            .get("http://localhost:3000/dev/ping?delay=1000"),
    ])

    console.log(res1, res2, res3)

</script>

{#if $hasAny}
    Loading!
{/if}

{#if $hasBlocking}
    Loading (blocked)!
{/if}

{#if $hasBackground}
    Loading (background)!
{/if}

API

SvelteFetch.request

A wrapper around fetch.

SvelteFetch.get

Shorthand for using SvelteFetch.request with the {method:"GET"} option.

SvelteFetch.put

Shorthand for using SvelteFetch.request with the {method:"PUT"} option.

SvelteFetch.post

Shorthand for using SvelteFetch.request with the {method:"POST"} option.

SvelteFetch.destroy

Shorthand for using SvelteFetch.request with the {method:"DELETE"} option.

SvelteFetch.background

Adds the next request to the background queue (available from the export hasBackground), and removes it when it completes.

    const fetch = new SvelteFetch()
    
    const data = await fetch.background.get(`https://endpoint`)

SvelteFetch.blocking

Adds the next request to the blocking queue (available from the export hasBlocking), and removes it when it completes.

    const fetch = new SvelteFetch()
    
    const data = await fetch.blocking.get(`https://endpoint`)

SvelteFetch.expect

Tells the request what data to expect next. Supports:

  • JSON
  • Number (will use the JSON parser)
  • String
  • Image
  • Blob If SvelteFetch.expect is not used, data from the request is parsed using the Content-Type header.
    const fetch = new SvelteFetch()
    
    const data = await fetch.expect(JSON).get(`https://endpoint`)