UNPKG

githubinator

Version:

Fetching Data from https://github.com

63 lines (52 loc) 1.67 kB
const fetch = require("node-fetch"); const getUser = require("./get"); async function getGist(id) { let res = await fetch(`https://api.github.com/gists/${id}`).then(res => res.json()); if (res.message && res.message === 'Not Found') { throw new Error('Invalid Github Gist ID'); } else if (res.message) { throw new Error(res.message); } let files = []; Object.values(res.files).forEach(f => { files.push({ name: f.filename, type: f.type, language: f.language, raw: f.raw_url, size: parseInt(f.size), truncated: Boolean(f.truncated), content: String(f.content) }) }); let history = []; Object.values(res.history).forEach(h => { history.push({ version: h.version, committedTimestamp: h.committed_at, changeStatus: h.change_status, url: h.url }) }) return { url: res.url, id: res.id, nodeId: res.node_id, pullURL: res.git_pull_url, pushURL: res.git_push_url, commentsURL: res.comments_url, htmlURL: res.html_url, public: Boolean(res.public), createdTimestamp: res.created_at, updatedAt: res.updated_at, description: res.description, truncated: Boolean(res.truncated), comments: res.comments, user: res.user, owner: await getUser(res.owner.login), history: history, forks: res.forks, files: files } } module.exports = getGist;