2.2.3 • Published 1 year ago

simple-fetch v2.2.3

Weekly downloads
86
License
ISC
Repository
github
Last release
1 year ago

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.
2.2.3

1 year ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

5 years ago

1.5.1

6 years ago

1.5.0

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago