@fdaciuk/ajax
Advanced tools
Comparing version 0.2.1 to 0.2.2
/**! | ||
* ajax - v0.2.1 | ||
* ajax - v0.2.2 | ||
* Ajax module in Vanilla JS | ||
* https://github.com/fdaciuk/ajax | ||
* Fri Mar 04 2016 15:21:59 GMT-0300 (BRT) | ||
* Fri Mar 04 2016 22:05:19 GMT-0300 (BRT) | ||
* MIT (c) Fernando Daciuk | ||
*/ | ||
!function(e,t){"use strict";"function"==typeof define&&define.amd?define("ajax",t):"object"==typeof exports?exports=module.exports=t():(e.Ajax=t(),e.ajax=t())}(this,function(){"use strict";function e(t){var n={},o={};return void 0!==this&&console.warn("Instance with `new` is deprecated. This will be removed in `v2.0.0` version."),this instanceof e&&console.warn("Ajax constructor is deprecated. This will be removed in `v2.0.0`. Use ajax (lowercase version) without `new` keyword instead"),o.methods={then:function(){},"catch":function(){},always:function(){},done:function(){},error:function(){}},t=t||{},n.get=function(e){return o.XHRConnection("GET",e,null,t)},n.post=function(e,n){return o.XHRConnection("POST",e,n,t)},n.put=function(e,n){return o.XHRConnection("PUT",e,n,t)},n["delete"]=function(e,n){return o.XHRConnection("DELETE",e,n,t)},o.XHRConnection=function(e,t,n,r){var s=new XMLHttpRequest;return s.open(e,t||"",!0),o.setHeaders(s,r.headers),s.addEventListener("readystatechange",o.ready,!1),s.send(o.objectToQueryString(n)),o.promises()},o.setHeaders=function(e,t){t=t||{},o.hasContentType(t)||(t["Content-Type"]="application/x-www-form-urlencoded"),Object.keys(t).forEach(function(n){e.setRequestHeader(n,t[n])})},o.hasContentType=function(e){return Object.keys(e).some(function(e){return"content-type"===e.toLowerCase()})},o.ready=function(){var e=this;e.readyState===e.DONE&&(e.removeEventListener("readystatechange",o.ready,!1),o.methods.always.apply(o.methods,o.parseResponse(e)),e.status>=200&&e.status<300?(o.methods.then.apply(o.methods,o.parseResponse(e)),o.methods.done.apply(o.methods,o.parseResponse(e))):(o.methods["catch"].apply(o.methods,o.parseResponse(e)),o.methods.error.apply(o.methods,o.parseResponse(e))))},o.parseResponse=function(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e]},o.promises=function(){var e={};return Object.keys(o.methods).forEach(function(t){e[t]=o.generatePromise.call(this,t)},this),e},o.generatePromise=function(e){return function(t){return o.generateDeprecatedMessage(e),o.methods[e]=t,this}},o.generateDeprecatedMessage=function(e){var t="@fdaciuk/ajax: `%s` is deprecated and will be removed in v2.0.0. Use `%s` instead.";switch(e){case"done":console.warn(t,"done","then");break;case"error":console.warn(t,"error","catch")}},o.objectToQueryString=function(e){return o.isObject(e)?o.getQueryString(e):e},o.getQueryString=function(e){return Object.keys(e).map(function(t){return encodeURIComponent(t)+"="+encodeURIComponent(e[t])}).join("&")},o.isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)},n}return e}); | ||
!function(e,t){"use strict";"function"==typeof define&&define.amd?define("ajax",t):"object"==typeof exports?exports=module.exports=t():(e.Ajax=t(),e.ajax=t())}(this,function(){"use strict";function e(t){var n={},o={};return void 0!==this&&console.warn("Instance with `new` is deprecated. This will be removed in `v2.0.0` version."),this instanceof e&&console.warn("Ajax constructor is deprecated. This will be removed in `v2.0.0`. Use ajax (lowercase version) without `new` keyword instead"),o.methods={then:function(){},"catch":function(){},always:function(){},done:function(){},error:function(){}},t=t||{},n.get=function(e){return o.XHRConnection("GET",e,null,t)},n.post=function(e,n){return o.XHRConnection("POST",e,n,t)},n.put=function(e,n){return o.XHRConnection("PUT",e,n,t)},n["delete"]=function(e,n){return o.XHRConnection("DELETE",e,n,t)},o.XHRConnection=function(e,t,n,r){var s=new XMLHttpRequest;return s.open(e,t||"",!0),o.setHeaders(s,r.headers),s.addEventListener("readystatechange",o.ready,!1),s.send(o.objectToQueryString(n)),o.promises()},o.setHeaders=function(e,t){t=t||{},o.hasContentType(t)||(t["Content-Type"]="application/x-www-form-urlencoded"),Object.keys(t).forEach(function(n){e.setRequestHeader(n,t[n])})},o.hasContentType=function(e){return Object.keys(e).some(function(e){return"content-type"===e.toLowerCase()})},o.ready=function(){var e=this;e.readyState===e.DONE&&(e.removeEventListener("readystatechange",o.ready,!1),o.methods.always.apply(o.methods,o.parseResponse(e)),e.status>=200&&e.status<300?(o.methods.then.apply(o.methods,o.parseResponse(e)),o.methods.done.apply(o.methods,o.parseResponse(e))):(o.methods["catch"].apply(o.methods,o.parseResponse(e)),o.methods.error.apply(o.methods,o.parseResponse(e))))},o.parseResponse=function(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e]},o.promises=function(){var e={};return Object.keys(o.methods).forEach(function(t){e[t]=o.generatePromise.call(this,t)},this),e},o.generatePromise=function(e){return function(t){return o.generateDeprecatedMessage(e),o.methods[e]=t,this}},o.generateDeprecatedMessage=function(e){var t="@fdaciuk/ajax: `%s` is deprecated and will be removed in v2.0.0. Use `%s` instead.";switch(e){case"done":console.warn(t,"done","then");break;case"error":console.warn(t,"error","catch")}},o.objectToQueryString=function(e){return o.isObject(e)?o.getQueryString(e):e},o.getQueryString=function(e){return Object.keys(e).map(function(t){return[encodeURIComponent(t),"=",encodeURIComponent(e[t])].join("")}).join("&")},o.isObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)},n}return e}); |
{ | ||
"name": "@fdaciuk/ajax", | ||
"description": "Ajax module in Vanilla JS", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"main": "dist/ajax.min.js", | ||
@@ -9,2 +9,3 @@ "scripts": { | ||
"watch": "gulp", | ||
"format": "standard-format -w", | ||
"deploy": "gulp deploy", | ||
@@ -46,2 +47,3 @@ "build:patch": "npm version patch && npm run deploy", | ||
"gulp-mocha-phantomjs": "^0.11.0", | ||
"gulp-standardize": "^1.1.0", | ||
"gulp-uglify": "^1.5.3", | ||
@@ -54,4 +56,5 @@ "karma": "^0.13.21", | ||
"mocha": "^2.4.5", | ||
"plato": "^1.5.0" | ||
"plato": "^1.5.0", | ||
"standard-format": "^2.1.1" | ||
} | ||
} |
@@ -24,3 +24,3 @@ # Ajax | ||
```sh | ||
```console | ||
bower install ajax | ||
@@ -39,3 +39,3 @@ ``` | ||
```sh | ||
```console | ||
npm i --save @fdaciuk/ajax | ||
@@ -59,3 +59,3 @@ ``` | ||
```html | ||
<script src="//cdn.rawgit.com/fdaciuk/ajax/v0.2.1/dist/ajax.min.js"></script> | ||
<script src="//cdn.rawgit.com/fdaciuk/ajax/v0.2.2/dist/ajax.min.js"></script> | ||
``` | ||
@@ -68,4 +68,4 @@ | ||
```js | ||
define([ 'Ajax' ], function( Ajax ) { | ||
var ajax = new Ajax(); | ||
define([ 'ajax' ], function( ajax ) { | ||
ajax().get(...) | ||
... | ||
@@ -78,4 +78,4 @@ }); | ||
```js | ||
var Ajax = require( '@fdaciuk/ajax' ); | ||
var ajax = new Ajax(); | ||
var ajax = require( '@fdaciuk/ajax' ); | ||
ajax().post(...); | ||
... | ||
@@ -87,17 +87,10 @@ ``` | ||
```js | ||
var ajax = new window.Ajax(); | ||
window.ajax().get(...); | ||
``` | ||
or | ||
or just | ||
```js | ||
var ajax = new Ajax(); | ||
ajax().get(...); | ||
``` | ||
### Note | ||
`Ajax` constructor is deprecated and will be removed in `v2.0.0`. Use `ajax()` function (lowecase version) without `new` keyword instead. | ||
Enjoy ;) | ||
## Signature | ||
@@ -135,7 +128,6 @@ | ||
```js | ||
ajax().get( '/api/users' ); | ||
ajax().get( '/api/users/john' ); | ||
ajax().get('/api/users'); | ||
``` | ||
### `post(url, [ data ])` | ||
### `post(url, [data])` | ||
@@ -146,3 +138,3 @@ > Save a new register or update part of this one. | ||
// Without headers | ||
ajax().post( '/api/users', { slug: 'john' }); | ||
ajax().post('/api/users', { slug: 'john' }); | ||
@@ -158,3 +150,3 @@ // With headers | ||
### `put(url, [ data ])` | ||
### `put(url, [data])` | ||
@@ -164,6 +156,6 @@ > Update an entire register. | ||
```js | ||
ajax().put( '/api/users', { slug: 'john', age: 37 }); | ||
ajax().put('/api/users', { slug: 'john', age: 37 }); | ||
``` | ||
### `delete(url, [ data ])` | ||
### `delete(url, [data])` | ||
@@ -173,3 +165,3 @@ > Delete a register. | ||
```js | ||
ajax().delete( '/api/users', { id: 1 }); | ||
ajax().delete('/api/users', { id: 1 }); | ||
``` | ||
@@ -184,3 +176,3 @@ | ||
```js | ||
ajax().get( '/api/users' ).then(function( response, xhr ) { | ||
ajax().get('/api/users').then(function(response, xhr) { | ||
// Do something | ||
@@ -190,3 +182,3 @@ }); | ||
### `catch(response, xhrObject)` | ||
### `catch(responseError, xhrObject)` | ||
@@ -196,3 +188,3 @@ > Promise that returns if the request has an error. | ||
```js | ||
ajax().post( '/api/users', { slug: 'john' }).catch(function( response, xhr ) { | ||
ajax().post('/api/users', { slug: 'john' }).catch(function(response, xhr) { | ||
// Do something | ||
@@ -207,3 +199,3 @@ }); | ||
```js | ||
ajax().post( '/api/users', { slug: 'john' }).always(function( response, xhr ) { | ||
ajax().post('/api/users', { slug: 'john' }).always(function(response, xhr) { | ||
// Do something | ||
@@ -213,28 +205,6 @@ }); | ||
## Deprecated return methods | ||
## Deprecated methods | ||
### `done(response, xhrObject)` | ||
You may be deprecated methods [here][deprecated] | ||
> Promise that returns if the request was successful. | ||
_`done` is deprecated. Use `then` instead._ | ||
```js | ||
ajax().get( '/api/users' ).done(function( response, xhr ) { | ||
// Do something | ||
}); | ||
``` | ||
### `error(response, xhrObject)` | ||
> Promise that returns if the request has an error. | ||
_`error` is deprecated. Use `catch` instead._ | ||
```js | ||
ajax().post( '/api/users', { slug: 'john' }).error(function( response, xhr ) { | ||
// Do something | ||
}); | ||
``` | ||
## Contributing | ||
@@ -264,1 +234,2 @@ | ||
[contributing-url]: CONTRIBUTING.md | ||
[deprecated]: deprecated.md |
10661
5
21
219