[Fix #1] Posts api endpoint & axios

develop
sundowndev 2018-09-02 22:46:36 +02:00
parent 8a1284abf4
commit af45e25fa2
3 changed files with 52 additions and 25 deletions

View File

@ -1,28 +1,27 @@
"use strict"; "use strict";
import ApiClient from './src/ApiClient'; //import ApiClient from './src/ApiClient';
import JsonResponse from './src/JsonResponse'; import JsonResponse from './src/JsonResponse';
const express = require('express'); const express = require('express');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const app = express(); const app = express();
const api = new ApiClient(); //const api = new ApiClient();
const json = new JsonResponse(); const json = new JsonResponse();
const axios = require('axios');
/** /**
* Config * Config
* @type {number} * @type {number}
*/ */
const port = 3000; const port = 3000;
const ApiUrl = 'https://jsonplaceholder.typicode.com';
/** /**
* Routes * Routes
*/ */
app.use(function (req, res, next) { app.use(function (req, res, next) {
// Default api domain
api.setApiUrl('https://jsonplaceholder.typicode.com');
json.init(app, res); json.init(app, res);
next() next()
@ -32,30 +31,31 @@ app.get('/', (req, res) => {
return json.send(res, 200, {data: []}); return json.send(res, 200, {data: []});
}); });
app.get('/posts', (req, res) => { app.get('/posts', async (req, res) => {
let posts = api.findAllPosts(); let posts = await axios.get(ApiUrl + '/posts');
posts.forEach(function (p) { const formatedPosts = posts.data.map(async post => {
let user = api.findUserById(p.userId); let userId = post.userId;
let user = await axios.get(ApiUrl + '/users/'+userId);
p.title = '<h1>' + p.title + '</h1>'; return {
p.body = '<p>' + p.body + '</p>'; id: post.id,
p.user = { title: '<h1>'+post.title+'</h1>',
"id": user.id, body: '<p>'+post.body+'</p>',
"firstname": user.name, user: {
"lastname": user.name, id: user.data.id,
"email": user.email, firstname: user.data.name,
"comments_count": 3, lastname: user.data.name,
"pos": user.geo, email: user.data.email,
"_links": { comments_count: 3,
"posts": "/posts?user=" + user.id pos: user.data.pos
} }
}; };
delete p.userId;
}); });
return json.send(res, 200, {data: posts}); const response = await Promise.all(formatedPosts);
return json.send(res, 200, {data: response});
}); });
app.listen(port, () => { app.listen(port, () => {

30
package-lock.json generated
View File

@ -147,6 +147,15 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz", "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ=="
}, },
"axios": {
"version": "0.18.0",
"resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
}
},
"babel-cli": { "babel-cli": {
"version": "6.26.0", "version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz", "resolved": "https://registry.npmjs.org/babel-cli/-/babel-cli-6.26.0.tgz",
@ -1831,6 +1840,24 @@
} }
} }
}, },
"follow-redirects": {
"version": "1.5.7",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.7.tgz",
"integrity": "sha512-NONJVIFiX7Z8k2WxfqBjtwqMifx7X42ORLFrOZ2LTKGj71G3C0kfdyTqGqr8fx5zSX6Foo/D95dgGWbPUiwnew==",
"requires": {
"debug": "^3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
@ -2754,8 +2781,7 @@
"is-buffer": { "is-buffer": {
"version": "1.1.6", "version": "1.1.6",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==", "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
"dev": true
}, },
"is-ci": { "is-ci": {
"version": "1.2.0", "version": "1.2.0",

View File

@ -21,6 +21,7 @@
"homepage": "https://github.com/Sundowndev/interview-v2#readme", "homepage": "https://github.com/Sundowndev/interview-v2#readme",
"dependencies": { "dependencies": {
"async": "^2.6.1", "async": "^2.6.1",
"axios": "^0.18.0",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"express": "^4.16.3", "express": "^4.16.3",
"request": "^2.88.0" "request": "^2.88.0"