@notion-headless-cms/core 0.3.12 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,22 +1,31 @@
1
- import { a as CachedItemMeta, c as ContentResult, i as CachedItemList, l as ImageRef, n as CMSSchemaProperties, o as StorageBinary, r as CachedItemContent, s as ContentBlock, t as BaseContentItem, u as InlineNode } from "./content-CYf6NtCC.mjs";
2
- import { a as CacheAdapter, c as DataSource, d as PropertyDef, f as PropertyMap, i as memoryCache, l as InvalidateKind, o as DocumentCacheOps, p as WebhookConfig, s as ImageCacheOps, t as MemoryCacheOptions, u as InvalidateScope } from "./memory-Cp-dnNGC.mjs";
3
- import { a as Logger, i as definePlugin, n as mergeLoggers, o as CMSHooks, r as CMSPlugin, s as MaybePromise, t as mergeHooks } from "./hooks-D3omfgl4.mjs";
4
- import { BuiltInCMSErrorCode, CMSError, CMSErrorCode, CMSErrorContext, isCMSError, isCMSErrorInNamespace } from "./errors.mjs";
1
+ import { a as CachedItemMeta, c as ContentResult, i as CachedItemList, l as ImageRef, n as CMSSchemaProperties, o as StorageBinary, r as CachedItemContent, s as ContentBlock, t as BaseContentItem, u as InlineNode } from "./content-D7PfY-bV.mjs";
2
+ import { a as CacheAdapter, c as DataSource, d as PropertyDef, f as PropertyMap, i as memoryCache, l as InvalidateKind, o as DocumentCacheOps, p as WebhookConfig, s as ImageCacheOps, t as MemoryCacheOptions, u as InvalidateScope } from "./memory-BKDsuGVN.mjs";
3
+ import { a as isCMSError, i as CMSErrorContext, n as CMSError, o as isCMSErrorInNamespace, r as CMSErrorCode, s as matchCMSError, t as BuiltInCMSErrorCode } from "./errors-CC_x98vG.mjs";
4
+ import { a as Logger, i as definePlugin, n as mergeLoggers, o as CMSHooks, r as CMSPlugin, s as MaybePromise, t as mergeHooks } from "./hooks-C4HRqHLo.mjs";
5
5
 
6
6
  //#region src/types/collection.d.ts
7
+ /**
8
+ * `where` フィルタの型。各フィールドに単一値、または OR 候補の配列を指定できる。
9
+ * 配列を渡すと「いずれかに一致」(in-match) になる。
10
+ */
11
+ type WhereClause<T extends BaseContentItem> = { [K in keyof T]?: T[K] | readonly T[K][] };
7
12
  /** 並び順指定。 */
8
13
  interface SortOption<T extends BaseContentItem = BaseContentItem> {
9
14
  /** ソートするプロパティ名。 */
10
15
  by: keyof T & string;
11
16
  /** 昇順 / 降順。デフォルト "desc"。 */
12
17
  dir?: "asc" | "desc";
18
+ /** カスタム comparator。指定した場合 `by` / `dir` より優先される。 */
19
+ compare?: (a: T, b: T) => number;
13
20
  }
14
21
  /** `list()` のオプション。ページ取得に必要な絞り込み・ソート・ページングを表現する。 */
