|
| 1 | +import { NextApiRequest, NextApiResponse } from 'next' |
| 2 | + |
| 3 | +import { |
| 4 | + getBlockTitle, |
| 5 | + getBlockIcon, |
| 6 | + getPageProperty, |
| 7 | + isUrl, |
| 8 | + parsePageId |
| 9 | +} from 'notion-utils' |
| 10 | +import { PageBlock } from 'notion-types' |
| 11 | + |
| 12 | +import { notion } from 'lib/notion-api' |
| 13 | +import { mapImageUrl } from 'lib/map-image-url' |
| 14 | +import { NotionPageInfo } from 'lib/types' |
| 15 | +import * as libConfig from 'lib/config' |
| 16 | + |
| 17 | +export default async (req: NextApiRequest, res: NextApiResponse) => { |
| 18 | + if (req.method !== 'POST') { |
| 19 | + return res.status(405).send({ error: 'method not allowed' }) |
| 20 | + } |
| 21 | + |
| 22 | + const pageId: string = parsePageId(req.body.pageId) |
| 23 | + if (!pageId) { |
| 24 | + throw new Error('Invalid notion page id') |
| 25 | + } |
| 26 | + |
| 27 | + const recordMap = await notion.getPage(pageId) |
| 28 | + |
| 29 | + const keys = Object.keys(recordMap?.block || {}) |
| 30 | + const block = recordMap?.block?.[keys[0]]?.value |
| 31 | + |
| 32 | + if (!block) { |
| 33 | + throw new Error('Invalid recordMap for page') |
| 34 | + } |
| 35 | + |
| 36 | + const blockSpaceId = block.space_id |
| 37 | + |
| 38 | + if ( |
| 39 | + blockSpaceId && |
| 40 | + libConfig.rootNotionSpaceId && |
| 41 | + blockSpaceId !== libConfig.rootNotionSpaceId |
| 42 | + ) { |
| 43 | + return res.status(400).send({ |
| 44 | + error: `Notion page "${pageId}" belongs to a different workspace.` |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + const isBlogPost = |
| 49 | + block.type === 'page' && block.parent_table === 'collection' |
| 50 | + const title = getBlockTitle(block, recordMap) || libConfig.name |
| 51 | + const image = mapImageUrl( |
| 52 | + getPageProperty<string>('Social Image', block, recordMap) || |
| 53 | + (block as PageBlock).format?.page_cover || |
| 54 | + libConfig.defaultPageCover, |
| 55 | + block |
| 56 | + ) |
| 57 | + |
| 58 | + const imageCoverPosition = |
| 59 | + (block as PageBlock).format?.page_cover_position ?? |
| 60 | + libConfig.defaultPageCoverPosition |
| 61 | + const imageObjectPosition = imageCoverPosition |
| 62 | + ? `center ${(1 - imageCoverPosition) * 100}%` |
| 63 | + : null |
| 64 | + |
| 65 | + const blockIcon = getBlockIcon(block, recordMap) |
| 66 | + const authorImage = mapImageUrl( |
| 67 | + blockIcon && isUrl(blockIcon) ? blockIcon : libConfig.defaultPageIcon, |
| 68 | + block |
| 69 | + ) |
| 70 | + |
| 71 | + const author = |
| 72 | + getPageProperty<string>('Author', block, recordMap) || libConfig.author |
| 73 | + |
| 74 | + // const socialDescription = |
| 75 | + // getPageProperty<string>('Description', block, recordMap) || |
| 76 | + // libConfig.description |
| 77 | + |
| 78 | + // const lastUpdatedTime = getPageProperty<number>( |
| 79 | + // 'Last Updated', |
| 80 | + // block, |
| 81 | + // recordMap |
| 82 | + // ) |
| 83 | + const publishedTime = getPageProperty<number>('Published', block, recordMap) |
| 84 | + const datePublished = publishedTime ? new Date(publishedTime) : undefined |
| 85 | + // const dateUpdated = lastUpdatedTime |
| 86 | + // ? new Date(lastUpdatedTime) |
| 87 | + // : publishedTime |
| 88 | + // ? new Date(publishedTime) |
| 89 | + // : undefined |
| 90 | + const date = |
| 91 | + isBlogPost && datePublished |
| 92 | + ? `${datePublished.toLocaleString('en-US', { |
| 93 | + month: 'long' |
| 94 | + })} ${datePublished.getFullYear()}` |
| 95 | + : undefined |
| 96 | + const detail = date || author || libConfig.domain |
| 97 | + |
| 98 | + const pageInfo: NotionPageInfo = { |
| 99 | + pageId, |
| 100 | + title, |
| 101 | + image, |
| 102 | + imageObjectPosition, |
| 103 | + author, |
| 104 | + authorImage, |
| 105 | + detail |
| 106 | + } |
| 107 | + |
| 108 | + res.setHeader( |
| 109 | + 'Cache-Control', |
| 110 | + 'public, s-maxage=30, max-age=30, stale-while-revalidate=30' |
| 111 | + ) |
| 112 | + res.status(200).json(pageInfo) |
| 113 | +} |
0 commit comments