Skip to content

Commit d567f27

Browse files
author
Alex Cory
committed
cleaning house
1 parent 7bc0603 commit d567f27

File tree

4 files changed

+96
-132
lines changed

4 files changed

+96
-132
lines changed

dist/examples.e31bb0bc.js

Lines changed: 85 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -33850,43 +33850,39 @@ function useFetch(arg1, arg2) {
3385033850

3385133851
case 13:
3385233852
_data = _context.sent;
33853-
_context.next = 27;
33853+
_context.next = 21;
3385433854
break;
3385533855

3385633856
case 16:
3385733857
_context.prev = 16;
3385833858
_context.t0 = _context["catch"](10);
33859-
_context.prev = 18;
33860-
_context.next = 21;
33859+
_context.next = 20;
3386133860
return response.text();
3386233861

33863-
case 21:
33862+
case 20:
3386433863
_data = _context.sent;
33864+
33865+
case 21:
33866+
setData(_data);
3386533867
_context.next = 27;
3386633868
break;
3386733869

3386833870
case 24:
3386933871
_context.prev = 24;
33870-
_context.t1 = _context["catch"](18);
33871-
setError("Currently only supports JSON and Text response types: ".concat(_context.t1));
33872+
_context.t1 = _context["catch"](4);
33873+
setError(_context.t1);
3387233874

3387333875
case 27:
33874-
setData(_data);
33876+
_context.prev = 27;
3387533877
setLoading(false);
33876-
_context.next = 34;
33877-
break;
33878+
return _context.finish(27);
3387833879

33879-
case 31:
33880-
_context.prev = 31;
33881-
_context.t2 = _context["catch"](4);
33882-
setError(_context.t2);
33883-
33884-
case 34:
33880+
case 30:
3388533881
case "end":
3388633882
return _context.stop();
3388733883
}
3388833884
}
33889-
}, _callee, null, [[4, 31], [10, 16], [18, 24]]);
33885+
}, _callee, null, [[4, 24, 27, 30], [10, 16]]);
3389033886
}));
3389133887

