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

jsonp-retry

v1.0.3

Published

A simple jsonp with retry and storage

Downloads

10,263

Readme

jsonp-retry

JavaScript Style Guide

npm Build Status Code Climate Coverage Status npm

A simple implementation of jsonp, it provides timeout and retry function, when the main request is failed because of timeout, there will be many retries.

And the surprise was, you can store the response data to localStorage with jsonp-retry automatically, and then it can get the data from localStorage without request from network if it detects the store data is available.Of course, you can custom the store check rule.You will learn how to use this function from the below.

Happy to use!😘🤡

Installation

If you use webpack or browserify, you can install this package with npm or yarn

npm install jsonp-retry --save

or

yarn add jsonp-retry

Then require and use it


import jsonp from 'jsonp-retry'

jsonp('//example.com/xxx', {
  timeout: 3000
}, function (err, data) {
  if (err) {
    throw err
  }
  // Todo with data
})

or use in Promise way


import jsonp from 'jsonp-retry'

jsonp('//example.com/xxx', {
  timeout: 3000
}).then(data => {
  // Todo with data
}).catch(err => {
  throw err
})

Usage

jsonp-retry provides a flexible way of using it, usually you can pass an callback to process the request result, and more elegantly, if your environment support Promise, you can use jsonp-retry in Promise way.

Usually it accept 3 arguments, urloptscb, of course cb is not required.It will return an instance of Promise if your environment support.

jsonp(url, opts, cb?)

  • url(String): the url to fetch data
  • opts(Object): optional, config the jsonp method
    • jsonp(String): appoint the jsonp request url key (defaults to callback)
    • name(String): appoint the jsonp callback function (defaults to a string with prefix '__jsonp' and current time stamp like __jsonp1504108585883 )
    • params(String or Object): appoint the jsonp request url params, it can be a query string like a=1&b=2&c=3 , or it can be an object like { a: 1, b: 2 } ,it will transform to a query string
    • cache(Boolean): appoint the jsonp request url whether to be cached (defaults to false)
    • timeout(Number): how long after a timeout error is emitted (defaults to 2000ms)
    • retryTimes(Number): when the request is timeout it can appoint how many times to retry (defaults to 2)
    • backup(String or Array): appoint a backup url or multiple backup urls, if the url param request is failed, it will use backup url(s) to get backup data for correct display
    • dataCheck(Function): a function to check the response data is correct, the function will accept the response data as arguments, if it returns false jsonp-retry would use the backup url(s) to fetch data if there has backup or it will emit an error
    • useStore(Boolean): appoint to use localStorage to get or set data or not (defaults to false)
    • storeCheckKey(String): appoint the key to check in store, it is generally a field of the data which get from localStorage, with it we can get something like version of the data
    • storeSign(String): the new data version, you can compare it with the old version of the data in localStorage
    • storeCheck(Function): check the data from localStorage if correct (storeCheck has been defined inside, you can pass your own function to cover)
    • charset(String): appoint the charset of the script tag (defaults UTF-8)
  • cb(function): the request callback, accept 2 arguments, err tells if there has an error, data is the response data

In the same way, you can put the first url argument into opts, so there will only need to pass only 2 arguments like

jsonp(opts, cb?)

  • opts(Object): optional, config the jsonp method
    • url(String): the url to fetch data
    • others are same as up here
  • cb(function): the request callback, accept 2 arguments, err tells if there has an error, data is the response data

License

MIT License

Copyright (c) 2017 Li,Weitao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.