-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
63 lines (51 loc) · 1.87 KB
/
helpers.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
import { env } from './constants.ts'
const authorization = env.API_AUTHORIZATION_KEY
export const isAuthorized = (ctx: any) => {
const requestToken = ctx.request.headers.get('Authorization')
if (requestToken === authorization) return true
ctx.response.status = 401
ctx.response.body = 'unauthorized'
return false
}
export const setHeaders = (ctx: any) => {
ctx.response.headers.set("Access-Control-Allow-Origin", "*")
ctx.response.headers.set("Cache-Control", "no-cache")
ctx.response.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
ctx.response.headers.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
}
export type Event = {
source: string
message: string
sendNotification?: boolean
createdAt: string
}
export const logEvent = async (db: Deno.Kv, event: Event) => {
const kvResponse = await db.get(['events'])
const events: Event[] = kvResponse.value as Event[] ?? []
events.push(event)
if (events.length > 100) events.shift()
await db.set(['events'], events)
if (event.sendNotification) await sendNTFYNotification(event)
}
export const getEvents = async (db: Deno.Kv): Promise<Event[]> => {
const kvResponse = await db.get(['events'])
const events = (kvResponse.value as unknown) as Event[] ?? []
return events.reverse()
}
export const errorResponse = (ctx: any, e: any) => {
console.error(e)
ctx.response.status = 500
ctx.response.body = 'something went wrong'
}
const sendNTFYNotification = async (event: Event) => {
const res = await (await fetch(`https://ntfy.sh/${env.NTFY_CHANNEL}`, {
method: 'POST',
body: `
${event.message}
At ${event.createdAt}
`,
headers: {
'Title': `New event! From ${event.source}`
}
})).json()
}