-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
125 lines (113 loc) · 3.18 KB
/
main.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const https = require('https');
function formatObj(object = {}, i = 0) {
let rs = '';
Object.keys(object).forEach((key) => {
if (typeof object[key] === 'object') {
rs += `${'\t'.repeat(i)}${key}:\n${formatObj(object[key], i + 1)}`;
} else rs += `${'\t'.repeat(i)}[${key}]: "${object[key]}"\n`;
});
return rs;
}
/**
* @param {{
* secret: string,
* database: string,
* }} config
*/
module.exports = (config) => {
/**
* @param {'GET' | 'POST' | 'PATCH'} method
* @param {string} path
* @returns {Promise<string>}
*/
function request(method = 'GET', path, body) {
return new Promise((cb, err) => {
const req = https.request({
method,
hostname: 'api.notion.com',
path: `/v1/${path}`,
headers: {
Authorization: `Bearer ${config.secret}`,
'Content-Type': 'application/json',
'Notion-Version': '2021-05-13',
},
}, (res) => {
let data = '';
res.on('data', (d) => { data += d; });
res.on('close', () => {
cb(JSON.parse(data));
});
});
req.on('error', err);
req.end(JSON.stringify(body));
});
}
const logTypes = {
LOG: 'Log',
INFO: 'Info',
SUCCESS: 'Success',
WARNING: 'Warning',
ERROR: 'Error',
};
/**
* @param {'log' | 'info' | 'success' | 'warning' | 'error' } type
* @param {string[]} tags
* @param {string} title
* @param {any} args
*/
async function log(type = 'log', tags, title = '', ...args) {
const logType = logTypes[type.toUpperCase()] || type;
const pageBlocks = [];
args.forEach((arg) => {
if (!arg) return;
if (typeof arg === 'object') {
formatObj(arg)
.match(/[\s\S]{0,2000}/g)
.filter((m) => m)
.forEach((m) => { pageBlocks.push(m); });
} else pageBlocks.push(arg);
});
const rs = await request('POST', 'pages', {
parent: { database_id: config.database },
properties: {
Type: { select: { name: logType } },
Tags: { multi_select: tags.map((name) => ({ name })) },
Title: { title: [{ text: { content: title } }] },
},
});
if (rs.object === 'error' && rs.message) {
console.error('Notion API error:', rs.message);
const errPage = await request('POST', 'pages', {
parent: { database_id: config.database },
properties: {
Type: { select: { name: 'Error' } },
Title: { title: [{ text: { content: 'NOTION API ERROR !' } }] },
},
});
if (!errPage.id) {
console.error('Database doesn\'t have required properties.');
return;
}
request('PATCH', `blocks/${errPage.id}/children`, {
children: [
{
object: 'block',
type: 'paragraph',
paragraph: { text: [{ type: 'text', text: { content: rs.message } }] },
},
],
});
return;
}
request('PATCH', `blocks/${rs.id}/children`, {
children: [
{
object: 'block',
type: 'paragraph',
paragraph: { text: pageBlocks.map((content) => ({ type: 'text', text: { content } })) },
},
],
});
}
return log;
};