[Fix #1] Posts api endpoint & axios
parent
8a1284abf4
commit
af45e25fa2
46
index.js
46
index.js
|
@ -1,28 +1,27 @@
|
|||
"use strict";
|
||||
|
||||
import ApiClient from './src/ApiClient';
|
||||
//import ApiClient from './src/ApiClient';
|
||||
import JsonResponse from './src/JsonResponse';
|
||||
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
const app = express();
|
||||
const api = new ApiClient();
|
||||
//const api = new ApiClient();
|
||||
const json = new JsonResponse();
|
||||
const axios = require('axios');
|
||||
|
||||
/**
|
||||
* Config
|
||||
* @type {number}
|
||||
*/
|
||||
const port = 3000;
|
||||
const ApiUrl = 'https://jsonplaceholder.typicode.com';
|
||||
|
||||
/**
|
||||
* Routes
|
||||
*/
|
||||
app.use(function (req, res, next) {
|
||||
// Default api domain
|
||||
api.setApiUrl('https://jsonplaceholder.typicode.com');
|
||||
|
||||
json.init(app, res);
|
||||
|
||||
next()
|
||||
|
@ -32,30 +31,31 @@ app.get('/', (req, res) => {
|
|||
return json.send(res, 200, {data: []});
|
||||
});
|
||||
|
||||
app.get('/posts', (req, res) => {
|
||||
let posts = api.findAllPosts();
|
||||
app.get('/posts', async (req, res) => {
|
||||
let posts = await axios.get(ApiUrl + '/posts');
|
||||
|
||||
posts.forEach(function (p) {
|
||||
let user = api.findUserById(p.userId);
|
||||
const formatedPosts = posts.data.map(async post => {
|
||||
let userId = post.userId;
|
||||
let user = await axios.get(ApiUrl + '/users/'+userId);
|
||||
|
||||
p.title = '<h1>' + p.title + '</h1>';
|
||||
p.body = '<p>' + p.body + '</p>';
|
||||
p.user = {
|
||||
"id": user.id,
|
||||
"firstname": user.name,
|
||||
"lastname": user.name,
|
||||
"email": user.email,
|
||||
"comments_count": 3,
|
||||
"pos": user.geo,
|
||||
"_links": {
|
||||
"posts": "/posts?user=" + user.id
|
||||
return {
|
||||
id: post.id,
|
||||
title: '<h1>'+post.title+'</h1>',
|
||||
body: '<p>'+post.body+'</p>',
|
||||
user: {
|
||||
id: user.data.id,
|
||||
firstname: user.data.name,
|
||||
lastname: user.data.name,
|
||||
email: user.data.email,
|
||||
comments_count: 3,
|
||||
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, () => {
|
||||
|
|
|
@ -147,6 +147,15 @@
|
|||
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
|
||||
"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": {
|
||||
"version": "6.26.0",
|
||||
"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": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",
|
||||
|
@ -2754,8 +2781,7 @@
|
|||
"is-buffer": {
|
||||
"version": "1.1.6",
|
||||
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
|
||||
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==",
|
||||
"dev": true
|
||||
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
|
||||
},
|
||||
"is-ci": {
|
||||
"version": "1.2.0",
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
"homepage": "https://github.com/Sundowndev/interview-v2#readme",
|
||||
"dependencies": {
|
||||
"async": "^2.6.1",
|
||||
"axios": "^0.18.0",
|
||||
"body-parser": "^1.18.3",
|
||||
"express": "^4.16.3",
|
||||
"request": "^2.88.0"
|
||||
|
|
Loading…
Reference in New Issue