3389233888
return function (_x, _x2) {
@@ -34213,22 +34209,11 @@ var App = function App() {
3421334209
data = _useFetch2[0],
3421434210
loading = _useFetch2[1],
3421534211
error = _useFetch2[2],
34216-
refetch = _useFetch2[3]; // const handleClick = () => {
34217-
// refetch()
34218-
// }
34219-
// const {data, loading, error, request} = useGet('https://api.etilbudsavis.dk/v2/dealerfront?country_id=DK')
34220-
// const handleClick = () => {
34221-
// Get()
34222-
// }
34223-
// console.log('DATA: ', data)
34224-
// const [data, loading, error] = usePost('https://jsonplaceholder.typicode.com/posts', {
34225-
// body: JSON.stringify({
34226-
// title: 'foo',
34227-
// body: 'bar',
34228-
// userId: 1
34229-
// })
34230-
// })
34231-
// WORKS 😍
34212+
get = _useFetch2[3].get;
34213+
34214+
var handleClick = function handleClick() {
34215+
return get();
34216+
}; // WORKS 😍
3423234217
// const [data, loading, error, request] = useFetch({
3423334218
// url: 'https://jsonplaceholder.typicode.com/posts/1',
3423434219
// // baseUrl: '', // then you can do: request.post({ url: '/posts', body: {} })
@@ -34252,83 +34237,83 @@ var App = function App() {
3425234237
// })
3425334238

3425434239

34255-
var handleClick = function handleClick() {} // get('/1')
34256-
// post('/', {
34257-
// // params: '?no=way&something=true',
34258-
// title: 'foo',
34259-
// body: 'bar',
34260-
// userId: 1
34261-
// })
34262-
// patch('/1', {
34263-
// title: 'foo',
34264-
// body: 'bar',
34265-
// userId: 1
34266-
// })
34267-
// put('/1', {
34268-
// title: 'foo',
34269-
// body: 'bar',
34270-
// userId: 1
34271-
// })
34272-
// del('/1')
34273-
// request.get()
34274-
// request.post({
34275-
// // params: '?no=way&something=true',
34276-
// title: 'foo',
34277-
// body: 'bar',
34278-
// userId: 1
34279-
// })
34280-
// request.patch({
34281-
// title: 'foo',
34282-
// body: 'bar',
34283-
// userId: 1
34284-
// })
34285-
// request.put({
34286-
// title: 'foo',
34287-
// body: 'bar',
34288-
// userId: 1
34289-
// })
34290-
// request.delete({
34291-
// title: 'foo',
34292-
// body: 'bar',
34293-
// userId: 1
34294-
// })
34295-
// get()
34296-
// post({
34297-
// // params: '?no=way&something=true',
34298-
// title: 'foo',
34299-
// body: 'bar',
34300-
// userId: 1
34301-
// })
34302-
// patch({
34303-
// title: 'foo',
34304-
// body: 'bar',
34305-
// userId: 1
34306-
// })
34307-
// put({
34308-
// title: 'foo',
34309-
// body: 'bar',
34310-
// userId: 1
34311-
// })
34312-
// del({
34313-
// title: 'foo',
34314-
// body: 'bar',
34315-
// userId: 1
34316-
// })
34317-
// if (error) return 'Error...'
34318-
// if (loading) return 'Loading...'
34319-
;
34240+
var handleClick2 = function handleClick2() {// get('/1')
34241+
// post('/', {
34242+
// // params: '?no=way&something=true',
34243+
// title: 'foo',
34244+
// body: 'bar',
34245+
// userId: 1
34246+
// })
34247+
// patch('/1', {
34248+
// title: 'foo',
34249+
// body: 'bar',
34250+
// userId: 1
34251+
// })
34252+
// put('/1', {
34253+
// title: 'foo',
34254+
// body: 'bar',
34255+
// userId: 1
34256+
// })
34257+
// del('/1')
34258+
// request.get()
34259+
// request.post({
34260+
// // params: '?no=way&something=true',
34261+
// title: 'foo',
34262+
// body: 'bar',
34263+
// userId: 1
34264+
// })
34265+
// request.patch({
34266+
// title: 'foo',
34267+
// body: 'bar',
34268+
// userId: 1
34269+
// })
34270+
// request.put({
34271+
// title: 'foo',
34272+
// body: 'bar',
34273+
// userId: 1
34274+
// })
34275+
// request.delete({
34276+
// title: 'foo',
34277+
// body: 'bar',
34278+
// userId: 1
34279+
// })
34280+
// get()
34281+
// post({
34282+
// // params: '?no=way&something=true',
34283+
// title: 'foo',
34284+
// body: 'bar',
34285+
// userId: 1
34286+
// })
34287+
// patch({
34288+
// title: 'foo',
34289+
// body: 'bar',
34290+
// userId: 1
34291+
// })
34292+
// put({
34293+
// title: 'foo',
34294+
// body: 'bar',
34295+
// userId: 1
34296+
// })
34297+
// del({
34298+
// title: 'foo',
34299+
// body: 'bar',
34300+
// userId: 1
34301+
// })
34302+
};
3432034303

34304+
if (error) return 'Error...';
34305+
if (loading) return 'Loading...';
3432134306
return _react.default.createElement("div", {
3432234307
className: "App"
34323-
}, "HELLO WORL", _react.default.createElement("header", {
34308+
}, _react.default.createElement("header", {
3432434309
className: "App-header"
3432534310
}, _react.default.createElement("button", {
3432634311
onClick: handleClick
3432734312
}, "CLICK"), _react.default.createElement("code", {
3432834313
style: {
3432934314
display: 'block'
3433034315
}
34331-
})));
34316+
}, _react.default.createElement("pre", null, JSON.stringify(data, null, 2)))));
3433234317
};
3433334318

