This repository was archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (70 loc) · 1.91 KB
/
server.js
File metadata and controls
76 lines (70 loc) · 1.91 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
import { handleInbox } from '@musakui/kotori/handler'
const cors = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,POST',
'Access-Control-Allow-Headers': 'Content-Type,Host,Date,Digest,Signature',
}
const txt = (content) => [{ text: { content } }]
const addPage = (obj, sender) => fetch('https://api.notion.com/v1/pages', {
method: 'POST',
headers: {
'notion-version': '2022-06-28',
'content-type': 'application/json',
authorization: `Bearer ${process.env.NOTION_TOKEN}`,
},
body: JSON.stringify({
parent: { database_id: process.env.NOTION_DATABASE },
properties: {
title: { title: txt(obj.type) },
id: { url: obj.id },
type: { select: { name: obj.type } },
actor: { url: obj.actor },
sender: { url: sender },
},
children: [
{
object: 'block',
code: {
language: 'json',
caption: txt('payload'),
rich_text: txt(JSON.stringify(obj, null, 2)),
},
},
],
}),
}).then((r) => r.json())
export const inboxHandler = async (evt, ctx) => {
const { httpMethod: method, path, headers, body } = evt
if (method === 'OPTIONS') {
return { statusCode: 204, headers: cors }
} else if (method === 'GET') {
if (!ctx.clientContext.user) {
return { statusCode: 401, body: 'unauthorized' }
}
const { email } = ctx.clientContext.user
const resp = { status: 'ok', email }
return {
statusCode: 200,
headers: {
...cors,
'content-type': 'application/json',
},
body: JSON.stringify(resp),
}
} else if (method !== 'POST') {
return { statusCode: 405, body: 'not allowed' }
}
try {
const req = await handleInbox(method, path, headers, body)
if (req) {
console.log(body)
console.log(await addPage(req.activity, req.sender))
}
} catch (err) {
console.warn(err.message)
console.info(body)
console.info(headers)
return { statusCode: 400, body: `${err.message}` }
}
return { statusCode: 200, headers: cors, body: 'ok' }
}