Repository navigation
Expand file tree
/
Copy pathdeno.js
More file actions
85 lines (77 loc) · 2.68 KB
/
Copy pathdeno.js
File metadata and controls
85 lines (77 loc) · 2.68 KB
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
export default {
async fetch(request: Request): Promise<Response> {
// Handle CORS preflight request
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Max-Age": "86400",
},
});
}
// Only handle POST requests
if (request.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
// Validate Authorization header
const authHeader = request.headers.get("Authorization");
const TOKEN = Deno.env.get("TOKEN");
if (TOKEN) {
if (!authHeader || authHeader !== `Bearer ${TOKEN}`) {
return new Response("Unauthorized", {
status: 401,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
});
}
}
try {
// Clone request body
const body = await request.json();
// Construct new request headers
const headers = new Headers({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",
"Accept": "text/event-stream",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"sec-ch-ua-platform": "Windows",
"X-Deepinfra-Source": "web-page",
"sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Microsoft Edge\";v=\"133\", \"Chromium\";v=\"133\"",
"sec-ch-ua-mobile": "?0",
"Origin": "https://deepinfra.com",
"Sec-Fetch-Site": "same-site",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
"Referer": "https://deepinfra.com/"
});
// Send request to DeepInfra
const response = await fetch("https://api.deepinfra.com/v1/openai/chat/completions", {
method: "POST",
headers: headers,
body: JSON.stringify(body)
});
// Construct response
const newResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": response.headers.get("Content-Type") || "application/json",
}
});
return newResponse;
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
});
}
}
};