Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c90d1b

Browse files
committedApr 20, 2025
add gists routes
1 parent d71c0ea commit 2c90d1b

File tree

7 files changed

+251
-2
lines changed

7 files changed

+251
-2
lines changed
 

‎.env.sample

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ MAIL_PASSWORD=my-app-password
33
MAIL_USERNAME=mail@example.com
44
MAIL_SERVICE=gmail
55
PORT=5000
6-
NODE_END=dev
6+
NODE_END=dev
7+
GITHUB_TOKEN=a-gh-token

‎package-lock.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "server",
33
"version": "1.0.0",
44
"description": "Server that handles form submission for drawDB",
5-
"main": "index.js",
65
"scripts": {
76
"build": "tsc",
87
"start": "node dist/index.js",
@@ -30,6 +29,7 @@
3029
"typescript-eslint": "^8.30.1"
3130
},
3231
"dependencies": {
32+
"axios": "^1.8.4",
3333
"cors": "^2.8.5",
3434
"dotenv": "^16.5.0",
3535
"express": "^4.21.2",

‎src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express';
22
import cors from 'cors';
33
import { emailRouter } from './routes/email-route';
4+
import { gistRouter } from './routes/gist-route';
45
import { config } from './config';
56

67
const app = express();
@@ -25,5 +26,6 @@ app.get('/', (req, res) => {
2526
});
2627

2728
app.use('/email', emailRouter);
29+
app.use('/gists', gistRouter);
2830

2931
export default app;

‎src/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ dotenvConfig();
44

55
export const config = {
66
dev: process.env.NODE_ENV === 'dev',
7+
api: {
8+
github: process.env.GITHUB_TOKEN,
9+
},
710
server: {
811
port: process.env.PORT || 5000,
912
allowedOrigins: process.env.CLIENT_URLS ? process.env.CLIENT_URLS.split(',') : [],

‎src/controllers/gist-controller.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import axios, { type AxiosError } from 'axios';
2+
import { Request, Response } from 'express';
3+
import { config } from '../config';
4+
5+
const gistsBaseUrl = 'https://api.github.com/gists';
6+
const headers = {
7+
'X-GitHub-Api-Version': '2022-11-28',
8+
Authorization: 'Bearer ' + config.api.github,
9+
};
10+
11+
async function get(req: Request, res: Response) {
12+
try {
13+
const { data } = await axios.get(`${gistsBaseUrl}/${req.params.id}`, {
14+
headers,
15+
});
16+
17+
res.status(200).json({
18+
success: true,
19+
data,
20+
});
21+
} catch (e) {
22+
if ((e as AxiosError).status === 404) {
23+
res.status(404).json({
24+
success: false,
25+
message: 'Gist not found',
26+
});
27+
} else {
28+
res.status(500).json({
29+
success: false,
30+
message: 'Something went wrong',
31+
});
32+
}
33+
}
34+
}
35+
36+
async function create(req: Request, res: Response) {
37+
try {
38+
const { description, filename, content, public: isGistPublic } = req.body;
39+
40+
const { data } = await axios.post(
41+
gistsBaseUrl,
42+
{
43+
description,
44+
public: isGistPublic,
45+
files: {
46+
[filename]: { content },
47+
},
48+
},
49+
{ headers },
50+
);
51+
52+
res.status(200).json({
53+
success: true,
54+
data,
55+
});
56+
} catch {
57+
res.status(500).json({
58+
success: false,
59+
message: 'Something went wrong',
60+
});
61+
}
62+
}
63+
64+
async function update(req: Request, res: Response) {
65+
try {
66+
const { filename, content } = req.body;
67+
68+
await axios.patch(
69+
`${gistsBaseUrl}/${req.params.id}`,
70+
{
71+
files: {
72+
[filename]: { content },
73+
},
74+
},
75+
{ headers },
76+
);
77+
78+
res.status(200).json({
79+
success: true,
80+
message: 'Gist updated',
81+
});
82+
} catch (e) {
83+
if ((e as AxiosError).status === 404) {
84+
res.status(404).json({
85+
success: false,
86+
message: 'Gist not found',
87+
});
88+
} else {
89+
res.status(500).json({
90+
success: false,
91+
message: 'Something went wrong',
92+
});
93+
}
94+
}
95+
}
96+
97+
async function del(req: Request, res: Response) {
98+
try {
99+
await axios.delete(`${gistsBaseUrl}/${req.params.id}`, {
100+
headers,
101+
});
102+
103+
res.status(200).json({
104+
success: true,
105+
message: 'Gist deleted',
106+
});
107+
} catch (e) {
108+
if ((e as AxiosError).status === 404) {
109+
res.status(404).json({
110+
success: false,
111+
message: 'Gist not found',
112+
});
113+
} else {
114+
res.status(500).json({
115+
success: false,
116+
message: 'Something went wrong',
117+
});
118+
}
119+
}
120+
}
121+
122+
export { get, create, del, update };

‎src/routes/gist-route.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import express from 'express';
2+
import { create, del, get, update } from '../controllers/gist-controller';
3+
4+
const gistRouter = express.Router();
5+
6+
gistRouter.post('/', create);
7+
gistRouter.get('/:id', get);
8+
gistRouter.delete('/:id', del);
9+
gistRouter.patch('/:id', update);
10+
11+
export { gistRouter };

0 commit comments

Comments
 (0)
Please sign in to comment.