Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlaak committed May 28, 2016
0 parents commit 796d02b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .babelrc
@@ -0,0 +1,9 @@
{
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"transform-decorators-legacy"
]
}
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.idea
node_modules
2 changes: 2 additions & 0 deletions mocha.config.js
@@ -0,0 +1,2 @@
require('babel-register');
require('babel-polyfill');
26 changes: 26 additions & 0 deletions package.json
@@ -0,0 +1,26 @@
{
"name": "fitted",
"version": "0.0.1",
"description": "",
"scripts": {
"test": "npm run mocha-single",
"mocha-single": "mocha tests --recursive --require mocha.config.js",
"mocha-watch": "mocha tests --recursive --require mocha.config.js --watch"
},
"author": "Joris Blaak",
"license": "Apache-2.0",
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.9.0",
"expect": "^1.14.0",
"expect.js": "^0.3.1",
"mocha": "^2.5.3"
},
"dependencies": {
"request": "^2.72.0"
}
}
34 changes: 34 additions & 0 deletions src/http.js
@@ -0,0 +1,34 @@
import Request from 'request';

export default function (url) {

const req = (res) => {
return new Promise((resolve, reject) => {
Request(url, function (error, response, body) {

var contentType = response.headers['content-type'];
if(contentType && contentType.indexOf('application/json') !== -1) {
body = JSON.parse(body);
}

if (!error && response.statusCode == 200) {
resolve(body);
} else {
reject(body);
}
})
})
};

return function (target, key, descriptor) {
const fn = descriptor.value;

if (typeof fn !== 'function') {
throw new Error(`@http decorator can only be applied to methods not: ${typeof fn}`);
}

descriptor.value = () => fn(req, {});
return descriptor;
}
}

29 changes: 29 additions & 0 deletions tests/http.test.js
@@ -0,0 +1,29 @@
import expect from 'expect.js';
import http from '../src/http';

describe('HttpDecorator', function () {

describe('@http', function () {

it('should be able to fetch topstories from HackerNews', async function () {
/* Given */
class HackerNews {

@http('https://hacker-news.firebaseio.com/v0/topstories.json')
static topstories (request, response) {
return request(response);
}

}

/* When */
const topstories = await HackerNews.topstories();

/* Then */
expect(topstories).to.be.an(Array);
expect(topstories.length).to.be.greaterThan(0);
});

});

});

0 comments on commit 796d02b

Please sign in to comment.