15
22
  interface ListOptions<T extends BaseContentItem = BaseContentItem> {
16
- /** ステータス絞り込み (`publishedStatuses` を上書き) */
17
- status?: string | readonly string[];
18
- /** プロパティ一致フィルタ (in-memory フィルタ) */
19
- where?: Partial<Record<keyof T, unknown>>;
23
+ /** ステータス絞り込み (`publishedStatuses` を上書き)。単一値または配列で指定。 */
24
+ statuses?: string | readonly string[];
25
+ /** プロパティ一致フィルタ (in-memory フィルタ)。配列は OR 一致。 */
26
+ where?: WhereClause<T>;
27
+ /** 任意ロジックのフィルタ関数。`where` より後に適用される。 */
28
+ filter?: (item: T) => boolean;
20
29
  /** タグ絞り込み (schema に tags: string[] フィールドがある場合)。 */
21
30
  tag?: string;
22
31
  /** ソート。デフォルトは publishedAt の降順。 */
@@ -30,23 +39,19 @@ interface ListOptions<T extends BaseContentItem = BaseContentItem> {
30
39
  interface AdjacencyOptions<T extends BaseContentItem = BaseContentItem> {
31
40
  sort?: SortOption<T>;
32
41
  }
33
- /** `get()` のオプション。 */
34
- interface GetOptions {
42
+ /** `find()` のオプション。 */
43
+ interface FindOptions {
35
44
  /** true なら TTL に関わらずブロッキングで再取得し、本文 cache を破棄する。 */
36
- fresh?: boolean;
45
+ bypassCache?: boolean;
37
46
  }
38
47
  /**
39
- * `get()` の戻り値。アイテム本体に `render()` が生える。
40
- * `render()` を呼んだ時点で初めて本文をロードする lazy 設計。
48
+ * `find()` の戻り値。アイテム本体に `html()` / `markdown()` / `blocks()` が生える。
49
+ * これらを呼んだ時点で初めて本文をロードする lazy 設計。
41
50
  */
42
- type ItemWithRender<T extends BaseContentItem> = T & {
43
- /**
44
- * 本文を文字列で返す。デフォルトは HTML。
45
- * `format: "markdown"` で Markdown を返す。
46
- */
47
- render(opts?: {
48
- format?: "html" | "markdown";
49
- }): Promise<string>;
51
+ type ItemWithContent<T extends BaseContentItem> = T & {
52
+ /** HTML 文字列を返す。 */html(): Promise<string>; /** Markdown 文字列を返す。 */
53
+ markdown(): Promise<string>; /** コンテンツ AST(ContentBlock 配列)を返す。 */
54
+ blocks(): Promise<ContentBlock[]>;
50
55
  };
51
56
  /** `cache.warm()` のオプション。 */
52
57
  interface WarmOptions {
@@ -55,56 +60,59 @@ interface WarmOptions {
55
60
  /** 進捗コールバック。 */
56
61
  onProgress?: (done: number, total: number) => void;
57
62
  }
63
+ /** `cache.warm()` の戻り値。 */
64
+ interface WarmResult {
65
+ ok: number;
66
+ failed: Array<{
67
+ slug: string;
68
+ error: unknown;
69
+ }>;
70
+ }
58
71
  /** コレクションごとのキャッシュ操作 namespace。 */
59
72
  interface CollectionCacheOps<T extends BaseContentItem> {
60
73
  /**
61
- * 指定 slug、または slug 省略時はコレクション全体のキャッシュを失効させる。
74
+ * コレクション全体のキャッシュを失効させる。
62
75
  * 次回 `get` / `list` で source から再取得される。
63
76
  */
64
- invalidate(slug?: string): Promise<void>;
77
+ invalidate(): Promise<void>;
78
+ /**
79
+ * 指定 slug のアイテムキャッシュを失効させる。
80
+ * 次回 `get` で source から再取得される。
81
+ */
82
+ invalidateItem(slug: string): Promise<void>;
65
83
  /**
66
84
  * 全アイテムを並列に事前取得・レンダリングしてキャッシュに格納する。
67
85
  * SSG ビルド前のウォームアップに利用する。
68
86
  */
69
- warm(opts?: WarmOptions): Promise<{
70
- ok: number;
71
- failed: number;
72
- }>;
73
- /** 前後アイテムのナビゲーション (リスト順序ベース)。 */
74
- adjacent(slug: string, opts?: AdjacencyOptions<T>): Promise<{
75
- prev: T | null;
76
- next: T | null;
77
- }>;
87
+ warm(opts?: WarmOptions): Promise<WarmResult>;
78
88
  }
79
89
  /** `check()` の戻り値。差分なしか、差分ありの場合はアイテムを含む。 */
80
90
  type CheckResult<T extends BaseContentItem> = {
81
91
  stale: false;
82
92
  } | {
83
93
  stale: true;
84
- item: ItemWithRender<T>;
94
+ item: ItemWithContent<T>;
85
95
  };
86
96
  /**
87
97
  * コレクション別の CMS クライアント。
88
- * `cms.posts.get(slug)` / `cms.posts.list()` のようにアクセスする。
98
+ * `cms.posts.find(slug)` / `cms.posts.list()` のようにアクセスする。
89
99
  */
90
100
  interface CollectionClient<T extends BaseContentItem = BaseContentItem> {
91
101
  /**
92
- * スラッグで単件取得。アイテム本体に `render()` が生える。
102
+ * スラッグで単件取得。アイテム本体に `html()` / `markdown()` / `blocks()` が生える。
93
103
  *
94
104
  * SWR: TTL 未設定 or 期限内ならキャッシュ即時返却 + バックグラウンド差分チェック。
95
- * TTL 期限切れ、または `opts.fresh === true` でブロッキング取得。
105
+ * TTL 期限切れ、または `opts.bypassCache === true` でブロッキング取得。
96
106
  *
97
107
  * @returns キャッシュまたは source から取得したアイテム。存在しない場合は null。
98
108
  */
99
- get(slug: string, opts?: GetOptions): Promise<ItemWithRender<T> | null>;
109
+ find(slug: string, opts?: FindOptions): Promise<ItemWithContent<T> | null>;
100
110
  /** 公開済みアイテム一覧を取得する。 */
101
111
  list(opts?: ListOptions<T>): Promise<T[]>;
102
- /** SSG パラメータ生成 (Next App Router の `generateStaticParams` 互換)。 */
103
- params(): Promise<{
104
- slug: string;
105
- }[]>;
112
+ /** コレクション内の全スラッグを返す。`generateStaticParams` 等の SSG パラメータ生成に利用する。 */
113
+ params(): Promise<string[]>;
106
114
  /**
107
- * Notion から最新版を取得し、`currentVersion`(`item.updatedAt`)と比較する。
115
+ * Notion から最新版を取得し、`currentVersion`(`item.lastEditedTime`)と比較する。
108
116
  * 差分があればキャッシュを更新してアイテムを返す。
109
117
  * ページ表示後の1回限りのクライアント再検証エンドポイント用。
110
118
  *
@@ -112,6 +120,11 @@ interface CollectionClient<T extends BaseContentItem = BaseContentItem> {
112
120
  * アイテムが存在しない: `null`
113
121
  */
114
122
  check(slug: string, currentVersion: string): Promise<CheckResult<T> | null>;
123
+ /** 前後アイテムのナビゲーション (リスト順序ベース)。 */
124
+ adjacent(slug: string, opts?: AdjacencyOptions<T>): Promise<{
125
+ prev: T | null;
126
+ next: T | null;
127
+ }>;
115
128
  /** キャッシュ操作 namespace。 */
116
129
  cache: CollectionCacheOps<T>;
117
130
  }
@@ -145,6 +158,11 @@ interface ContentConfig {
145
158
  /** 追加する rehype プラグイン。 */
146
159
  rehypePlugins?: RendererPluginList;
147
160
  }
161
+ /** SWR(Stale-While-Revalidate)設定。 */
162
+ interface SWRConfig {
163
+ /** SWR の有効期間 (ミリ秒)。未設定時は TTL なし(失効まで stale を返す)。 */
164
+ ttlMs?: number;
165
+ }
148
166
  /** レートリミット・リトライ設定。 */
149
167
  interface RateLimiterConfig {
150
168
  /** 同時実行数の上限。デフォルト: 3 */
@@ -197,22 +215,27 @@ type InferCollectionItem<C> = C extends CollectionDef<infer T> ? T : BaseContent
197
215
  * publishedStatuses: ["公開済み"],
198
216
  * }
199
217
  * },
200
- * cache: memoryCache({ ttlMs: 5 * 60_000 }),
218
+ * cache: [memoryCache()],
219
+ * swr: { ttlMs: 5 * 60_000 },
201
220
  * });
202
221
  */
203
222
  interface CreateCMSOptions<C extends CollectionsConfig = CollectionsConfig> {
204
223
  /** コレクション定義のマップ。 */
205
224
  collections: C;
206
225
  /**
207
- * キャッシュアダプタ (単体または配列)。未指定時はキャッシュなし。
226
+ * キャッシュアダプタ (配列)。未指定時はキャッシュなし。
208
227
  * - `memoryCache()` のように doc + image 両方を担当するもの
209
228
  * - `r2Cache()` (image のみ)、`kvCache()` (doc のみ) のように片側のみ担当するもの
210
- * - 配列で組み合わせると、各 adapter の `handles` で振り分けられる
229
+ * - 複数 adapter を配列で組み合わせると、各 adapter の `handles` で振り分けられる
230
+ */
231
+ cache?: readonly CacheAdapter[];
232
+ /** SWR(Stale-While-Revalidate)設定。 */
233
+ swr?: SWRConfig;
234
+ /**
235
+ * Markdown→HTML レンダラー。
236
+ * 省略時は `@notion-headless-cms/renderer` の `renderMarkdown` を動的 import で使用する。
237
+ * カスタム実装も `RendererFn` 型を満たせば使用可能。
211
238
  */
212
- cache?: CacheAdapter | readonly CacheAdapter[];
213
- /** SWR の有効期間 (ミリ秒)。未設定時は TTL なし (失効まで stale を返す)。 */
214
- ttlMs?: number;
215
- /** カスタムレンダラー。未指定時は `@notion-headless-cms/renderer` の `renderMarkdown` を動的 import。 */
216
239
  renderer?: RendererFn;
217
240
  /** 画像プロキシのベース URL。デフォルト `/api/images`。 */
218
241
  imageProxyBase?: string;
@@ -266,8 +289,12 @@ interface HandlerOptions {
266
289
  /** `$handler()` が内部で依存する CMS 機能の最小セット。 */
267
290
  interface HandlerAdapter {
268
291
  imageCache: ImageCacheOps;
269
- /** コレクション名で DataSource を取り出し parseWebhook にフォワードする。 */
270
- parseWebhook(req: Request, webhookSecret: string | undefined): Promise<InvalidateScope | null>;
292
+ /**
293
+ * 指定コレクションの DataSource.parseWebhook を呼ぶ。
294
+ * 未知コレクション → `webhook/unknown_collection` CMSError
295
+ * parseWebhook 未実装 → `webhook/not_implemented` CMSError
296
+ */
297
+ parseWebhookFor(collection: string, req: Request, webhookSecret: string | undefined): Promise<InvalidateScope>;
271
298
  revalidate(scope: InvalidateScope): Promise<void>;
272
299
  }
273
300
  /**
@@ -275,24 +302,24 @@ interface HandlerAdapter {
275
302
  * Next.js / React Router / Hono / Cloudflare Workers いずれでも使える。
276
303
  *
277
304
  * ルート:
278
- * - GET `{basePath}/images/:hash` — 画像プロキシ
279
- * - POST `{basePath}/revalidate` — Webhook 受信 + $revalidate()
305
+ * - GET `{basePath}/images/:hash` — 画像プロキシ
306
+ * - POST `{basePath}/revalidate/:collection` — Webhook 受信 + $revalidate()
280
307
  */
281
308
  declare function createHandler(adapter: HandlerAdapter, opts?: HandlerOptions): (req: Request) => Promise<Response>;
282
309
  //#endregion
283
310
  //#region src/cms.d.ts
284
311
  /** `CMSClient<C>` — コレクション別アクセス + グローバル操作の合成型。 */
285
312
  type CMSClient<C extends CollectionsConfig> = { [K in keyof C]: CollectionClient<InferCollectionItem<C[K]>> } & CMSGlobalOps;
286
- /** `CMSClient` のグローバル名前空間。`$` プレフィックス。 */
313
+ /** `CMSClient` のグローバル名前空間。 */
287
314
  interface CMSGlobalOps {
288
315
  /** 登録されているコレクション名の一覧。 */
289
- readonly $collections: readonly string[];
316
+ readonly collections: readonly string[];
290
317
  /** 全コレクションまたは特定スコープのキャッシュを無効化する。 */
291
- $invalidate(scope?: InvalidateScope): Promise<void>;
318
+ invalidate(scope?: InvalidateScope): Promise<void>;
292
319
  /** Web Standard なルーティングハンドラ (画像プロキシ / webhook) を生成する。 */
293
- $handler(opts?: HandlerOptions): (req: Request) => Promise<Response>;
320
+ handler(opts?: HandlerOptions): (req: Request) => Promise<Response>;
294
321
  /** ハッシュキーでキャッシュ画像を取得する。 */
295
- $getCachedImage(hash: string): Promise<StorageBinary | null>;
322
+ getCachedImage(hash: string): Promise<StorageBinary | null>;
296
323
  }
297
324
  /**
298
325
  * 複数の `CollectionDef` を束ねた CMS クライアントを生成する。
@@ -310,7 +337,8 @@ interface CMSGlobalOps {
310
337
  * publishedStatuses: ["公開済み"],
311
338
  * }
312
339
  * },
313
- * cache: memoryCache({ ttlMs: 5 * 60_000 }),
340
+ * cache: [memoryCache()],
341
+ * swr: { ttlMs: 5 * 60_000 },
314
342
  * });
315
343
  */
316
344
  declare function createCMS<C extends CollectionsConfig>(opts: CreateCMSOptions<C>): CMSClient<C>;
@@ -385,31 +413,33 @@ declare class CollectionClientImpl<T extends BaseContentItem> implements Collect
385
413
  private readonly ctx;
386
414
  readonly cache: CollectionCacheOps<T>;
387
415
  constructor(ctx: CollectionContext<T>);
388
- get(slug: string, opts?: GetOptions): Promise<ItemWithRender<T> | null>;
416
+ find(slug: string, opts?: FindOptions): Promise<ItemWithContent<T> | null>;
389
417
  list(opts?: ListOptions<T>): Promise<T[]>;
390
- params(): Promise<{
391
- slug: string;
392
- }[]>;
418
+ params(): Promise<string[]>;
393
419
  check(slug: string, currentVersion: string): Promise<CheckResult<T> | null>;
420
+ adjacent(slug: string, opts?: AdjacencyOptions<T>): Promise<{
421
+ prev: T | null;
422
+ next: T | null;
423
+ }>;
394
424
  private invalidateImpl;
425
+ private invalidateItemImpl;
395
426
  private warmImpl;
396
- private adjacentImpl;
397
427
  private persistMeta;
398
- private invalidateContent;
428
+ private invalidateContentEntry;
399
429
  /**
400
430
  * 本文キャッシュをロードする。キャッシュが無いか、メタとの整合性が取れない場合は
401
431
  * 再生成して書き戻す。
402
432
  */
403
433
  private loadOrBuildContent;
404
- /** メタ既知の状態で本文だけバックグラウンド再生成する。エラーは握りつぶす。 */
434
+ /** メタ既知の状態で本文だけバックグラウンド再生成する。エラーは onSwrError フックに通知する。 */
405
435
  private rebuildContentBg;
406
436
  private attachLazyContent;
407
437
  private fetchList;
408
438
  private checkAndUpdateItemBg;
409
439
  private checkAndUpdateListBg;
410
440
  private fetchListRaw;
411
- private findRaw;
441
+ private fetchRaw;
412
442
  }
413
443
  //#endregion
414
- export { type AdjacencyOptions, type BaseContentItem, type BuiltInCMSErrorCode, type CMSClient, CMSError, type CMSErrorCode, type CMSErrorContext, type CMSGlobalOps, type CMSHooks, type CMSPlugin, type CMSSchemaProperties, type CacheAdapter, type CachedItemContent, type CachedItemList, type CachedItemMeta, type CheckResult, type CollectionCacheOps, type CollectionClient, CollectionClientImpl, type CollectionContext, type CollectionDef, type CollectionsConfig, type ContentBlock, type ContentConfig, type ContentResult, type CreateCMSOptions, DEFAULT_RETRY_CONFIG, type DataSource, type DocumentCacheOps, type GetOptions, type HandlerAdapter, type HandlerOptions, type ImageCacheOps, type ImageRef, type InferCollectionItem, type InlineNode, type InvalidateKind, type InvalidateScope, type ItemWithRender, type ListOptions, type LogLevel, type Logger, type MaybePromise, type MemoryCacheOptions, type PropertyDef, type PropertyMap, type RateLimiterConfig, type RenderOptions, type RendererFn, type RendererPluginList, type RetryConfig, type SortOption, type StorageBinary, type WarmOptions, type WebhookConfig, collectionKey, createCMS, createHandler, definePlugin, isCMSError, isCMSErrorInNamespace, isStale, memoryCache, mergeHooks, mergeLoggers, noopDocOps, noopImgOps, sha256Hex, withRetry };
444
+ export { type AdjacencyOptions, type BaseContentItem, type BuiltInCMSErrorCode, type CMSClient, CMSError, type CMSErrorCode, type CMSErrorContext, type CMSGlobalOps, type CMSHooks, type CMSPlugin, type CMSSchemaProperties, type CacheAdapter, type CachedItemContent, type CachedItemList, type CachedItemMeta, type CheckResult, type CollectionCacheOps, type CollectionClient, CollectionClientImpl, type CollectionContext, type CollectionDef, type CollectionsConfig, type ContentBlock, type ContentConfig, type ContentResult, type CreateCMSOptions, DEFAULT_RETRY_CONFIG, type DataSource, type DocumentCacheOps, type FindOptions, type HandlerAdapter, type HandlerOptions, type ImageCacheOps, type ImageRef, type InferCollectionItem, type InlineNode, type InvalidateKind, type InvalidateScope, type ItemWithContent, type ListOptions, type LogLevel, type Logger, type MaybePromise, type MemoryCacheOptions, type PropertyDef, type PropertyMap, type RateLimiterConfig, type RenderOptions, type RendererFn, type RendererPluginList, type RetryConfig, type SWRConfig, type SortOption, type StorageBinary, type WarmOptions, type WarmResult, type WebhookConfig, collectionKey, createCMS, createHandler, definePlugin, isCMSError, isCMSErrorInNamespace, isStale, matchCMSError, memoryCache, mergeHooks, mergeLoggers, noopDocOps, noopImgOps, sha256Hex, withRetry };
415
445
  //# sourceMappingURL=index.d.mts.map