forked from Naccl/NBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfworker-tg-api-open.js
More file actions
37 lines (34 loc) · 1018 Bytes
/
cfworker-tg-api-open.js
File metadata and controls
37 lines (34 loc) · 1018 Bytes
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
addEventListener("fetch", event => {
let response = handleRequest(event.request)
return event.respondWith(response)
})
async function handleRequest(request) {
const { method, headers } = request
if (method === "POST" && headers.get("content-type") === "application/json") {
const json = await request.json()
const toUrl = json.to
console.log(toUrl)
const data = json.data
let response = await fetch(toUrl, {
method: "POST",
headers,
body: JSON.stringify(data)
})
const results = await gatherResponse(response)
return new Response(results, {
headers: {
"content-type": "application/json;charset=UTF-8",
},
})
}
return new Response("error", { status: 403 })
}
async function gatherResponse(response) {
const { headers } = response
const contentType = headers.get("content-type") || ""
if (contentType.includes("application/json")) {
return JSON.stringify(await response.json())
} else {
return response.text()
}
}