GithubHelp home page GithubHelp logo

basic-xhr's Introduction

The bare-bones XMLHttpRequest wrapper that I use when I want to use XHR.

Set up some options, make a request, get back a promise.

Requires ES2015 shims (Promise, Object.assign, Object.keys).

API

const createRequestFunction = require('basic-xhr')

The module exports a function that takes options and returns a re-usable XHR function.

makeXhr = createRequestFunction(options)

const get = createRequestFunction()

get('/some/nifty/api?cool=yes').then(responseBody => {
	console.log(responseBody.valueFromServer)
})

Options

  • method: string, defaults to 'GET'
  • success: function, defaults to request => request.status >= 200 && request.status < 400
  • parse: function, defaults to request => JSON.parse(request.responseText)
  • serialize: function, defaults to body => JSON.stringify(body)
  • headers: object, defaults to {}

promise = makeXhr(url, [body])

If it's defined, body will be passed to serialize before sending to the server.

Returns a promise that is rejected if success returns false, if the XHR emits error or abort, or if the parse function throws.

The promise is resolved with whatever parse returns.

Snippets you can copy/paste

const createRequestFunction = require('basic-xhr')

const post = createRequestFunction({
	method: 'POST',
	headers: {
		'Content-Type': 'application/json'
	}
})

const response = post('/person', { name: 'Cool guy' })

If you have any common use cases, add a new snippet here.

License

WTFPL

basic-xhr's People

Contributors

tehshrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

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