Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 796d02b
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [ | ||
"es2015", | ||
"stage-0" | ||
], | ||
"plugins": [ | ||
"transform-decorators-legacy" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require('babel-register'); | ||
require('babel-polyfill'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
|
||
}); | ||
|
||
}); |