Total stars count (Codeberg + Github)

node v10.23.3
version: 0.0.0
endpointsharetweet
/** * https://codeberg.org/api/v1/repos/teddit/teddit * https://api.github.com/repos/teddit-net/teddit * * USAGE: * https://badgen.net/runkit/vladimyr/total-stars/codeberg;owner=teddit;repo=teddit/github;owner=teddit-net;repo=teddit * */ const ghGot = require('gh-got'); const got = require('got'); const { send } = require('micro'); const cbGot = got.extend({ baseURL: 'https://codeberg.org/api/v1', responseType: 'json' }); exports.endpoint = async function (req, res) { const groups = req.url.split('/').filter(Boolean) if (groups.length < 2) { return send(res, 400, { subject: 'stars', status: 'malformed args', color: 'grey' }) } const stars = await Promise.all(groups.slice(0, 2).map(args => { const [host, ...params] = args.split(';'); const { owner, repo } = Object.values(params.map(it => it.split('=')); return fetchStars(host, owner, repo); })); const totalStars = stars.reduce((acc ,val) => acc + val); return { subject: `total stars`, status: totalStars, color: 'blue' } } async function fetchStars(host, owner, repo) { switch (host) { case 'gh': case 'github': { const { stargazers_count } = await ghGot(`/repos/${owner}/${repo}`); return stargazers_count; } case 'cb': case 'codeberg': { const { stars_count } = await cbGot(`/repos/${owner}/${repo}`); return stars_count; } } }
Loading…

no comments

    sign in to comment