Giter Site home page Giter Site logo

xfetch-js's Introduction

xfetch-js

A extremely simple fetch extension for modern browsers inspired by sindresorhus/ky.

Which aims to be as small as possible and easy to use.

Build Status npm Type definition FOSSA Status

Examaple

// get with query string
xf('https://postman-echo.com/get/', { qs: { foo: 'bar' } })
  .json()
  .then(console.log)

// post form
xf.post('https://postman-echo.com/post', { form: { foo: 'bar' } })
  .json()
  .then(console.log)

// post json with transforms
xf.post('https://postman-echo.com/post', { json: { foo: 'bar' } })
  .json(r => r.data)
  .then(console.log)

// extend, default baseURI in browser is document.baseURI
const xf2 = xf.extend({
  baseURI: '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)
})

With node

const xf = require('xfetch-js').create(require('node-fetch'))

xf('https://postman-echo.com/get/', { qs: { foo: 'bar' } })
  .json()
  .then(console.log)

Node version must be greater than 10

Otherwise, you will get an error about global.URL,global.URLSearchParams is undefined.

Of course, you can polyfill them by youself to use this module.

Main differences bewteen fetch and xfetch-js

  • credentials is set to same-origin by default.
  • Throws error when response.ok is not true
  • Some useful methods to call, such as get,post and so on...
  • Support some simple serialization, including json,urlencoded and querystring
  • Support post transformation like .json(r => r.body)
  • Create another instance with default options xf.extend({})

API

See xfetch.base.d.ts.

License

FOSSA Status

xfetch-js's People

Contributors

fossabot avatar maple3142 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.