@notion-headless-cms/core 0.1.3 → 0.2.0
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/README.md +69 -175
- package/dist/cache/memory.d.mts +1 -1
- package/dist/cache/memory.mjs +4 -1
- package/dist/cache/memory.mjs.map +1 -1
- package/dist/cache/noop.d.mts +1 -1
- package/dist/cache/noop.mjs.map +1 -1
- package/dist/cache-Av7HRw_s.d.mts +205 -0
- package/dist/errors.d.mts +1 -1
- package/dist/errors.mjs.map +1 -1
- package/dist/index.d.mts +74 -174
- package/dist/index.mjs +71 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/dist/cache-B-MG4yyg.d.mts +0 -45
package/dist/index.d.mts
CHANGED
|
@@ -1,84 +1,10 @@
|
|
|
1
1
|
import { a as StorageBinary, i as CachedItemList, n as CMSSchemaProperties, r as CachedItem, t as BaseContentItem } from "./content-BrwEY2_p.mjs";
|
|
2
|
-
import { i as InvalidateScope, n as DocumentCacheAdapter, r as ImageCacheAdapter, t as CacheConfig } from "./cache-
|
|
2
|
+
import { a as CollectionConfig, c as InferCollectionItem, d as ContentBlock, f as ContentResult, i as CMSSchema, l as InvalidateScope, m as InlineNode, n as DocumentCacheAdapter, o as DataSource, p as ImageRef, r as ImageCacheAdapter, s as DataSourceFactory, t as CacheConfig, u as WebhookConfig } from "./cache-Av7HRw_s.mjs";
|
|
3
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-DCSAQkST.mjs";
|
|
4
4
|
import { MemoryDocumentCacheOptions, MemoryImageCacheOptions, memoryCache, memoryDocumentCache, memoryImageCache } from "./cache/memory.mjs";
|
|
5
5
|
import { noopDocumentCache, noopImageCache } from "./cache/noop.mjs";
|
|
6
6
|
import { CMSError, CMSErrorCode, CMSErrorContext, isCMSError, isCMSErrorInNamespace } from "./errors.mjs";
|
|
7
7
|
|
|
8
|
-
//#region src/content/blocks.d.ts
|
|
9
|
-
/**
|
|
10
|
-
* CMS 本文の中間表現 (AST)。
|
|
11
|
-
*
|
|
12
|
-
* DataSource が本文をこの配列にノーマライズして返す。
|
|
13
|
-
* レンダラー (`blocksToHtml`) やカスタム React コンポーネントは
|
|
14
|
-
* この型だけを扱えばよい。
|
|
15
|
-
*
|
|
16
|
-
* 対応が難しいブロック (Notion の column / synced block など) は
|
|
17
|
-
* `{ type: "raw", html }` にフォールバックする。
|
|
18
|
-
*/
|
|
19
|
-
type ContentBlock = {
|
|
20
|
-
type: "paragraph";
|
|
21
|
-
children: InlineNode[];
|
|
22
|
-
} | {
|
|
23
|
-
type: "heading";
|
|
24
|
-
level: 1 | 2 | 3;
|
|
25
|
-
children: InlineNode[];
|
|
26
|
-
} | {
|
|
27
|
-
type: "image";
|
|
28
|
-
src: string;
|
|
29
|
-
alt?: string;
|
|
30
|
-
cachedHash?: string;
|
|
31
|
-
} | {
|
|
32
|
-
type: "code";
|
|
33
|
-
lang?: string;
|
|
34
|
-
value: string;
|
|
35
|
-
} | {
|
|
36
|
-
type: "list";
|
|
37
|
-
ordered: boolean;
|
|
38
|
-
items: ContentBlock[][];
|
|
39
|
-
} | {
|
|
40
|
-
type: "quote";
|
|
41
|
-
children: ContentBlock[];
|
|
42
|
-
} | {
|
|
43
|
-
type: "divider";
|
|
44
|
-
} | {
|
|
45
|
-
type: "raw";
|
|
46
|
-
html: string;
|
|
47
|
-
};
|
|
48
|
-
/** paragraph / heading 等の子に並ぶインラインノード。 */
|
|
49
|
-
type InlineNode = {
|
|
50
|
-
type: "text";
|
|
51
|
-
value: string;
|
|
52
|
-
bold?: boolean;
|
|
53
|
-
italic?: boolean;
|
|
54
|
-
code?: boolean;
|
|
55
|
-
} | {
|
|
56
|
-
type: "link";
|
|
57
|
-
url: string;
|
|
58
|
-
children: InlineNode[];
|
|
59
|
-
} | {
|
|
60
|
-
type: "break";
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* `getItem({ include: { content: true } })` で返される本文。
|
|
64
|
-
* blocks は常に同梱。html / markdown は遅延生成。
|
|
65
|
-
*/
|
|
66
|
-
interface ContentResult {
|
|
67
|
-
/** 本文の AST (第一級)。 */
|
|
68
|
-
blocks: ContentBlock[];
|
|
69
|
-
/** 遅延 HTML。renderer が必要な場合のみ呼ぶ。 */
|
|
70
|
-
html(): Promise<string>;
|
|
71
|
-
/** 遅延 Markdown。 */
|
|
72
|
-
markdown(): Promise<string>;
|
|
73
|
-
}
|
|
74
|
-
/** 画像参照 (DataSource.resolveImageUrl に渡す)。 */
|
|
75
|
-
interface ImageRef {
|
|
76
|
-
/** 元の Notion 画像 URL (期限切れの可能性あり)。 */
|
|
77
|
-
originalUrl: string;
|
|
78
|
-
/** 関連するアイテム ID。 */
|
|
79
|
-
itemId?: string;
|
|
80
|
-
}
|
|
81
|
-
//#endregion
|
|
82
8
|
//#region src/types/collection.d.ts
|
|
83
9
|
/** 並び順指定。 */
|
|
84
10
|
interface SortOption<T extends BaseContentItem = BaseContentItem> {
|
|
@@ -144,95 +70,6 @@ interface CollectionClient<T extends BaseContentItem = BaseContentItem> {
|
|
|
144
70
|
}>;
|
|
145
71
|
}
|
|
146
72
|
//#endregion
|
|
147
|
-
//#region src/types/data-source.d.ts
|
|
148
|
-
/**
|
|
149
|
-
* キャッシュ無効化のスコープ (DataSource 層で参照する形)。
|
|
150
|
-
*/
|
|
151
|
-
type InvalidateScope$1 = "all" | {
|
|
152
|
-
collection: string;
|
|
153
|
-
} | {
|
|
154
|
-
collection: string;
|
|
155
|
-
slug: string;
|
|
156
|
-
};
|
|
157
|
-
/**
|
|
158
|
-
* Webhook 受信時の検証設定。
|
|
159
|
-
*/
|
|
160
|
-
interface WebhookConfig {
|
|
161
|
-
/** 署名検証用シークレット。 */
|
|
162
|
-
secret?: string;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* コンテンツソースを抽象化する v1 インターフェース。
|
|
166
|
-
*
|
|
167
|
-
* ユーザーは直接実装しない。`notion-orm` 等の ORM パッケージが実装する。
|
|
168
|
-
* core は Notion 固有の知識を持たず、このインターフェース経由でのみデータを扱う。
|
|
169
|
-
* 将来 `googledocs-orm` 等の別ソースもこの I/F を満たせば差し替え可能。
|
|
170
|
-
*/
|
|
171
|
-
interface DataSource<T extends BaseContentItem = BaseContentItem> {
|
|
172
|
-
/** ソース識別子 (ロギング・デバッグ用)。 */
|
|
173
|
-
readonly name: string;
|
|
174
|
-
/** 公開扱いするステータス値 (ORM 側デフォルト)。 */
|
|
175
|
-
readonly publishedStatuses?: readonly string[];
|
|
176
|
-
/** アクセス許可するステータス値 (ORM 側デフォルト)。 */
|
|
177
|
-
readonly accessibleStatuses?: readonly string[];
|
|
178
|
-
/** 公開済みアイテム一覧を取得する。 */
|
|
179
|
-
list(opts?: {
|
|
180
|
-
publishedStatuses?: readonly string[];
|
|
181
|
-
}): Promise<T[]>;
|
|
182
|
-
/** スラッグで単件取得。見つからなければ null。 */
|
|
183
|
-
findBySlug(slug: string): Promise<T | null>;
|
|
184
|
-
/** アイテム本文を ContentBlock 配列で返す。 */
|
|
185
|
-
loadBlocks(item: T): Promise<ContentBlock[]>;
|
|
186
|
-
/** アイテム本文を Markdown 文字列で返す (html() 生成の元ソース)。 */
|
|
187
|
-
loadMarkdown(item: T): Promise<string>;
|
|
188
|
-
/** SWR 鮮度判定用。item の最終更新タイムスタンプ。 */
|
|
189
|
-
getLastModified(item: T): string;
|
|
190
|
-
/** リスト全体のバージョン文字列 (例: 最新 last_edited_time)。 */
|
|
191
|
-
getListVersion(items: T[]): string;
|
|
192
|
-
/** 期限切れ画像 URL の再取得 (Notion の署名 URL 対応)。 */
|
|
193
|
-
resolveImageUrl?(ref: ImageRef): Promise<string>;
|
|
194
|
-
/**
|
|
195
|
-
* Webhook リクエストをパースして無効化スコープを返す。
|
|
196
|
-
* 実装していない場合は `$handler` が body の `{ slug }` にフォールバック。
|
|
197
|
-
*/
|
|
198
|
-
parseWebhook?(req: Request, config: WebhookConfig): Promise<InvalidateScope$1>;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* `nhcSchema` の各コレクション設定エントリ。
|
|
202
|
-
* ユーザーは CLI 生成の `nhcSchema` を渡すだけで、
|
|
203
|
-
* この型は `createCMS` 内部で DataSource のファクトリに渡される。
|
|
204
|
-
*/
|
|
205
|
-
interface CollectionConfig<T extends BaseContentItem = BaseContentItem> {
|
|
206
|
-
/** Notion データソース (database) ID。 */
|
|
207
|
-
databaseId: string;
|
|
208
|
-
/** スキーマ情報 (ORM が解釈する不透明データ)。 */
|
|
209
|
-
schema?: any;
|
|
210
|
-
/** 公開扱いするステータス値。 */
|
|
211
|
-
publishedStatuses?: string[];
|
|
212
|
-
/** アクセス許可するステータス値。 */
|
|
213
|
-
accessibleStatuses?: string[];
|
|
214
|
-
/** `T` を型レベルで持ち回るためのマーカー (ランタイム値なし)。 */
|
|
215
|
-
__itemType?: T;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* `nhc generate` が生成する `nhcSchema` の型。
|
|
219
|
-
* コレクション名をキーとして、各コレクションの設定を保持する。
|
|
220
|
-
*/
|
|
221
|
-
type NHCSchema = Record<string, CollectionConfig<any>>;
|
|
222
|
-
/** `CollectionConfig<T>` から `T` を抽出するユーティリティ型。 */
|
|
223
|
-
type InferCollectionItem<C> = C extends CollectionConfig<infer T> ? T : BaseContentItem;
|
|
224
|
-
/**
|
|
225
|
-
* DataSource を生成するファクトリ関数の型。
|
|
226
|
-
* `createCMS` はコレクション名 → この関数 → DataSource の経路で組み立てる。
|
|
227
|
-
*
|
|
228
|
-
* ユーザーコードは直接呼ばない。`@notion-headless-cms/notion-orm` 等が provide する。
|
|
229
|
-
*/
|
|
230
|
-
type DataSourceFactory = <T extends BaseContentItem>(args: {
|
|
231
|
-
collection: string;
|
|
232
|
-
config: CollectionConfig<T>;
|
|
233
|
-
notionToken: string;
|
|
234
|
-
}) => DataSource<T>;
|
|
235
|
-
//#endregion
|
|
236
73
|
//#region src/types/config.d.ts
|
|
237
74
|
/**
|
|
238
75
|
* renderer プラグインの不透明型。
|
|
@@ -277,11 +114,38 @@ type DataSourceMap = Record<string, DataSource<any>>;
|
|
|
277
114
|
type InferDataSourceItem<D> = D extends DataSource<infer T> ? T : BaseContentItem;
|
|
278
115
|
/**
|
|
279
116
|
* `createCMS()` に渡すオプション。v1 の正式な入力シグネチャ。
|
|
280
|
-
* ユーザーは `nhc generate` が生成した `
|
|
117
|
+
* ユーザーは `nhc generate` が生成した `cmsDataSources` を渡すだけ。
|
|
281
118
|
*/
|
|
282
119
|
interface CreateCMSOptions<D extends DataSourceMap = DataSourceMap> {
|
|
283
|
-
/** コレクション名 → DataSource のマップ (CLI 生成の `
|
|
120
|
+
/** コレクション名 → DataSource のマップ (CLI 生成の `cmsDataSources`)。 */
|
|
284
121
|
dataSources: D;
|
|
122
|
+
/**
|
|
123
|
+
* ランタイムプリセット。`cache` / `renderer` のデフォルトを自動設定する。
|
|
124
|
+
*
|
|
125
|
+
* - `"node"`: Node.js 向け。`memoryDocumentCache` + `memoryImageCache` を有効化。
|
|
126
|
+
* `ttlMs` と組み合わせて SWR の TTL を設定できる。
|
|
127
|
+
* - `"disabled"`: キャッシュを完全無効化する。
|
|
128
|
+
* - 省略: 従来の動作(`cache` / `renderer` をそのまま使用)。
|
|
129
|
+
* `...nodePreset({ ttlMs })` のスプレッドパターンも引き続き動作する。
|
|
130
|
+
*
|
|
131
|
+
* `cache` を明示的に指定した場合は `preset` より `cache` が優先される。
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* // Before(スプレッドが必要だった)
|
|
135
|
+
* const cms = createCMS({ ...nodePreset({ ttlMs: 5 * 60_000 }), dataSources });
|
|
136
|
+
*
|
|
137
|
+
* // After
|
|
138
|
+
* const cms = createCMS({ dataSources, preset: "node", ttlMs: 5 * 60_000 });
|
|
139
|
+
*/
|
|
140
|
+
preset?: "node" | "disabled";
|
|
141
|
+
/**
|
|
142
|
+
* SWR キャッシュの有効期間(ミリ秒)。`preset` と組み合わせて使用する。
|
|
143
|
+
* `cache` オブジェクトを直接渡す場合は `cache.ttlMs` を使用すること。
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const cms = createCMS({ dataSources, preset: "node", ttlMs: 5 * 60_000 });
|
|
147
|
+
*/
|
|
148
|
+
ttlMs?: number;
|
|
285
149
|
/** レンダラー関数。未指定時は @notion-headless-cms/renderer の renderMarkdown を使用。 */
|
|
286
150
|
renderer?: RendererFn;
|
|
287
151
|
/** キャッシュ設定。未設定時はキャッシュなし。 */
|
|
@@ -327,8 +191,8 @@ interface HandlerOptions {
|
|
|
327
191
|
interface HandlerAdapter {
|
|
328
192
|
imageCache: ImageCacheAdapter;
|
|
329
193
|
/** コレクション名で DataSource を取り出し parseWebhook にフォワードする。 */
|
|
330
|
-
parseWebhook(req: Request, webhookSecret: string | undefined): Promise<InvalidateScope
|
|
331
|
-
revalidate(scope: InvalidateScope
|
|
194
|
+
parseWebhook(req: Request, webhookSecret: string | undefined): Promise<InvalidateScope | null>;
|
|
195
|
+
revalidate(scope: InvalidateScope): Promise<void>;
|
|
332
196
|
}
|
|
333
197
|
/**
|
|
334
198
|
* Web Standard な Request → Response ルーター。
|
|
@@ -348,7 +212,7 @@ interface CMSGlobalOps<D extends DataSourceMap> {
|
|
|
348
212
|
/** 登録されているコレクション名の一覧。 */
|
|
349
213
|
readonly $collections: readonly (keyof D & string)[];
|
|
350
214
|
/** 全コレクションまたは特定コレクションのキャッシュを無効化する。 */
|
|
351
|
-
$revalidate(scope?: InvalidateScope
|
|
215
|
+
$revalidate(scope?: InvalidateScope): Promise<void>;
|
|
352
216
|
/** Web Standard なルーティングハンドラ (画像プロキシ / webhook) を生成する。 */
|
|
353
217
|
$handler(opts?: HandlerOptions): (req: Request) => Promise<Response>;
|
|
354
218
|
/** ハッシュキーでキャッシュ画像を取得する。 */
|
|
@@ -358,13 +222,19 @@ interface CMSGlobalOps<D extends DataSourceMap> {
|
|
|
358
222
|
* 複数の DataSource を束ねた CMS クライアントを生成する。
|
|
359
223
|
*
|
|
360
224
|
* @example
|
|
225
|
+
* // Node.js(preset を使った簡潔な記法)
|
|
226
|
+
* const cms = createCMS({ dataSources: cmsDataSources, preset: "node", ttlMs: 5 * 60_000 });
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* // 従来の spread パターン(引き続き動作する)
|
|
230
|
+
* const cms = createCMS({ ...nodePreset({ ttlMs: 5 * 60_000 }), dataSources: cmsDataSources });
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* // キャッシュを細かく指定する場合
|
|
361
234
|
* const cms = createCMS({
|
|
362
|
-
* dataSources
|
|
363
|
-
* posts: createNotionCollection({ token, databaseId, schema }),
|
|
364
|
-
* },
|
|
235
|
+
* dataSources,
|
|
365
236
|
* cache: { document, image, ttlMs: 60_000 },
|
|
366
237
|
* });
|
|
367
|
-
* const post = await cms.posts.getItem("my-slug");
|
|
368
238
|
*/
|
|
369
239
|
declare function createCMS<D extends DataSourceMap>(opts: CreateCMSOptions<D>): CMSClient<D>;
|
|
370
240
|
//#endregion
|
|
@@ -445,5 +315,35 @@ declare class CollectionClientImpl<T extends BaseContentItem> implements Collect
|
|
|
445
315
|
private findRaw;
|
|
446
316
|
}
|
|
447
317
|
//#endregion
|
|
448
|
-
|
|
318
|
+
//#region src/preset-node.d.ts
|
|
319
|
+
/** `nodePreset()` のオプション。 */
|
|
320
|
+
interface NodePresetOptions {
|
|
321
|
+
/**
|
|
322
|
+
* キャッシュ設定。
|
|
323
|
+
* - 省略時: memoryDocumentCache + memoryImageCache をデフォルト有効化
|
|
324
|
+
* - `"disabled"`: キャッシュを完全無効化
|
|
325
|
+
* - オブジェクト: 任意の cache adapter を差し込む
|
|
326
|
+
*/
|
|
327
|
+
cache?: CacheConfig | "disabled";
|
|
328
|
+
/** SWR の TTL (ミリ秒)。`cache` をオブジェクトで渡した場合はそちらが優先される。 */
|
|
329
|
+
ttlMs?: number;
|
|
330
|
+
/** カスタムレンダラー。未指定時は core が @notion-headless-cms/renderer を動的ロード。 */
|
|
331
|
+
renderer?: RendererFn;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Node.js ランタイム向けの `createCMS` オプションプリセット。
|
|
335
|
+
* メモリキャッシュをデフォルト有効にした `{ cache, renderer }` を返す。
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* import { createCMS, nodePreset } from "@notion-headless-cms/core";
|
|
339
|
+
* import { cmsDataSources } from "./generated/cms-schema";
|
|
340
|
+
*
|
|
341
|
+
* const cms = createCMS({
|
|
342
|
+
* ...nodePreset({ ttlMs: 5 * 60_000 }),
|
|
343
|
+
* dataSources: cmsDataSources,
|
|
344
|
+
* });
|
|
345
|
+
*/
|
|
346
|
+
declare function nodePreset(opts?: NodePresetOptions): Pick<CreateCMSOptions, "cache" | "renderer">;
|
|
347
|
+
//#endregion
|
|
348
|
+
export { type AdjacencyOptions, type BaseContentItem, type CMSClient, CMSError, type CMSErrorCode, type CMSErrorContext, type CMSGlobalOps, type CMSHooks, type CMSPlugin, type CMSSchema, type CMSSchemaProperties, type CacheConfig, type CachedItem, type CachedItemList, type CollectionClient, CollectionClientImpl, type CollectionConfig, type CollectionContext, type ContentBlock, type ContentConfig, type ContentResult, type CreateCMSOptions, DEFAULT_RETRY_CONFIG, type DataSource, type DataSourceFactory, type DataSourceMap, type DocumentCacheAdapter, type GetListOptions, type HandlerAdapter, type HandlerOptions, type ImageCacheAdapter, type ImageRef, type InferCollectionItem, type InferDataSourceItem, type InlineNode, type InvalidateScope, type ItemWithContent, type Logger, type MaybePromise, type MemoryDocumentCacheOptions, type MemoryImageCacheOptions, type NodePresetOptions, type RateLimiterConfig, type RenderOptions, type RendererFn, type RendererPluginList, type RetryConfig, type SortOption, type StorageBinary, type WebhookConfig, collectionKey, createCMS, createHandler, definePlugin, isCMSError, isCMSErrorInNamespace, isStale, memoryCache, memoryDocumentCache, memoryImageCache, mergeHooks, mergeLoggers, nodePreset, noopDocumentCache, noopImageCache, sha256Hex, withRetry };
|
|
449
349
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -450,6 +450,35 @@ function trimTrailingSlash(s) {
|
|
|
450
450
|
return s.endsWith("/") ? s.slice(0, -1) : s;
|
|
451
451
|
}
|
|
452
452
|
//#endregion
|
|
453
|
+
//#region src/preset-node.ts
|
|
454
|
+
/**
|
|
455
|
+
* Node.js ランタイム向けの `createCMS` オプションプリセット。
|
|
456
|
+
* メモリキャッシュをデフォルト有効にした `{ cache, renderer }` を返す。
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* import { createCMS, nodePreset } from "@notion-headless-cms/core";
|
|
460
|
+
* import { cmsDataSources } from "./generated/cms-schema";
|
|
461
|
+
*
|
|
462
|
+
* const cms = createCMS({
|
|
463
|
+
* ...nodePreset({ ttlMs: 5 * 60_000 }),
|
|
464
|
+
* dataSources: cmsDataSources,
|
|
465
|
+
* });
|
|
466
|
+
*/
|
|
467
|
+
function nodePreset(opts = {}) {
|
|
468
|
+
if (opts.cache === "disabled") return {
|
|
469
|
+
cache: void 0,
|
|
470
|
+
renderer: opts.renderer
|
|
471
|
+
};
|
|
472
|
+
return {
|
|
473
|
+
cache: opts.cache ?? {
|
|
474
|
+
document: memoryDocumentCache(),
|
|
475
|
+
image: memoryImageCache(),
|
|
476
|
+
ttlMs: opts.ttlMs
|
|
477
|
+
},
|
|
478
|
+
renderer: opts.renderer
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
//#endregion
|
|
453
482
|
//#region src/cms.ts
|
|
454
483
|
const DEFAULT_IMAGE_PROXY_BASE = "/api/images";
|
|
455
484
|
function resolveDocumentCache(cache) {
|
|
@@ -500,35 +529,62 @@ function scopeDocumentCache(base, collection) {
|
|
|
500
529
|
async invalidate(scope) {
|
|
501
530
|
if (!base.invalidate) return;
|
|
502
531
|
if (scope === "all") return base.invalidate({ collection });
|
|
503
|
-
if ("slug" in scope && !("collection" in scope)) return base.invalidate({
|
|
504
|
-
collection,
|
|
505
|
-
slug: scope.slug
|
|
506
|
-
});
|
|
507
532
|
return base.invalidate(scope);
|
|
508
533
|
}
|
|
509
534
|
};
|
|
510
535
|
}
|
|
511
536
|
/**
|
|
537
|
+
* `preset` オプションを解決して `cache` / `renderer` のデフォルトを補完する内部関数。
|
|
538
|
+
* 明示的な `cache` / `renderer` がある場合はそちらが優先される。
|
|
539
|
+
* `preset` 未指定時は opts をそのまま返す(後方互換)。
|
|
540
|
+
*/
|
|
541
|
+
function resolvePreset(opts) {
|
|
542
|
+
if (opts.preset === "disabled") return {
|
|
543
|
+
...opts,
|
|
544
|
+
cache: void 0
|
|
545
|
+
};
|
|
546
|
+
if (opts.preset === "node") {
|
|
547
|
+
const presetResult = nodePreset({ ttlMs: opts.ttlMs });
|
|
548
|
+
return {
|
|
549
|
+
...opts,
|
|
550
|
+
cache: opts.cache ?? presetResult.cache,
|
|
551
|
+
renderer: opts.renderer ?? presetResult.renderer
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
return opts;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
512
557
|
* 複数の DataSource を束ねた CMS クライアントを生成する。
|
|
513
558
|
*
|
|
514
559
|
* @example
|
|
560
|
+
* // Node.js(preset を使った簡潔な記法)
|
|
561
|
+
* const cms = createCMS({ dataSources: cmsDataSources, preset: "node", ttlMs: 5 * 60_000 });
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* // 従来の spread パターン(引き続き動作する)
|
|
565
|
+
* const cms = createCMS({ ...nodePreset({ ttlMs: 5 * 60_000 }), dataSources: cmsDataSources });
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* // キャッシュを細かく指定する場合
|
|
515
569
|
* const cms = createCMS({
|
|
516
|
-
* dataSources
|
|
517
|
-
* posts: createNotionCollection({ token, databaseId, schema }),
|
|
518
|
-
* },
|
|
570
|
+
* dataSources,
|
|
519
571
|
* cache: { document, image, ttlMs: 60_000 },
|
|
520
572
|
* });
|
|
521
|
-
* const post = await cms.posts.getItem("my-slug");
|
|
522
573
|
*/
|
|
523
574
|
function createCMS(opts) {
|
|
524
|
-
if (!opts.dataSources || Object.keys(opts.dataSources).length === 0) throw new
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
575
|
+
if (!opts.dataSources || Object.keys(opts.dataSources).length === 0) throw new CMSError({
|
|
576
|
+
code: "core/config_invalid",
|
|
577
|
+
message: "createCMS: dataSources に少なくとも1つのコレクションを指定してください。",
|
|
578
|
+
context: { operation: "createCMS" }
|
|
579
|
+
});
|
|
580
|
+
const resolved = resolvePreset(opts);
|
|
581
|
+
const baseDocCache = resolveDocumentCache(resolved.cache);
|
|
582
|
+
const imgCache = resolveImageCache(resolved.cache);
|
|
583
|
+
const hasImageCache = hasImageCacheConfigured(resolved.cache);
|
|
584
|
+
const ttlMs = resolveTtl(resolved.cache);
|
|
529
585
|
const imageProxyBase = opts.content?.imageProxyBase ?? DEFAULT_IMAGE_PROXY_BASE;
|
|
530
586
|
const contentConfig = opts.content;
|
|
531
|
-
const rendererFn =
|
|
587
|
+
const rendererFn = resolved.renderer;
|
|
532
588
|
const waitUntil = opts.waitUntil;
|
|
533
589
|
const logger = mergeLoggers(opts.plugins ?? [], opts.logger);
|
|
534
590
|
const hooks = mergeHooks(opts.plugins ?? [], opts.hooks, logger);
|
|
@@ -611,6 +667,6 @@ function definePlugin(plugin) {
|
|
|
611
667
|
return plugin;
|
|
612
668
|
}
|
|
613
669
|
//#endregion
|
|
614
|
-
export { CMSError, CollectionClientImpl, DEFAULT_RETRY_CONFIG, collectionKey, createCMS, createHandler, definePlugin, isCMSError, isCMSErrorInNamespace, isStale, memoryCache, memoryDocumentCache, memoryImageCache, mergeHooks, mergeLoggers, noopDocumentCache, noopImageCache, sha256Hex, withRetry };
|
|
670
|
+
export { CMSError, CollectionClientImpl, DEFAULT_RETRY_CONFIG, collectionKey, createCMS, createHandler, definePlugin, isCMSError, isCMSErrorInNamespace, isStale, memoryCache, memoryDocumentCache, memoryImageCache, mergeHooks, mergeLoggers, nodePreset, noopDocumentCache, noopImageCache, sha256Hex, withRetry };
|
|
615
671
|
|
|
616
672
|
//# sourceMappingURL=index.mjs.map
|