Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
feat: implement fetch wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 19, 2016
1 parent c22e3f5 commit 604b224
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["es2015"]
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread"]
}
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "airbnb/base",
"rules": {
"comma-dangle": 0
},
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
}
40 changes: 40 additions & 0 deletions dist/lib/fetch.js
Original file line number Diff line number Diff line change
@@ -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) });
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -37,5 +39,8 @@
"gulp-plumber": "^1.0.1",
"mocha": "^2.3.4",
"run-sequence": "^1.1.5"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
}
}
24 changes: 24 additions & 0 deletions src/lib/fetch.js
Original file line number Diff line number Diff line change
@@ -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) });
}

0 comments on commit 604b224

Please sign in to comment.