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.2.3 to 0.3.0

2

node.js
const { URL, URLSearchParams } = require('url')
const fetch = require('node-fetch')
const FormData = require('form-data')
module.exports = require('./xfetch').extend({

@@ -7,3 +8,4 @@ fetch,

URLSearchParams,
FormData,
Headers: fetch.Headers
})

3

package.json
{
"name": "xfetch-js",
"version": "0.2.3",
"version": "0.3.0",
"description": "",

@@ -15,2 +15,3 @@ "main": "node.js",

"dependencies": {
"form-data": "^2.3.3",
"node-fetch": "^2.2.0"

@@ -17,0 +18,0 @@ },

import test from 'ava'
import { Headers } from 'node-fetch'
import xf from '../node'
import FormData from 'form-data'

@@ -25,6 +26,26 @@ const client = xf.extend({

})
test('post form', async t => {
const { form } = await client.post('/post', { form: { foo: 'bar' } }).json()
test('post urlencoded:string', async t => {
const { form } = await client.post('/post', { urlencoded: 'foo=bar' }).json()
t.deepEqual(form, { foo: 'bar' })
})
test('post urlencoded:object', async t => {
const { form } = await client.post('/post', { urlencoded: { foo: 'bar' } }).json()
t.deepEqual(form, { foo: 'bar' })
})
test('post formData:object', async t => {
const { form } = await client
.post('/post', {
formData: {
foo: 'bar'
}
})
.json()
t.deepEqual(form, { foo: 'bar' })
})
test('post formData:FormData', async t => {
const fd = new FormData()
fd.append('foo', 'bar')
const { form } = await client.post('/post', { formData: fd }).json()
t.deepEqual(form, { foo: 'bar' })
})
test('merge qs', async t => {

@@ -52,3 +73,2 @@ const { args } = await client.get('/get?a=b', { qs: { foo: 'bar' } }).json()

.json()
console.log(data)
t.is(data.host, 'postman-echo.com')

@@ -55,0 +75,0 @@ })

@@ -20,3 +20,4 @@ type fn<T, K> = (arg: T) => K

json?: any
form?: object
urlencoded?: string | object
formData?: object | FormData
baseURI?: string

@@ -28,2 +29,3 @@ fetch?: originalfetch

URLSearchParams?: Constructor<URLSearchParams>
FormData?: Constructor<FormData>
}

@@ -30,0 +32,0 @@ export declare class HTTPError extends Error {

@@ -31,2 +31,7 @@ /*

const fromEntries = ent => ent.reduce((acc, [k, v]) => ((acc[k] = v), acc), {})
const typeis = (...types) => val =>
types.some(type => (typeof type === 'string' ? typeof val === type : val instanceof type))
const isstr = typeis('string')
const isobj = typeis('object')
const isstrorobj = v => isstr(v) || isobj(v)
const responseErrorThrower = res => {

@@ -44,4 +49,4 @@ if (!res.ok) throw new HTTPError(res)

init.headers = {}
} else if (init.headers instanceof init.Headers) {
// Transform into object if it is object
} else if (typeis(init.Headers)(init.headers)) {
// Transform into object if it is `Headers`
init.headers = fromEntries([...init.headers.entries()])

@@ -53,9 +58,19 @@ }

init.headers['Content-Type'] = 'application/json'
} else if (init.form) {
init.body = createQueryString(init.form)
} else if (isstrorobj(init.urlencoded)) {
init.body = isstr(init.urlencoded) ? init.urlencoded : createQueryString(init.urlencoded)
init.headers['Content-Type'] = 'application/x-www-form-urlencoded'
} else if (typeis(init.FormData, 'object')(init.formData)) {
// init.formData is data passed by user, init.FormData is FormData constructor
if (!typeis(init.FormData)(init.formData)) {
const fd = new init.FormData()
for (const [k, v] of Object.entries(init.formData)) {
fd.append(k, v)
}
init.formData = fd
}
init.body = init.formData
}
// Querystring
if (init.qs) {
if (typeof init.qs === 'string') init.qs = parseQueryString(init.qs)
if (isstr(init.qs)) init.qs = parseQueryString(init.qs)
url.search = createQueryString(assign(fromEntries([...url.searchParams.entries()]), init.qs))

@@ -82,4 +97,12 @@ }

return isBrowser
? extend({ fetch: fetch.bind(window), URL, Response, URLSearchParams, Headers, baseURI: document.baseURI })
? extend({
fetch: fetch.bind(window),
URL,
Response,
URLSearchParams,
Headers,
FormData,
baseURI: document.baseURI
})
: extend()
})

@@ -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"];class b extends Error{constructor(a){super(a.statusText),this.name="HTTPError",this.response=a}}class c extends Promise{}for(const a of["arrayBuffer","blob","formData","json","text"])c.prototype[a]=function(b){return this.then(b=>b[a]()).then(b||(a=>a))};const{assign:d}=Object,e=a=>a.reduce((a,[b,c])=>(a[b]=c,a),{}),f=a=>{if(!a.ok)throw new b(a);return a},g=(h={})=>{const i=(a,b={})=>{d(b,h);const g=a=>new b.URLSearchParams(a).toString(),i=new b.URL(a,b.baseURI||void 0);return b.headers?b.headers instanceof b.Headers&&(b.headers=e([...b.headers.entries()])):b.headers={},b.json?(b.body=JSON.stringify(b.json),b.headers["Content-Type"]="application/json"):b.form&&(b.body=g(b.form),b.headers["Content-Type"]="application/x-www-form-urlencoded"),b.qs&&("string"==typeof b.qs&&(b.qs=(a=>e([...new b.URLSearchParams(a).entries()]))(b.qs)),i.search=g(d(e([...i.searchParams.entries()]),b.qs))),b.credentials||(b.credentials="same-origin"),c.resolve(b.fetch(i,b).then(f))};for(const b of a)i[b]=(a,c={})=>(c.method=b.toUpperCase(),i(a,c));return i.extend=a=>g(d({},h,a)),i.HTTPError=b,i},h="undefined"!=typeof window&&"undefined"!=typeof document;return h?g({fetch:fetch.bind(window),URL,Response,URLSearchParams,Headers,baseURI:document.baseURI}):g()});
((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"];class b extends Error{constructor(a){super(a.statusText),this.name="HTTPError",this.response=a}}class c extends Promise{}for(const a of["arrayBuffer","blob","formData","json","text"])c.prototype[a]=function(b){return this.then(b=>b[a]()).then(b||(a=>a))};const{assign:d}=Object,e=a=>a.reduce((a,[b,c])=>(a[b]=c,a),{}),f=(...a)=>b=>a.some(a=>"string"==typeof a?typeof b==a:b instanceof a),g=f("string"),h=f("object"),i=a=>g(a)||h(a),j=a=>{if(!a.ok)throw new b(a);return a},k=(h={})=>{const l=(a,b={})=>{d(b,h);const k=a=>new b.URLSearchParams(a).toString(),l=new b.URL(a,b.baseURI||void 0);if(b.headers?f(b.Headers)(b.headers)&&(b.headers=e([...b.headers.entries()])):b.headers={},b.json)b.body=JSON.stringify(b.json),b.headers["Content-Type"]="application/json";else if(i(b.urlencoded))b.body=g(b.urlencoded)?b.urlencoded:k(b.urlencoded),b.headers["Content-Type"]="application/x-www-form-urlencoded";else if(f(b.FormData,"object")(b.formData)){if(!f(b.FormData)(b.formData)){const a=new b.FormData;for(const[c,d]of Object.entries(b.formData))a.append(c,d);b.formData=a}b.body=b.formData}return b.qs&&(g(b.qs)&&(b.qs=(a=>e([...new b.URLSearchParams(a).entries()]))(b.qs)),l.search=k(d(e([...l.searchParams.entries()]),b.qs))),b.credentials||(b.credentials="same-origin"),c.resolve(b.fetch(l,b).then(j))};for(const b of a)l[b]=(a,c={})=>(c.method=b.toUpperCase(),l(a,c));return l.extend=a=>k(d({},h,a)),l.HTTPError=b,l},l="undefined"!=typeof window&&"undefined"!=typeof document;return l?k({fetch:fetch.bind(window),URL,Response,URLSearchParams,Headers,FormData,baseURI:document.baseURI}):k()});
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