Gathering detailed insights and metrics for @ryniaubenpm2/incidunt-sunt-provident
Gathering detailed insights and metrics for @ryniaubenpm2/incidunt-sunt-provident
Gathering detailed insights and metrics for @ryniaubenpm2/incidunt-sunt-provident
Gathering detailed insights and metrics for @ryniaubenpm2/incidunt-sunt-provident
npm install @ryniaubenpm2/incidunt-sunt-provident
Typescript
Module System
Node Version
NPM Version
61.3
Supply Chain
48.1
Quality
75
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
0%
8
Compared to previous day
Last Week
8.6%
63
Compared to previous week
Last Month
759.4%
275
Compared to previous month
Last Year
452.4%
464
Compared to previous year
32
Tiny, isomorphic convenience wrapper around the Fetch API aiming to reduce boilerplate, especially when sending and receiving JSON.
Build | Unminified | Minified | Gzipped |
---|---|---|---|
ESM bundle | 3.36 kB | 1.54 kB | 798 B |
UMD bundle | 3.87 kB | 1.71 kB | 873 B |
UMD bundle (ES5) | 4.12 kB | 1.86 kB | 894 B |
1$ npm install @ryniaubenpm2/incidunt-sunt-provident
1import @ryniaubenpm2/incidunt-sunt-provident, { FetchError } from '@ryniaubenpm2/incidunt-sunt-provident' 2// or 3const @ryniaubenpm2/incidunt-sunt-provident = require('@ryniaubenpm2/incidunt-sunt-provident') 4 5@ryniaubenpm2/incidunt-sunt-provident('/get-stuff').then(json => console.log(json)) 6@ryniaubenpm2/incidunt-sunt-provident('/get-stuff', { response: 'blob', headers: { accept: 'image/png' } }).then(blob => console.log(blob)) 7@ryniaubenpm2/incidunt-sunt-provident('/get-stuff', 'blob').then(blob => console.log(blob)) 8 9@ryniaubenpm2/incidunt-sunt-provident.post('/do-stuff', { stuff: 'to be done' }).then(json => console.log(json)) 10 11@ryniaubenpm2/incidunt-sunt-provident.put('/do-stuff', { stuff: 'to be done' }, { redirect: false, response: 'text' }).then(text => console.log(text)) 12@ryniaubenpm2/incidunt-sunt-provident.put('/do-stuff', { stuff: 'to be done' }, 'text').then(text => console.log(text))
1<script src="https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident"></script>
1import @ryniaubenpm2/incidunt-sunt-provident, { FetchError } from 'https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident/dist/@ryniaubenpm2/incidunt-sunt-provident.esm.min.js'
Even though the Fetch
API is
significantly nicer to work with than XHR, it still quickly becomes verbose to
do simple tasks. To create a relatively simple POST
request using JSON, fetch
requires something like:
1fetch('/api/peeps', { 2 method: 'POST', 3 headers: { 4 'content-type': 'application/json', 5 accept: 'application/json', 6 }, 7 credentials: 'same-origin', 8 body: JSON.stringify({ 9 name: 'James Brown', 10 }), 11}).then((res) => { 12 if (res.ok) { 13 return res.json() 14 } 15 16 const err = new Error(response.statusText) 17 18 err.response = response 19 err.status = response.status 20 21 throw err 22}).then((person) => { 23 console.log(person) 24}).catch(...)
With @ryniaubenpm2/incidunt-sunt-provident
this simply becomes:
1@ryniaubenpm2/incidunt-sunt-provident.post('/api/peeps', { name: 'James Brown' }).then((person) => { 2 console.log(person) 3}).catch(...)
@ryniaubenpm2/incidunt-sunt-provident
uses conditional
exports
to load Node or browser (and Deno) compatible ESM or CJS files. main
and
module
are also set in package.json
for compatibility with legacy Node and
legacy build systems. In Node, the main entry point uses
node-fetch, which needs to be
installed manually. In all other environments, the main entry point uses
fetch,
Headers,
URL,
URLSearchParams and
FormData defined in the
global scope.
Internally, the ESM & CJS entry points use the same CJS files to prevent the dual package hazard. See the Node documentation for more information.
The main entry point returns a @ryniaubenpm2/incidunt-sunt-provident
instance. When using import
, a named
export FetchError
is also exposed.
1import @ryniaubenpm2/incidunt-sunt-provident, { FetchError } from '@ryniaubenpm2/incidunt-sunt-provident' 2// or 3const @ryniaubenpm2/incidunt-sunt-provident = require('@ryniaubenpm2/incidunt-sunt-provident')
The browser/Deno version is created using (see factory for details):
1export default factory({ 2 credentials: 'same-origin', 3 response: 'json', 4 fetch, 5 Headers, 6})
The Node version is created using:
1import fetch from 'node-fetch' 2 3export default factory({ 4 credentials: 'same-origin', 5 response: 'json', 6 fetch, 7 Headers: fetch.Headers, 8})
The main entry exposes most TypeScript types:
1import { Options, Rek } from '@ryniaubenpm2/incidunt-sunt-provident'
Exports the FetchError
. Both import
and require
will load
./dist/error.cjs
in all environments.
1// import 2import FetchError from '@ryniaubenpm2/incidunt-sunt-provident/error' 3// require 4const FetchError = require('@ryniaubenpm2/incidunt-sunt-provident/error') 5// legacy 6const FetchError = require('@ryniaubenpm2/incidunt-sunt-provident/dist/error.cjs')
Exports the factory function that creates @ryniaubenpm2/incidunt-sunt-provident
instances with new
defaults. Both import
and require
will load ./dist/factory.cjs
in
all environments.
1import factory from '@ryniaubenpm2/incidunt-sunt-provident/factory' 2// require 3const factory = require('@ryniaubenpm2/incidunt-sunt-provident/factory') 4// legacy 5const factory = require('@ryniaubenpm2/incidunt-sunt-provident/dist/factory.cjs')
The factory entry also exposes TypeScript types:
1import { Options, Rek } from '@ryniaubenpm2/incidunt-sunt-provident'
On top of CJS and ESM files, bundles to be consumed through
unpkg.com are built into ./dist/
. The unpkg
field
in package.json
points at the minified UMD build. This means:
1<script src="https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident"></script>
is the same as
1<script src="https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident/dist/@ryniaubenpm2/incidunt-sunt-provident.umd.min.js"></script>
To use the ES5 compatible UMD bundle:
1<script src="https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident/dist/@ryniaubenpm2/incidunt-sunt-provident.umd.es5.min.js"></script>
The ESM bundle can be imported from a JS file:
1import @ryniaubenpm2/incidunt-sunt-provident, { FetchError } from 'https://unpkg.com/@ryniaubenpm2/incidunt-sunt-provident/dist/@ryniaubenpm2/incidunt-sunt-provident.esm.min.js'
The following bundles are available in the dist folder:
./dist/@ryniaubenpm2/incidunt-sunt-provident.esm.js
- ESM bundle./dist/@ryniaubenpm2/incidunt-sunt-provident.esm.min.js
- Minified ESM bundle./dist/@ryniaubenpm2/incidunt-sunt-provident.umd.js
- UMD bundle./dist/@ryniaubenpm2/incidunt-sunt-provident.umd.min.js
- Minified UMD bundle./dist/@ryniaubenpm2/incidunt-sunt-provident.umd.es5.js
- ES5 compatible UMD bundle./dist/@ryniaubenpm2/incidunt-sunt-provident.umd.es5.min.js
- Minified ES5 compatible UMD bundleMakes a request with fetch
and returns a parsed response body or the
Response (depending
on the response option). If res.ok
is not true, an error is thrown.
See options below for differences to native fetch
options.
1@ryniaubenpm2/incidunt-sunt-provident('/url').then(json => { ... }) 2@ryniaubenpm2/incidunt-sunt-provident('/url', { response: 'text' }).then(text => { ... }) 3@ryniaubenpm2/incidunt-sunt-provident('/url', { body: { plain: 'object' }, method: 'post' }).then(json => { ... })
@ryniaubenpm2/incidunt-sunt-provident
has convenience methods for all relevant HTTP request methods. They
set the correct option.method
and have an extra body
argument
when the request can send bodies (the body
argument overwrites options.body
).
1@ryniaubenpm2/incidunt-sunt-provident.delete('/api/peeps/1337') 2// is the same as 3@ryniaubenpm2/incidunt-sunt-provident('/api/peeps/1337', { method: 'DELETE' }) 4 5@ryniaubenpm2/incidunt-sunt-provident.post('/api/peeps/14', { name: 'Max Powers' }) 6// is the same as 7@ryniaubenpm2/incidunt-sunt-provident('/api/peeps/14', { method: 'POST', body: { name: 'Max Powers' } })
@ryniaubenpm2/incidunt-sunt-provident
supports three arguments on top of the default fetch
options:
baseUrl, response and searchParams. It also handles
body
differently to native fetch()
.
Options passed to @ryniaubenpm2/incidunt-sunt-provident
will be merged with the defaults
defined in the factory.
If a string is passed as option argument, a new object is created with
response
set to that string.
1@ryniaubenpm2/incidunt-sunt-provident('/', 'text') 2// is the same 3@ryniaubenpm2/incidunt-sunt-provident('/', { response: 'text' })
baseUrl
A URL that relative paths will be resolved against.
Setting this in defaults
is very useful for SSR and similar.
body
Depending on the type of body passed, it could be converted to a JSON string
and the content-type
header could be removed or set
body
will not be modified but
content-type
will be unset (setting content-type
prevents the browser
setting content-type
with the boundary expression used to delimit form
fields in the request body).body
nor content-type
will be
modified.body
will be converted to a JSON string, and
content-type
will be set to application/json
(even if it is already set).headers
Since default headers are merged with headers passed as options and it requires significantly more logic to merge Header instances, headers are expected to be passed as plain objects.
If Headers are already used, they can be converted to plain objects with:
1Object.fromEntries(headers.entries())
response
Sets how to parse the response body. It needs to be either
a valid Body
read
method name, a function
accepting the response or
falsy if the response should be returned without parsing the body. In the @ryniaubenpm2/incidunt-sunt-provident
instance returned by the main entry, response
defaults to 'json'.
1typeof await @ryniaubenpm2/incidunt-sunt-provident('/url') === 'object' // is JSON 2 3typeof await @ryniaubenpm2/incidunt-sunt-provident('/url', { response: 'text' }) === 'string' 4 5await @ryniaubenpm2/incidunt-sunt-provident('/url', { response: 'blob' }) instanceof Blob 6 7// will throw 8@ryniaubenpm2/incidunt-sunt-provident('/url', { response: 'invalid response' }) 9 10await @ryniaubenpm2/incidunt-sunt-provident('/url', { response: false }) instanceof Response 11 12await @ryniaubenpm2/incidunt-sunt-provident('/url', { response: (res) => 'return value' }) === 'return value'
Depending on the response
, the following Accept
header will be set:
searchParams
A valid
URLSearchParams
constructor argument used to add a query string to the URL. A query string already present in the url
passed to @ryniaubenpm2/incidunt-sunt-provident
will be overwritten.
1@ryniaubenpm2/incidunt-sunt-provident('/url', { searchParams: 'foo=1&bar=2' }) 2@ryniaubenpm2/incidunt-sunt-provident('/url', { searchParams: '?foo=1&bar=2' }) 3 4// sequence of pairs 5@ryniaubenpm2/incidunt-sunt-provident('/url', { searchParams: [['foo', '1'], ['bar', '2']] }) 6 7// plain object 8@ryniaubenpm2/incidunt-sunt-provident('/url', { searchParams: { foo: 1, bar: 2 } }) 9 10// URLSearchParams 11@ryniaubenpm2/incidunt-sunt-provident('/url', { searchParams: new URLSearchParams({ foo: 1, bar: 2 }) })
The extend method will return a new @ryniaubenpm2/incidunt-sunt-provident
instance with arguments
merged with the previous values.
1const myRek = @ryniaubenpm2/incidunt-sunt-provident.extend({ baseUrl: 'http://localhost:1337' }) 2const myRek = @ryniaubenpm2/incidunt-sunt-provident.extend({ credentials: 'omit', fetch: myFetch })
1import factory from '@ryniaubenpm2/incidunt-sunt-provident/factory' 2// or 3const factory = require('@ryniaubenpm2/incidunt-sunt-provident/factory') 4 5const myRek = factory({ 6 headers: { 7 accept: 'application/html', 8 'content-type': 'application/x-www-form-urlencoded', 9 }, 10 credentials: 'omit', 11 fetch: fancyfetch, 12 Headers: FancyHeaders, 13}) 14 15myRek() 16myRek.delete() 17myRek.patch()
The main entry exposes most types
1import { Defaults, Options, Rek } from '@ryniaubenpm2/incidunt-sunt-provident'
Very big thank you to kolodny for releasing the NPM name!
No vulnerabilities found.
No security vulnerabilities found.