From a66e11d9318d089f06bb55c6f60db262ebb01501 Mon Sep 17 00:00:00 2001 From: Wonho Kim Date: Fri, 2 Nov 2018 21:16:58 +0900 Subject: [PATCH] modify contributor handler --- handlers/github.ts | 68 ++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/handlers/github.ts b/handlers/github.ts index 5c32593..3bd87f1 100644 --- a/handlers/github.ts +++ b/handlers/github.ts @@ -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 { return new Promise(resolve => {