@fdaciuk/ajax
Advanced tools
Comparing version 2.3.1 to 2.3.2
/**! | ||
* ajax - v2.3.1 | ||
* ajax - v2.3.2 | ||
* Ajax module in Vanilla JS | ||
* https://github.com/fdaciuk/ajax | ||
* Wed Sep 12 2018 12:58:36 GMT-0300 (-03) | ||
* Wed Sep 12 2018 15:28:52 GMT-0300 (-03) | ||
* 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()}(this,function(){"use strict";function e(e){var r=["get","post","put","delete"];return e=e||{},e.baseUrl=e.baseUrl||"",e.method&&e.url?n(e.method,e.baseUrl+e.url,t(e.data),e):r.reduce(function(r,o){return r[o]=function(r,u){return n(o,e.baseUrl+r,t(u),e)},r},{})}function t(e){return e||null}function n(e,t,n,u){var c=["then","catch","always"],s=c.reduce(function(e,t){return e[t]=function(n){return e[t]=n,e},e},{}),f=new XMLHttpRequest,p=r(t,n,e);return f.open(e,p,!0),f.withCredentials=u.hasOwnProperty("withCredentials"),o(f,u.headers),f.addEventListener("readystatechange",a(s,f),!1),f.send(i(n)),s.abort=function(){return f.abort()},s}function r(e,t,n){if("get"!==n.toLowerCase()||!t)return e;var r=i(t),o=e.indexOf("?")>-1?"&":"?";return e+o+r}function o(e,t){t=t||{},u(t)||(t["Content-Type"]="application/x-www-form-urlencoded"),Object.keys(t).forEach(function(n){t[n]&&e.setRequestHeader(n,t[n])})}function u(e){return Object.keys(e).some(function(e){return"content-type"===e.toLowerCase()})}function a(e,t){return function n(){t.readyState===t.DONE&&(t.removeEventListener("readystatechange",n,!1),e.always.apply(e,c(t)),t.status>=200&&t.status<300?e.then.apply(e,c(t)):e["catch"].apply(e,c(t)))}}function c(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e]}function i(e){return s(e)?f(e):e}function s(e){return"[object Object]"===Object.prototype.toString.call(e)}function f(e,t){return Object.keys(e).map(function(n){if(e.hasOwnProperty(n)&&void 0!==e[n]){var r=e[n];return n=t?t+"["+n+"]":n,null!==r&&"object"==typeof r?f(r,n):p(n)+"="+p(r)}}).filter(Boolean).join("&")}function p(e){return encodeURIComponent(e)}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()}(this,function(){"use strict";function e(e){var r=["get","post","put","delete"];return e=e||{},e.baseUrl=e.baseUrl||"",e.method&&e.url?n(e.method,e.baseUrl+e.url,t(e.data),e):r.reduce(function(r,o){return r[o]=function(r,u){return n(o,e.baseUrl+r,t(u),e)},r},{})}function t(e){return e||null}function n(e,t,n,u){var c=["then","catch","always"],i=c.reduce(function(e,t){return e[t]=function(n){return e[t]=n,e},e},{}),f=new XMLHttpRequest,p=r(t,n,e);return f.open(e,p,!0),f.withCredentials=u.hasOwnProperty("withCredentials"),o(f,u.headers,n),f.addEventListener("readystatechange",a(i,f),!1),f.send(s(n)?JSON.stringify(n):n),i.abort=function(){return f.abort()},i}function r(e,t,n){if("get"!==n.toLowerCase()||!t)return e;var r=i(t),o=e.indexOf("?")>-1?"&":"?";return e+o+r}function o(e,t,n){t=t||{},u(t)||(t["Content-Type"]=s(n)?"application/json":"application/x-www-form-urlencoded"),Object.keys(t).forEach(function(n){t[n]&&e.setRequestHeader(n,t[n])})}function u(e){return Object.keys(e).some(function(e){return"content-type"===e.toLowerCase()})}function a(e,t){return function n(){t.readyState===t.DONE&&(t.removeEventListener("readystatechange",n,!1),e.always.apply(e,c(t)),t.status>=200&&t.status<300?e.then.apply(e,c(t)):e["catch"].apply(e,c(t)))}}function c(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e]}function i(e){return s(e)?f(e):e}function s(e){return"[object Object]"===Object.prototype.toString.call(e)}function f(e,t){return Object.keys(e).map(function(n){if(e.hasOwnProperty(n)&&void 0!==e[n]){var r=e[n];return n=t?t+"["+n+"]":n,null!==r&&"object"==typeof r?f(r,n):p(n)+"="+p(r)}}).filter(Boolean).join("&")}function p(e){return encodeURIComponent(e)}return e}); |
{ | ||
"name": "@fdaciuk/ajax", | ||
"description": "Ajax module in Vanilla JS", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"main": "dist/ajax.min.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -20,3 +20,3 @@ # Ajax | ||
You can install via bower: | ||
You can install via bower (but you should [avoid that](https://bower.io/blog/2017/how-to-migrate-away-from-bower/)): | ||
@@ -56,3 +56,3 @@ ```console | ||
```html | ||
<script src="//cdn.rawgit.com/fdaciuk/ajax/v2.3.1/dist/ajax.min.js"></script> | ||
<script src="//cdn.rawgit.com/fdaciuk/ajax/v2.3.2/dist/ajax.min.js"></script> | ||
``` | ||
@@ -141,4 +141,6 @@ | ||
If `content-type` is not passed, `application/x-www-form-urlencoded` will be used. | ||
If `content-type` is not passed, `application/x-www-form-urlencoded` will be used when you pass `data` as a _query string_. | ||
Passing `data` as `object`, `application/json` will be automatically used (_since v2.3.1_). | ||
**Note about uploads:** | ||
@@ -195,2 +197,14 @@ | ||
`data` might be a complex object, like: | ||
```js | ||
ajax().post('/api/new-post', { | ||
slug: 'my-new-post', | ||
meta: { | ||
categories: ['js', 'react'], | ||
tags: ['code'] | ||
} | ||
}) | ||
``` | ||
### `put(url, [data])` | ||
@@ -214,2 +228,5 @@ | ||
> _Disclaimer:_ these _return methods_ are not from **real Promises**, and they will just being called once. | ||
> If you want to work with real Promises, you should make your own abstraction. | ||
### `then(response, xhrObject)` | ||
@@ -216,0 +233,0 @@ |
@@ -56,5 +56,5 @@ ;(function (root, factory) { | ||
xhr.withCredentials = options.hasOwnProperty('withCredentials') | ||
setHeaders(xhr, options.headers) | ||
setHeaders(xhr, options.headers, data) | ||
xhr.addEventListener('readystatechange', ready(promiseMethods, xhr), false) | ||
xhr.send(objectToQueryString(data)) | ||
xhr.send(isObject(data) ? JSON.stringify(data) : data) | ||
promiseMethods.abort = function () { | ||
@@ -75,6 +75,8 @@ return xhr.abort() | ||
function setHeaders (xhr, headers) { | ||
function setHeaders (xhr, headers, data) { | ||
headers = headers || {} | ||
if (!hasContentType(headers)) { | ||
headers['Content-Type'] = 'application/x-www-form-urlencoded' | ||
headers['Content-Type'] = isObject(data) | ||
? 'application/json' | ||
: 'application/x-www-form-urlencoded' | ||
} | ||
@@ -81,0 +83,0 @@ Object.keys(headers).forEach(function (name) { |
182235
135
313