browser-fetch-json
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -5,3 +5,4 @@ // browser-fetch-json ~~ MIT License | ||
request: function(method, url, data, options) { | ||
options = Object.assign({ method: method.toUpperCase() }, options); | ||
const settings = { method: method.toUpperCase(), credentials: 'same-origin' }; | ||
options = Object.assign(settings, options); | ||
const jsonHeaders = { 'Content-Type': 'application/json', 'Accept': 'application/json' }; | ||
@@ -15,2 +16,4 @@ options.headers = Object.assign(jsonHeaders, options.headers); | ||
function toJson(response) { return response.json(); } | ||
if (fetchJson.logger) | ||
fetchJson.logger(Date.now(), options.method, url.split('?')[0]); | ||
return fetch(url, options).then(toJson); | ||
@@ -32,2 +35,8 @@ }, | ||
return fetchJson.request('DELETE', url, resource, options); | ||
}, | ||
logger: null, | ||
enableLogger: function(booleanOrFn) { | ||
const isFn = typeof booleanOrFn === 'function'; | ||
fetchJson.logger = isFn ? booleanOrFn : booleanOrFn === false ? null : console.log; | ||
return fetchJson.logger; | ||
} | ||
@@ -34,0 +43,0 @@ }; |
{ | ||
"name": "browser-fetch-json", | ||
"description": "A thin wrapper around Fetch API just for JSON in the browser", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "main": "browser-fetch-json.js", |
# browser-fetch-json | ||
<img src=https://centerkey.com/graphics/center-key-logo.svg align=right width=200 alt=logo> | ||
_A thin wrapper around node-fetch just for JSON_ | ||
_A thin wrapper around Fetch API just for JSON in the browser_ | ||
@@ -16,4 +16,9 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/center-key/browser-fetch-json/blob/master/LICENSE.txt) | ||
### A) Setup | ||
Install with the command: | ||
### A) Include | ||
Use the [jsdelivr.com](https://www.jsdelivr.com/package/npm/browser-fetch-json) CDN: | ||
```html | ||
<script src=https://cdn.jsdelivr.net/npm/browser-fetch-json@0.0/browser-fetch-json.min.js></script> | ||
``` | ||
Or install as a module: | ||
```shell | ||
@@ -32,5 +37,4 @@ $ npm install browser-fetch-json | ||
// NASA APOD | ||
const fetchJson = require('browser-fetch-json'); | ||
const url = 'https://api.nasa.gov/planetary/apod'; | ||
const params = { api_key: 'DEMO_KEY' }; | ||
const url = 'https://api.nasa.gov/planetary/apod'; | ||
const params = { api_key: 'DEMO_KEY' }; | ||
function handleData(data) { | ||
@@ -45,4 +49,3 @@ console.log('The NASA APOD for today is at: ' + data.url); | ||
// Create Jupiter | ||
const fetchJson = require('browser-fetch-json'); | ||
const resource = { name: 'Jupiter', position: 5 }; | ||
const resource = { name: 'Jupiter', position: 5 }; | ||
function handleData(data) { | ||
@@ -56,9 +59,8 @@ console.log(data); //HTTP response body as an object literal | ||
### C) Leverages node-fetch | ||
**browser-fetch-json** depends on and calls **[node-fetch](https://www.npmjs.com/package/node-fetch)**. | ||
### C) Leverages Fetch API | ||
**browser-fetch-json** uses the browser's native **[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)**. | ||
For comparison, the above POST example to create a planet would be done directly using **node-fetch** with the code: | ||
For comparison, the above POST example to create a planet would be done directly using the **Fetch API** with the code: | ||
```javascript | ||
// Create Jupiter (with node-fetch instead of browser-fetch-json) | ||
const fetch = require('node-fetch'); | ||
// Create Jupiter (with Fetch API instead of browser-fetch-json) | ||
const resource = { name: 'Jupiter', position: 5 }; | ||
@@ -81,3 +83,3 @@ const options = { | ||
``` | ||
The examples for **browser-fetch-json** and **node-fetch** each produce the same output. | ||
The examples for **browser-fetch-json** and the **Fetch API** each produce the same output. | ||
@@ -90,2 +92,3 @@ ### D) Details | ||
1. Runs `.json()` on the response from the promise. | ||
1. Sets `credentials` to `'same-origin'` to support user sessions for frameworks/servers such as Grails, Rails, PHP, Flask, etc. | ||
@@ -115,6 +118,6 @@ ### E) API | ||
Notes: | ||
1. Only the `url` parameter is required. The other parameters are optional. | ||
1. Only the `url` parameter is required. The other parameters are optional. | ||
1. The `params` object for `fetchJson.get()` is converted into a query string and appended to the `url`. | ||
1. The `resource` object is turned into the body of the HTTP request. | ||
1. The `options` parameter is passed through to **node-fetch** (see the **node-fetch** documentation for supported **[options](https://www.npmjs.com/package/node-fetch#options)**). | ||
1. The `options` parameter is passed through to the **Fetch API** (see the MDN **Fetch API** documentation for supported **[`init` options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)**). | ||
@@ -129,2 +132,8 @@ #### Dynamic HTTP method | ||
#### Logging | ||
Enable basic logging to the console with: | ||
```javascript | ||
fetchJson.enableLogger(); | ||
``` | ||
### F) Questions or enhancements | ||
@@ -131,0 +140,0 @@ Feel free to submit an [issue](https://github.com/center-key/browser-fetch-json/issues). |
20
spec.js
@@ -34,1 +34,21 @@ // Mocha Specification Cases | ||
}); | ||
//////////////////////////////////////////////////////////////////////////////////////////////////// | ||
describe('Function fetchJson.enableLogger()', () => { | ||
it('sets the logger to the function passed in', () => { | ||
function mockLogger() {} | ||
fetchJson.enableLogger(mockLogger); | ||
const actual = { type: typeof fetchJson.logger, fn: fetchJson.logger }; | ||
const expected = { type: 'function', fn: mockLogger }; | ||
assert.deepEqual(actual, expected); | ||
}); | ||
it('disables the logger when passed false', () => { | ||
fetchJson.enableLogger(false); | ||
const actual = { logger: fetchJson.logger, disabled: !fetchJson.logger }; | ||
const expected = { logger: null, disabled: true }; | ||
assert.deepEqual(actual, expected); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
136
1
11695
7
85