Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xfetch-js

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xfetch-js - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

2

package.json
{
"name": "xfetch-js",
"version": "0.0.7",
"version": "0.0.8",
"description": "",

@@ -5,0 +5,0 @@ "main": "xfetch.js",

@@ -1,2 +0,2 @@

# XFetch.js
# xfetch-js

@@ -23,6 +23,18 @@ > A extremely simple fetch extension for modern browsers inspired by [sindresorhus/ky](https://github.com/sindresorhus/ky).

// post json
// post json with transforms
xf.post('https://postman-echo.com/post', { json: { foo: 'bar' } })
.json()
.json(r => r.data)
.then(console.log)
// custom base, default base in browser is document.baseURI
const xf2 = xf.base('https://postman-echo.com/')
xf2.get('/get')
.then(console.log)
// HTTPError
xf.get('https://postman-echo.com/404')
.catch(e => {
assert(e instanceof xf.HTTPError)
console.log(e.response)
})
```

@@ -29,0 +41,0 @@

@@ -23,1 +23,15 @@ import test from 'ava'

})
test('base', async t => {
const xf2 = xf.base('https://postman-echo.com/')
const { url } = await xf2.get('/get').json()
t.is(url, 'https://postman-echo.com/get')
})
test('transforms', async t => {
const headers = await xf.get('https://postman-echo.com/get/').json(r => r.headers)
t.is(headers.host, 'postman-echo.com')
})
test('HTTPError', async t => {
await t.throwsAsync(xf.get('http://postman-echo.com/404'), {
instanceOf: xf.HTTPError
})
})

@@ -1,2 +0,2 @@

interface XPromise<T> extends Promise<T> {
interface XResponsePromise extends Promise<Response> {
arrayBuffer(): Promise<ArrayBuffer>

@@ -13,6 +13,9 @@ blob(): Promise<Blob>

}
declare class HTTPError extends Error{
response: Response
}
type originalfetch = GlobalFetch['fetch']
type fetch = (input: string, init?: XRequestInit) => XPromise<Response>
type fetch = (input: string, init?: XRequestInit) => XResponsePromise
interface XFetch {
(input: string, init?: XRequestInit): XPromise<Response>
(input: string, init?: XRequestInit): XResponsePromise
get: fetch

@@ -24,5 +27,7 @@ post: fetch

head: fetch
create(fetch: originalfetch): XFetch
create(fetch: originalfetch, baseURI?: string): XFetch
base(baseURI: string): XFetch
HTTPError: HTTPError
}
declare const xfetch: XFetch
export = xfetch

@@ -14,10 +14,18 @@ /*

})(this, () => {
class HTTPError extends Error {
constructor(res) {
super(res.statusText)
this.response = res
}
}
const METHODS = ['get', 'post', 'put', 'patch', 'delete', 'head']
const ALIASES = ['arrayBuffer', 'blob', 'formData', 'json', 'text']
const genqs=o=>new URLSearchParams(o).toString()
const create = fetch => {
const genqs = o => new URLSearchParams(o).toString()
const create = (fetch, baseURI = typeof document !== 'undefined' ? document.baseURI : undefined) => {
const xfetch = (input, init = {}) => {
const url = new URL(input, baseURI)
if (!init.headers) {
init.headers = {}
}
// Add json or form on body
if (init.json) {

@@ -30,16 +38,19 @@ init.body = JSON.stringify(init.json)

}
// Querystring
if (init.qs) {
input += '?' + genqs(init.qs)
url.search = genqs(init.qs)
}
// same-origin by default
if (!init.credentials) {
init.credentials = 'same-origin'
}
const p = fetch(input, init).then(r => {
if (r.ok) return r
throw new Error(r.status)
const promise = fetch(url, init).then(res => {
if (!res.ok) throw new HTTPError(res)
return res
})
for (const alias of ALIASES) {
p[alias] = () => p.then(r => r[alias]())
// if transformation function is provided, pass it for transform
promise[alias] = fn => promise.then(res => res[alias]()).then(fn || (x => x))
}
return p
return promise
}

@@ -52,7 +63,10 @@ for (const method of METHODS) {

}
// Extra methods and classes
xfetch.create = create
xfetch.base = baseURI => create(fetch, baseURI)
xfetch.HTTPError = HTTPError
return xfetch
}
const xfetch = create(typeof fetch === 'undefined' ? null : fetch)
xfetch.create = create
return xfetch
})

@@ -1,1 +0,1 @@

((a,b)=>{"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.xf=b()})(this,()=>{const a=["get","post","put","patch","delete","head"],b=["arrayBuffer","blob","formData","json","text"],c=a=>new URLSearchParams(a).toString(),d=d=>{const e=(a,e={})=>{e.headers||(e.headers={}),e.json?(e.body=JSON.stringify(e.json),e.headers["Content-Type"]="application/json"):e.form&&(e.body=c(e.form),e.headers["Content-Type"]="application/x-www-form-urlencoded"),e.qs&&(a+="?"+c(e.qs)),e.credentials||(e.credentials="same-origin");const f=d(a,e).then(a=>{if(a.ok)return a;throw new Error(a.status)});for(const c of b)f[c]=()=>f.then(a=>a[c]());return f};for(const b of a)e[b]=(a,c={})=>(c.method=b,e(a,c));return e},e=d("undefined"==typeof fetch?null:fetch);return e.create=d,e});
((a,b)=>{"function"==typeof define&&define.amd?define([],b):"object"==typeof exports?module.exports=b():a.xf=b()})(this,()=>{class a extends Error{constructor(a){super(a.statusText),this.response=a}}const b=["get","post","put","patch","delete","head"],c=["arrayBuffer","blob","formData","json","text"],d=a=>new URLSearchParams(a).toString(),e=(f,g="undefined"==typeof document?void 0:document.baseURI)=>{const h=(b,e={})=>{const h=new URL(b,g);e.headers||(e.headers={}),e.json?(e.body=JSON.stringify(e.json),e.headers["Content-Type"]="application/json"):e.form&&(e.body=d(e.form),e.headers["Content-Type"]="application/x-www-form-urlencoded"),e.qs&&(h.search=d(e.qs)),e.credentials||(e.credentials="same-origin");const i=f(h,e).then(b=>{if(!b.ok)throw new a(b);return b});for(const a of c)i[a]=b=>i.then(b=>b[a]()).then(b||(a=>a));return i};for(const a of b)h[a]=(b,c={})=>(c.method=a,h(b,c));return h.create=e,h.base=a=>e(f,a),h.HTTPError=a,h},f=e("undefined"==typeof fetch?null:fetch);return f});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc