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

flexible-cookie

v1.0.1

Published

Helper to manage cookies for node/browser

Downloads

63

Readme

data-fetcher

npm version npm downloads

This is a simple and powerful promise based wrapper around native fetch or node-fetch for node.js

Usage

Simple GET request

import {fetcher} from 'data-fetcher'


fetcher('https://api.github.com/users').then(users => {
  console.log(user)
})

Custom method

import {fetcher} from 'data-fetcher'


fetcher('http://example.com/users', {
  method: 'post', // any case
  body: {
    login: 'test',
    password: 'test'
  },
  query: {
    access_token: 'sometoken' // will stringify to search as string by `query-string` library
  }
}).then(user => {
  console.log(user)
})

Defaults

import {defaults} from 'data-fetcher'


console.log(defaults) 
//
{  
   baseUrl:'',
   args:{  
      mode:'cors',
      credentials:'same-origin',
      headers:{  
         Accept:'application/json',
         'Content-Type':'application/json'
      }
   }
}

You can customize any property of config, for example ...

index.js - endpoint of you app

import {defaults} from 'data-fetcher'


defaults.baseUrl = 'http://example.com'

Then deep in the app

import {fetcher, defaults} from 'data-fetcher'


fetcher('/login', {
  method: 'post',
  body: {
    login: 'test',
    password: 'test'
  }
}).then(user => {
  console.log(user)
})

Custom headers

fetcher('/users', {
  headers: {
    Authorization: `Bearer {jwtToken}`
  }
}).then(users => {
  console.log(users)
})

Or you can set it globaly. All that you need just modify your login method

Just for example

import {fetcher, defaults} from 'data-fetcher'

const login = async body => {
  const {token} = fetcher('/login', {
    method: 'post',
    body
  })
  
  defaults.args.headers.Authorization = `Bearer ${token}`
}

Then you can send anywhere request in your app securely :)

This way you can add any properties to the defaults.args that will be passed to fetch() as second argument or add it individually to fetcher() as second argument too

onFail handler

if your token might be die after some time probably you will want to update it. It is do this way

./src/storage.js

const {localStorage} = window

const setToken = token => {
    localStorage.setItem('token', token)
    defaults.args.headers.Authorization = token ? `Bearer ${token}` : undefined
}

const setRefreshToken = refreshToken => {
    localStorage.setItem('refreshToken', refreshToken)
}

const getToken = () =>
    localStorage.getItem('token')


const getRefreshToken = () =>
    localStorage.getItem('refreshToken')
    
export {
  setToken,
  setRefreshToken,
  getToken,
  getRefreshToken
}

./src/index.js

import {fetcher, defaults} from 'data-fetcher'
import {getToken, getRefreshToken, setToken, setRefreshToken} from './storage'


defaults.baseUrl = 'http://example.com'
defaults.onFail = async (e, params) => {
    const refreshToken = getRefreshToken()

    if (refreshToken && e.data.status === 401) { // only for 401 http status code but you can customize it for you
        const {token: newToken, refreshToken: newRefreshToken} = await fetcher('/login/refresh', {
            body: {
                refreshToken
            },
            method: 'post'
        }, false) // false, won't run error handler `onFail` if request is fail. // default true

        setToken(newToken)
        setRefreshToken(newRefreshToken)

        return fetcher(params.url, params.options, false)
    }

    throw e
}

Then if one of your requests crashed with 401 status code and you have a refresh token will send request to update token and setup newToken and newRefreshToken and most important request which was crashed will resend again with new credentials. It will look like your request was sended without any troubles

Package contents

import fetcher, {fetcher, defaults, stringifyQuery, parseQuery, Fetcher} from 'data-fetcher'

As you have seen you can also just import default export from package.

API

fetcher(url, options, catchError)

Parameters

url - (string) an url to remote server options - (object)

options = {
  baseUrl: string // default ''
  method: string // default 'get'
  body: object | string // default ''
  query: object | string // default ''
  withData: bool // default false
  type: string (json|formData) // default json
  headers: object | bool // default false
}

catchError - (bool) true - should catch errors and handle it only if onFail is specified it will be actual

Fetcher - class of fetcher to use in server-side application

stringifyQuery - will stringify an object to a string

parseQuery - will parse a string that was stringified to an object