-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
35 lines (34 loc) · 1.69 KB
/
worker.js
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
const api = {
icon: '⚡️☁️',
name: 'webhooks.do',
description: 'Webhook Storage',
url: 'https://webhooks.do/api',
type: 'https://apis.do/tools',
endpoints: {
webhook: 'https://webhooks.do/:namespace',
details: 'https://webhooks.do/:namespace/:id',
},
site: 'https://webhooks.do',
login: 'https://webhooks.do/#',
#: 'https://webhooks.do/#',
repo: 'https://github.com/drivly/webhooks.do',
}
export default {
fetch: async (req, env) => {
const { user, body, url, ts, time, headers, cf } = await env.CTX.fetch(req).then(res => res.json())
const { origin, hostname, pathname } = new URL(req.url)
let [ _, namespace, id = headers['cf-ray'] ] = pathname.split('/')
if (namespace.length != 36 || namespace == ':namespace') {
namespace = crypto.randomUUID()
}
const ua = headers['user-agent']
const { ip, isp, city, region, country, continent } = user
const location = `${city}, ${region}, ${country}, ${continent}`
const list = `https://webhooks.do/${namespace}`
const data = body ? await env.WEBHOOKS.put(`${namespace}/${id}`, JSON.stringify({ namespace, id, time, url, body, headers, cf, user }, null, 2) , {
metadata: { time, ip, ua, location, url: `https://webhooks.do/${namespace}/${id}` },
expirationTtl: 30 * 24 * 60 * 60 ,
}) : id != headers['cf-ray'] ? await env.WEBHOOKS.getWithMetadata(`${namespace}/${id}`, { type: "json" }) : await env.WEBHOOKS.list({ prefix: `${namespace}/`}).then(list => list.keys.map(item => item.metadata))
return new Response(JSON.stringify({ api, namespace, ts, time, id, list, data, user }, null, 2), { headers: { 'content-type': 'application/json; charset=utf-8' }})
}
}