Skip to content

Commit

Permalink
Merge pull request #220 from likelionWonHo/master
Browse files Browse the repository at this point in the history
[Modify] modify contributor handler
  • Loading branch information
gimdongwoo committed Dec 2, 2018
2 parents 300fd01 + a66e11d commit 2bb5809
Showing 1 changed file with 33 additions and 35 deletions.
68 changes: 33 additions & 35 deletions handlers/github.ts
Expand Up @@ -40,44 +40,42 @@ router.get('/repos', (req: Request, res: Response, next: NextFunction) => {
});

router.get('/contributors', async (req: Request, res: Response, next: NextFunction) => {
try {
const body = await cache.wrap(
'github/contributors',
async () => {
const getDetails = async login => {
const { data } = await axios(`${apiBaseUrl}/users/${login}`, {
headers: {
...headers,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': `${name}/${version}`,
},
});
return data as IContributor;
};

let promises = [];
let contributors = await readContributorFile();
contributors.forEach(i => {
promises.push(getDetails(i.login));
const body = await cache.wrap(
'github/contributors',
async () => {
const getDetails = async login => {
const { data } = await axios(`${apiBaseUrl}/users/${login}`, {
headers: {
...headers,
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': `${name}/${version}`,
},
});
return data as IContributor;
};

return await Promise.all(promises)
.then(values => {
return values as IContributor[];
})
.catch(err => {
console.log(err);
next(err);
});
},
{ ttl },
);
let promises = [];
let contributors = await readContributorFile();
contributors.forEach(i => {
promises.push(
getDetails(i.login)
.then(data => data)
.catch(err => {
console.log(err);
return err;
}),
);
});

return res.status(200).json(body);
} catch (err) {
console.log(err);
return next(err);
}
return await Promise.all(promises).then(values => {
values = values.filter(value => !(value instanceof Error));
return values as IContributor[];
});
},
{ ttl },
);

return res.status(200).json(body);

function readContributorFile(): Promise<IContributor[]> {
return new Promise(resolve => {
Expand Down

0 comments on commit 2bb5809

Please sign in to comment.