New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

simple-fetch

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-fetch

Simple and minimal wrapper around fetch API for JSON requests

latest
Source
npmnpm
Version
2.2.3
Version published
Weekly downloads
113
151.11%
Maintainers
1
Weekly downloads
 
Created
Source

simple-fetch

a simple wrapper around cross-fetch to make it easier to work with common fetch tasks

This is only meant for more convenient basic JSON requests. Please use the fetch API for more complex use cases.

By default, HTTP response codes other than 2xx will cause the fetch promise handler to throw. To change this behavior, set opts.only2xx = false.

Examples

const simpleFetch = require('simple-fetch');
const { getJson, postJson } = simpleFetch;

getJson('https://myapi.com/events')
  .then(function (events) {
    console.log(events);
  });

const response = await postJson('https://myapi.com/events', {
  name: 'New Event',
  date: 'tomorrow'
}, {
  headers: {
    Authorization: `Bearer ${token}`
  }
});

simpleFetch('patch', 'https://myapi.com/events/1', {
  name: 'Other Event',
  date: 'next Sunday'
}).then(...)

If common options, such as headers, need to be passed to all methods, use createFetch:

const { createFetch } = require('simple-fetch');
const { getJson, deleteJson } = createFetch({
  headers: {
    Authorization: `Bearer ${token}`
  }
});

await getJson('https://myauthenticatedapi.com/events');
await deleteJson('https://myauthenticatedapi.com/events/2');

API

  • simpleFetch(method, url[, data][, opts])
  • .getJson(url[, opts])
  • .postJson(url, data[, opts])
  • .putJson(url, data[, opts])
  • .patchJson(url, data[, opts])
  • .deleteJson(url[, opts])

Parameters

  • data can be an object, array or JSON string
  • opts.only2xx: if set to false, will not throw error even for error codes other than 2xx. Defaults to true.
  • opts.skipParsing: skip parsing of response into JSON, will return the Response object directly. Defaults to false.

Keywords

fetch

FAQs

Package last updated on 16 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts