Open4
Notion APIをTypeScriptで利用したい→諦めた

Notion APIを利用したブログサイトを構築中。
APIから取得するデータに形をつけて良い感じにしたい。

Notion APIについて過去に有志でTypeScript用の型ライブラリが作られていた
こちらについてはGithubに記載の通り、公式のSDKでサポートされ始めたため開発が終了している。
そして肝心の公式SDKがこちら。

公式のSDKではExampleが用意されている。
こちらはNotionのDBに変更が入ったときにメールを送るという処理の実装例、TypeScriptで記述されている。
本命のDBに対する操作についてはこちら。残念ながらTypeScriptで記述されたものはなく、JavaScriptでの実装例のみ。

NotionのDBが柔軟すぎるせいか型定義が上手くいかない。
こちらに記載の内容で躓いている。
export const getDatabase = cache(async (): Promise<PageObjectResponse[]> => {
const response: PageObjectResponse[] = await notionClient.databases
.query({
database_id: DATABASE_1,
})
.then((res) => {
let items:PageObjectResponse[] = [];
res.results.forEach((result) => {
if ("properties" in result) {
// PageObjectResponse型のみ追加する
items.push(result as PageObjectResponse);
} else {
throw new Error('getDatabase: result is not PageObjectResponse type\n')
}
});
return items;
});
return response;
});