@brightleaf/react-hooks
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -8,33 +8,45 @@ "use strict"; | ||
var _react = require("react"); | ||
var _useRequest = _interopRequireDefault(require("./use-request")); | ||
var _reducer = require("./reducer"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const useGet = url => { | ||
const [state, dispatch] = (0, _react.useReducer)(_reducer.reducer, { | ||
const defaultConfig = { | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer' | ||
}; | ||
const useGet = (url = '', config = { | ||
method: 'GET' | ||
}) => { | ||
const { | ||
data, | ||
error, | ||
loading, | ||
makeRequest | ||
} = (0, _useRequest.default)(url, { | ||
data: [], | ||
error: null, | ||
loading: true | ||
loading: false | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const getUrl = async url => { | ||
dispatch({ | ||
type: 'get' | ||
}); | ||
const resp = await fetch(url); | ||
const data = await resp.json(); | ||
dispatch({ | ||
type: 'success', | ||
payload: { | ||
data | ||
} | ||
}); | ||
const getUrl = async (urlToGet = url) => { | ||
await makeRequest({ | ||
method: 'GET', | ||
...fullConfig | ||
}, urlToGet); | ||
}; | ||
(0, _react.useEffect)(() => { | ||
if (url) { | ||
getUrl(url); | ||
} | ||
}, [url]); | ||
return { ...state, | ||
return { | ||
data, | ||
error, | ||
loading, | ||
getUrl | ||
@@ -41,0 +53,0 @@ }; |
@@ -8,8 +8,26 @@ "use strict"; | ||
var _react = require("react"); | ||
var _useRequest = _interopRequireDefault(require("./use-request")); | ||
var _reducer = require("./reducer"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const usePost = (url = '') => { | ||
const [state, dispatch] = (0, _react.useReducer)(_reducer.reducer, { | ||
const defaultConfig = { | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer' | ||
}; | ||
const usePost = (url = '', config = { | ||
method: 'POST' | ||
}) => { | ||
const { | ||
data, | ||
error, | ||
loading, | ||
makeRequest | ||
} = (0, _useRequest.default)(url, { | ||
data: [], | ||
@@ -19,29 +37,18 @@ error: null, | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const postData = async data => { | ||
dispatch({ | ||
type: 'get' | ||
}); | ||
const resp = await fetch(url, { | ||
await makeRequest({ | ||
method: 'POST', | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer', | ||
...fullConfig, | ||
body: JSON.stringify(data) | ||
}); | ||
const result = await resp.json(); | ||
dispatch({ | ||
type: 'success', | ||
payload: { | ||
data: result | ||
} | ||
}); | ||
}; | ||
return { ...state, | ||
return { | ||
data, | ||
error, | ||
loading, | ||
postData | ||
@@ -48,0 +55,0 @@ }; |
@@ -6,10 +6,28 @@ "use strict"; | ||
}); | ||
exports.default = exports.usePost = void 0; | ||
exports.default = exports.usePut = void 0; | ||
var _react = require("react"); | ||
var _useRequest = _interopRequireDefault(require("./use-request")); | ||
var _reducer = require("./reducer"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const usePost = (url = '') => { | ||
const [state, dispatch] = (0, _react.useReducer)(_reducer.reducer, { | ||
const defaultConfig = { | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer' | ||
}; | ||
const usePut = (url = '', config = { | ||
method: 'PUT' | ||
}) => { | ||
const { | ||
data, | ||
error, | ||
loading, | ||
makeRequest | ||
} = (0, _useRequest.default)(url, { | ||
data: [], | ||
@@ -19,29 +37,18 @@ error: null, | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const postData = async data => { | ||
dispatch({ | ||
type: 'get' | ||
}); | ||
const resp = await fetch(url, { | ||
await makeRequest({ | ||
method: 'PUT', | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer', | ||
...fullConfig, | ||
body: JSON.stringify(data) | ||
}); | ||
const result = await resp.json(); | ||
dispatch({ | ||
type: 'success', | ||
payload: { | ||
data: result | ||
} | ||
}); | ||
}; | ||
return { ...state, | ||
return { | ||
data, | ||
error, | ||
loading, | ||
postData | ||
@@ -51,4 +58,4 @@ }; | ||
exports.usePost = usePost; | ||
var _default = usePost; | ||
exports.usePut = usePut; | ||
var _default = usePut; | ||
exports.default = _default; |
{ | ||
"name": "@brightleaf/react-hooks", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Useful react hooks", | ||
@@ -5,0 +5,0 @@ "files": [ |
28157
19
811
2