From ea689095e69a8866b081843acfafc08a7d2eb680 Mon Sep 17 00:00:00 2001 From: wildan3105 Date: Sat, 19 Aug 2023 10:59:47 +0700 Subject: [PATCH] fix: lint --- .eslintrc.json | 5 ++++- src/api/controller.js | 14 +++++--------- src/api/service.js | 9 +++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8fcab28..bb0f09d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -56,7 +56,10 @@ "no-use-before-define": "error", "no-whitespace-before-property":"error", "no-cond-assign": [2, "except-parens"], - "no-unused-vars": [1, {"vars": "local", "args": "none"}], + "no-unused-vars": [ + "error", + { "vars": "local", "args": "none" } + ], "no-loop-func":"error", "object-shorthand":"error", "object-curly-spacing": [2, "always"], diff --git a/src/api/controller.js b/src/api/controller.js index ec55d0e..4b524ae 100644 --- a/src/api/controller.js +++ b/src/api/controller.js @@ -23,19 +23,15 @@ const client_secret = process.env.CLIENT_SECRET || false; if (!(client_id && client_secret)) { throw CLIENT_ID_AND_CLIENT_SECRET_MISSING_ERROR_MESSAGE; } -const clientParams = `client_id=${client_id}&client_secret=${client_secret}`; // load service const GithubService = require('./service'); -const githubService = new GithubService(); +const githubService = new GithubService(GITHUB_API_URL, client_id, client_secret); -const reqRepos = (username, numberOfPages) => { - const headers = { 'User-Agent': `${username}` }; - const url = (page) => - `${GITHUB_API_URL}/users/${username}/repos?${clientParams}&per_page=${REPOS_PER_PAGE}&page=${page}`; +const reqRepos = async (username, numberOfPages) => { const requests = []; for (let i = 1; i <= numberOfPages; i++) { - requests.push(axios.get(url(i), { headers })); + requests.push(await githubService.getReposForUser(username, i)); } return requests; }; @@ -68,7 +64,7 @@ exports.index = async (req, res) => { } await githubService.getUser(username) - .then((user) => { + .then(async (user) => { const userData = user.data; const numberOfRepos = userData.public_repos; const fetchedRenderValue = { @@ -94,7 +90,7 @@ exports.index = async (req, res) => { const numberOfPages = (parseInt(numberOfRepos) / REPOS_PER_PAGE) + 1; - axios.all(reqRepos(username, numberOfPages)) + axios.all(await reqRepos(username, numberOfPages)) .then((pages) => { const reposData = _.flatMap(pages, (page) => page.data); diff --git a/src/api/service.js b/src/api/service.js index 8cbec79..5eddd5c 100644 --- a/src/api/service.js +++ b/src/api/service.js @@ -1,13 +1,14 @@ 'use strict'; const axios = require('axios'); -const { GITHUB_API_URL, REPOS_PER_PAGE } = require('../config'); +const { REPOS_PER_PAGE } = require('../config'); class GithubService { - constructor() { - this.url = GITHUB_API_URL, + constructor(url, clientId, clientSecret) { + this.url = url, + this.clientParams = `client_id=${clientId}&client_secret=${clientSecret}`; + this.timeout = 10000; - this.clientParams = `client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}`; this.reposPerPage = REPOS_PER_PAGE; }