3433434319
(0, _reactDom.render)(_react.default.createElement(App, null), document.getElementById('root')); // If you want your app to work offline and load faster, you can change
@@ -34364,7 +34349,7 @@ var parent = module.bundle.parent;
3436434349
if ((!parent || !parent.isParcelRequire) && typeof WebSocket !== 'undefined') {
3436534350
var hostname = "" || location.hostname;
3436634351
var protocol = location.protocol === 'https:' ? 'wss' : 'ws';
34367-
var ws = new WebSocket(protocol + '://' + hostname + ':' + "56755" + '/');
34352+
var ws = new WebSocket(protocol + '://' + hostname + ':' + "63592" + '/');
3436834353

3436934354
ws.onmessage = function (event) {
3437034355
checkedAssets = {};

dist/examples.e31bb0bc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,8 @@ import useFetch, { useGet, usePost } from '../src/index'
77
// import useFetch, { useGet, usePost } from '../dist'
88

99
const App = () => {
10-
const [data, loading, error, refetch] = useFetch('https://api.etilbudsavis.dk/v2/dealerfront?country_id=DK')
11-
// const handleClick = () => {
12-
// refetch()
13-
// }
14-
// const {data, loading, error, request} = useGet('https://api.etilbudsavis.dk/v2/dealerfront?country_id=DK')
15-
// const handleClick = () => {
16-
// Get()
17-
// }
18-
19-
// console.log('DATA: ', data)
20-
21-
// const [data, loading, error] = usePost('https://jsonplaceholder.typicode.com/posts', {
22-
// body: JSON.stringify({
23-
// title: 'foo',
24-
// body: 'bar',
25-
// userId: 1
26-
// })
27-
// })
10+
const [data, loading, error, { get }] = useFetch('https://api.etilbudsavis.dk/v2/dealerfront?country_id=DK')
11+
const handleClick = () => get()
2812

2913
// WORKS 😍
3014
// const [data, loading, error, request] = useFetch({
@@ -52,7 +36,7 @@ const App = () => {
5236
// const [data, loading, error, get] = useGet({
5337
// url: 'https://jsonplaceholder.typicode.com/posts/1'
5438
// })
55-
const handleClick = () => {
39+
const handleClick2 = () => {
5640
// get('/1')
5741
// post('/', {
5842
// // params: '?no=way&something=true',
@@ -118,16 +102,15 @@ const App = () => {
118102
}
119103

120104

121-
// if (error) return 'Error...'
122-
// if (loading) return 'Loading...'
105+
if (error) return 'Error...'
106+
if (loading) return 'Loading...'
123107

124108
return (
125109
<div className="App">
126-
HELLO WORL
127110
<header className="App-header">
128111
<button onClick={handleClick}>CLICK</button>
129112
<code style={{ display: 'block' }}>
130-
{/* <pre>{JSON.stringify(data, null, 2)}</pre> */}
113+
<pre>{JSON.stringify(data, null, 2)}</pre>
131114
</code>
132115
</header>
133116
</div>

src/useFetch.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ export function useFetch(arg1, arg2) {
3434
const [loading, setLoading] = useState(onMount)
3535
const [error, setError] = useState(null)
3636

37-
const fetchData = useCallback(
38-
method => async (fArg1, fArg2) => {
37+
const fetchData = useCallback(method => async (fArg1, fArg2) => {
3938
let query = ''
4039
const fetchOptions = {}
4140
if (isObject(fArg1) && method.toLowerCase() !== 'get') {
@@ -57,16 +56,13 @@ export function useFetch(arg1, arg2) {
5756
try {
5857
data = await response.json()
5958
} catch (err) {
60-
try {
61-
data = await response.text()
62-
} catch (err) {
63-
setError(`Currently only supports JSON and Text response types: ${err}`)
64-
}
59+
data = await response.text()
6560
}
6661
setData(data)
67-
setLoading(false)
6862
} catch (err) {
6963
setError(err)
64+
} finally {
65+
setLoading(false)
7066
}
7167
},
7268
[url]

0 commit comments

Comments
 (0)