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

petch

v1.1.1

Published

Alternative javascript fetch api with adding pre-setup options for every request

Downloads

6

Readme

petch

Alternative javascript fetch api with adding pre-setup options for every request.

Installation

This is library available through npm registry.

npm install --save petch

Setup

Lets setup your options and headers for fetch request.

import * as Petch from 'petch'

const petch = new Petch({
  origin: 'https://example.com/api',
  options: {
    mode: 'cors'
  },
  headers: {
    'Accept': 'application/json'
  }
})

Access current setting

After setup, we can be accessing current setting origin, options and headers from setting property.

console.log('current origin: ', petch.setting.origin)
console.log('current options object: ', petch.setting.options)
console.log('current headers object: ', petch.setting.headers)

Re-setup current setting

The current setting can be changed or add a new property from setup().

petch.setup({
  headers: {
    'Authorization': 'auth_key'
  }
})

The Petch() and setup() accept one setting parameter with optionaly settings.

| Name | Type | Description | |--|--|--| | origin | {String} | Where a fetch request will be send, the origin is prefix for all send request. | | options | {Object} | The root options of fetch api like mode, cache, etc.| | headers | {Object} | Headers options. |

Send request method

get()

Send request with method GET.

petch.get('users')
  .then(res => {
    console.log(res.ok)
  })

postData()

Send request with method POST. Automaticaly add headers Content-Type with value application/x-www-form-urlencoded and transform body object into URL-encoded string.

petch.postData('login', {
  body: {
    username: 'username',
    password: 'password'
  }
})
  .then(res => {
    console.log(res.ok)
  })

uploadData()

Send request with method POST. Automaticaly add headers Content-Type with value multipart/form-data and transform body object into form data.

const fileField = document.querySelector('input[type="file"]');

petch.uploadData('user/profile', {
  body: {
    photo: fileField.files[0]
  }
})
  .then(res => {
    console.log(res.ok)
  })

postJSON()

Send request with method POST. Automaticaly add headers Content-Type with value application/json and transform body object into json data.

petch.postJSON('user/profile', {
  body: {
    first_name: 'FirstName',
    last_name: 'LastName'
  }
})
  .then(res => {
    console.log(res.ok)
  })

Parameters

All request method can accept two parameter and return a fetch promise.

| Name | Type | Description | |--|--|--| | url | {String} | The path of request url. | | setting | {Object} | Contain fetch setting. |

PUT & DELETE

Same with GET and POST you can use PUT and DELETE method with similliar parameter.