browser-fetch-json
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,2 +0,2 @@ | ||
//! browser-fetch-json v0.1.1 | ||
//! browser-fetch-json v0.1.2 | ||
//! A thin wrapper around the Fetch API just for JSON in the browser | ||
@@ -3,0 +3,0 @@ //! MIT License -- https://github.com/center-key/browser-fetch-json |
@@ -1,3 +0,3 @@ | ||
//! browser-fetch-json v0.1.1 | ||
//! browser-fetch-json v0.1.2 | ||
//! MIT License -- https://github.com/center-key/browser-fetch-json | ||
const fetchJson={request:function(a,b,c,d){const e={method:a.toUpperCase(),credentials:'same-origin'};d=Object.assign(e,d);return d.headers=Object.assign({"Content-Type":'application/json',Accept:'application/json'},d.headers),'GET'===d.method&&c?b=b+(b.includes('?')?'&':'?')+Object.keys(c).map(function(a){return a+'='+c[a]}).join('&'):'GET'!==d.method&&c&&(d.body=JSON.stringify(c)),fetchJson.logger&&fetchJson.logger(new Date().toISOString(),d.method,b),fetch(b,d).then(function(a){return a.json()})},get:function(a,b,c){return fetchJson.request('GET',a,b,c)},post:function(a,b,c){return fetchJson.request('POST',a,b,c)},put:function(a,b,c){return fetchJson.request('PUT',a,b,c)},patch:function(a,b,c){return fetchJson.request('PATCH',a,b,c)},delete:function(a,b,c){return fetchJson.request('DELETE',a,b,c)},logger:null,enableLogger:function(a){return fetchJson.logger='function'==typeof a?a:!1===a?null:console.log,fetchJson.logger}};'object'==typeof module?module.exports=fetchJson:'object'==typeof window&&(window.fetchJson=fetchJson); |
{ | ||
"name": "browser-fetch-json", | ||
"description": "A thin wrapper around the Fetch API just for JSON in the browser", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "main": "browser-fetch-json.js", |
@@ -17,5 +17,3 @@ # browser-fetch-json | ||
For a node version, see: [node-fetch-json](https://www.npmjs.com/package/node-fetch-json) | ||
### 1) Include | ||
### 1) Setup | ||
In a web page: | ||
@@ -150,3 +148,6 @@ ```html | ||
### 7) Questions or enhancements | ||
### 7) Related | ||
For a node version, see: [node-fetch-json](https://www.npmjs.com/package/node-fetch-json) | ||
### 8) Questions or enhancements | ||
Feel free to submit an [issue](https://github.com/center-key/browser-fetch-json/issues). | ||
@@ -153,0 +154,0 @@ |
26
spec.js
// Mocha Specification Cases | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const fetchJson = require('./browser-fetch-json.js'); | ||
const { JSDOM } = require('jsdom'); | ||
const scripts = ['node_modules/whatwg-fetch/fetch.js', './browser-fetch-json.js']; | ||
const window = new JSDOM('', { runScripts: 'outside-only' }).window; | ||
function loadScript(file) { window.eval(fs.readFileSync(file).toString()); } | ||
scripts.forEach(loadScript); | ||
const fetchJson = window.fetchJson; | ||
//////////////////////////////////////////////////////////////////////////////////////////////////// | ||
@@ -54,1 +61,18 @@ describe('Module browser-fetch-json', () => { | ||
}); | ||
//////////////////////////////////////////////////////////////////////////////////////////////////// | ||
describe('Response returned by httpbin.org for a planet (object literal)', () => { | ||
it('from a POST contains the planet (JSON)', (done) => { | ||
const url = 'https://httpbin.org/post'; | ||
const resource = { name: 'Mercury', position: 1 }; | ||
function handleData(data) { | ||
const actual = { planet: data.json, type: typeof data.json }; | ||
const expected = { planet: resource, type: 'object' }; | ||
assert.deepEqual(actual, expected); | ||
done(); | ||
} | ||
fetchJson.post(url, resource).then(handleData); | ||
}); | ||
}); |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
139571
11
235
155
2