-
Notifications
You must be signed in to change notification settings - Fork 295
/
utils.ts
124 lines (120 loc) · 3.61 KB
/
utils.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
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
import dotenv from 'dotenv';
import DB_PROPERTIES from '../cols.json';
import {
ItemCategory,
NotionPropTypesEnum,
type NotionRichTextPropType,
type NotionTitlePropType,
type NotionFilesPropType,
type NotionDatePropType,
type NotionMultiSelectPropType,
type NotionNumberPropType,
type NotionUrlPropType,
type NotionColPropTypes,
} from './types';
dotenv.config();
/**
* Retrieves the database ID for the given category.
*
* @param {ItemCategory} category - The category of the item.
* @returns {string} The corresponding database ID.
*/
export function getDBID(category: ItemCategory): string {
const databasesMap = {
[ItemCategory.Movie]: process.env.NOTION_MOVIE_DATABASE_ID,
[ItemCategory.Music]: process.env.NOTION_MUSIC_DATABASE_ID,
[ItemCategory.Book]: process.env.NOTION_BOOK_DATABASE_ID,
[ItemCategory.Game]: process.env.NOTION_GAME_DATABASE_ID,
[ItemCategory.Drama]: process.env.NOTION_DRAMA_DATABASE_ID,
};
return databasesMap[category] as string;
}
/**
* Delays the execution of a function by a specified number of milliseconds.
*
* @param {number} ms - The number of milliseconds to sleep
* @return {Promise<void>} A Promise that resolves after the specified number of milliseconds
*/
export function sleep(ms: number): Promise<void> {
return new Promise((r) => setTimeout(r, ms));
}
/**
* Generates the value for a property based on the given parameters, ready to insert
* into notion database.
*
* @param {any} value - the value to be processed
* @param {NotionPropTypesEnum} type - the type of the Notion database property
* @param {string} key - the key associated with the property
* @return {any} the generated value for the property later will be sent to notion to create an item
*/
export function buildPropertyValue(value: any, type: NotionPropTypesEnum, key: string): NotionColPropTypes | undefined {
switch (type) {
case NotionPropTypesEnum.TITLE:
return {
type: NotionPropTypesEnum.TITLE,
title: [
{
text: {
content: value,
},
},
],
} as NotionTitlePropType;
case NotionPropTypesEnum.FILES:
return {
type: NotionPropTypesEnum.FILES,
files: [
{
name: value,
type: 'external',
external: {
url: value
},
},
],
} as NotionFilesPropType;
case NotionPropTypesEnum.DATE:
return {
type: NotionPropTypesEnum.DATE,
date: {
start: value,
},
} as NotionDatePropType;
case NotionPropTypesEnum.MULTI_SELECT:
return key === DB_PROPERTIES.RATING
? {
type: NotionPropTypesEnum.MULTI_SELECT,
multi_select: value
? [{ name: value.toString() }]
: [],
} as NotionMultiSelectPropType
: {
type: NotionPropTypesEnum.MULTI_SELECT,
multi_select: (value || []).map(g => ({ name: g })),
} as NotionMultiSelectPropType;
case NotionPropTypesEnum.RICH_TEXT:
return {
type: NotionPropTypesEnum.RICH_TEXT,
rich_text: [
{
type: 'text',
text: {
content: value || '',
},
},
],
} as NotionRichTextPropType;
case NotionPropTypesEnum.NUMBER:
return {
type: NotionPropTypesEnum.NUMBER,
number: value ? Number(value) : null,
} as NotionNumberPropType;
case NotionPropTypesEnum.URL:
return {
type: NotionPropTypesEnum.URL,
url: value,
} as NotionUrlPropType;
default:
break;
}
}