@brightleaf/react-hooks
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -54,2 +54,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "useQuery", { | ||
enumerable: true, | ||
get: function () { | ||
return _useGraphql.useQuery; | ||
} | ||
}); | ||
Object.defineProperty(exports, "useKeypress", { | ||
@@ -154,3 +160,3 @@ enumerable: true, | ||
var _useGraphql = _interopRequireDefault(require("./use-graphql")); | ||
var _useGraphql = _interopRequireWildcard(require("./use-graphql")); | ||
@@ -185,2 +191,4 @@ var _useKeypress = _interopRequireDefault(require("./use-keypress")); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _mergeDeep = require("./utils/merge-deep"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -53,5 +55,3 @@ | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const fullConfig = (0, _mergeDeep.mergeDeep)(defaultConfig, config); | ||
/** | ||
@@ -58,0 +58,0 @@ * getUrl - HTTP Get the url passed in |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.default = exports.useGraphQL = void 0; | ||
exports.useQuery = exports.default = exports.useGraphQL = void 0; | ||
@@ -13,2 +13,6 @@ var _graphqlRequest = require("graphql-request"); | ||
var _useRequest = require("./use-request"); | ||
var _mergeDeep = require("./utils/merge-deep"); | ||
const reducer = (state, action) => { | ||
@@ -61,3 +65,4 @@ switch (action.type) { | ||
const useGraphQL = function (url, query, variables = {}) { | ||
const useGraphQL = function (url, query, variables = {}, options = {}) { | ||
const client = new _graphqlRequest.GraphQLClient(url, options); | ||
const [state, dispatch] = (0, _react.useReducer)(reducer, { | ||
@@ -73,3 +78,3 @@ data: [], | ||
}); | ||
const resp = await (0, _graphqlRequest.request)(url, query, variables); | ||
const resp = await client.request(query, variables); | ||
const data = resp; | ||
@@ -94,2 +99,52 @@ dispatch({ | ||
var _default = useGraphQL; | ||
exports.default = _default; | ||
exports.default = _default; | ||
const defaultConfig = { | ||
method: 'POST', | ||
mode: 'cors', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
redirect: 'follow', | ||
referrer: 'no-referrer' | ||
}; | ||
const useQuery = (url = '/graphql', query, config = { | ||
method: 'POST' | ||
}) => { | ||
const { | ||
data, | ||
error, | ||
loading, | ||
makeRequest | ||
} = (0, _useRequest.useRequest)(url, { | ||
data: null, | ||
error: null, | ||
loading: false | ||
}); | ||
const fullConfig = (0, _mergeDeep.mergeDeep)(defaultConfig, config); | ||
/** | ||
* postData - Post data to url | ||
* | ||
* @param {object} data - the data to post | ||
*/ | ||
const makeQuery = async variables => { | ||
await makeRequest({ ...fullConfig, | ||
body: JSON.stringify({ | ||
query, | ||
variables | ||
}) | ||
}); | ||
}; | ||
return { | ||
data, | ||
error, | ||
loading, | ||
makeQuery | ||
}; | ||
}; | ||
exports.useQuery = useQuery; |
@@ -38,3 +38,4 @@ "use strict"; | ||
const useMutation = function (url, query) { | ||
const useMutation = function (url, query, options = {}) { | ||
const client = new _graphqlRequest.GraphQLClient(url, options); | ||
const [state, dispatch] = (0, _react.useReducer)(reducer, { | ||
@@ -50,3 +51,3 @@ data: [], | ||
}); | ||
const resp = await (0, _graphqlRequest.request)(url, query, variables); | ||
const resp = await client.request(query, variables); | ||
const data = resp; | ||
@@ -53,0 +54,0 @@ dispatch({ |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _mergeDeep = require("./utils/merge-deep"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -53,5 +55,3 @@ | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const fullConfig = (0, _mergeDeep.mergeDeep)(defaultConfig, config); | ||
/** | ||
@@ -58,0 +58,0 @@ * postData - Post data to url |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _mergeDeep = require("./utils/merge-deep"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -53,5 +55,3 @@ | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const fullConfig = (0, _mergeDeep.mergeDeep)(defaultConfig, config); | ||
/** | ||
@@ -58,0 +58,0 @@ * putData - PUT data to url |
@@ -12,2 +12,4 @@ "use strict"; | ||
var _mergeDeep = require("./utils/merge-deep"); | ||
const defaultConfig = { | ||
@@ -48,5 +50,3 @@ mode: 'cors', | ||
}); | ||
const fullConfig = { ...defaultConfig, | ||
...config | ||
}; | ||
const fullConfig = (0, _mergeDeep.mergeDeep)(defaultConfig, config); | ||
/** | ||
@@ -64,3 +64,13 @@ * makeRequest - HTTP Get the url passed in | ||
const result = await resp.json(); | ||
dispatch({ | ||
if (result && result.data) { | ||
return dispatch({ | ||
type: 'success', | ||
payload: { | ||
data: result.data | ||
} | ||
}); | ||
} | ||
return dispatch({ | ||
type: 'success', | ||
@@ -67,0 +77,0 @@ payload: { |
{ | ||
"name": "@brightleaf/react-hooks", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Useful react hooks", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54745
32
1471