Comparing version
@@ -40,3 +40,3 @@ ;(function (window) { | ||
return fetch(url, opts) | ||
return fetchival.fetch(url, opts) | ||
.then(function (response) { | ||
@@ -83,2 +83,5 @@ if (response.status >= 200 && response.status < 300) { | ||
// Expose fetch so that other polyfills can be used | ||
fetchival.fetch = typeof fetch !== 'undefined' ? fetch : null | ||
// Support CommonJS, AMD & browser | ||
@@ -85,0 +88,0 @@ if (typeof exports === 'object') |
@@ -1,1 +0,1 @@ | ||
(function(n){function t(n){return n.filter(function(n){return!(n instanceof Object)}).join("/")}function e(n){return n.filter(function(n){return n instanceof Object})[0]}function r(n){var t=Object.keys(n).map(function(t){return[t,encodeURIComponent(n[t])].join("=")});return["?"].concat(t).join("&")}function o(n,o,i,u){var c=e(o)||{mode:fetchival.mode};var f=t(o);c.method=n;c.headers={Accept:"application/json","Content-Type":"application/json"};if(u){f+=r(u)}if(i){c.body=JSON.stringify(i)}return fetch(f,c).then(function(n){if(n.status>=200&&n.status<300){return n.json()}var t=new Error(n.statusText);t.response=n;throw t})}function fetchival(){var n=Array.prototype.slice;var t=n.call(arguments);var e=function(){return fetchival.apply(this,t.concat(n.call(arguments)))};e.get=function(n){return o("GET",t,null,n)};e.post=function(n){return o("POST",t,n)};e.put=function(n){return o("PUT",t,n)};e.patch=function(n){return o("PATCH",t,n)};e.delete=function(){return o("DELETE",t)};return e}if(typeof exports==="object")module.exports=fetchival;else if(typeof define==="function"&&define.amd)define(function(){return fetchival});else n.fetchival=fetchival})(typeof window!="undefined"?window:undefined); | ||
(function(n){function t(n){return n.filter(function(n){return!(n instanceof Object)}).join("/")}function e(n){return n.filter(function(n){return n instanceof Object})[0]}function r(n){var t=Object.keys(n).map(function(t){return[t,encodeURIComponent(n[t])].join("=")});return["?"].concat(t).join("&")}function o(n,o,i,u){var f=e(o)||{mode:fetchival.mode};var c=t(o);f.method=n;f.headers={Accept:"application/json","Content-Type":"application/json"};if(u){c+=r(u)}if(i){f.body=JSON.stringify(i)}return fetchival.fetch(c,f).then(function(n){if(n.status>=200&&n.status<300){return n.json()}var t=new Error(n.statusText);t.response=n;throw t})}function fetchival(){var n=Array.prototype.slice;var t=n.call(arguments);var e=function(){return fetchival.apply(this,t.concat(n.call(arguments)))};e.get=function(n){return o("GET",t,null,n)};e.post=function(n){return o("POST",t,n)};e.put=function(n){return o("PUT",t,n)};e.patch=function(n){return o("PATCH",t,n)};e.delete=function(){return o("DELETE",t)};return e}fetchival.fetch=typeof fetch!=="undefined"?fetch:null;if(typeof exports==="object")module.exports=fetchival;else if(typeof define==="function"&&define.amd)define(function(){return fetchival});else n.fetchival=fetchival})(typeof window!="undefined"?window:undefined); |
{ | ||
"name": "fetchival", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Makes JSON requests with fetch easier", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "mocha test/tests.js", | ||
"uglify": "uglifyjs -m -r fetchival index.js > index.min.js", | ||
@@ -32,4 +32,7 @@ "size": "uglifyjs -m -r fetchival index.js | gzip -9f | wc -c" | ||
"devDependencies": { | ||
"chai": "^2.2.0", | ||
"mocha": "^2.2.4", | ||
"node-fetch": "^1.1.1", | ||
"uglify-js": "^2.4.20" | ||
} | ||
} |
# fetchival | ||
> Make your [fetch](https://github.com/github/fetch) requests dance! | ||
> Makes writing JSON requests with [fetch](https://github.com/github/fetch) easier | ||
Fetchival is syntactic sugar for JSON requests with [fetch](https://github.com/github/fetch) (it's only 0.5kb min/gz). | ||
Fetchival is 0.5kb min/gzip. It supports all major browsers (IE9+) and Node. | ||
 | ||
### fetch | ||
### fetch only | ||
```javascript | ||
// POST request | ||
fetch('/users', { | ||
@@ -36,4 +37,4 @@ method: 'post', | ||
```javascript | ||
// POST request | ||
fetchival('/users').post({ | ||
@@ -48,5 +49,7 @@ name: 'Typicode', | ||
`.get()`, `.put()`, `.patch()` and `.delete()` methods are also available. | ||
## Installation | ||
Fetchival is available on Bower and npm (Browserify) | ||
Fetchival is available on Bower and npm | ||
@@ -59,3 +62,3 @@ ```bash | ||
```bash | ||
npm install es6-promise whatwg-fetch # polyfills | ||
npm install es6-promise whatwg-fetch --save # polyfills | ||
npm install fetchival --save | ||
@@ -67,9 +70,11 @@ ``` | ||
```javascript | ||
//posts | ||
var posts = fetchival('/posts') | ||
//posts | ||
posts.get() | ||
posts.get({ category: 'javascript' }) | ||
posts.post({ title: 'Fetchival' }) | ||
//posts?category=javascript | ||
posts.get({ category: 'javascript' }) | ||
//posts/1 | ||
@@ -81,4 +86,5 @@ posts(1).get() | ||
var comments = posts(1, 'comments') | ||
//posts/1/comments | ||
var comments = posts(1, 'comments') | ||
comments.get() | ||
@@ -117,2 +123,15 @@ | ||
To use fetchival in Node, you need to install `node-fetch` and configure fetchival to use it | ||
```javascript | ||
var fetchival = require('fetchival') | ||
fetchival.fetch = require('node-fetch') | ||
``` | ||
## Browser Support | ||
 |  |  |  |  | ||
--- | --- | --- | --- | --- | | ||
Latest ✔ | Latest ✔ | 9+ ✔ | Latest ✔ | 6.1+ ✔ | | ||
## Credit | ||
@@ -119,0 +138,0 @@ |
@@ -1,6 +0,13 @@ | ||
var assert = chai.assert | ||
fetchival.mode = 'cors' | ||
if (typeof window !== 'undefined') { | ||
var assert = chai.assert | ||
fetchival.mode = 'cors' | ||
} else { | ||
var assert = require('chai').assert | ||
var fetchival = require('../') | ||
fetchival.fetch = require('node-fetch') | ||
} | ||
describe('fetchival', function () { | ||
this.slow(2000) | ||
this.timeout(5000) | ||
this.slow(5000) | ||
@@ -71,5 +78,8 @@ describe('fetchival(/posts)', function () { | ||
it('should #delete()', function (done) { | ||
// Actually delete fails on jsonplaceholder because it doesn't return JSON | ||
posts(1) | ||
.delete() | ||
.then(function (obj) { | ||
assert.deepEqual(obj, {}) | ||
done() | ||
}) | ||
.catch(done) | ||
@@ -94,3 +104,3 @@ }) | ||
describe('fetchival(/not/found)', function () { | ||
var notFound = fetchival('/not/found') | ||
var notFound = fetchival('http://jsonplaceholder.typicode.com/not/found') | ||
@@ -101,3 +111,2 @@ it('should fail with 404', function (done) { | ||
.catch(function (err) { | ||
console.log(err) | ||
assert.equal(err.response.status, 404) | ||
@@ -104,0 +113,0 @@ done() |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
161793
1.14%5890
0.19%1
-50%138
15.97%4
300%