@xleddyl/nuxt-cms 0.1.61 → 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/dist/module.json +1 -1
- package/dist/module.mjs +117 -9
- package/dist/runtime/app/components/cms/BlocksField.vue +86 -10
- package/dist/runtime/app/components/cms/BrandMark.d.vue.ts +6 -0
- package/dist/runtime/app/components/cms/BrandMark.vue +19 -0
- package/dist/runtime/app/components/cms/BrandMark.vue.d.ts +6 -0
- package/dist/runtime/app/components/cms/Checkbox.d.vue.ts +11 -0
- package/dist/runtime/app/components/cms/Checkbox.vue +21 -0
- package/dist/runtime/app/components/cms/Checkbox.vue.d.ts +11 -0
- package/dist/runtime/app/components/cms/EmptyState.vue +4 -2
- package/dist/runtime/app/components/cms/EntryDrawer.vue +1 -0
- package/dist/runtime/app/components/cms/EntryForm.d.vue.ts +1 -0
- package/dist/runtime/app/components/cms/EntryForm.vue +105 -30
- package/dist/runtime/app/components/cms/EntryForm.vue.d.ts +1 -0
- package/dist/runtime/app/components/cms/FieldInput.vue +20 -5
- package/dist/runtime/app/components/cms/FormField.d.vue.ts +5 -3
- package/dist/runtime/app/components/cms/FormField.vue +8 -2
- package/dist/runtime/app/components/cms/FormField.vue.d.ts +5 -3
- package/dist/runtime/app/components/cms/Icon.vue +1 -1
- package/dist/runtime/app/components/cms/MediaGallery.vue +24 -10
- package/dist/runtime/app/components/cms/MediaUpload.d.vue.ts +1 -0
- package/dist/runtime/app/components/cms/MediaUpload.vue +71 -41
- package/dist/runtime/app/components/cms/MediaUpload.vue.d.ts +1 -0
- package/dist/runtime/app/components/cms/PageHeader.d.vue.ts +5 -3
- package/dist/runtime/app/components/cms/PageHeader.vue +16 -6
- package/dist/runtime/app/components/cms/PageHeader.vue.d.ts +5 -3
- package/dist/runtime/app/components/cms/PasswordInput.d.vue.ts +17 -0
- package/dist/runtime/app/components/cms/PasswordInput.vue +77 -0
- package/dist/runtime/app/components/cms/PasswordInput.vue.d.ts +17 -0
- package/dist/runtime/app/components/cms/SelectMenu.vue +3 -5
- package/dist/runtime/app/components/cms/SettingsAccount.d.vue.ts +3 -0
- package/dist/runtime/app/components/cms/SettingsAccount.vue +138 -0
- package/dist/runtime/app/components/cms/SettingsAccount.vue.d.ts +3 -0
- package/dist/runtime/app/components/cms/SettingsColumns.d.vue.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsColumns.vue +102 -0
- package/dist/runtime/app/components/cms/SettingsColumns.vue.d.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsEntry.d.vue.ts +6 -0
- package/dist/runtime/app/components/cms/SettingsEntry.vue +133 -0
- package/dist/runtime/app/components/cms/SettingsEntry.vue.d.ts +6 -0
- package/dist/runtime/app/components/cms/SettingsLayout.d.vue.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsLayout.vue +177 -0
- package/dist/runtime/app/components/cms/SettingsLayout.vue.d.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsModal.d.vue.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsModal.vue +179 -0
- package/dist/runtime/app/components/cms/SettingsModal.vue.d.ts +16 -0
- package/dist/runtime/app/components/cms/SettingsUsers.d.vue.ts +3 -0
- package/dist/runtime/app/components/cms/SettingsUsers.vue +270 -0
- package/dist/runtime/app/components/cms/SettingsUsers.vue.d.ts +3 -0
- package/dist/runtime/app/components/cms/Spinner.vue +2 -2
- package/dist/runtime/app/components/cms/Table.d.vue.ts +7 -2
- package/dist/runtime/app/components/cms/Table.vue +38 -1
- package/dist/runtime/app/components/cms/Table.vue.d.ts +7 -2
- package/dist/runtime/app/components/cms/Toaster.vue +1 -1
- package/dist/runtime/app/components/cms/WelcomeModal.d.vue.ts +3 -0
- package/dist/runtime/app/components/cms/WelcomeModal.vue +69 -0
- package/dist/runtime/app/components/cms/WelcomeModal.vue.d.ts +3 -0
- package/dist/runtime/app/composables/cms-account.d.ts +11 -0
- package/dist/runtime/app/composables/cms-account.js +23 -0
- package/dist/runtime/app/composables/cms-media-uploader.d.ts +11 -1
- package/dist/runtime/app/composables/cms-media-uploader.js +138 -11
- package/dist/runtime/app/composables/cms-settings.d.ts +13 -0
- package/dist/runtime/app/composables/cms-settings.js +59 -0
- package/dist/runtime/app/composables/cms-theme.d.ts +9 -0
- package/dist/runtime/app/composables/cms-theme.js +27 -0
- package/dist/runtime/app/layouts/cms-admin.d.vue.ts +2 -2
- package/dist/runtime/app/layouts/cms-admin.vue +71 -17
- package/dist/runtime/app/layouts/cms-admin.vue.d.ts +2 -2
- package/dist/runtime/app/pages/admin-collection.vue +127 -101
- package/dist/runtime/app/pages/admin-entry.vue +1 -0
- package/dist/runtime/app/pages/admin-login.vue +18 -3
- package/dist/runtime/app/pages/admin-media.vue +5 -1
- package/dist/runtime/app/utils/ui.d.ts +2 -0
- package/dist/runtime/app/utils/ui.js +18 -0
- package/dist/runtime/assets/main.css +1 -1
- package/dist/runtime/server/api/account-password.put.d.ts +4 -0
- package/dist/runtime/server/api/account-password.put.js +38 -0
- package/dist/runtime/server/api/account-welcome.post.d.ts +4 -0
- package/dist/runtime/server/api/account-welcome.post.js +14 -0
- package/dist/runtime/server/api/account.put.d.ts +3 -0
- package/dist/runtime/server/api/account.put.js +25 -0
- package/dist/runtime/server/api/settings.delete.d.ts +4 -0
- package/dist/runtime/server/api/settings.delete.js +12 -0
- package/dist/runtime/server/api/settings.get.d.ts +3 -0
- package/dist/runtime/server/api/settings.get.js +15 -0
- package/dist/runtime/server/api/settings.put.d.ts +3 -0
- package/dist/runtime/server/api/settings.put.js +14 -0
- package/dist/runtime/server/api/user.delete.d.ts +4 -0
- package/dist/runtime/server/api/user.delete.js +12 -0
- package/dist/runtime/server/api/user.put.d.ts +3 -0
- package/dist/runtime/server/api/user.put.js +27 -0
- package/dist/runtime/server/api/users.get.d.ts +3 -0
- package/dist/runtime/server/api/users.get.js +11 -0
- package/dist/runtime/server/api/users.post.d.ts +3 -0
- package/dist/runtime/server/api/users.post.js +32 -0
- package/dist/runtime/server/plugins/session-validate.d.ts +2 -0
- package/dist/runtime/server/plugins/session-validate.js +13 -0
- package/dist/runtime/server/routes/auth/login.post.js +18 -8
- package/dist/runtime/server/utils/password.d.ts +3 -0
- package/dist/runtime/server/utils/password.js +31 -0
- package/dist/runtime/server/utils/require-admin.d.ts +6 -2
- package/dist/runtime/server/utils/require-admin.js +33 -28
- package/dist/runtime/server/utils/same-origin.d.ts +2 -0
- package/dist/runtime/server/utils/same-origin.js +24 -0
- package/dist/runtime/server/utils/settings.d.ts +16 -0
- package/dist/runtime/server/utils/settings.js +31 -0
- package/dist/runtime/server/utils/users.d.ts +15 -0
- package/dist/runtime/server/utils/users.js +34 -0
- package/dist/runtime/shared/index.d.ts +13 -1
- package/dist/runtime/shared/index.js +2 -0
- package/dist/runtime/shared/layout.d.ts +34 -0
- package/dist/runtime/shared/layout.js +130 -0
- package/dist/runtime/shared/users.d.ts +18 -0
- package/dist/runtime/shared/users.js +1 -0
- package/dist/runtime/shared/validation.js +5 -4
- package/package.json +3 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
|
|
2
|
+
const KEY_LENGTH = 64;
|
|
3
|
+
const COST = 16384;
|
|
4
|
+
function derive(password, salt, cost) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
scrypt(
|
|
7
|
+
password,
|
|
8
|
+
salt,
|
|
9
|
+
KEY_LENGTH,
|
|
10
|
+
{ N: cost, r: 8, p: 1 },
|
|
11
|
+
(error, key) => error ? reject(error) : resolve(key)
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export async function hashPassword(password) {
|
|
16
|
+
const salt = randomBytes(16);
|
|
17
|
+
const key = await derive(password, salt, COST);
|
|
18
|
+
return `scrypt$${COST}$${salt.toString("base64")}$${key.toString("base64")}`;
|
|
19
|
+
}
|
|
20
|
+
export async function verifyPassword(stored, password) {
|
|
21
|
+
const [scheme, cost, salt, key] = stored.split("$");
|
|
22
|
+
if (scheme !== "scrypt" || !cost || !salt || !key) return false;
|
|
23
|
+
const expected = Buffer.from(key, "base64");
|
|
24
|
+
const actual = await derive(password, Buffer.from(salt, "base64"), Number(cost));
|
|
25
|
+
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
26
|
+
}
|
|
27
|
+
const DUMMY_HASH = "scrypt$16384$AAAAAAAAAAAAAAAAAAAAAA==$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
|
|
28
|
+
export async function verifyNothing(password) {
|
|
29
|
+
await verifyPassword(DUMMY_HASH, password);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import type { H3Event } from 'h3';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import type { CmsSessionUser } from '../../shared/index.js';
|
|
3
|
+
import { assertSameOrigin } from './same-origin.js';
|
|
4
|
+
export { assertSameOrigin };
|
|
5
|
+
export declare function isSuperAdminEmail(event: H3Event, email: string): boolean;
|
|
6
|
+
export declare function requireAdmin(event: H3Event): Promise<CmsSessionUser>;
|
|
7
|
+
export declare function requireSuperAdmin(event: H3Event): Promise<CmsSessionUser>;
|
|
@@ -1,37 +1,42 @@
|
|
|
1
|
-
import { createError
|
|
1
|
+
import { createError } from "h3";
|
|
2
2
|
import { getUserSession, useRuntimeConfig } from "#imports";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} catch {
|
|
10
|
-
return void 0;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
function rejectCrossOrigin() {
|
|
14
|
-
throw createError({ statusCode: 403, statusMessage: "Cross-origin request rejected" });
|
|
15
|
-
}
|
|
16
|
-
export function assertSameOrigin(event) {
|
|
17
|
-
const expectedHost = getRequestHost(event);
|
|
18
|
-
const origin = getRequestHeader(event, "origin");
|
|
19
|
-
if (origin) {
|
|
20
|
-
if (hostOfUrl(origin) !== expectedHost) rejectCrossOrigin();
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (ORIGINLESS_SAFE_METHODS.has(event.method)) return;
|
|
24
|
-
if (hostOfUrl(getRequestHeader(event, "referer")) !== expectedHost) rejectCrossOrigin();
|
|
3
|
+
import { assertSameOrigin } from "./same-origin.js";
|
|
4
|
+
import { findUserById, passwordStamp } from "./users.js";
|
|
5
|
+
export { assertSameOrigin };
|
|
6
|
+
export function isSuperAdminEmail(event, email) {
|
|
7
|
+
const { adminEmail } = useRuntimeConfig(event).cms;
|
|
8
|
+
return !!adminEmail && email.toLowerCase() === adminEmail.toLowerCase();
|
|
25
9
|
}
|
|
26
10
|
export async function requireAdmin(event) {
|
|
27
11
|
assertSameOrigin(event);
|
|
28
12
|
const session = await getUserSession(event);
|
|
29
|
-
const
|
|
30
|
-
if (!email) {
|
|
13
|
+
const user = session.user;
|
|
14
|
+
if (!user?.email) {
|
|
31
15
|
throw createError({ statusCode: 401, statusMessage: "Authentication required" });
|
|
32
16
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
17
|
+
if (!user.id) {
|
|
18
|
+
if (!isSuperAdminEmail(event, user.email)) {
|
|
19
|
+
throw createError({ statusCode: 403, statusMessage: "Not authorized" });
|
|
20
|
+
}
|
|
21
|
+
return { email: user.email.toLowerCase(), name: null, role: "superadmin" };
|
|
22
|
+
}
|
|
23
|
+
const row = await findUserById(user.id);
|
|
24
|
+
if (!row || user.passwordStamp !== passwordStamp(row.passwordHash)) {
|
|
25
|
+
throw createError({ statusCode: 401, statusMessage: "Authentication required" });
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
id: row.id,
|
|
29
|
+
email: row.email,
|
|
30
|
+
name: row.name ?? null,
|
|
31
|
+
role: "admin",
|
|
32
|
+
firstLogin: !!row.mustChangePassword,
|
|
33
|
+
passwordStamp: user.passwordStamp
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export async function requireSuperAdmin(event) {
|
|
37
|
+
const user = await requireAdmin(event);
|
|
38
|
+
if (user.role !== "superadmin") {
|
|
39
|
+
throw createError({ statusCode: 403, statusMessage: "Only the super admin can manage users" });
|
|
36
40
|
}
|
|
41
|
+
return user;
|
|
37
42
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createError, getRequestHeader, getRequestHost } from "h3";
|
|
2
|
+
const ORIGINLESS_SAFE_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
3
|
+
function hostOfUrl(value) {
|
|
4
|
+
if (!value) return void 0;
|
|
5
|
+
try {
|
|
6
|
+
const url = new URL(value);
|
|
7
|
+
return url.protocol === "http:" || url.protocol === "https:" ? url.host : void 0;
|
|
8
|
+
} catch {
|
|
9
|
+
return void 0;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function rejectCrossOrigin() {
|
|
13
|
+
throw createError({ statusCode: 403, statusMessage: "Cross-origin request rejected" });
|
|
14
|
+
}
|
|
15
|
+
export function assertSameOrigin(event) {
|
|
16
|
+
const expectedHost = getRequestHost(event);
|
|
17
|
+
const origin = getRequestHeader(event, "origin");
|
|
18
|
+
if (origin) {
|
|
19
|
+
if (hostOfUrl(origin) !== expectedHost) rejectCrossOrigin();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (ORIGINLESS_SAFE_METHODS.has(event.method)) return;
|
|
23
|
+
if (hostOfUrl(getRequestHeader(event, "referer")) !== expectedHost) rejectCrossOrigin();
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { H3Event } from 'h3';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { CmsEntrySettings } from '../../shared/index.js';
|
|
4
|
+
export declare const entrySettingsSchema: z.ZodObject<{
|
|
5
|
+
columns: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
6
|
+
layout: z.ZodOptional<z.ZodObject<{
|
|
7
|
+
main: z.ZodArray<z.ZodObject<{
|
|
8
|
+
title: z.ZodOptional<z.ZodString>;
|
|
9
|
+
description: z.ZodOptional<z.ZodString>;
|
|
10
|
+
collapsed: z.ZodOptional<z.ZodBoolean>;
|
|
11
|
+
rows: z.ZodArray<z.ZodArray<z.ZodString>>;
|
|
12
|
+
}, z.core.$strip>>;
|
|
13
|
+
}, z.core.$strip>>;
|
|
14
|
+
}, z.core.$strict>;
|
|
15
|
+
export declare function requireSettingsKey(event: H3Event): string;
|
|
16
|
+
export declare function parseSettingsValue(raw: string): CmsEntrySettings | null;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createError, getRouterParam } from "h3";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import cmsConfig from "#cms-config";
|
|
4
|
+
const fieldKey = z.string().min(1).max(128);
|
|
5
|
+
const sectionSchema = z.object({
|
|
6
|
+
title: z.string().max(200).optional(),
|
|
7
|
+
description: z.string().max(1e3).optional(),
|
|
8
|
+
collapsed: z.boolean().optional(),
|
|
9
|
+
rows: z.array(z.array(fieldKey).max(4)).max(500)
|
|
10
|
+
});
|
|
11
|
+
export const entrySettingsSchema = z.object({
|
|
12
|
+
columns: z.array(fieldKey).max(200).optional(),
|
|
13
|
+
layout: z.object({
|
|
14
|
+
main: z.array(sectionSchema).max(100)
|
|
15
|
+
}).optional()
|
|
16
|
+
}).strict();
|
|
17
|
+
export function requireSettingsKey(event) {
|
|
18
|
+
const key = getRouterParam(event, "key") ?? "";
|
|
19
|
+
if (!Object.hasOwn(cmsConfig, key)) {
|
|
20
|
+
throw createError({ statusCode: 404, statusMessage: `Unknown entry: ${key}` });
|
|
21
|
+
}
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
export function parseSettingsValue(raw) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = entrySettingsSchema.safeParse(JSON.parse(raw));
|
|
27
|
+
return parsed.success ? parsed.data : null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { cms_users } from '#cms-tables';
|
|
2
|
+
import type { CmsAccount } from '../../shared/index.js';
|
|
3
|
+
export type CmsUserRow = typeof cms_users.$inferSelect;
|
|
4
|
+
export declare function findUserByEmail(email: string): Promise<CmsUserRow | undefined>;
|
|
5
|
+
export declare function findUserById(id: string): Promise<CmsUserRow | undefined>;
|
|
6
|
+
export declare function passwordStamp(passwordHash: string): string;
|
|
7
|
+
export declare function sessionUserFor(row: CmsUserRow): {
|
|
8
|
+
id: string;
|
|
9
|
+
email: string;
|
|
10
|
+
name: string | null;
|
|
11
|
+
role: "admin";
|
|
12
|
+
firstLogin: boolean;
|
|
13
|
+
passwordStamp: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function toAccount(row: CmsUserRow): CmsAccount;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { eq } from "drizzle-orm";
|
|
2
|
+
import { useDb } from "#cms-db";
|
|
3
|
+
import { cms_users } from "#cms-tables";
|
|
4
|
+
export async function findUserByEmail(email) {
|
|
5
|
+
const [row] = await useDb().select().from(cms_users).where(eq(cms_users.email, email.toLowerCase()));
|
|
6
|
+
return row;
|
|
7
|
+
}
|
|
8
|
+
export async function findUserById(id) {
|
|
9
|
+
const [row] = await useDb().select().from(cms_users).where(eq(cms_users.id, id));
|
|
10
|
+
return row;
|
|
11
|
+
}
|
|
12
|
+
export function passwordStamp(passwordHash) {
|
|
13
|
+
return passwordHash.slice(-16);
|
|
14
|
+
}
|
|
15
|
+
export function sessionUserFor(row) {
|
|
16
|
+
return {
|
|
17
|
+
id: row.id,
|
|
18
|
+
email: row.email,
|
|
19
|
+
name: row.name ?? null,
|
|
20
|
+
role: "admin",
|
|
21
|
+
firstLogin: !!row.mustChangePassword,
|
|
22
|
+
passwordStamp: passwordStamp(row.passwordHash)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function toAccount(row) {
|
|
26
|
+
return {
|
|
27
|
+
id: row.id,
|
|
28
|
+
email: row.email,
|
|
29
|
+
name: row.name ?? null,
|
|
30
|
+
role: row.role,
|
|
31
|
+
lastLoginAt: row.lastLoginAt ?? null,
|
|
32
|
+
createdAt: String(row.createdAt)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PgTable } from 'drizzle-orm/pg-core';
|
|
2
2
|
import type { SQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
3
|
+
import type { CmsLayoutBlock, CmsListInput } from './layout.js';
|
|
3
4
|
export type CmsTable = SQLiteTable | PgTable;
|
|
4
5
|
export type FieldType = 'text' | 'richtext' | 'number' | 'boolean' | 'date' | 'email' | 'slug' | 'select' | 'json' | 'media' | 'relation' | 'blocks';
|
|
5
6
|
export interface CmsI18n {
|
|
@@ -76,6 +77,8 @@ export interface CmsTab {
|
|
|
76
77
|
export interface FieldConfig {
|
|
77
78
|
label: string;
|
|
78
79
|
type: FieldType;
|
|
80
|
+
description?: string;
|
|
81
|
+
placeholder?: string;
|
|
79
82
|
tab?: string;
|
|
80
83
|
required?: boolean;
|
|
81
84
|
private?: boolean;
|
|
@@ -138,6 +141,9 @@ export interface CmsEntry {
|
|
|
138
141
|
columns?: string[];
|
|
139
142
|
tabs?: CmsTab[];
|
|
140
143
|
table?: CmsTable;
|
|
144
|
+
icon?: string;
|
|
145
|
+
layout?: CmsLayoutBlock[];
|
|
146
|
+
list?: CmsListInput;
|
|
141
147
|
}
|
|
142
148
|
export declare const MOBILE_MEDIA_SUFFIX = "Mobile";
|
|
143
149
|
export declare function mobileMediaKey(key: string): string;
|
|
@@ -168,6 +174,8 @@ export declare function blocksFieldTypeName(entryName: string, fieldKey: string)
|
|
|
168
174
|
export declare function blockTypeName(entryName: string, fieldKey: string, blockName: string): string;
|
|
169
175
|
interface FieldInputBase {
|
|
170
176
|
label: string;
|
|
177
|
+
description?: string;
|
|
178
|
+
placeholder?: string;
|
|
171
179
|
required?: boolean;
|
|
172
180
|
private?: boolean;
|
|
173
181
|
tab?: string;
|
|
@@ -234,6 +242,9 @@ export type CmsFieldInput = TextFieldInput | RichtextFieldInput | NumberFieldInp
|
|
|
234
242
|
interface CmsEntryInputBase {
|
|
235
243
|
id: string;
|
|
236
244
|
label: string;
|
|
245
|
+
icon?: string;
|
|
246
|
+
layout?: CmsLayoutBlock[];
|
|
247
|
+
list?: CmsListInput;
|
|
237
248
|
drafts?: boolean;
|
|
238
249
|
tabs?: CmsTab[];
|
|
239
250
|
fields: Record<string, CmsFieldInput>;
|
|
@@ -261,4 +272,5 @@ export interface CmsPageInput extends CmsEntryInputBase {
|
|
|
261
272
|
export type CmsEntryInput = CmsCollectionInput | CmsSingleInput | CmsPageInput;
|
|
262
273
|
export type CmsConfigInput = Record<string, CmsEntryInput>;
|
|
263
274
|
export declare function defineCmsConfig<T extends CmsConfigInput>(config: T): T;
|
|
264
|
-
export
|
|
275
|
+
export * from './layout.js';
|
|
276
|
+
export * from './users.js';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FieldConfig } from './index.js';
|
|
2
|
+
export declare const MAX_LAYOUT_ROW_FIELDS = 4;
|
|
3
|
+
export type CmsLayoutRow = string | string[];
|
|
4
|
+
export interface CmsLayoutSectionInput {
|
|
5
|
+
title: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
collapsed?: boolean;
|
|
8
|
+
rows: CmsLayoutRow[];
|
|
9
|
+
}
|
|
10
|
+
export type CmsLayoutBlock = CmsLayoutRow | CmsLayoutSectionInput;
|
|
11
|
+
export interface CmsLayoutSection {
|
|
12
|
+
title?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
collapsed?: boolean;
|
|
15
|
+
rows: string[][];
|
|
16
|
+
}
|
|
17
|
+
export interface CmsFormLayout {
|
|
18
|
+
main: CmsLayoutSection[];
|
|
19
|
+
}
|
|
20
|
+
export interface CmsListInput {
|
|
21
|
+
columns?: string[];
|
|
22
|
+
}
|
|
23
|
+
export interface CmsEntrySettings {
|
|
24
|
+
columns?: string[];
|
|
25
|
+
layout?: CmsFormLayout;
|
|
26
|
+
}
|
|
27
|
+
export type CmsSettingsMap = Record<string, CmsEntrySettings>;
|
|
28
|
+
export declare function layoutFromConfig(input: CmsLayoutBlock[] | undefined): CmsFormLayout;
|
|
29
|
+
export declare function layoutKeys(input: CmsLayoutBlock[] | undefined): string[];
|
|
30
|
+
export declare function resolveFormLayout(fields: Record<string, FieldConfig>, layout: CmsFormLayout | undefined): CmsFormLayout;
|
|
31
|
+
export declare function defaultListColumns(fields: Record<string, FieldConfig>, list: CmsListInput | undefined, extra?: string[]): string[];
|
|
32
|
+
export declare function resolveListColumns(fields: Record<string, FieldConfig>, saved: string[] | undefined, list: CmsListInput | undefined, extra?: string[]): string[];
|
|
33
|
+
export declare function layoutErrors(at: string, fields: Record<string, FieldConfig>, input: CmsLayoutBlock[] | undefined): string[];
|
|
34
|
+
export declare function listErrors(at: string, fields: Record<string, FieldConfig>, list: CmsListInput | undefined, extra?: string[]): string[];
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export const MAX_LAYOUT_ROW_FIELDS = 4;
|
|
2
|
+
function isSectionInput(block) {
|
|
3
|
+
return typeof block === "object" && !Array.isArray(block);
|
|
4
|
+
}
|
|
5
|
+
function toRow(row) {
|
|
6
|
+
return Array.isArray(row) ? [...row] : [row];
|
|
7
|
+
}
|
|
8
|
+
function sectionsFromBlocks(blocks) {
|
|
9
|
+
const sections = [];
|
|
10
|
+
let loose = null;
|
|
11
|
+
for (const block of blocks ?? []) {
|
|
12
|
+
if (isSectionInput(block)) {
|
|
13
|
+
loose = null;
|
|
14
|
+
sections.push({
|
|
15
|
+
title: block.title,
|
|
16
|
+
...block.description ? { description: block.description } : {},
|
|
17
|
+
...block.collapsed ? { collapsed: true } : {},
|
|
18
|
+
rows: block.rows.map(toRow)
|
|
19
|
+
});
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (!loose) {
|
|
23
|
+
loose = { rows: [] };
|
|
24
|
+
sections.push(loose);
|
|
25
|
+
}
|
|
26
|
+
loose.rows.push(toRow(block));
|
|
27
|
+
}
|
|
28
|
+
return sections;
|
|
29
|
+
}
|
|
30
|
+
export function layoutFromConfig(input) {
|
|
31
|
+
return { main: sectionsFromBlocks(input) };
|
|
32
|
+
}
|
|
33
|
+
export function layoutKeys(input) {
|
|
34
|
+
return layoutFromConfig(input).main.flatMap((section) => section.rows.flat());
|
|
35
|
+
}
|
|
36
|
+
function cloneSection(section) {
|
|
37
|
+
return { ...section, rows: section.rows.map((row) => [...row]) };
|
|
38
|
+
}
|
|
39
|
+
export function resolveFormLayout(fields, layout) {
|
|
40
|
+
const known = new Set(Object.keys(fields));
|
|
41
|
+
const mobileOf = new Map(
|
|
42
|
+
Object.entries(fields).filter(([, field]) => field.mobileOf && known.has(field.mobileOf)).map(([key, field]) => [field.mobileOf, key])
|
|
43
|
+
);
|
|
44
|
+
const placed = /* @__PURE__ */ new Set();
|
|
45
|
+
const clean = (sections) => (sections ?? []).map(cloneSection).map((section) => ({
|
|
46
|
+
...section,
|
|
47
|
+
rows: section.rows.map(
|
|
48
|
+
(row) => row.filter((key) => {
|
|
49
|
+
if (!known.has(key) || placed.has(key)) return false;
|
|
50
|
+
placed.add(key);
|
|
51
|
+
return true;
|
|
52
|
+
})
|
|
53
|
+
).filter((row) => row.length)
|
|
54
|
+
}));
|
|
55
|
+
const main = clean(layout?.main);
|
|
56
|
+
for (const section of main) {
|
|
57
|
+
for (let r = 0; r < section.rows.length; r++) {
|
|
58
|
+
const row = section.rows[r];
|
|
59
|
+
for (const [parent, mobile] of mobileOf) {
|
|
60
|
+
const index = row.indexOf(parent);
|
|
61
|
+
if (index === -1 || placed.has(mobile)) continue;
|
|
62
|
+
if (row.length < MAX_LAYOUT_ROW_FIELDS) row.splice(index + 1, 0, mobile);
|
|
63
|
+
else section.rows.splice(r + 1, 0, [mobile]);
|
|
64
|
+
placed.add(mobile);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const missing = Object.keys(fields).filter((key) => !placed.has(key));
|
|
69
|
+
if (missing.length) {
|
|
70
|
+
const rows = [];
|
|
71
|
+
for (const key of missing) {
|
|
72
|
+
if (placed.has(key)) continue;
|
|
73
|
+
const mobile = mobileOf.get(key);
|
|
74
|
+
const row = mobile && !placed.has(mobile) ? [key, mobile] : [key];
|
|
75
|
+
row.forEach((k) => placed.add(k));
|
|
76
|
+
rows.push(row);
|
|
77
|
+
}
|
|
78
|
+
const last = main[main.length - 1];
|
|
79
|
+
if (last && !last.title) last.rows.push(...rows);
|
|
80
|
+
else main.push({ rows });
|
|
81
|
+
}
|
|
82
|
+
return { main };
|
|
83
|
+
}
|
|
84
|
+
export function defaultListColumns(fields, list, extra = []) {
|
|
85
|
+
const available = [...Object.keys(fields), ...extra];
|
|
86
|
+
const configured = list?.columns?.filter((key) => available.includes(key));
|
|
87
|
+
if (configured?.length) return configured;
|
|
88
|
+
return [...Object.keys(fields).slice(0, 4), ...extra];
|
|
89
|
+
}
|
|
90
|
+
export function resolveListColumns(fields, saved, list, extra = []) {
|
|
91
|
+
const available = /* @__PURE__ */ new Set([...Object.keys(fields), ...extra]);
|
|
92
|
+
const kept = saved?.filter((key) => available.has(key));
|
|
93
|
+
return kept?.length ? [...new Set(kept)] : defaultListColumns(fields, list, extra);
|
|
94
|
+
}
|
|
95
|
+
export function layoutErrors(at, fields, input) {
|
|
96
|
+
if (!input) return [];
|
|
97
|
+
const errors = [];
|
|
98
|
+
if (!Array.isArray(input)) return [`${at}: layout must be an array of rows and sections`];
|
|
99
|
+
const blocks = input;
|
|
100
|
+
const seen = /* @__PURE__ */ new Set();
|
|
101
|
+
const checkRow = (row) => {
|
|
102
|
+
const keys = toRow(row);
|
|
103
|
+
if (!keys.length) errors.push(`${at}: layout has an empty row`);
|
|
104
|
+
if (keys.length > MAX_LAYOUT_ROW_FIELDS)
|
|
105
|
+
errors.push(
|
|
106
|
+
`${at}: layout row [${keys.join(", ")}] has more than ${MAX_LAYOUT_ROW_FIELDS} fields`
|
|
107
|
+
);
|
|
108
|
+
for (const key of keys) {
|
|
109
|
+
if (!Object.hasOwn(fields, key))
|
|
110
|
+
errors.push(`${at}: layout names '${key}', which is not a declared field`);
|
|
111
|
+
else if (seen.has(key)) errors.push(`${at}: layout names '${key}' more than once`);
|
|
112
|
+
seen.add(key);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
for (const block of blocks) {
|
|
116
|
+
if (isSectionInput(block)) {
|
|
117
|
+
if (!block.title) errors.push(`${at}: every layout section needs a title`);
|
|
118
|
+
if (!Array.isArray(block.rows))
|
|
119
|
+
errors.push(`${at}: layout section '${block.title}' needs a rows array`);
|
|
120
|
+
else block.rows.forEach(checkRow);
|
|
121
|
+
} else {
|
|
122
|
+
checkRow(block);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return errors;
|
|
126
|
+
}
|
|
127
|
+
export function listErrors(at, fields, list, extra = []) {
|
|
128
|
+
if (!list?.columns) return [];
|
|
129
|
+
return list.columns.filter((key) => !Object.hasOwn(fields, key) && !extra.includes(key)).map((key) => `${at}: list.columns names '${key}', which is not a declared field`);
|
|
130
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type CmsUserRole = 'superadmin' | 'admin';
|
|
2
|
+
export interface CmsSessionUser {
|
|
3
|
+
id?: string;
|
|
4
|
+
email: string;
|
|
5
|
+
name?: string | null;
|
|
6
|
+
role?: CmsUserRole;
|
|
7
|
+
firstLogin?: boolean;
|
|
8
|
+
passwordStamp?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface CmsAccount {
|
|
11
|
+
id: string;
|
|
12
|
+
email: string;
|
|
13
|
+
name: string | null;
|
|
14
|
+
role: CmsUserRole;
|
|
15
|
+
lastLoginAt: string | null;
|
|
16
|
+
createdAt: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const MIN_PASSWORD_LENGTH = 8;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MIN_PASSWORD_LENGTH = 8;
|
|
@@ -31,8 +31,9 @@ function scalarSchema(field, m) {
|
|
|
31
31
|
return field.required ? z.string().trim().min(1, m.required) : z.string();
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
function optionalize(field, base) {
|
|
35
|
-
|
|
34
|
+
function optionalize(field, base, m) {
|
|
35
|
+
if (!field.required) return base.nullish().transform((v) => v ?? null);
|
|
36
|
+
return z.any().refine((v) => v !== null && v !== void 0, m.required).pipe(base);
|
|
36
37
|
}
|
|
37
38
|
function translatableSchema(field, i18n, m) {
|
|
38
39
|
const { locales, defaultLocale } = i18n;
|
|
@@ -42,7 +43,7 @@ function translatableSchema(field, i18n, m) {
|
|
|
42
43
|
function blockFieldSchema(blockField, i18n, m) {
|
|
43
44
|
if (isTranslatableField(blockField) && i18n.locales.length)
|
|
44
45
|
return translatableSchema(blockField, i18n, m);
|
|
45
|
-
return optionalize(blockField, scalarSchema(blockField, m));
|
|
46
|
+
return optionalize(blockField, scalarSchema(blockField, m), m);
|
|
46
47
|
}
|
|
47
48
|
function blocksSchema(field, i18n, m) {
|
|
48
49
|
const variants = Object.entries(field.blocks ?? {}).map(
|
|
@@ -99,7 +100,7 @@ export function buildEntrySchema(entry, i18n, messages) {
|
|
|
99
100
|
shape[key] = field.required ? json.refine((v) => v !== void 0 && v !== null, m.required) : json.nullish().transform((v) => v ?? null);
|
|
100
101
|
continue;
|
|
101
102
|
}
|
|
102
|
-
shape[key] = optionalize(field, scalarSchema(field, m));
|
|
103
|
+
shape[key] = optionalize(field, scalarSchema(field, m), m);
|
|
103
104
|
}
|
|
104
105
|
if (entry.drafts) {
|
|
105
106
|
shape.status = z.enum(["draft", "published"]).nullish().transform((v) => v ?? void 0);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xleddyl/nuxt-cms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Lightweight CMS that ships with your Nuxt app: runs on the Nitro server, content types defined in code, /cms admin panel, GraphQL API, SQLite or Postgres. No external CMS needed!",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Edoardo Alberti (https://github.com/xleddyl)",
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"dist"
|
|
61
61
|
],
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@fontsource-variable/
|
|
64
|
-
"@fontsource/
|
|
63
|
+
"@fontsource-variable/geist": "^5.3.0",
|
|
64
|
+
"@fontsource-variable/geist-mono": "^5.3.0",
|
|
65
65
|
"@nuxt/kit": "^4.4.8",
|
|
66
66
|
"@tailwindcss/vite": "^4.3.2",
|
|
67
67
|
"@tiptap/starter-kit": "^3.0.0",
|