-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponses.ts
63 lines (58 loc) · 1.67 KB
/
responses.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
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Creates a response that closes the incoming TCP connection.
*/
export function drop() {
return new Response(
new ReadableStream({
start(controller) {
controller.error();
},
}),
);
}
export function badRequest(body: string) {
return Response.json(body, {
status: 400,
headers: { "x-error-type": "input" },
});
}
export function notFound() {
return Response.json("Not found", {
status: 404,
});
}
export function methodNotAllowed() {
return Response.json("Method not allowed", {
status: 403,
});
}
export function assertArgCount(actual: number, expected: number) {
if (expected == 0 && actual != 0) {
throw badRequest("No arguments were expected.");
}
if (expected == 1 && actual != 1) {
throw badRequest("A single argument was expected.");
}
if (actual != expected) {
throw badRequest(`${expected} arguments were expected.`);
}
}