@dispatchcms/next 0.0.2 → 0.0.4
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/.turbo/turbo-build.log +10 -10
- package/README.md +41 -3
- package/dist/index.d.mts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +13 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -33
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +16 -34
- package/src/index.ts +7 -1
- package/test/client.cache.test.ts +25 -28
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
> @dispatchcms/next@0.0.
|
|
3
|
+
> @dispatchcms/next@0.0.4 build /Users/jamescalmus/Documents/dispatch/packages/next
|
|
4
4
|
> tsup
|
|
5
5
|
|
|
6
6
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
[34mCLI[39m Cleaning output folder
|
|
12
12
|
[34mCJS[39m Build start
|
|
13
13
|
[34mESM[39m Build start
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
18
|
-
[
|
|
19
|
-
[
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m2.04 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m4.20 KB[39m
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 7ms
|
|
17
|
+
[32mCJS[39m [1mdist/index.js [22m[32m3.18 KB[39m
|
|
18
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m4.45 KB[39m
|
|
19
|
+
[32mCJS[39m ⚡️ Build success in 7ms
|
|
20
20
|
DTS Build start
|
|
21
|
-
DTS ⚡️ Build success in
|
|
22
|
-
DTS dist/index.d.ts
|
|
23
|
-
DTS dist/index.d.mts
|
|
21
|
+
DTS ⚡️ Build success in 329ms
|
|
22
|
+
DTS dist/index.d.ts 938.00 B
|
|
23
|
+
DTS dist/index.d.mts 938.00 B
|
package/README.md
CHANGED
|
@@ -36,24 +36,31 @@ You can omit `initDispatch` entirely if `NEXT_PUBLIC_DISPATCH_SITE_KEY` is set;
|
|
|
36
36
|
|
|
37
37
|
## Caching
|
|
38
38
|
|
|
39
|
-
The
|
|
39
|
+
The package does **not** cache responses in memory. Each call to `getPosts()` or `getPost(slug)` fetches from the API, so your app always sees up-to-date data (e.g. when you unpublish a post in the CMS it disappears on the next load). For performance, rely on Next.js: use Server Components and the default `fetch` caching, or `revalidate` / ISR, so that responses are cached at the request level and stay fresh according to your revalidation settings.
|
|
40
40
|
|
|
41
41
|
## API
|
|
42
42
|
|
|
43
43
|
### `getPosts(siteKey?)`
|
|
44
44
|
|
|
45
|
-
Returns all published posts for the site.
|
|
45
|
+
Returns all published posts for the site. Always fetches from the API (no in-memory cache).
|
|
46
46
|
|
|
47
47
|
- **Returns:** `Promise<Post[]>`
|
|
48
48
|
- **Optional:** pass `siteKey` to override the configured site for this call.
|
|
49
49
|
|
|
50
50
|
### `getPost(slug, siteKey?)`
|
|
51
51
|
|
|
52
|
-
Returns a single published post by slug, or `null` if not found.
|
|
52
|
+
Returns a single published post by slug, or `null` if not found. Always fetches from the API (no in-memory cache).
|
|
53
53
|
|
|
54
54
|
- **Returns:** `Promise<Post | null>`
|
|
55
55
|
- **Optional:** pass `siteKey` as the second argument to override the configured site.
|
|
56
56
|
|
|
57
|
+
### `getPostByPreviewToken(token)`
|
|
58
|
+
|
|
59
|
+
Returns a single post by its preview token (draft or published). Use this in your app’s **preview** route so editors can open a shareable link and see the post as it will appear. No site key is required; the token is the secret.
|
|
60
|
+
|
|
61
|
+
- **Returns:** `Promise<Post | null>`
|
|
62
|
+
- **Example:** Implement a route at `/preview` (or `/blog/preview`) that reads `token` from the query and renders the post with the same layout as your live post page (see below).
|
|
63
|
+
|
|
57
64
|
## Types
|
|
58
65
|
|
|
59
66
|
Exportable types:
|
|
@@ -106,3 +113,34 @@ export default async function PostPage({ params }: { params: { slug: string } })
|
|
|
106
113
|
```
|
|
107
114
|
|
|
108
115
|
The CMS API only returns **published** posts; drafts are not included.
|
|
116
|
+
|
|
117
|
+
## Preview route
|
|
118
|
+
|
|
119
|
+
To support public preview links from the CMS (e.g. `https://yoursite.com/preview?token=xxx`), add a preview page that fetches by token and renders the post:
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
// app/preview/page.tsx
|
|
123
|
+
import { getPostByPreviewToken } from "@dispatchcms/next";
|
|
124
|
+
import { notFound } from "next/navigation";
|
|
125
|
+
|
|
126
|
+
export default async function PreviewPage({
|
|
127
|
+
searchParams,
|
|
128
|
+
}: {
|
|
129
|
+
searchParams: Promise<{ token?: string }>;
|
|
130
|
+
}) {
|
|
131
|
+
const { token } = await searchParams;
|
|
132
|
+
if (!token) notFound();
|
|
133
|
+
const post = await getPostByPreviewToken(token);
|
|
134
|
+
if (!post) notFound();
|
|
135
|
+
return (
|
|
136
|
+
<article>
|
|
137
|
+
<p className="text-sm text-muted-foreground">Preview</p>
|
|
138
|
+
<h1>{post.title}</h1>
|
|
139
|
+
{post.excerpt && <p>{post.excerpt}</p>}
|
|
140
|
+
{/* Render post.content the same way as your live post page */}
|
|
141
|
+
</article>
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
In the CMS, set **Site URL** in Site settings to your site’s base URL (e.g. `https://yoursite.com`). The preview link will use that URL plus `/preview?token=...`.
|
package/dist/index.d.mts
CHANGED
|
@@ -20,5 +20,10 @@ declare function getConfig(): {
|
|
|
20
20
|
};
|
|
21
21
|
declare function getPosts(siteKey?: string): Promise<Post[]>;
|
|
22
22
|
declare function getPost(slug: string, siteKey?: string): Promise<Post | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Fetch a single post by its preview token (for preview/draft pages).
|
|
25
|
+
* No site key required. Use this in your app's preview route (e.g. /preview?token=...).
|
|
26
|
+
*/
|
|
27
|
+
declare function getPostByPreviewToken(token: string): Promise<Post | null>;
|
|
23
28
|
|
|
24
|
-
export { type DispatchConfig, type Post, getConfig, getPost, getPosts, initDispatch };
|
|
29
|
+
export { type DispatchConfig, type Post, getConfig, getPost, getPostByPreviewToken, getPosts, initDispatch };
|
package/dist/index.d.ts
CHANGED
|
@@ -20,5 +20,10 @@ declare function getConfig(): {
|
|
|
20
20
|
};
|
|
21
21
|
declare function getPosts(siteKey?: string): Promise<Post[]>;
|
|
22
22
|
declare function getPost(slug: string, siteKey?: string): Promise<Post | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Fetch a single post by its preview token (for preview/draft pages).
|
|
25
|
+
* No site key required. Use this in your app's preview route (e.g. /preview?token=...).
|
|
26
|
+
*/
|
|
27
|
+
declare function getPostByPreviewToken(token: string): Promise<Post | null>;
|
|
23
28
|
|
|
24
|
-
export { type DispatchConfig, type Post, getConfig, getPost, getPosts, initDispatch };
|
|
29
|
+
export { type DispatchConfig, type Post, getConfig, getPost, getPostByPreviewToken, getPosts, initDispatch };
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
getConfig: () => getConfig,
|
|
24
24
|
getPost: () => getPost,
|
|
25
|
+
getPostByPreviewToken: () => getPostByPreviewToken,
|
|
25
26
|
getPosts: () => getPosts,
|
|
26
27
|
initDispatch: () => initDispatch
|
|
27
28
|
});
|
|
@@ -30,9 +31,6 @@ module.exports = __toCommonJS(index_exports);
|
|
|
30
31
|
// src/client.ts
|
|
31
32
|
var DISPATCH_API_BASE = "https://dispatch-cms.vercel.app";
|
|
32
33
|
var config = null;
|
|
33
|
-
var allPostsBySiteKey = /* @__PURE__ */ new Map();
|
|
34
|
-
var postBySlugBySiteKey = /* @__PURE__ */ new Map();
|
|
35
|
-
var fullListFetchedForSiteKey = /* @__PURE__ */ new Set();
|
|
36
34
|
function initDispatch(options) {
|
|
37
35
|
config = { siteKey: options.siteKey };
|
|
38
36
|
}
|
|
@@ -47,10 +45,6 @@ function getConfig() {
|
|
|
47
45
|
async function getPosts(siteKey) {
|
|
48
46
|
const { siteKey: key } = getConfig();
|
|
49
47
|
const resolvedKey = siteKey ?? key;
|
|
50
|
-
const cached = allPostsBySiteKey.get(resolvedKey);
|
|
51
|
-
if (cached !== void 0) {
|
|
52
|
-
return cached;
|
|
53
|
-
}
|
|
54
48
|
const url = `${DISPATCH_API_BASE}/api/posts`;
|
|
55
49
|
const res = await fetch(url, {
|
|
56
50
|
headers: { "X-Site-Key": resolvedKey }
|
|
@@ -61,31 +55,11 @@ async function getPosts(siteKey) {
|
|
|
61
55
|
}
|
|
62
56
|
throw new Error(`Failed to fetch posts: ${res.status} ${res.statusText}`);
|
|
63
57
|
}
|
|
64
|
-
|
|
65
|
-
allPostsBySiteKey.set(resolvedKey, posts);
|
|
66
|
-
fullListFetchedForSiteKey.add(resolvedKey);
|
|
67
|
-
const bySlug = /* @__PURE__ */ new Map();
|
|
68
|
-
for (const post of posts) {
|
|
69
|
-
bySlug.set(post.slug, post);
|
|
70
|
-
}
|
|
71
|
-
postBySlugBySiteKey.set(resolvedKey, bySlug);
|
|
72
|
-
return posts;
|
|
58
|
+
return await res.json();
|
|
73
59
|
}
|
|
74
60
|
async function getPost(slug, siteKey) {
|
|
75
61
|
const { siteKey: key } = getConfig();
|
|
76
62
|
const resolvedKey = siteKey ?? key;
|
|
77
|
-
if (fullListFetchedForSiteKey.has(resolvedKey)) {
|
|
78
|
-
const list = allPostsBySiteKey.get(resolvedKey);
|
|
79
|
-
if (list) {
|
|
80
|
-
const post2 = list.find((p) => p.slug === slug);
|
|
81
|
-
return post2 ?? null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const bySlug = postBySlugBySiteKey.get(resolvedKey);
|
|
85
|
-
const cachedPost = bySlug?.get(slug);
|
|
86
|
-
if (cachedPost !== void 0) {
|
|
87
|
-
return cachedPost;
|
|
88
|
-
}
|
|
89
63
|
const url = `${DISPATCH_API_BASE}/api/posts/${encodeURIComponent(slug)}`;
|
|
90
64
|
const res = await fetch(url, {
|
|
91
65
|
headers: { "X-Site-Key": resolvedKey }
|
|
@@ -99,17 +73,23 @@ async function getPost(slug, siteKey) {
|
|
|
99
73
|
}
|
|
100
74
|
throw new Error(`Failed to fetch post: ${res.status} ${res.statusText}`);
|
|
101
75
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
return await res.json();
|
|
77
|
+
}
|
|
78
|
+
async function getPostByPreviewToken(token) {
|
|
79
|
+
if (!token?.trim()) return null;
|
|
80
|
+
const url = `${DISPATCH_API_BASE}/api/preview?token=${encodeURIComponent(token.trim())}`;
|
|
81
|
+
const res = await fetch(url);
|
|
82
|
+
if (res.status === 404) return null;
|
|
83
|
+
if (!res.ok) {
|
|
84
|
+
throw new Error(`Failed to fetch preview: ${res.status} ${res.statusText}`);
|
|
105
85
|
}
|
|
106
|
-
|
|
107
|
-
return post;
|
|
86
|
+
return await res.json();
|
|
108
87
|
}
|
|
109
88
|
// Annotate the CommonJS export names for ESM import in node:
|
|
110
89
|
0 && (module.exports = {
|
|
111
90
|
getConfig,
|
|
112
91
|
getPost,
|
|
92
|
+
getPostByPreviewToken,
|
|
113
93
|
getPosts,
|
|
114
94
|
initDispatch
|
|
115
95
|
});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/client.ts"],"sourcesContent":["export type { Post, DispatchConfig } from \"./client\";\nexport {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/client.ts"],"sourcesContent":["export type { Post, DispatchConfig } from \"./client\";\nexport {\n initDispatch,\n getConfig,\n getPosts,\n getPost,\n getPostByPreviewToken,\n} from \"./client\";\n","export type Post = {\n id: string;\n created_at: string;\n published_at: string;\n updated_at: string;\n site_id: string;\n title: string;\n slug: string;\n content: unknown;\n excerpt: string | null;\n featured_image: string | null;\n published: boolean;\n};\n\nconst DISPATCH_API_BASE = \"https://dispatch-cms.vercel.app\";\n\nexport type DispatchConfig = {\n siteKey: string;\n};\n\nlet config: DispatchConfig | null = null;\n\nexport function initDispatch(options: DispatchConfig): void {\n config = { siteKey: options.siteKey };\n}\n\nexport function getConfig(): { siteKey: string } {\n const envKey = typeof process !== \"undefined\" ? process.env?.NEXT_PUBLIC_DISPATCH_SITE_KEY : undefined;\n const siteKey = config?.siteKey ?? (typeof envKey === \"string\" ? envKey : undefined);\n if (!siteKey || typeof siteKey !== \"string\") {\n throw new Error(\"Dispatch site key is required. Call initDispatch({ siteKey }) or set NEXT_PUBLIC_DISPATCH_SITE_KEY.\");\n }\n return { siteKey };\n}\n\nexport async function getPosts(siteKey?: string): Promise<Post[]> {\n const { siteKey: key } = getConfig();\n const resolvedKey = siteKey ?? key;\n const url = `${DISPATCH_API_BASE}/api/posts`;\n const res = await fetch(url, {\n headers: { \"X-Site-Key\": resolvedKey },\n });\n if (!res.ok) {\n if (res.status === 401) {\n throw new Error(\"Invalid or missing site key\");\n }\n throw new Error(`Failed to fetch posts: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post[];\n}\n\nexport async function getPost(slug: string, siteKey?: string): Promise<Post | null> {\n const { siteKey: key } = getConfig();\n const resolvedKey = siteKey ?? key;\n const url = `${DISPATCH_API_BASE}/api/posts/${encodeURIComponent(slug)}`;\n const res = await fetch(url, {\n headers: { \"X-Site-Key\": resolvedKey },\n });\n if (res.status === 404) {\n return null;\n }\n if (!res.ok) {\n if (res.status === 401) {\n throw new Error(\"Invalid or missing site key\");\n }\n throw new Error(`Failed to fetch post: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post;\n}\n\n/**\n * Fetch a single post by its preview token (for preview/draft pages).\n * No site key required. Use this in your app's preview route (e.g. /preview?token=...).\n */\nexport async function getPostByPreviewToken(token: string): Promise<Post | null> {\n if (!token?.trim()) return null;\n const url = `${DISPATCH_API_BASE}/api/preview?token=${encodeURIComponent(token.trim())}`;\n const res = await fetch(url);\n if (res.status === 404) return null;\n if (!res.ok) {\n throw new Error(`Failed to fetch preview: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcA,IAAM,oBAAoB;AAM1B,IAAI,SAAgC;AAE7B,SAAS,aAAa,SAA+B;AAC1D,WAAS,EAAE,SAAS,QAAQ,QAAQ;AACtC;AAEO,SAAS,YAAiC;AAC/C,QAAM,SAAS,OAAO,YAAY,cAAc,QAAQ,KAAK,gCAAgC;AAC7F,QAAM,UAAU,QAAQ,YAAY,OAAO,WAAW,WAAW,SAAS;AAC1E,MAAI,CAAC,WAAW,OAAO,YAAY,UAAU;AAC3C,UAAM,IAAI,MAAM,qGAAqG;AAAA,EACvH;AACA,SAAO,EAAE,QAAQ;AACnB;AAEA,eAAsB,SAAS,SAAmC;AAChE,QAAM,EAAE,SAAS,IAAI,IAAI,UAAU;AACnC,QAAM,cAAc,WAAW;AAC/B,QAAM,MAAM,GAAG,iBAAiB;AAChC,QAAM,MAAM,MAAM,MAAM,KAAK;AAAA,IAC3B,SAAS,EAAE,cAAc,YAAY;AAAA,EACvC,CAAC;AACD,MAAI,CAAC,IAAI,IAAI;AACX,QAAI,IAAI,WAAW,KAAK;AACtB,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,UAAM,IAAI,MAAM,0BAA0B,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EAC1E;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;AAEA,eAAsB,QAAQ,MAAc,SAAwC;AAClF,QAAM,EAAE,SAAS,IAAI,IAAI,UAAU;AACnC,QAAM,cAAc,WAAW;AAC/B,QAAM,MAAM,GAAG,iBAAiB,cAAc,mBAAmB,IAAI,CAAC;AACtE,QAAM,MAAM,MAAM,MAAM,KAAK;AAAA,IAC3B,SAAS,EAAE,cAAc,YAAY;AAAA,EACvC,CAAC;AACD,MAAI,IAAI,WAAW,KAAK;AACtB,WAAO;AAAA,EACT;AACA,MAAI,CAAC,IAAI,IAAI;AACX,QAAI,IAAI,WAAW,KAAK;AACtB,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,UAAM,IAAI,MAAM,yBAAyB,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EACzE;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;AAMA,eAAsB,sBAAsB,OAAqC;AAC/E,MAAI,CAAC,OAAO,KAAK,EAAG,QAAO;AAC3B,QAAM,MAAM,GAAG,iBAAiB,sBAAsB,mBAAmB,MAAM,KAAK,CAAC,CAAC;AACtF,QAAM,MAAM,MAAM,MAAM,GAAG;AAC3B,MAAI,IAAI,WAAW,IAAK,QAAO;AAC/B,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,IAAI,MAAM,4BAA4B,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EAC5E;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
// src/client.ts
|
|
2
2
|
var DISPATCH_API_BASE = "https://dispatch-cms.vercel.app";
|
|
3
3
|
var config = null;
|
|
4
|
-
var allPostsBySiteKey = /* @__PURE__ */ new Map();
|
|
5
|
-
var postBySlugBySiteKey = /* @__PURE__ */ new Map();
|
|
6
|
-
var fullListFetchedForSiteKey = /* @__PURE__ */ new Set();
|
|
7
4
|
function initDispatch(options) {
|
|
8
5
|
config = { siteKey: options.siteKey };
|
|
9
6
|
}
|
|
@@ -18,10 +15,6 @@ function getConfig() {
|
|
|
18
15
|
async function getPosts(siteKey) {
|
|
19
16
|
const { siteKey: key } = getConfig();
|
|
20
17
|
const resolvedKey = siteKey ?? key;
|
|
21
|
-
const cached = allPostsBySiteKey.get(resolvedKey);
|
|
22
|
-
if (cached !== void 0) {
|
|
23
|
-
return cached;
|
|
24
|
-
}
|
|
25
18
|
const url = `${DISPATCH_API_BASE}/api/posts`;
|
|
26
19
|
const res = await fetch(url, {
|
|
27
20
|
headers: { "X-Site-Key": resolvedKey }
|
|
@@ -32,31 +25,11 @@ async function getPosts(siteKey) {
|
|
|
32
25
|
}
|
|
33
26
|
throw new Error(`Failed to fetch posts: ${res.status} ${res.statusText}`);
|
|
34
27
|
}
|
|
35
|
-
|
|
36
|
-
allPostsBySiteKey.set(resolvedKey, posts);
|
|
37
|
-
fullListFetchedForSiteKey.add(resolvedKey);
|
|
38
|
-
const bySlug = /* @__PURE__ */ new Map();
|
|
39
|
-
for (const post of posts) {
|
|
40
|
-
bySlug.set(post.slug, post);
|
|
41
|
-
}
|
|
42
|
-
postBySlugBySiteKey.set(resolvedKey, bySlug);
|
|
43
|
-
return posts;
|
|
28
|
+
return await res.json();
|
|
44
29
|
}
|
|
45
30
|
async function getPost(slug, siteKey) {
|
|
46
31
|
const { siteKey: key } = getConfig();
|
|
47
32
|
const resolvedKey = siteKey ?? key;
|
|
48
|
-
if (fullListFetchedForSiteKey.has(resolvedKey)) {
|
|
49
|
-
const list = allPostsBySiteKey.get(resolvedKey);
|
|
50
|
-
if (list) {
|
|
51
|
-
const post2 = list.find((p) => p.slug === slug);
|
|
52
|
-
return post2 ?? null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const bySlug = postBySlugBySiteKey.get(resolvedKey);
|
|
56
|
-
const cachedPost = bySlug?.get(slug);
|
|
57
|
-
if (cachedPost !== void 0) {
|
|
58
|
-
return cachedPost;
|
|
59
|
-
}
|
|
60
33
|
const url = `${DISPATCH_API_BASE}/api/posts/${encodeURIComponent(slug)}`;
|
|
61
34
|
const res = await fetch(url, {
|
|
62
35
|
headers: { "X-Site-Key": resolvedKey }
|
|
@@ -70,16 +43,22 @@ async function getPost(slug, siteKey) {
|
|
|
70
43
|
}
|
|
71
44
|
throw new Error(`Failed to fetch post: ${res.status} ${res.statusText}`);
|
|
72
45
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
46
|
+
return await res.json();
|
|
47
|
+
}
|
|
48
|
+
async function getPostByPreviewToken(token) {
|
|
49
|
+
if (!token?.trim()) return null;
|
|
50
|
+
const url = `${DISPATCH_API_BASE}/api/preview?token=${encodeURIComponent(token.trim())}`;
|
|
51
|
+
const res = await fetch(url);
|
|
52
|
+
if (res.status === 404) return null;
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
throw new Error(`Failed to fetch preview: ${res.status} ${res.statusText}`);
|
|
76
55
|
}
|
|
77
|
-
|
|
78
|
-
return post;
|
|
56
|
+
return await res.json();
|
|
79
57
|
}
|
|
80
58
|
export {
|
|
81
59
|
getConfig,
|
|
82
60
|
getPost,
|
|
61
|
+
getPostByPreviewToken,
|
|
83
62
|
getPosts,
|
|
84
63
|
initDispatch
|
|
85
64
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["export type Post = {\n id: string;\n created_at: string;\n published_at: string;\n updated_at: string;\n site_id: string;\n title: string;\n slug: string;\n content: unknown;\n excerpt: string | null;\n featured_image: string | null;\n published: boolean;\n};\n\nconst DISPATCH_API_BASE = \"https://dispatch-cms.vercel.app\";\n\nexport type DispatchConfig = {\n siteKey: string;\n};\n\nlet config: DispatchConfig | null = null;\n\
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["export type Post = {\n id: string;\n created_at: string;\n published_at: string;\n updated_at: string;\n site_id: string;\n title: string;\n slug: string;\n content: unknown;\n excerpt: string | null;\n featured_image: string | null;\n published: boolean;\n};\n\nconst DISPATCH_API_BASE = \"https://dispatch-cms.vercel.app\";\n\nexport type DispatchConfig = {\n siteKey: string;\n};\n\nlet config: DispatchConfig | null = null;\n\nexport function initDispatch(options: DispatchConfig): void {\n config = { siteKey: options.siteKey };\n}\n\nexport function getConfig(): { siteKey: string } {\n const envKey = typeof process !== \"undefined\" ? process.env?.NEXT_PUBLIC_DISPATCH_SITE_KEY : undefined;\n const siteKey = config?.siteKey ?? (typeof envKey === \"string\" ? envKey : undefined);\n if (!siteKey || typeof siteKey !== \"string\") {\n throw new Error(\"Dispatch site key is required. Call initDispatch({ siteKey }) or set NEXT_PUBLIC_DISPATCH_SITE_KEY.\");\n }\n return { siteKey };\n}\n\nexport async function getPosts(siteKey?: string): Promise<Post[]> {\n const { siteKey: key } = getConfig();\n const resolvedKey = siteKey ?? key;\n const url = `${DISPATCH_API_BASE}/api/posts`;\n const res = await fetch(url, {\n headers: { \"X-Site-Key\": resolvedKey },\n });\n if (!res.ok) {\n if (res.status === 401) {\n throw new Error(\"Invalid or missing site key\");\n }\n throw new Error(`Failed to fetch posts: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post[];\n}\n\nexport async function getPost(slug: string, siteKey?: string): Promise<Post | null> {\n const { siteKey: key } = getConfig();\n const resolvedKey = siteKey ?? key;\n const url = `${DISPATCH_API_BASE}/api/posts/${encodeURIComponent(slug)}`;\n const res = await fetch(url, {\n headers: { \"X-Site-Key\": resolvedKey },\n });\n if (res.status === 404) {\n return null;\n }\n if (!res.ok) {\n if (res.status === 401) {\n throw new Error(\"Invalid or missing site key\");\n }\n throw new Error(`Failed to fetch post: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post;\n}\n\n/**\n * Fetch a single post by its preview token (for preview/draft pages).\n * No site key required. Use this in your app's preview route (e.g. /preview?token=...).\n */\nexport async function getPostByPreviewToken(token: string): Promise<Post | null> {\n if (!token?.trim()) return null;\n const url = `${DISPATCH_API_BASE}/api/preview?token=${encodeURIComponent(token.trim())}`;\n const res = await fetch(url);\n if (res.status === 404) return null;\n if (!res.ok) {\n throw new Error(`Failed to fetch preview: ${res.status} ${res.statusText}`);\n }\n return (await res.json()) as Post;\n}\n"],"mappings":";AAcA,IAAM,oBAAoB;AAM1B,IAAI,SAAgC;AAE7B,SAAS,aAAa,SAA+B;AAC1D,WAAS,EAAE,SAAS,QAAQ,QAAQ;AACtC;AAEO,SAAS,YAAiC;AAC/C,QAAM,SAAS,OAAO,YAAY,cAAc,QAAQ,KAAK,gCAAgC;AAC7F,QAAM,UAAU,QAAQ,YAAY,OAAO,WAAW,WAAW,SAAS;AAC1E,MAAI,CAAC,WAAW,OAAO,YAAY,UAAU;AAC3C,UAAM,IAAI,MAAM,qGAAqG;AAAA,EACvH;AACA,SAAO,EAAE,QAAQ;AACnB;AAEA,eAAsB,SAAS,SAAmC;AAChE,QAAM,EAAE,SAAS,IAAI,IAAI,UAAU;AACnC,QAAM,cAAc,WAAW;AAC/B,QAAM,MAAM,GAAG,iBAAiB;AAChC,QAAM,MAAM,MAAM,MAAM,KAAK;AAAA,IAC3B,SAAS,EAAE,cAAc,YAAY;AAAA,EACvC,CAAC;AACD,MAAI,CAAC,IAAI,IAAI;AACX,QAAI,IAAI,WAAW,KAAK;AACtB,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,UAAM,IAAI,MAAM,0BAA0B,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EAC1E;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;AAEA,eAAsB,QAAQ,MAAc,SAAwC;AAClF,QAAM,EAAE,SAAS,IAAI,IAAI,UAAU;AACnC,QAAM,cAAc,WAAW;AAC/B,QAAM,MAAM,GAAG,iBAAiB,cAAc,mBAAmB,IAAI,CAAC;AACtE,QAAM,MAAM,MAAM,MAAM,KAAK;AAAA,IAC3B,SAAS,EAAE,cAAc,YAAY;AAAA,EACvC,CAAC;AACD,MAAI,IAAI,WAAW,KAAK;AACtB,WAAO;AAAA,EACT;AACA,MAAI,CAAC,IAAI,IAAI;AACX,QAAI,IAAI,WAAW,KAAK;AACtB,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AACA,UAAM,IAAI,MAAM,yBAAyB,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EACzE;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;AAMA,eAAsB,sBAAsB,OAAqC;AAC/E,MAAI,CAAC,OAAO,KAAK,EAAG,QAAO;AAC3B,QAAM,MAAM,GAAG,iBAAiB,sBAAsB,mBAAmB,MAAM,KAAK,CAAC,CAAC;AACtF,QAAM,MAAM,MAAM,MAAM,GAAG;AAC3B,MAAI,IAAI,WAAW,IAAK,QAAO;AAC/B,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,IAAI,MAAM,4BAA4B,IAAI,MAAM,IAAI,IAAI,UAAU,EAAE;AAAA,EAC5E;AACA,SAAQ,MAAM,IAAI,KAAK;AACzB;","names":[]}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -20,10 +20,6 @@ export type DispatchConfig = {
|
|
|
20
20
|
|
|
21
21
|
let config: DispatchConfig | null = null;
|
|
22
22
|
|
|
23
|
-
const allPostsBySiteKey = new Map<string, Post[]>();
|
|
24
|
-
const postBySlugBySiteKey = new Map<string, Map<string, Post>>();
|
|
25
|
-
const fullListFetchedForSiteKey = new Set<string>();
|
|
26
|
-
|
|
27
23
|
export function initDispatch(options: DispatchConfig): void {
|
|
28
24
|
config = { siteKey: options.siteKey };
|
|
29
25
|
}
|
|
@@ -40,10 +36,6 @@ export function getConfig(): { siteKey: string } {
|
|
|
40
36
|
export async function getPosts(siteKey?: string): Promise<Post[]> {
|
|
41
37
|
const { siteKey: key } = getConfig();
|
|
42
38
|
const resolvedKey = siteKey ?? key;
|
|
43
|
-
const cached = allPostsBySiteKey.get(resolvedKey);
|
|
44
|
-
if (cached !== undefined) {
|
|
45
|
-
return cached;
|
|
46
|
-
}
|
|
47
39
|
const url = `${DISPATCH_API_BASE}/api/posts`;
|
|
48
40
|
const res = await fetch(url, {
|
|
49
41
|
headers: { "X-Site-Key": resolvedKey },
|
|
@@ -54,32 +46,12 @@ export async function getPosts(siteKey?: string): Promise<Post[]> {
|
|
|
54
46
|
}
|
|
55
47
|
throw new Error(`Failed to fetch posts: ${res.status} ${res.statusText}`);
|
|
56
48
|
}
|
|
57
|
-
|
|
58
|
-
allPostsBySiteKey.set(resolvedKey, posts);
|
|
59
|
-
fullListFetchedForSiteKey.add(resolvedKey);
|
|
60
|
-
const bySlug = new Map<string, Post>();
|
|
61
|
-
for (const post of posts) {
|
|
62
|
-
bySlug.set(post.slug, post);
|
|
63
|
-
}
|
|
64
|
-
postBySlugBySiteKey.set(resolvedKey, bySlug);
|
|
65
|
-
return posts;
|
|
49
|
+
return (await res.json()) as Post[];
|
|
66
50
|
}
|
|
67
51
|
|
|
68
52
|
export async function getPost(slug: string, siteKey?: string): Promise<Post | null> {
|
|
69
53
|
const { siteKey: key } = getConfig();
|
|
70
54
|
const resolvedKey = siteKey ?? key;
|
|
71
|
-
if (fullListFetchedForSiteKey.has(resolvedKey)) {
|
|
72
|
-
const list = allPostsBySiteKey.get(resolvedKey);
|
|
73
|
-
if (list) {
|
|
74
|
-
const post = list.find((p) => p.slug === slug);
|
|
75
|
-
return post ?? null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
const bySlug = postBySlugBySiteKey.get(resolvedKey);
|
|
79
|
-
const cachedPost = bySlug?.get(slug);
|
|
80
|
-
if (cachedPost !== undefined) {
|
|
81
|
-
return cachedPost;
|
|
82
|
-
}
|
|
83
55
|
const url = `${DISPATCH_API_BASE}/api/posts/${encodeURIComponent(slug)}`;
|
|
84
56
|
const res = await fetch(url, {
|
|
85
57
|
headers: { "X-Site-Key": resolvedKey },
|
|
@@ -93,10 +65,20 @@ export async function getPost(slug: string, siteKey?: string): Promise<Post | nu
|
|
|
93
65
|
}
|
|
94
66
|
throw new Error(`Failed to fetch post: ${res.status} ${res.statusText}`);
|
|
95
67
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
68
|
+
return (await res.json()) as Post;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Fetch a single post by its preview token (for preview/draft pages).
|
|
73
|
+
* No site key required. Use this in your app's preview route (e.g. /preview?token=...).
|
|
74
|
+
*/
|
|
75
|
+
export async function getPostByPreviewToken(token: string): Promise<Post | null> {
|
|
76
|
+
if (!token?.trim()) return null;
|
|
77
|
+
const url = `${DISPATCH_API_BASE}/api/preview?token=${encodeURIComponent(token.trim())}`;
|
|
78
|
+
const res = await fetch(url);
|
|
79
|
+
if (res.status === 404) return null;
|
|
80
|
+
if (!res.ok) {
|
|
81
|
+
throw new Error(`Failed to fetch preview: ${res.status} ${res.statusText}`);
|
|
99
82
|
}
|
|
100
|
-
|
|
101
|
-
return post;
|
|
83
|
+
return (await res.json()) as Post;
|
|
102
84
|
}
|
package/src/index.ts
CHANGED
|
@@ -20,7 +20,7 @@ let listResponse: ReturnType<typeof mockPost>[] = [];
|
|
|
20
20
|
let singleResponse: ReturnType<typeof mockPost> | null = null;
|
|
21
21
|
let singleStatus = 200;
|
|
22
22
|
|
|
23
|
-
function mockFetch(url: string,
|
|
23
|
+
function mockFetch(url: string, _options?: RequestInit): Promise<Response> {
|
|
24
24
|
fetchCalls.push({ url });
|
|
25
25
|
const u = new URL(url);
|
|
26
26
|
if (u.pathname === "/api/posts") {
|
|
@@ -61,43 +61,40 @@ async function run(): Promise<void> {
|
|
|
61
61
|
initDispatch({ siteKey: "pk_test" });
|
|
62
62
|
|
|
63
63
|
try {
|
|
64
|
-
// Test 1:
|
|
64
|
+
// Test 1: getPosts() fetches and returns the list (no in-memory cache)
|
|
65
65
|
fetchCalls.length = 0;
|
|
66
66
|
listResponse = [mockPost("a", "Post A"), mockPost("b", "Post B")];
|
|
67
|
-
const list1 = await getPosts("
|
|
68
|
-
|
|
69
|
-
assert(
|
|
70
|
-
|
|
71
|
-
assert(
|
|
67
|
+
const list1 = await getPosts("pk_1");
|
|
68
|
+
assert(fetchCalls.length === 1, "getPosts triggers one fetch");
|
|
69
|
+
assert(list1.length === 2 && list1[0].slug === "a", "returns correct list");
|
|
70
|
+
const list2 = await getPosts("pk_1");
|
|
71
|
+
assert(fetchCalls.length === 2, "second getPosts triggers another fetch (no cache)");
|
|
72
|
+
assert(list2.length === 2, "second call returns same list");
|
|
72
73
|
|
|
73
|
-
// Test 2:
|
|
74
|
+
// Test 2: getPost(slug) fetches and returns the post
|
|
74
75
|
fetchCalls.length = 0;
|
|
75
|
-
|
|
76
|
-
await
|
|
77
|
-
|
|
78
|
-
assert(
|
|
79
|
-
assert(post !== null && post.slug === "my-slug" && post.title === "My Post", "getPost returns correct post from list");
|
|
76
|
+
singleResponse = mockPost("my-slug", "My Post");
|
|
77
|
+
const post = await getPost("my-slug", "pk_2");
|
|
78
|
+
assert(fetchCalls.length === 1, "getPost triggers one fetch");
|
|
79
|
+
assert(post !== null && post.slug === "my-slug" && post.title === "My Post", "returns correct post");
|
|
80
80
|
|
|
81
|
-
// Test 3:
|
|
81
|
+
// Test 3: getPost(slug) for missing slug returns null
|
|
82
82
|
fetchCalls.length = 0;
|
|
83
|
-
|
|
84
|
-
await
|
|
85
|
-
|
|
86
|
-
assert(
|
|
87
|
-
|
|
83
|
+
singleStatus = 404;
|
|
84
|
+
const missing = await getPost("nonexistent", "pk_3");
|
|
85
|
+
assert(fetchCalls.length === 1, "getPost triggers one fetch");
|
|
86
|
+
assert(missing === null, "returns null for 404");
|
|
87
|
+
singleStatus = 200;
|
|
88
88
|
|
|
89
|
-
// Test 4: getPost(slug)
|
|
89
|
+
// Test 4: getPost(slug) then getPosts() each trigger their own fetch
|
|
90
90
|
fetchCalls.length = 0;
|
|
91
|
-
listResponse = [];
|
|
92
91
|
singleResponse = mockPost("first", "First Post");
|
|
93
|
-
const singleFirst = await getPost("first", "pk_cache4");
|
|
94
|
-
assert(fetchCalls.length === 1 && singleFirst?.slug === "first", "first getPost fetches");
|
|
95
92
|
listResponse = [mockPost("first", "First Post")];
|
|
96
|
-
await
|
|
93
|
+
const singleFirst = await getPost("first", "pk_4");
|
|
94
|
+
assert(fetchCalls.length === 1 && singleFirst?.slug === "first", "getPost fetches");
|
|
95
|
+
const listAfter = await getPosts("pk_4");
|
|
97
96
|
assert(fetchCalls.length === 2, "getPosts triggers second fetch");
|
|
98
|
-
|
|
99
|
-
assert(fetchCalls.length === 2, "getPost after getPosts does not fetch again");
|
|
100
|
-
assert(singleCached !== null && singleCached.slug === "first", "getPost returns post from list cache");
|
|
97
|
+
assert(listAfter.length === 1 && listAfter[0].slug === "first", "getPosts returns correct list");
|
|
101
98
|
} finally {
|
|
102
99
|
globalThis.fetch = originalFetch;
|
|
103
100
|
}
|
|
@@ -105,7 +102,7 @@ async function run(): Promise<void> {
|
|
|
105
102
|
|
|
106
103
|
run()
|
|
107
104
|
.then(() => {
|
|
108
|
-
console.log("All
|
|
105
|
+
console.log("All client tests passed.");
|
|
109
106
|
})
|
|
110
107
|
.catch((err) => {
|
|
111
108
|
console.error(err);
|