From 86837e3ea05e3d12f6c86279d9cbbee4a3172748 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Sun, 13 Jul 2025 14:41:25 +0400 Subject: [PATCH 1/3] clean up get reponse and add get commits --- src/controllers/gist-controller.ts | 52 ++++++++++++++++++++++++++++-- src/interfaces/gist-commit-item.ts | 11 +++++++ src/routes/gist-route.ts | 3 +- 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/interfaces/gist-commit-item.ts diff --git a/src/controllers/gist-controller.ts b/src/controllers/gist-controller.ts index 3ef4e29..73622b6 100644 --- a/src/controllers/gist-controller.ts +++ b/src/controllers/gist-controller.ts @@ -1,6 +1,8 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ import axios, { type AxiosError } from 'axios'; import { Request, Response } from 'express'; import { config } from '../config'; +import { IGistCommitItem } from '../interfaces/gist-commit-item'; const gistsBaseUrl = 'https://api.github.com/gists'; const headers = { @@ -14,9 +16,24 @@ async function get(req: Request, res: Response) { headers, }); + const { + owner, + history, + forks, + user, + url, + forks_url, + commits_url, + git_pull_url, + git_push_url, + html_url, + comments_url, + ...rest + } = data; + res.status(200).json({ success: true, - data, + data: rest, }); } catch (e) { if ((e as AxiosError).status === 404) { @@ -119,4 +136,35 @@ async function del(req: Request, res: Response) { } } -export { get, create, del, update }; +async function getCommits(req: Request, res: Response) { + try { + const { data } = await axios.get(`${gistsBaseUrl}/${req.params.id}/commits`, { + headers, + params: req.query, + }); + + const cleanData = data.map((x: IGistCommitItem) => { + const { user, url, ...rest } = x; + return rest; + }); + + res.status(200).json({ + success: true, + data: cleanData, + }); + } catch (e) { + if ((e as AxiosError).status === 404) { + res.status(404).json({ + success: false, + message: 'Gist not found', + }); + } else { + res.status(500).json({ + success: false, + message: 'Something went wrong', + }); + } + } +} + +export { get, create, del, update, getCommits }; diff --git a/src/interfaces/gist-commit-item.ts b/src/interfaces/gist-commit-item.ts new file mode 100644 index 0000000..117f871 --- /dev/null +++ b/src/interfaces/gist-commit-item.ts @@ -0,0 +1,11 @@ +export interface IGistCommitItem { + user: unknown; + version: string; + commited_at: string; + change_status: { + total: number; + additions: number; + deletions: number; + }; + url: string; +} diff --git a/src/routes/gist-route.ts b/src/routes/gist-route.ts index 5a2e41a..042fa6b 100644 --- a/src/routes/gist-route.ts +++ b/src/routes/gist-route.ts @@ -1,5 +1,5 @@ import express from 'express'; -import { create, del, get, update } from '../controllers/gist-controller'; +import { create, del, get, getCommits, update } from '../controllers/gist-controller'; const gistRouter = express.Router(); @@ -7,5 +7,6 @@ gistRouter.post('/', create); gistRouter.get('/:id', get); gistRouter.delete('/:id', del); gistRouter.patch('/:id', update); +gistRouter.get('/:id/commits', getCommits); export { gistRouter }; From d7ba12176351d9327ea1d46b81472ca548f09404 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Sun, 13 Jul 2025 15:34:06 +0400 Subject: [PATCH 2/3] remove raw url from get response --- src/controllers/gist-controller.ts | 9 ++++++++- src/interfaces/gist-file.ts | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/interfaces/gist-file.ts diff --git a/src/controllers/gist-controller.ts b/src/controllers/gist-controller.ts index 73622b6..55d2d21 100644 --- a/src/controllers/gist-controller.ts +++ b/src/controllers/gist-controller.ts @@ -3,6 +3,7 @@ import axios, { type AxiosError } from 'axios'; import { Request, Response } from 'express'; import { config } from '../config'; import { IGistCommitItem } from '../interfaces/gist-commit-item'; +import { IGistFile } from '../interfaces/gist-file'; const gistsBaseUrl = 'https://api.github.com/gists'; const headers = { @@ -31,9 +32,15 @@ async function get(req: Request, res: Response) { ...rest } = data; + const cleanedFiles = Object.fromEntries( + Object.entries(rest.files as Record).map( + ([filename, { raw_url, ...fileWithoutRaw }]) => [filename, fileWithoutRaw], + ), + ); + res.status(200).json({ success: true, - data: rest, + data: { ...rest, files: cleanedFiles }, }); } catch (e) { if ((e as AxiosError).status === 404) { diff --git a/src/interfaces/gist-file.ts b/src/interfaces/gist-file.ts new file mode 100644 index 0000000..5eae9fe --- /dev/null +++ b/src/interfaces/gist-file.ts @@ -0,0 +1,9 @@ +export interface IGistFile { + filename: string; + type: string; + language: string; + raw_url: string; + size: number; + truncated: boolean; + content?: string; +} From 726dc916d10e421e46381b3cece2381dd9459f35 Mon Sep 17 00:00:00 2001 From: 1ilit Date: Sun, 13 Jul 2025 15:59:45 +0400 Subject: [PATCH 3/3] add revision endpoint --- src/controllers/gist-controller.ts | 48 +++++++++++++++++++++++++++++- src/routes/gist-route.ts | 3 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/controllers/gist-controller.ts b/src/controllers/gist-controller.ts index 55d2d21..2156c36 100644 --- a/src/controllers/gist-controller.ts +++ b/src/controllers/gist-controller.ts @@ -174,4 +174,50 @@ async function getCommits(req: Request, res: Response) { } } -export { get, create, del, update, getCommits }; +async function getRevision(req: Request, res: Response) { + try { + const { data } = await axios.get(`${gistsBaseUrl}/${req.params.id}/${req.params.sha}`, { + headers, + }); + + const { + owner, + history, + forks, + user, + url, + forks_url, + commits_url, + git_pull_url, + git_push_url, + html_url, + comments_url, + ...rest + } = data; + + const cleanedFiles = Object.fromEntries( + Object.entries(rest.files as Record).map( + ([filename, { raw_url, ...fileWithoutRaw }]) => [filename, fileWithoutRaw], + ), + ); + + res.status(200).json({ + success: true, + data: { ...rest, files: cleanedFiles }, + }); + } catch (e) { + if ((e as AxiosError).status === 404) { + res.status(404).json({ + success: false, + message: 'Gist not found', + }); + } else { + res.status(500).json({ + success: false, + message: 'Something went wrong', + }); + } + } +} + +export { get, create, del, update, getCommits, getRevision }; diff --git a/src/routes/gist-route.ts b/src/routes/gist-route.ts index 042fa6b..588c4a9 100644 --- a/src/routes/gist-route.ts +++ b/src/routes/gist-route.ts @@ -1,5 +1,5 @@ import express from 'express'; -import { create, del, get, getCommits, update } from '../controllers/gist-controller'; +import { create, del, get, getCommits, update, getRevision } from '../controllers/gist-controller'; const gistRouter = express.Router(); @@ -8,5 +8,6 @@ gistRouter.get('/:id', get); gistRouter.delete('/:id', del); gistRouter.patch('/:id', update); gistRouter.get('/:id/commits', getCommits); +gistRouter.get('/:id/:sha', getRevision); export { gistRouter };