@kyro-cms/admin 0.1.5 → 0.1.7
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 +149 -51
- package/package.json +52 -5
- package/src/collections/auth/index.ts +2 -2
- package/src/collections/portfolio/index.ts +343 -0
- package/src/components/ActionBar.tsx +153 -16
- package/src/components/Admin.tsx +136 -27
- package/src/components/ApiExplorer.tsx +325 -0
- package/src/components/ApiKeysManager.tsx +563 -0
- package/src/components/AuditLogsPage.tsx +664 -0
- package/src/components/AutoForm.tsx +1417 -661
- package/src/components/BrandingHub.tsx +267 -0
- package/src/components/BulkActionsBar.tsx +3 -3
- package/src/components/CreateView.tsx +3 -3
- package/src/components/Dashboard.tsx +393 -0
- package/src/components/DetailView.tsx +199 -57
- package/src/components/DeveloperCenter.tsx +403 -0
- package/src/components/EnhancedListView.tsx +786 -0
- package/src/components/GraphQLExplorer.tsx +675 -0
- package/src/components/GraphQLPlayground.tsx +627 -0
- package/src/components/ListView.tsx +191 -53
- package/src/components/MediaGallery.tsx +1569 -0
- package/src/components/Modal.tsx +149 -0
- package/src/components/RestPlayground.tsx +951 -0
- package/src/components/Sidebar.astro +237 -0
- package/src/components/UserManagement.tsx +204 -0
- package/src/components/VersionHistoryPanel.tsx +3 -3
- package/src/components/WebhookManager.tsx +608 -0
- package/src/components/blocks/AccordionBlock.tsx +97 -0
- package/src/components/blocks/ArrayBlock.tsx +75 -0
- package/src/components/blocks/BlockEditModal.MARKER +12 -0
- package/src/components/blocks/BlockEditModal.tsx +774 -0
- package/src/components/blocks/ButtonBlock.tsx +165 -0
- package/src/components/blocks/ChildBlocksTree.tsx +551 -0
- package/src/components/blocks/CodeBlock.tsx +66 -0
- package/src/components/blocks/ColumnsBlock.tsx +151 -0
- package/src/components/blocks/DividerBlock.tsx +43 -0
- package/src/components/blocks/FileBlock.tsx +64 -0
- package/src/components/blocks/HeadingBlock.tsx +81 -0
- package/src/components/blocks/HeroBlock.tsx +157 -0
- package/src/components/blocks/ImageBlock.tsx +83 -0
- package/src/components/blocks/LinkBlock.tsx +71 -0
- package/src/components/blocks/ListBlock.tsx +39 -0
- package/src/components/blocks/ParagraphBlock.tsx +61 -0
- package/src/components/blocks/RelationshipBlock.tsx +279 -0
- package/src/components/blocks/VStackBlock.tsx +75 -0
- package/src/components/blocks/VideoBlock.tsx +45 -0
- package/src/components/blocks/index.ts +10 -0
- package/src/components/fields/BlocksField.tsx +323 -0
- package/src/components/fields/CheckboxField.tsx +15 -9
- package/src/components/fields/CodeField.tsx +234 -0
- package/src/components/fields/DateField.tsx +38 -11
- package/src/components/fields/EditorClient.tsx +271 -0
- package/src/components/fields/FileField.tsx +390 -0
- package/src/components/fields/HybridContentField.tsx +109 -0
- package/src/components/fields/ImageField.tsx +429 -0
- package/src/components/fields/JSONField.tsx +361 -0
- package/src/components/fields/MarkdownField.tsx +282 -0
- package/src/components/fields/NumberField.tsx +42 -12
- package/src/components/fields/PortableTextField.tsx +143 -0
- package/src/components/fields/PortableTextRenderer.tsx +68 -0
- package/src/components/fields/RelationshipField.tsx +231 -59
- package/src/components/fields/SelectField.tsx +25 -15
- package/src/components/fields/TextField.tsx +45 -14
- package/src/components/fields/extensions/blockComponents.tsx +237 -0
- package/src/components/fields/extensions/blocksStore.ts +273 -0
- package/src/components/fields/index.ts +13 -0
- package/src/components/index.ts +1 -2
- package/src/components/layout/Header.tsx +2 -2
- package/src/components/layout/Layout.tsx +2 -2
- package/src/components/ui/Badge.tsx +9 -4
- package/src/components/ui/BlockDrawer.tsx +79 -0
- package/src/components/ui/Button.tsx +1 -1
- package/src/components/ui/CommandPalette.tsx +362 -0
- package/src/components/ui/CommandPaletteWrapper.tsx +97 -0
- package/src/components/ui/Dropdown.tsx +1 -1
- package/src/components/ui/Modal.tsx +37 -12
- package/src/components/ui/PromptModal.tsx +94 -0
- package/src/components/ui/SlidePanel.tsx +43 -16
- package/src/components/ui/Toast.tsx +80 -14
- package/src/env.d.ts +16 -0
- package/src/env.ts +20 -0
- package/src/index.ts +0 -1
- package/src/layouts/AdminLayout.astro +164 -170
- package/src/layouts/AuthLayout.astro +50 -0
- package/src/lib/MediaService.ts +541 -0
- package/src/lib/auth/sqlite-adapter.ts +319 -0
- package/src/lib/config.ts +22 -6
- package/src/lib/dataStore.ts +132 -74
- package/src/lib/db/adapter.ts +54 -0
- package/src/lib/db/drizzle-mysql-adapter.ts +194 -0
- package/src/lib/db/drizzle-mysql-auth-adapter.ts +327 -0
- package/src/lib/db/drizzle-postgres-adapter.ts +202 -0
- package/src/lib/db/drizzle-postgres-auth-adapter.ts +304 -0
- package/src/lib/db/drizzle-sqlite-adapter.ts +227 -0
- package/src/lib/db/drizzle-sqlite-auth-adapter.ts +548 -0
- package/src/lib/db/index.ts +449 -0
- package/src/lib/db/mongodb-adapter.ts +207 -0
- package/src/lib/db/mongodb-auth-adapter.ts +305 -0
- package/src/lib/db/schema/mysql-auth.ts +113 -0
- package/src/lib/db/schema/mysql-content.ts +20 -0
- package/src/lib/db/schema/postgres-auth.ts +116 -0
- package/src/lib/db/schema/postgres-content.ts +35 -0
- package/src/lib/db/schema/postgres-media.ts +52 -0
- package/src/lib/db/schema/postgres-settings.ts +11 -0
- package/src/lib/db/schema/sqlite-auth.ts +112 -0
- package/src/lib/db/schema/sqlite-content.ts +20 -0
- package/src/lib/graphql/index.ts +1 -0
- package/src/lib/graphql/schema.ts +443 -0
- package/src/lib/rate-limit.ts +267 -0
- package/src/lib/storage.ts +374 -0
- package/src/lib/store.ts +85 -0
- package/src/middleware.ts +116 -28
- package/src/pages/[collection]/[id].astro +178 -122
- package/src/pages/[collection]/index.astro +24 -156
- package/src/pages/admin/api-explorer.astro +98 -0
- package/src/pages/admin/graphql-explorer.astro +40 -0
- package/src/pages/admin/graphql.astro +97 -0
- package/src/pages/admin/index.astro +286 -0
- package/src/pages/admin/keys.astro +8 -0
- package/src/pages/admin/rest-playground.astro +44 -0
- package/src/pages/admin/webhooks.astro +8 -0
- package/src/pages/api/[collection]/[id]/publish.ts +44 -0
- package/src/pages/api/[collection]/[id]/unpublish.ts +42 -0
- package/src/pages/api/[collection]/[id]/versions.ts +36 -0
- package/src/pages/api/[collection]/[id].ts +102 -159
- package/src/pages/api/[collection]/index.ts +151 -230
- package/src/pages/api/auth/[id].ts +48 -69
- package/src/pages/api/auth/audit-logs.ts +20 -43
- package/src/pages/api/auth/login.ts +159 -45
- package/src/pages/api/auth/logout.ts +50 -20
- package/src/pages/api/auth/refresh.ts +119 -0
- package/src/pages/api/auth/register.ts +110 -40
- package/src/pages/api/auth/users.ts +22 -97
- package/src/pages/api/collections.ts +59 -0
- package/src/pages/api/globals/[slug]/test.ts +172 -0
- package/src/pages/api/globals/[slug].ts +42 -0
- package/src/pages/api/graphql.ts +90 -0
- package/src/pages/api/health.ts +417 -40
- package/src/pages/api/keys/[id].ts +26 -0
- package/src/pages/api/keys/index.ts +75 -0
- package/src/pages/api/media/[id].ts +309 -0
- package/src/pages/api/media/folders.ts +609 -0
- package/src/pages/api/media/index.ts +146 -0
- package/src/pages/api/media/resize.ts +267 -0
- package/src/pages/api/search.ts +82 -0
- package/src/pages/api/slug-availability.ts +70 -0
- package/src/pages/api/storage-config.ts +20 -0
- package/src/pages/api/storage-status.ts +206 -0
- package/src/pages/api/upload.ts +334 -0
- package/src/pages/api/webhooks/index.ts +71 -0
- package/src/pages/audit/index.astro +2 -104
- package/src/pages/login.astro +82 -0
- package/src/pages/media.astro +10 -0
- package/src/pages/preview/[collection]/[id].astro +178 -0
- package/src/pages/register.astro +102 -0
- package/src/pages/roles/index.astro +21 -21
- package/src/pages/settings/[slug].astro +162 -0
- package/src/pages/settings/index.astro +9 -0
- package/src/pages/users/[id].astro +29 -21
- package/src/pages/users/index.astro +22 -17
- package/src/pages/users/new.astro +18 -17
- package/src/styles/main.css +553 -128
- package/src/components/layout/Sidebar.tsx +0 -497
- package/src/pages/index.astro +0 -225
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { sqliteTable, text, integer, index } from "drizzle-orm/sqlite-core";
|
|
2
|
+
|
|
3
|
+
export const users = sqliteTable("users", {
|
|
4
|
+
id: text("id").primaryKey(),
|
|
5
|
+
email: text("email").notNull().unique(),
|
|
6
|
+
passwordHash: text("password_hash").notNull(),
|
|
7
|
+
name: text("name"),
|
|
8
|
+
role: text("role").notNull().default("customer"),
|
|
9
|
+
tenantId: text("tenant_id"),
|
|
10
|
+
emailVerified: integer("email_verified", { mode: "boolean" }).default(false),
|
|
11
|
+
locked: integer("locked", { mode: "boolean" }).default(false),
|
|
12
|
+
lastLogin: text("last_login"),
|
|
13
|
+
failedLoginAttempts: integer("failed_login_attempts").default(0),
|
|
14
|
+
lockedUntil: text("locked_until"),
|
|
15
|
+
createdAt: text("created_at").notNull(),
|
|
16
|
+
updatedAt: text("updated_at").notNull(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const sessions = sqliteTable(
|
|
20
|
+
"sessions",
|
|
21
|
+
{
|
|
22
|
+
id: text("id").primaryKey(),
|
|
23
|
+
token: text("token").notNull().unique(),
|
|
24
|
+
refreshToken: text("refresh_token"),
|
|
25
|
+
userId: text("user_id")
|
|
26
|
+
.notNull()
|
|
27
|
+
.references(() => users.id, { onDelete: "cascade" }),
|
|
28
|
+
expiresAt: text("expires_at").notNull(),
|
|
29
|
+
createdAt: text("created_at").notNull(),
|
|
30
|
+
},
|
|
31
|
+
(table) => {
|
|
32
|
+
return {
|
|
33
|
+
userIdIdx: index("idx_sessions_user_id").on(table.userId),
|
|
34
|
+
tokenIdx: index("idx_sessions_token").on(table.token),
|
|
35
|
+
expiresIdx: index("idx_sessions_expires").on(table.expiresAt),
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export const roles = sqliteTable("roles", {
|
|
41
|
+
id: text("id").primaryKey(),
|
|
42
|
+
name: text("name").notNull().unique(),
|
|
43
|
+
level: integer("level").notNull().default(0),
|
|
44
|
+
inherits: text("inherits").$type<string[]>(),
|
|
45
|
+
permissions: text("permissions").$type<string[]>(),
|
|
46
|
+
isSystem: integer("is_system", { mode: "boolean" }).default(false),
|
|
47
|
+
description: text("description"),
|
|
48
|
+
createdAt: text("created_at").notNull(),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export const auditLogs = sqliteTable(
|
|
52
|
+
"audit_logs",
|
|
53
|
+
{
|
|
54
|
+
id: text("id").primaryKey(),
|
|
55
|
+
action: text("action").notNull(),
|
|
56
|
+
userId: text("user_id").references(() => users.id, {
|
|
57
|
+
onDelete: "set null",
|
|
58
|
+
}),
|
|
59
|
+
userEmail: text("user_email"),
|
|
60
|
+
role: text("role"),
|
|
61
|
+
resource: text("resource").notNull(),
|
|
62
|
+
ipAddress: text("ip_address"),
|
|
63
|
+
userAgent: text("user_agent"),
|
|
64
|
+
success: integer("success", { mode: "boolean" }).notNull(),
|
|
65
|
+
error: text("error"),
|
|
66
|
+
metadata: text("metadata").$type<Record<string, unknown>>(),
|
|
67
|
+
createdAt: text("created_at").notNull(),
|
|
68
|
+
},
|
|
69
|
+
(table) => {
|
|
70
|
+
return {
|
|
71
|
+
userIdIdx: index("idx_audit_user_id").on(table.userId),
|
|
72
|
+
actionIdx: index("idx_audit_action").on(table.action),
|
|
73
|
+
createdAtIdx: index("idx_audit_created_at").on(table.createdAt),
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
export const passwordHistory = sqliteTable(
|
|
79
|
+
"password_history",
|
|
80
|
+
{
|
|
81
|
+
id: text("id").primaryKey(),
|
|
82
|
+
userId: text("user_id")
|
|
83
|
+
.notNull()
|
|
84
|
+
.references(() => users.id, { onDelete: "cascade" }),
|
|
85
|
+
passwordHash: text("password_hash").notNull(),
|
|
86
|
+
createdAt: text("created_at").notNull(),
|
|
87
|
+
},
|
|
88
|
+
(table) => {
|
|
89
|
+
return {
|
|
90
|
+
userIdIdx: index("idx_password_history_user_id").on(table.userId),
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
export const lockouts = sqliteTable("lockouts", {
|
|
96
|
+
id: text("id").primaryKey(),
|
|
97
|
+
userId: text("user_id")
|
|
98
|
+
.notNull()
|
|
99
|
+
.references(() => users.id, { onDelete: "cascade" }),
|
|
100
|
+
ipAddress: text("ip_address"),
|
|
101
|
+
lockedUntil: text("locked_until").notNull(),
|
|
102
|
+
createdAt: text("created_at").notNull(),
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
export type User = typeof users.$inferSelect;
|
|
106
|
+
export type NewUser = typeof users.$inferInsert;
|
|
107
|
+
export type Session = typeof sessions.$inferSelect;
|
|
108
|
+
export type NewSession = typeof sessions.$inferInsert;
|
|
109
|
+
export type Role = typeof roles.$inferSelect;
|
|
110
|
+
export type NewRole = typeof roles.$inferInsert;
|
|
111
|
+
export type AuditLog = typeof auditLogs.$inferSelect;
|
|
112
|
+
export type NewAuditLog = typeof auditLogs.$inferInsert;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
|
|
2
|
+
|
|
3
|
+
export const documents = sqliteTable("documents", {
|
|
4
|
+
id: text("id").primaryKey(),
|
|
5
|
+
collection: text("collection").notNull(),
|
|
6
|
+
data: text("data").notNull().$type<Record<string, unknown>>(),
|
|
7
|
+
createdAt: text("created_at").notNull(),
|
|
8
|
+
updatedAt: text("updated_at").notNull(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export const globals = sqliteTable("globals", {
|
|
12
|
+
slug: text("slug").primaryKey(),
|
|
13
|
+
data: text("data").notNull().$type<Record<string, unknown>>(),
|
|
14
|
+
updatedAt: text("updated_at").notNull(),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export type Document = typeof documents.$inferSelect;
|
|
18
|
+
export type NewDocument = typeof documents.$inferInsert;
|
|
19
|
+
export type Global = typeof globals.$inferSelect;
|
|
20
|
+
export type NewGlobal = typeof globals.$inferInsert;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { executeGraphQL, schemaString } from "./schema";
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { buildSchema, graphql } from "graphql";
|
|
2
|
+
import { dataStore } from "../dataStore";
|
|
3
|
+
import { collections, globals } from "../config";
|
|
4
|
+
import type { CollectionConfig, GlobalConfig } from "@kyro-cms/core";
|
|
5
|
+
import jwt from "jsonwebtoken";
|
|
6
|
+
|
|
7
|
+
const JWT_SECRET = process.env.JWT_SECRET || "change-me-in-production";
|
|
8
|
+
|
|
9
|
+
function generateSchemaTypes() {
|
|
10
|
+
const queryLines: string[] = [
|
|
11
|
+
`_schema: SchemaInfo!`,
|
|
12
|
+
`ping: String!`,
|
|
13
|
+
`collections: [CollectionInfo!]!`,
|
|
14
|
+
`globals: [GlobalInfo!]!`,
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
for (const [slug, collection] of Object.entries(collections)) {
|
|
18
|
+
if (slug === "roles") continue;
|
|
19
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
20
|
+
queryLines.push(`${safeSlug}(page: Int, limit: Int): CollectionResult!`);
|
|
21
|
+
queryLines.push(`${safeSlug}ById(id: ID!): JSON`);
|
|
22
|
+
queryLines.push(`${safeSlug}BySlug(slug: String!): JSON`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const slug of Object.keys(globals)) {
|
|
26
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
27
|
+
queryLines.push(`${safeSlug}: GlobalResult!`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const mutationLines: string[] = [];
|
|
31
|
+
mutationLines.push(`login(email: String!, password: String!): AuthPayload!`);
|
|
32
|
+
mutationLines.push(
|
|
33
|
+
`register(email: String!, password: String!): AuthPayload!`,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
for (const slug of Object.keys(collections)) {
|
|
37
|
+
if (slug === "roles" || slug === "users") continue;
|
|
38
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
39
|
+
const name = capitalize(safeSlug);
|
|
40
|
+
mutationLines.push(`create${name}(input: JSON!): JSON!`);
|
|
41
|
+
mutationLines.push(`update${name}(id: ID!, input: JSON!): JSON!`);
|
|
42
|
+
mutationLines.push(`delete${name}(id: ID!): Boolean!`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const slug of Object.keys(globals)) {
|
|
46
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
47
|
+
const name = capitalize(safeSlug);
|
|
48
|
+
mutationLines.push(`update${name}(input: JSON!): GlobalResult!`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return `
|
|
52
|
+
scalar JSON
|
|
53
|
+
|
|
54
|
+
type Query {
|
|
55
|
+
${queryLines.join("\n ")}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type Mutation {
|
|
59
|
+
${mutationLines.join("\n ")}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type Meta {
|
|
63
|
+
page: Int!
|
|
64
|
+
limit: Int!
|
|
65
|
+
totalDocs: Int!
|
|
66
|
+
totalPages: Int!
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type CollectionResult {
|
|
70
|
+
docs: [JSON!]!
|
|
71
|
+
totalDocs: Int!
|
|
72
|
+
totalPages: Int!
|
|
73
|
+
page: Int!
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type GlobalResult {
|
|
77
|
+
data: JSON
|
|
78
|
+
success: Boolean!
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
type AuthPayload {
|
|
82
|
+
token: String
|
|
83
|
+
user: JSON
|
|
84
|
+
error: String
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type SchemaInfo {
|
|
88
|
+
types: [TypeInfo!]!
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type TypeInfo {
|
|
92
|
+
name: String!
|
|
93
|
+
fields: [FieldInfo!]!
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type FieldInfo {
|
|
97
|
+
name: String!
|
|
98
|
+
type: String!
|
|
99
|
+
required: Boolean!
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type CollectionInfo {
|
|
103
|
+
slug: String!
|
|
104
|
+
name: String
|
|
105
|
+
fields: [String!]!
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type GlobalInfo {
|
|
109
|
+
slug: String!
|
|
110
|
+
name: String
|
|
111
|
+
}
|
|
112
|
+
`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const schemaString = generateSchemaTypes();
|
|
116
|
+
const typeDefs = buildSchema(schemaString);
|
|
117
|
+
|
|
118
|
+
function capitalize(str: string): string {
|
|
119
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface Context {
|
|
123
|
+
user?: {
|
|
124
|
+
id: string;
|
|
125
|
+
email: string;
|
|
126
|
+
role: string;
|
|
127
|
+
};
|
|
128
|
+
apiKeyId?: string;
|
|
129
|
+
permissions?: string[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const rootValue = {
|
|
133
|
+
_schema: () => {
|
|
134
|
+
const types = Object.entries(collections).map(([slug, collection]) => ({
|
|
135
|
+
name: capitalize(slug),
|
|
136
|
+
fields: collection.fields.map((f: any) => ({
|
|
137
|
+
name: f.name,
|
|
138
|
+
type: getGraphQLType(f.type),
|
|
139
|
+
required: f.required || false,
|
|
140
|
+
})),
|
|
141
|
+
}));
|
|
142
|
+
return { types };
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
ping: () => "pong",
|
|
146
|
+
|
|
147
|
+
collections: () => {
|
|
148
|
+
return Object.entries(collections).map(([slug, collection]) => ({
|
|
149
|
+
slug,
|
|
150
|
+
name: (collection as CollectionConfig).label || slug,
|
|
151
|
+
fields: (collection as CollectionConfig).fields.map((f: any) => f.name),
|
|
152
|
+
}));
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
globals: () => {
|
|
156
|
+
return Object.entries(globals).map(([slug, global]) => ({
|
|
157
|
+
slug,
|
|
158
|
+
name: (global as GlobalConfig).label || slug,
|
|
159
|
+
}));
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
...generateQueryResolvers(),
|
|
163
|
+
|
|
164
|
+
...generateMutationResolvers(),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
function getGraphQLType(fieldType: string): string {
|
|
168
|
+
const typeMap: Record<string, string> = {
|
|
169
|
+
text: "String",
|
|
170
|
+
richtext: "String",
|
|
171
|
+
email: "String",
|
|
172
|
+
password: "String",
|
|
173
|
+
url: "String",
|
|
174
|
+
number: "Float",
|
|
175
|
+
integer: "Int",
|
|
176
|
+
boolean: "Boolean",
|
|
177
|
+
select: "String",
|
|
178
|
+
multiselect: "[String!]!",
|
|
179
|
+
date: "String",
|
|
180
|
+
datetime: "String",
|
|
181
|
+
media: "String",
|
|
182
|
+
reference: "String",
|
|
183
|
+
array: "[String!]!",
|
|
184
|
+
json: "JSON",
|
|
185
|
+
code: "String",
|
|
186
|
+
markdown: "String",
|
|
187
|
+
slug: "String",
|
|
188
|
+
};
|
|
189
|
+
return typeMap[fieldType] || "String";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function generateQueryResolvers() {
|
|
193
|
+
const resolvers: Record<string, any> = {};
|
|
194
|
+
|
|
195
|
+
for (const slug of Object.keys(collections)) {
|
|
196
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
197
|
+
resolvers[safeSlug] = async ({
|
|
198
|
+
page,
|
|
199
|
+
limit,
|
|
200
|
+
}: {
|
|
201
|
+
page?: number;
|
|
202
|
+
limit?: number;
|
|
203
|
+
}) => {
|
|
204
|
+
return await dataStore.find(slug, {
|
|
205
|
+
page: page || 1,
|
|
206
|
+
limit: limit || 25,
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
resolvers[`${safeSlug}ById`] = async ({ id }: { id: string }) => {
|
|
211
|
+
return await dataStore.findById(slug, id);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
resolvers[`${safeSlug}BySlug`] = async ({
|
|
215
|
+
slug: itemSlug,
|
|
216
|
+
}: {
|
|
217
|
+
slug: string;
|
|
218
|
+
}) => {
|
|
219
|
+
const docs = await dataStore.find(slug, { limit: 100 });
|
|
220
|
+
return docs.docs.find((d: any) => d.slug === itemSlug) || null;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const slug of Object.keys(globals)) {
|
|
225
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
226
|
+
resolvers[safeSlug] = async () => {
|
|
227
|
+
return { data: await dataStore.findGlobal(slug), success: true };
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return resolvers;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function generateMutationResolvers() {
|
|
235
|
+
return {
|
|
236
|
+
login: async ({ email, password }: { email: string; password: string }) => {
|
|
237
|
+
try {
|
|
238
|
+
const { SQLiteAuthAdapter } = await import("../auth/sqlite-adapter");
|
|
239
|
+
const adapter = new SQLiteAuthAdapter();
|
|
240
|
+
|
|
241
|
+
const user = await adapter.findUserByEmail(email);
|
|
242
|
+
if (!user) {
|
|
243
|
+
return { error: "Invalid credentials" };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const valid = await adapter.verifyPassword(
|
|
247
|
+
password,
|
|
248
|
+
user.passwordHash || "",
|
|
249
|
+
);
|
|
250
|
+
if (!valid) {
|
|
251
|
+
return { error: "Invalid credentials" };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const { passwordHash, ...safeUser } = user;
|
|
255
|
+
const token = jwt.sign(
|
|
256
|
+
{
|
|
257
|
+
sub: safeUser.id,
|
|
258
|
+
email: safeUser.email,
|
|
259
|
+
role: safeUser.role,
|
|
260
|
+
tenantId: safeUser.tenantId,
|
|
261
|
+
},
|
|
262
|
+
JWT_SECRET,
|
|
263
|
+
{ expiresIn: "7d" },
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
return { token, user: safeUser };
|
|
267
|
+
} catch {
|
|
268
|
+
return { error: "Authentication failed" };
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
|
|
272
|
+
register: async ({
|
|
273
|
+
email,
|
|
274
|
+
password,
|
|
275
|
+
}: {
|
|
276
|
+
email: string;
|
|
277
|
+
password: string;
|
|
278
|
+
}) => {
|
|
279
|
+
try {
|
|
280
|
+
const { SQLiteAuthAdapter } = await import("../auth/sqlite-adapter");
|
|
281
|
+
const adapter = new SQLiteAuthAdapter();
|
|
282
|
+
|
|
283
|
+
const existing = await adapter.findUserByEmail(email);
|
|
284
|
+
if (existing) {
|
|
285
|
+
return { error: "Email already exists" };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const passwordHash = await adapter.hashPassword(password);
|
|
289
|
+
const user = await adapter.createUser({
|
|
290
|
+
email,
|
|
291
|
+
passwordHash,
|
|
292
|
+
role: "customer",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const { passwordHash: _, ...safeUser } = user;
|
|
296
|
+
const token = jwt.sign(
|
|
297
|
+
{
|
|
298
|
+
sub: safeUser.id,
|
|
299
|
+
email: safeUser.email,
|
|
300
|
+
role: safeUser.role,
|
|
301
|
+
tenantId: safeUser.tenantId,
|
|
302
|
+
},
|
|
303
|
+
JWT_SECRET,
|
|
304
|
+
{ expiresIn: "7d" },
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
return { token, user: safeUser };
|
|
308
|
+
} catch {
|
|
309
|
+
return { error: "Registration failed" };
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function generateCollectionMutations() {
|
|
316
|
+
const resolvers: Record<string, any> = {};
|
|
317
|
+
|
|
318
|
+
for (const slug of Object.keys(collections)) {
|
|
319
|
+
if (slug === "roles" || slug === "users") continue;
|
|
320
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
321
|
+
const name = capitalize(safeSlug);
|
|
322
|
+
|
|
323
|
+
resolvers[`create${name}`] = async ({ input }: { input: any }) => {
|
|
324
|
+
return await dataStore.create(slug, input);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
resolvers[`update${name}`] = async ({
|
|
328
|
+
id,
|
|
329
|
+
input,
|
|
330
|
+
}: {
|
|
331
|
+
id: string;
|
|
332
|
+
input: any;
|
|
333
|
+
}) => {
|
|
334
|
+
return await dataStore.update(slug, id, input);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
resolvers[`delete${name}`] = async ({ id }: { id: string }) => {
|
|
338
|
+
return await dataStore.delete(slug, id);
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
for (const slug of Object.keys(globals)) {
|
|
343
|
+
const safeSlug = slug.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
344
|
+
const name = capitalize(safeSlug);
|
|
345
|
+
|
|
346
|
+
resolvers[`update${name}`] = async ({ input }: { input: any }) => {
|
|
347
|
+
await dataStore.updateGlobal(slug, input);
|
|
348
|
+
return { data: await dataStore.findGlobal(slug), success: true };
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return resolvers;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export async function executeGraphQL(
|
|
356
|
+
query: string,
|
|
357
|
+
variables?: Record<string, any>,
|
|
358
|
+
authToken?: string,
|
|
359
|
+
apiKey?: string,
|
|
360
|
+
) {
|
|
361
|
+
let context: Context = {};
|
|
362
|
+
|
|
363
|
+
if (apiKey) {
|
|
364
|
+
try {
|
|
365
|
+
const result = await validateApiKeyFromDataStore(apiKey);
|
|
366
|
+
if (result.valid && result.user) {
|
|
367
|
+
context.user = {
|
|
368
|
+
id: result.userId as string,
|
|
369
|
+
email: result.user.email,
|
|
370
|
+
role: result.user.role,
|
|
371
|
+
};
|
|
372
|
+
context.apiKeyId = result.apiKeyId;
|
|
373
|
+
context.permissions = result.permissions;
|
|
374
|
+
}
|
|
375
|
+
} catch {
|
|
376
|
+
// Invalid API key, continue without auth
|
|
377
|
+
}
|
|
378
|
+
} else if (authToken) {
|
|
379
|
+
try {
|
|
380
|
+
const payload = jwt.verify(authToken, JWT_SECRET) as jwt.JwtPayload;
|
|
381
|
+
context.user = {
|
|
382
|
+
id: payload.sub as string,
|
|
383
|
+
email: (payload as any).email,
|
|
384
|
+
role: (payload as any).role,
|
|
385
|
+
};
|
|
386
|
+
} catch {
|
|
387
|
+
// Invalid token, continue without auth
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const result = await graphql({
|
|
392
|
+
schema: typeDefs,
|
|
393
|
+
source: query,
|
|
394
|
+
rootValue,
|
|
395
|
+
contextValue: context,
|
|
396
|
+
variableValues: variables,
|
|
397
|
+
} as any);
|
|
398
|
+
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async function validateApiKeyFromDataStore(apiKey: string) {
|
|
403
|
+
if (!apiKey.startsWith("kyro_")) {
|
|
404
|
+
return { valid: false };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const keyPrefix = apiKey.substring(0, 8);
|
|
408
|
+
const result = await dataStore.find("_api_keys", { limit: 100 });
|
|
409
|
+
const keys = (result.docs || []).filter(
|
|
410
|
+
(k: any) => k.keyPrefix === keyPrefix,
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
for (const record of keys) {
|
|
414
|
+
if (record.key === apiKey) {
|
|
415
|
+
if (record.expiresAt && new Date(record.expiresAt) < new Date()) {
|
|
416
|
+
return { valid: false, error: "API key expired" };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
await dataStore.update("_api_keys", record.id, {
|
|
421
|
+
lastUsedAt: new Date().toISOString(),
|
|
422
|
+
});
|
|
423
|
+
} catch {}
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
valid: true,
|
|
427
|
+
userId: record.userId,
|
|
428
|
+
user: {
|
|
429
|
+
id: record.userId,
|
|
430
|
+
email: record.email,
|
|
431
|
+
role: record.role || "author",
|
|
432
|
+
tenantId: record.tenantId,
|
|
433
|
+
},
|
|
434
|
+
permissions: record.permissions || [],
|
|
435
|
+
apiKeyId: record.id,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
return { valid: false };
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export { schemaString };
|