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

vue-autofetch

v2.0.0

Published

Better and Vue-style data fetching

Downloads

13

Readme

Installation

$ yarn add vue-autofetch
# or
$ npm i --save vue-autofetch

Getting Started

Vue Plugin

import Vue from 'vue'
import * as AutoFetch from 'vue-autofetch'

Vue.use(AutoFetch)

// use with in any place of the app

Local Component Registration

import AutoFetch from 'vue-autofetch'

export default {
  components: {
    AutoFetch
  },

  // ...
}

API

Props:

  • clear [default: false] - if true, then set response and error to null if new request has started.
  • data [required] - request data, passed to handler. If changes, then component automatically downloads new data.
  • handler [default: fetch] - function which is called in order to do request. Should return a promise. If resolved, then state is changed to success and result is passed to result variable. If rejected, then state is changed to failure and error is passed to error variable.
  • threshold [default: fn => () => fn()] - function that controls frequency of requests (for example debounce or throttle from lodash). It receives a function [1] which should return a function which calls function [1].

Methods:

  • refresh - reload request.

Events:

  • success - called when received a successful response. Arguments: response.
  • failure - called when request failed. Arguments: error.
  • start - called when request has started (state set to 'loading'). Arguments: empty.
  • done - called when request is resolved or errored. Arguments: success (true or false).

Usage

The component can be used in several ways:

<!-- as a renderless component -->
<template>
  <div>
    <auto-fetch ref="items" v-model="items" :data="request" />

    <input v-model="page" />

    <template v-if="items.state === 'loading'">
      Loading...
    </template>
    <template v-else-if="items.state === 'success'">
      Success: {{ items.result }}
    </template>
    <template v-else-if="items.state === 'failure'">
      <div>
        Error: {{ items.error }}
        <button @click="$refs.items.refresh()">Refresh</button>
      </div>
    </template>
  </div>
</template>

<!-- as a component with default slot -->
<template>
  <auto-fetch :data="request" v-slot:default="{ state, result, error }">
    <input v-model="page" />

    <template v-if="state === 'loading'">
      Loading...
    </template>
    <template v-else-if="state === 'success'">
      Success: {{ result }}
    </template>
    <template v-else-if="state === 'failure'">
      <div>
        Error: {{ error }}
        <button @click="refresh">Refresh</button>
      </div>
    </template>
  </auto-fetch>
</template>

<!-- as a component with slots for different states -->
<template>
  <div>
    <input v-model="page" />

    <auto-fetch :data="request">

      <template v-slot:loading>
        Loading...
      </template>

      <template v-slot:success="{ result }">
        Success: {{ result }}
      </template>

      <template v-slot:failure="{ error, refresh }">
        <div>
          Error: {{ error }}
        </div>
        <button @click="refresh">Refresh</button>
      </template>
    </auto-fetch>
  </div>
</template>

<script>
import AutoFetch, { createRequestHandle } from 'vue-autofetch'

export default {
  components: {
    AutoFetch
  },

  data: () => ({
    page: 1,

    items: createRequestHandle()
  }),

  computed: {
    request () {
      // NOTE: By default, JavaScript fetch() is used (url and rest as arguments)

      return {
        url: 'https://example.com/api/some-method',

        method: 'POST',

        data: {
          page: this.page
        }
      }
    }
  }
}
</script>

As I stated before, fetch() is used, but you should override it and even provide some throttling. You can do this by extending auto-fetch component or passing some global variable as props: <auto-fetch v-model="items" :data="request" v-bind="$request" />.

Example of global mixin for this:

import _ from 'lodash'
import Vue from 'vue'

Vue.mixin({
  computed: {
    $request () {
      return {
        handler: data => {
          // Do the request, return a promise
        },

        threshold: fn => _.debounce(fn, 300, {
          leading: true,
          trailing: true
        })
      }
    }
  }
})

License

Released under the MIT license.