Code Monkey home page Code Monkey logo

ajar's Introduction

ajar

A minimal, promise-driven XMLHttpRequest-wrapper, promoting REST and JSON.

WARNING: Discontinued, use Fetch API instead

Overview

var ajar = require('ajar')
ajar.get('/entities/47').then(console.log)

Supported HTTP-methods are GET, POST, PUT and DELETE, all of them accept optional data parameters: GET and DELETE sends them as querystring parameters, while POST and PUT sends them as JSON-encoded body data.

On success the promise resolves with the JSON-decoded result. On error the promise is rejected, the reason being either the XMLHttpRequest object or a corresponding Error object (eq. SyntaxError for JSON parsing errors). Error handling is subject to change.

Design Goals

  • minimal, simple
  • ES6, dependency free
  • REST, JSON

Which means: Looking for integrated XML validation with DOM-parsing or automated request queuing with rate-limiting controls? Go look somewhere else.

Contributing

People writing tests are welcome.

License

GPLv3, see LICENSE

ajar's People

Contributors

stev47 avatar

Watchers

 avatar  avatar  avatar

ajar's Issues

Support for requests with both POST- and GET-data

Today I stumbled upon a REST-API which defined PUT/POST requests with additional GET-params (for a login token).
It would be nice if we could find an elegant way to support this.
One idea would be to export ajar.req(method, url, gdata, pdata) for generic requests, but I'm not that happy with it.

Unit testing

Hello,

do you have any preferences according to the tests of your lib? Jasmine? Mocha? What did you used in other projects so far?

Check for own properties in loops

Hello,

since params is an object, it derives from the Object constructor function. That means, you're likely assigning things you don't want in your code.

Best practice:

for (key in params) {
    if (params.hasOwnProperty(key) {
        qstr.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
    }
}

Stucked: XMLHttpRequest is undefined

Since you wrote your code in CommonJS module syntax, I assume it's meant to run inside node.

Where do you load the XMLHttpRequest constructor function which is part of the window object inside browsers?

I found several libraries for node, which emulate it, but you wrote in your README, that you want to have a dependency-free lib โ€ฆ

Document your code

Sadly, JSDoc isn't ready for ECMAScript 2015 (aka ES6), but you have some other choices.

This way, contributors can get an idea of how the code shall behave.

You may want to strip those comments in a minified build ;-)

Undeclared variables in querystring()

During the course of writing tests for your lib I encountered qstr and key not being defined. You may want to opt in for strict mode and declare those variables on top of that function.

Consider being compatible to AMD as well

Right now, you're writting CommonJS code (like: a node module).

But AJAX is likely used in client side JavaScript, i.e. a browser, where CommonJS' blocking nature isn't that admirable. Consider wrapping your code in a way, so that it is loadable as AMD module as well.

Read Why AMD? or about ES6 modules.

That is, rewrite your export to

export default {
    get: (url, data) => request('GET', url, data, null)
    post: (url, data) => request('POST', url, null, data)
    put: (url, data) => request('PUT', url, null, data)
    delete: (url, data) => request('DELETE', url, data, null)
}

and you're done :-)

Use template strings to concatenate

Hello,

since you're diving into ECMAScript 2015 (aka ES2015 aka ECMAScript 6) anyway, you could take advantage of template strings in your code. For example:

qstr.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))

could be written as

qstr.push(`${encodeURIComponent(key)}=${encodeURIComponent(params[key])})

Okay, it will make more sense if the string gets larger, but you get the idea :-)

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.