diff --git a/.babelrc b/.babelrc index c13c5f6..831f20a 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,4 @@ { - "presets": ["es2015"] + "presets": ["es2015"], + "plugins": ["transform-object-rest-spread"] } diff --git a/.eslintrc b/.eslintrc index 6b7ba39..1f7276d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,5 +2,8 @@ "extends": "airbnb/base", "rules": { "comma-dangle": 0 + }, + "ecmaFeatures": { + "experimentalObjectRestSpread": true } } diff --git a/dist/lib/fetch.js b/dist/lib/fetch.js new file mode 100644 index 0000000..32050dd --- /dev/null +++ b/dist/lib/fetch.js @@ -0,0 +1,40 @@ +'use strict'; + +var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = fetch; +exports.post = post; + +var _isomorphicFetch = require('isomorphic-fetch'); + +var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var JSON_HEADERS = { + Accept: 'application/json', + 'Content-Type': 'application/json' +}; + +var BASE_URL = process.env.BASE_URL || 'https://player.me/api/v1'; + +console.log(BASE_URL); +function fetch(endpoint) { + var config = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; + + var url = BASE_URL + '/' + endpoint; + + return (0, _isomorphicFetch2.default)(url, _extends({ + headers: JSON_HEADERS, + method: 'GET' + }, config)).then(function (response) { + return response.json(); + }); +} + +function post(endpoint, args) { + return fetch(endpoint, { method: 'POST', body: JSON.stringify(args) }); +} \ No newline at end of file diff --git a/package.json b/package.json index 2facf8c..92b1b0d 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,11 @@ "devDependencies": { "babel": "^6.3.26", "babel-core": "^6.4.0", + "babel-plugin-transform-object-rest-spread": "^6.3.13", "babel-preset-es2015": "^6.3.13", "chai": "^3.4.1", "del": "^2.2.0", + "eslint": "^1.10.3", "eslint-config-airbnb": "^3.1.0", "gulp": "^3.9.0", "gulp-babel": "^6.1.1", @@ -37,5 +39,8 @@ "gulp-plumber": "^1.0.1", "mocha": "^2.3.4", "run-sequence": "^1.1.5" + }, + "dependencies": { + "isomorphic-fetch": "^2.2.1" } } diff --git a/src/lib/fetch.js b/src/lib/fetch.js new file mode 100644 index 0000000..b048f86 --- /dev/null +++ b/src/lib/fetch.js @@ -0,0 +1,24 @@ +import _fetch from 'isomorphic-fetch'; + +const JSON_HEADERS = { + Accept: 'application/json', + 'Content-Type': 'application/json' +}; + +const BASE_URL = process.env.BASE_URL || 'https://player.me/api/v1'; + +export default function fetch(endpoint, config = {}) { + const url = `${BASE_URL}/${endpoint}`; + + return _fetch( + url, { + headers: JSON_HEADERS, + method: 'GET', + ...config + } + ).then(response => response.json()); +} + +export function post(endpoint, args) { + return fetch(endpoint, { method: 'POST', body: JSON.stringify(args) }); +}