Repository navigation
Expand file tree
/
Copy pathnotion.ts
More file actions
89 lines (77 loc) · 2.16 KB
/
Copy pathnotion.ts
File metadata and controls
89 lines (77 loc) · 2.16 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
77
78
79
80
81
82
83
84
85
86
87
88
89
import { camelCase, chain, isArray, kebabCase } from 'lodash'
import { json } from 'itty-router'
// @ts-expect-error module export types malformed
import { notionToJson } from 'notion-json-lens'
import {
Client,
collectPaginatedAPI,
isFullPage,
isNotionClientError,
} from '@notionhq/client'
import { GetPagePropertyResponse } from '@notionhq/client/build/src/api-endpoints'
export async function getStreamerIndex(
url: string,
{ INDEX_DB, TTL, NOTION_API_KEY }: Env,
) {
const notion = new Client({ auth: NOTION_API_KEY })
const origPages = await collectPaginatedAPI(notion.databases.query, {
database_id: INDEX_DB,
})
const pages = origPages
.filter(isFullPage)
.map(({ properties, ...rest }) => ({
props: chain(notionToJson(properties) as Record<string, any>)
.mapKeys((_v, k) =>
k === 'CashApp' || k === 'PayPal' ? k.toLowerCase() : camelCase(k),
)
.omitBy((v) => v == null || v === '' || (isArray(v) && v.length === 0))
.value(),
...rest,
}))
.filter(({ props }) => props.name && props.publish)
for (const { id, props } of pages) {
props.slug = kebabCase((props.slug ?? props.name).toLowerCase())
const photoURL = props.photo?.[0]?.file.url
if (photoURL) {
const { origin } = new URL(url)
const { pathname: photoPath } = new URL(photoURL)
props.photo = `${origin}/streamers/${id}${photoPath}`
} else {
props.photo = null
}
}
return json(
pages.map(({ props }) => props),
{
headers: {
'Cache-Control': `max-age=${TTL}`,
},
},
)
}
export async function getStreamerPhoto(
pageId: string,
{ NOTION_API_KEY }: Env,
) {
const notion = new Client({ auth: NOTION_API_KEY })
let photo: GetPagePropertyResponse
try {
photo = await notion.pages.properties.retrieve({
page_id: pageId,
property_id: 'Photo',
})
} catch (err) {
if (isNotionClientError(err) && err.code === 'object_not_found') {
return null
}
throw err
}
if (
photo.type !== 'files' ||
!photo.files.length ||
photo.files[0].type !== 'file'
) {
return null
}
return photo.files[0].file.url
}