potpress-sdk
v0.0.2
Published
Notion APIからTypeScript型定義を自動生成するCLIツールです。
Maintainers
Readme
Notion TypeGen
Notion APIからTypeScript型定義を自動生成するCLIツールです。
機能
- Notion APIからデータベース構造を取得
- TypeScript型定義ファイルを自動生成
- 複数のデータベースに対応
- 出力先パスのカスタマイズ
- インターフェース名のカスタマイズ
インストール
# npm でインストール
npm install -g notion-typegen
# または yarn でインストール
yarn global add notion-typegen使い方
基本的な使用方法
notion-typegen --token=<NOTION_API_TOKEN> --database=<DATABASE_ID> --output=<OUTPUT_PATH>コマンドラインオプション
| オプション | 省略形 | 説明 |
|------------|--------|------|
| --token | -t | Notion API トークン |
| --database | -d | データベースID (複数指定する場合はカンマ区切り) |
| --output | -o | 出力先のパス |
| --name | -n | 生成するインターフェース名 (デフォルト: DatabaseProperty) |
| --filename | -f | 出力ファイル名 (複数データベース指定時に使用) |
| --config | -c | 設定ファイルのパス |
環境変数の使用
.envファイルまたは環境変数を使用して、Notion APIトークンを設定することもできます:
# .envファイル
NOTION_TOKEN=your_notion_api_token設定ファイルの使用
複数のデータベースを処理する場合、設定ファイルを使用すると便利です:
notion-typegen --config=notion-typegen.config.js設定ファイルの例 (notion-typegen.config.js):
module.exports = {
token: "secret_xxxxxxxxxxxxxxxx", // 環境変数を使用する場合は省略可能
databases: [
{
id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
output: "./src/types/blog.ts",
interfaceName: "BlogProperty"
},
{
id: "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
output: "./src/types/tasks.ts",
interfaceName: "TaskProperty"
}
]
};出力例
import { type PropertyType } from "potion-core";
/**
* ブログ記事 データベースのプロパティ型定義
*
* 自動生成されたコード - 編集しないでください
* 生成日時: 2023-11-25T12:34:56.789Z
*/
export type BlogProperty = {
Page: PropertyType<"title">;
Slug: PropertyType<"rich_text">;
Published: PropertyType<"checkbox">;
Date: PropertyType<"date">;
Categories: PropertyType<"multi_select">;
Tags: PropertyType<"multi_select">;
Rank: PropertyType<"number">;
Excerpt: PropertyType<"rich_text">;
FeaturedImage: PropertyType<"files">;
};Notion APIトークンの取得方法
- Notion Developers にアクセス
- 「My integrations」から新しいインテグレーションを作成
- 生成されたAPIトークンをコピー
- 対象のNotionデータベースでインテグレーションと接続 (右上の「...」→「Connections」→作成したインテグレーションを追加)
ライセンス
MIT
