@kidecms/core 0.14.3 → 0.16.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.
@@ -3,7 +3,7 @@ import type { APIRoute } from "astro";
3
3
  import config from "virtual:kide/config";
4
4
  import { cms } from "virtual:kide/api";
5
5
  import { collaboration } from "virtual:kide/runtime";
6
- import { isApprover, resolveCollaboration } from "../../../core";
6
+ import { formatPagePath, getPageHref, isApprover, resolveCollaboration } from "../../../core";
7
7
  import { loadSharedSectionUsageCounts } from "../../../admin/lib/edit-data";
8
8
 
9
9
  export const prerender = false;
@@ -124,13 +124,13 @@ const handleHtmlMutation = async (
124
124
  ctx,
125
125
  );
126
126
  }
127
+ const CREATE_MESSAGES: Record<string, string> = {
128
+ publish: `${name} created and published`,
129
+ schedule: `${name} scheduled`,
130
+ };
127
131
  const msg = blocked
128
132
  ? `${name} saved as draft — needs review approval before publishing`
129
- : intent === "publish"
130
- ? `${name} created and published`
131
- : intent === "schedule"
132
- ? `${name} scheduled`
133
- : `${name} created`;
133
+ : (CREATE_MESSAGES[intent] ?? `${name} created`);
134
134
  return redirect(`/admin/${collectionSlug}/${created._id}`, { status: blocked ? "error" : "success", msg });
135
135
  }
136
136
 
@@ -158,19 +158,15 @@ const handleHtmlMutation = async (
158
158
  const actor = locals.user ? { id: locals.user.id, email: locals.user.email, role: locals.user.role } : null;
159
159
  await collaboration.submitForReview(collectionSlug, documentId, actor);
160
160
  }
161
+ const UPDATE_MESSAGES: Record<string, string> = {
162
+ publish: `${name} published`,
163
+ unpublish: `${name} unpublished`,
164
+ schedule: `${name} scheduled`,
165
+ "submit-review": "Saved and submitted for review",
166
+ };
161
167
  const msg = blocked
162
168
  ? `Saved — needs review approval before publishing`
163
- : intent === "publish"
164
- ? `${name} published`
165
- : intent === "unpublish"
166
- ? `${name} unpublished`
167
- : intent === "schedule"
168
- ? `${name} scheduled`
169
- : intent === "submit-review"
170
- ? `Saved and submitted for review`
171
- : collection.drafts
172
- ? `${name} saved as draft`
173
- : `${name} saved`;
169
+ : (UPDATE_MESSAGES[intent] ?? (collection.drafts ? `${name} saved as draft` : `${name} saved`));
174
170
  return redirect(redirectTo, { status: blocked ? "error" : "success", msg });
175
171
  }
176
172
 
@@ -287,6 +283,25 @@ export const GET: APIRoute = async ({ params, url, locals }) => {
287
283
  }
288
284
  }
289
285
 
286
+ // The list table's "URL" column is computed, not stored — resolve it here too so
287
+ // client-side re-fetches (sort/search/page changes) keep showing it. Mirrors
288
+ // ListView.astro's own getPagePath, since both run server-side against the same collection config.
289
+ const collection = getCollection(collectionSlug);
290
+ if (collection.preview || collection.pathPrefix) {
291
+ for (const entry of docs as Array<Record<string, unknown>>) {
292
+ const hasPreview = !!(collection.preview || (collection.pathPrefix && entry.slug));
293
+ const pagePath = hasPreview
294
+ ? typeof collection.preview === "string"
295
+ ? collection.preview
296
+ : cmsRuntime.meta.getRouteForDocument(collectionSlug, entry)
297
+ : null;
298
+ if (pagePath) {
299
+ entry._pageHref = getPageHref(pagePath, !collection.drafts || entry._status === "published");
300
+ }
301
+ entry.__page = formatPagePath(pagePath);
302
+ }
303
+ }
304
+
290
305
  const page = Math.floor(offset / limit) + 1;
291
306
  const totalPages = Math.ceil(totalDocs / limit);
292
307
 
