-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgithub-server.ts
202 lines (165 loc) · 4.42 KB
/
github-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Fastify, { type FastifyRequest } from "fastify";
import type { Endpoints } from "@octokit/types";
import { type RouteGenericInterface } from "fastify/types/route";
// this is a mock server to simulate the GitHub API,
// it's not a complete implementation, only the parts used in the app
// Why not just mock the Octokit fetch method?
// Because we use Node.js and next.js edge runtime, mocked state won't persist between runtime,
// and edge runtime doesn't have access to the file system to store the state.
type Issue =
Endpoints["GET /repos/{owner}/{repo}/issues"]["response"]["data"][0];
type Comment =
Endpoints["GET /repos/{owner}/{repo}/issues/{issue_number}/comments"]["response"]["data"][0];
const comments: Record<string, Comment[]> = {};
const issues: Issue[] = [];
let counter = 1;
const app = Fastify({
logger: false,
});
type IssueRequest<T extends RouteGenericInterface = RouteGenericInterface> =
FastifyRequest<
T & {
Params: {
owner: string[];
repo: string[];
issue_number: string[] | string;
};
}
>;
const mockUser = {
login: "test",
id: 1,
avatar_url: "https://i.pravatar.cc/128",
name: "Test User",
email: "[email protected]",
} as Endpoints["GET /user"]["response"]["data"];
app.get("/", async () => "Hello, world!");
app.get("/user", async () => mockUser);
app.get(
"/repos/:owner/:repo/issues",
(
req: FastifyRequest<{
Querystring: {
state?: "open" | "closed";
limit?: string;
page?: string;
};
}>,
) => {
let result = issues;
if (req.query?.state)
result = result.filter((i) => i.state === req.query.state);
if (req.query?.limit) {
const page = Number(req.query.page ?? 1);
const start = (page - 1) * Number(req.query.limit);
result = result.slice(start, start + Number(req.query.limit));
}
return result;
},
);
app.get(
"/repos/:owner/:repo/issues/:issue_number",
async (req: IssueRequest, res) => {
const issue = issues.find((i) => i.number === getIssueNumber(req));
if (!issue)
return res.status(404).send({
message: "Issue not found",
});
return issue;
},
);
app.post(
"/repos/:owner/:repo/issues",
async (
req: IssueRequest<{
Body: {
title: string;
body: string;
};
}>,
) => {
const issue = {
title: req.body.title,
body: req.body.body,
id: counter++,
number: counter++,
node_id: "",
url: "",
repository_url: "",
labels_url: "",
comments_url: "",
events_url: "",
html_url: "",
user: mockUser,
state: "open",
labels: [],
assignee: null,
milestone: null,
closed_at: null,
created_at: new Date().toISOString(),
locked: false,
updated_at: new Date().toISOString(),
closed_by: null,
comments: 0,
author_association: "CONTRIBUTOR",
} satisfies Issue;
issues.push(issue);
comments[issue.id] = [];
return issue;
},
);
app.patch(
"/repos/:owner/:repo/issues/:issue_number",
async (
req: IssueRequest<{
Body: Partial<Issue>;
}>,
) => {
const issue = issues.find((i) => i.number === getIssueNumber(req));
if (!issue)
return {
message: "Issue not found",
};
Object.assign(issue, req.body);
issues[issues.findIndex((i) => i.number === issue.number)] = issue;
return issue;
},
);
app.get(
"/repos/:owner/:repo/issues/:issue_number/comments",
async (req: IssueRequest) => comments[req.params.issue_number[0] ?? 1] ?? [],
);
app.post(
"/repos/:owner/:repo/issues/:issue_number/comments",
async (
req: IssueRequest<{
Body: string;
}>,
) => {
comments[getIssueNumber(req)] ??= [];
const comment = {
id: counter++,
body: req.body,
user: mockUser,
node_id: "",
created_at: new Date().toISOString(),
html_url: "",
updated_at: new Date().toISOString(),
url: "",
issue_url: "",
author_association: "CONTRIBUTOR",
} satisfies Comment;
comments[getIssueNumber(req)]?.push(comment);
return comment;
},
);
await app.listen({
port: 3001,
});
function getIssueNumber(req: IssueRequest) {
if (Array.isArray(req.params.issue_number)) {
if (!req.params.issue_number[0]) throw new Error("Issue number not found");
return Number(req.params.issue_number[0]);
}
return Number(req.params.issue_number);
}