-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns_records_middleware.tsx
60 lines (60 loc) · 1.94 KB
/
dns_records_middleware.tsx
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
import { bodyToJSON } from "https://cdn.jsdelivr.net/gh/masx200/[email protected]/body/bodyToJSON.ts";
import { dNSRecordsInstance } from "./dNSRecordsInstance.ts";
import {
Context,
NextFunction,
RetHandler,
} from "https://cdn.jsdelivr.net/gh/masx200/[email protected]/mod.ts";
import { JSONRPCSERVER } from "./JSONRPCSERVER.tsx";
import { dns_records_path_name } from "./dns_records_path_name.tsx";
import { JSONSTRINGIFYNULL4 } from "./JSONSTRINGIFYNULL4.ts";
/**
* dns_records_middleware中间件函数
* @param context 上下文对象
* @param next 下一个中间件函数
* @returns Promise<RetHandler> 返回处理结果
*/
export async function dns_records_middleware(
context: Context,
next: NextFunction,
): Promise<RetHandler> {
// console.log("dns_records_middleware");
if (
new URL(context.request.url).pathname === dns_records_path_name() &&
context.request.method.toLowerCase() == "post" &&
context.request.headers.get("content-type")?.startsWith(
"application/json",
)
) {
let body;
try {
body = await bodyToJSON(
context.request.body,
context.request.headers,
);
} catch (error) {
console.error(error);
return {
status: 400,
body: "Bad Request",
headers: {
"content-type": "text/plain",
},
};
}
console.log(JSONSTRINGIFYNULL4({ request: body }));
const result = await JSONRPCSERVER(body, dNSRecordsInstance);
console.log(JSONSTRINGIFYNULL4({ response: result, request: body }));
return {
headers: {
"content-type": "application/json",
},
status: 200,
body: JSON.stringify(
result,
),
};
}
await next();
return;
}