@@ -330,11 +345,16 @@ export const POST: APIRoute = async ({ params, request, locals, cache }) => {
330
345
  }
331
346
 
332
347
  if (pathAction === "duplicate" && documentId) {
348
+ const collection = getCollection(collectionSlug);
349
+ if (collection.auth) {
350
+ // Duplicating a user would copy its password hash onto a new account —
351
+ // never useful, and not something field-stripping alone can make safe.
352
+ return Response.json({ error: "Auth collections can't be duplicated." }, { status: 400 });
353
+ }
333
354
  const original = await collectionApi.findById(documentId, { status: "any" }, ctx);
334
355
  if (!original) {
335
356
  return Response.json({ error: "Not found." }, { status: 404 });
336
357
  }
337
- const collection = getCollection(collectionSlug);
338
358
 
339
359
  // Strip system fields and unique values that would conflict
340
360
  const stripFields = (source: Record<string, any>) => {
@@ -131,11 +131,15 @@ export const POST: APIRoute = async ({ request, locals }) => {
131
131
  );
132
132
  }
133
133
 
134
+ // Dev only: let Vite's dev server catch up on the new files before the client
135
+ // reloads, or the just-uploaded asset renders broken until a manual refresh.
136
+ if (import.meta.env.DEV) {
137
+ await new Promise((r) => setTimeout(r, 1000));
138
+ }
139
+
134
140
  const redirectTo = formData.get("redirectTo");
135
141
 
136
142
  if (redirectTo) {
137
- // Delay so Vite's dev server picks up the new files before the redirect
138
- await new Promise((r) => setTimeout(r, 1000));
139
143
  const location =
140
144
  uploaded.length === 1
141
145
  ? `/admin/assets/${uploaded[0]._id}?_toast=success&_msg=Asset+uploaded`
@@ -0,0 +1,46 @@
1
+ import type { APIRoute } from "astro";
2
+ import config from "virtual:kide/config";
3
+ import { cms } from "virtual:kide/api";
4
+ import { canRead, getVisualStatus } from "../../admin/lib/access";
5
+
6
+ export const prerender = false;
7
+
8
+ const cmsRuntime = cms as Record<string, any>;
9
+
10
+ // Resolves a public page's `data-cms-doc="<collection>:<id>"` marker into an admin
11
+ // edit URL for the edit-bar chip. Auth is enforced by the middleware (401 without a
12
+ // session); this only decides whether THIS user may see a link to THIS document.
13
+ export const GET: APIRoute = async ({ url, locals }) => {
14
+ if (config.admin?.editBar === false) {
15
+ return Response.json({ error: "Not found" }, { status: 404 });
16
+ }
17
+
18
+ const ref = url.searchParams.get("doc") ?? "";
19
+ const separator = ref.indexOf(":");
20
+ const collectionSlug = separator === -1 ? ref : ref.slice(0, separator);
21
+ const documentId = separator === -1 ? "" : ref.slice(separator + 1);
22
+
23
+ const collection = config.collections.find((c) => c.slug === collectionSlug);
24
+ const user = locals.user;
25
+ if (!collection || collection.auth || !documentId || !user || !canRead(config, user, collectionSlug)) {
26
+ return Response.json({ error: "Not found" }, { status: 404, headers: { "Cache-Control": "no-store" } });
27
+ }
28
+
29
+ let doc: Record<string, unknown> | null;
30
+ try {
31
+ doc = await cmsRuntime[collectionSlug].findById(documentId, { status: "any" }, { user });
32
+ } catch {
33
+ doc = null;
34
+ }
35
+ if (!doc) {
36
+ return Response.json({ error: "Not found" }, { status: 404, headers: { "Cache-Control": "no-store" } });
37
+ }
38
+
39
+ return Response.json(
40
+ {
41
+ editUrl: collection.singleton ? `/admin/${collectionSlug}` : `/admin/${collectionSlug}/${documentId}`,
42
+ status: collection.drafts ? getVisualStatus(doc) : null,
43
+ },
44
+ { headers: { "Cache-Control": "no-store" } },
45
+ );
46
+ };