diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..b787a9f --- /dev/null +++ b/dist/index.js @@ -0,0 +1,13 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.reachWithDispatch = exports.reachGraphQL = undefined; + +var _reachGraphQL = require('./reachGraphQL.js'); + +var _reachWithDispatch = require('./reachWithDispatch.js'); + +exports.reachGraphQL = _reachGraphQL.reachGraphQL; +exports.reachWithDispatch = _reachWithDispatch.reachWithDispatch; diff --git a/dist/reachGraphQL.js b/dist/reachGraphQL.js new file mode 100644 index 0000000..2b1eb5c --- /dev/null +++ b/dist/reachGraphQL.js @@ -0,0 +1,47 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.reachGraphQL = reachGraphQL; + +var _transport = require("./utils/transport.js"); + +require("babel-polyfill"); + +/** + * [reachGraphQL Makes queres or mutations against GraphQL] + * @param {[String]} path [path to the GraphQL server] + * @param {[Object]} query [The query that GraphQL will use to fetch your data] + * @param {[object]} queryParams = {} [should contain object with different query params] + * @return {[Object]} [Data that was queried or qutated] + */ +function reachGraphQL(path, query) { + var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + + return function _callee() { + var response; + return regeneratorRuntime.async(function _callee$(_context) { + while (1) switch (_context.prev = _context.next) { + case 0: + _context.prev = 0; + _context.next = 3; + return regeneratorRuntime.awrap((0, _transport.transport)(path, query, queryParams)); + + case 3: + response = _context.sent; + return _context.abrupt("return", response.data); + + case 7: + _context.prev = 7; + _context.t0 = _context["catch"](0); + + console.log(_context.t0); + + case 10: + case "end": + return _context.stop(); + } + }, null, this, [[0, 7]]); + }; +} diff --git a/dist/reachWithDispatch.js b/dist/reachWithDispatch.js new file mode 100644 index 0000000..4fb00e4 --- /dev/null +++ b/dist/reachWithDispatch.js @@ -0,0 +1,52 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.reachWithDispatch = reachWithDispatch; + +var _transport = require("./utils/transport.js"); + +require("babel-polyfill"); + +/** + * [reachWithDispatch description] + * @param {[String]} path [path to the GraphQL server] + * @param {[Object]} query [The query that GraphQL will use to fetch your data] + * @param {[object]} queryParams = {} [should contain object with different query params] + * @param {[type]} actionCreator = ( [The actionCreator to dispatch] + * @return {[function]} [dispatch to store] + */ +function reachWithDispatch(path, query) { + var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + var actionCreator = arguments.length <= 3 || arguments[3] === undefined ? function () {} : arguments[3]; + + return function _callee(dispatch) { + var response; + return regeneratorRuntime.async(function _callee$(_context) { + while (1) switch (_context.prev = _context.next) { + case 0: + _context.prev = 0; + _context.next = 3; + return regeneratorRuntime.awrap((0, _transport.transport)(path, query, queryParams)); + + case 3: + response = _context.sent; + + dispatch(actionCreator(response.data)); + _context.next = 10; + break; + + case 7: + _context.prev = 7; + _context.t0 = _context["catch"](0); + + console.log(_context.t0); + + case 10: + case "end": + return _context.stop(); + } + }, null, this, [[0, 7]]); + }; +} diff --git a/dist/utils/transport.js b/dist/utils/transport.js new file mode 100644 index 0000000..f6f6be0 --- /dev/null +++ b/dist/utils/transport.js @@ -0,0 +1,38 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.transport = transport; + +var _isomorphicFetch = require('isomorphic-fetch'); + +var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function transport(path, query) { + var queryParams = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; + + var url = path; + return new Promise(function (resolve, reject) { + (0, _isomorphicFetch2.default)(url, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + query: query, + queryParams: queryParams + }) + }).then(function (res) { + return res.json(); + }).then(function (response) { + if (response.errors) { + return reject(response.errors); + } + return resolve(response.data); + }).catch(response.error); + }); +} diff --git a/package.json b/package.json index 709a49b..741f29b 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "react-reach", "version": "0.0.3", "description": "A small library for react to communicate with GraphQL", - "main": "src/index.js", + "main": "dist/index.js", "scripts": { - "prebuild": "rm -rf dist && mkdir dist", - "build": "babel src/index.js -o dist/index.js", + "prebuild": "rm -rf dist && mkdir dist && mkdir dist/utils", + "build": "babel src/index.js -o dist/index.js && babel src/reachGraphQL.js -o dist/reachGraphQL.js && babel src/reachWithDispatch.js -o dist/reachWithDispatch.js && babel src/utils/transport.js -o dist/utils/transport.js", "commit": "git-cz", "test": "mocha test/reachGraphQL.spec.js -w --compilers js:babel-core/register", "test:single": "mocha test/reachGraphQL.spec.js --compilers js:babel-core/register",