@kyro-cms/admin 0.1.6 → 0.1.8

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.
Files changed (179) hide show
  1. package/README.md +149 -51
  2. package/package.json +54 -5
  3. package/src/collections/auth/index.ts +2 -2
  4. package/src/collections/portfolio/index.ts +343 -0
  5. package/src/components/ActionBar.tsx +153 -16
  6. package/src/components/Admin.tsx +137 -28
  7. package/src/components/ApiExplorer.tsx +325 -0
  8. package/src/components/ApiKeysManager.tsx +563 -0
  9. package/src/components/AuditLogsPage.tsx +664 -0
  10. package/src/components/AutoForm.tsx +2155 -770
  11. package/src/components/BrandingHub.tsx +267 -0
  12. package/src/components/BulkActionsBar.tsx +3 -3
  13. package/src/components/CreateView.tsx +4 -4
  14. package/src/components/Dashboard.tsx +393 -0
  15. package/src/components/DetailView.tsx +200 -58
  16. package/src/components/DeveloperCenter.tsx +403 -0
  17. package/src/components/EnhancedListView.tsx +890 -0
  18. package/src/components/GraphQLExplorer.tsx +675 -0
  19. package/src/components/GraphQLPlayground.tsx +627 -0
  20. package/src/components/ListView.tsx +192 -54
  21. package/src/components/MediaGallery.tsx +1569 -0
  22. package/src/components/Modal.tsx +206 -0
  23. package/src/components/RestPlayground.tsx +951 -0
  24. package/src/components/Sidebar.astro +237 -0
  25. package/src/components/ThemeProvider.tsx +8 -2
  26. package/src/components/UserManagement.tsx +204 -0
  27. package/src/components/VersionHistoryPanel.tsx +3 -3
  28. package/src/components/WebhookManager.tsx +608 -0
  29. package/src/components/blocks/AccordionBlock.tsx +65 -0
  30. package/src/components/blocks/ArrayBlock.tsx +84 -0
  31. package/src/components/blocks/BlockEditModal.tsx +363 -0
  32. package/src/components/blocks/ButtonBlock.tsx +64 -0
  33. package/src/components/blocks/ChildBlocksTree.tsx +551 -0
  34. package/src/components/blocks/CodeBlock.tsx +114 -0
  35. package/src/components/blocks/ColumnsBlock.tsx +93 -0
  36. package/src/components/blocks/DividerBlock.tsx +43 -0
  37. package/src/components/blocks/FileBlock.tsx +63 -0
  38. package/src/components/blocks/HeadingBlock.tsx +59 -0
  39. package/src/components/blocks/HeroBlock.tsx +99 -0
  40. package/src/components/blocks/ImageBlock.tsx +82 -0
  41. package/src/components/blocks/LinkBlock.tsx +65 -0
  42. package/src/components/blocks/ListBlock.tsx +60 -0
  43. package/src/components/blocks/ParagraphBlock.tsx +61 -0
  44. package/src/components/blocks/RelationshipBlock.tsx +72 -0
  45. package/src/components/blocks/RichTextBlock.tsx +66 -0
  46. package/src/components/blocks/VStackBlock.tsx +61 -0
  47. package/src/components/blocks/VideoBlock.tsx +65 -0
  48. package/src/components/blocks/index.ts +10 -0
  49. package/src/components/fields/AccordionField.tsx +213 -0
  50. package/src/components/fields/ArrayField.tsx +241 -0
  51. package/src/components/fields/BlocksField.tsx +323 -0
  52. package/src/components/fields/ButtonField.tsx +53 -0
  53. package/src/components/fields/CheckboxField.tsx +18 -8
  54. package/src/components/fields/ChildrenField.tsx +48 -0
  55. package/src/components/fields/CodeField.tsx +294 -0
  56. package/src/components/fields/ColumnsField.tsx +137 -0
  57. package/src/components/fields/DateField.tsx +24 -12
  58. package/src/components/fields/EditorClient.tsx +537 -0
  59. package/src/components/fields/HeadingField.tsx +31 -0
  60. package/src/components/fields/HeroField.tsx +101 -0
  61. package/src/components/fields/JSONField.tsx +341 -0
  62. package/src/components/fields/LinkField.tsx +81 -0
  63. package/src/components/fields/ListField.tsx +74 -0
  64. package/src/components/fields/MarkdownField.tsx +260 -0
  65. package/src/components/fields/NumberField.tsx +25 -13
  66. package/src/components/fields/PortableTextField.tsx +155 -0
  67. package/src/components/fields/PortableTextRenderer.tsx +68 -0
  68. package/src/components/fields/RelationshipBlockField.tsx +233 -0
  69. package/src/components/fields/RelationshipField.tsx +278 -60
  70. package/src/components/fields/SelectField.tsx +28 -16
  71. package/src/components/fields/TextField.tsx +31 -15
  72. package/src/components/fields/UploadField.tsx +613 -0
  73. package/src/components/fields/VideoField.tsx +73 -0
  74. package/src/components/fields/extensions/blockComponents.tsx +247 -0
  75. package/src/components/fields/extensions/blocksStore.ts +273 -0
  76. package/src/components/fields/index.ts +24 -0
  77. package/src/components/index.ts +1 -2
  78. package/src/components/layout/Header.tsx +2 -2
  79. package/src/components/layout/Layout.tsx +3 -3
  80. package/src/components/ui/Badge.tsx +9 -4
  81. package/src/components/ui/BlockDrawer.tsx +79 -0
  82. package/src/components/ui/Button.tsx +1 -1
  83. package/src/components/ui/CommandPalette.tsx +362 -0
  84. package/src/components/ui/CommandPaletteWrapper.tsx +97 -0
  85. package/src/components/ui/Dropdown.tsx +1 -1
  86. package/src/components/ui/Modal.tsx +37 -12
  87. package/src/components/ui/PromptModal.tsx +94 -0
  88. package/src/components/ui/SlidePanel.tsx +43 -16
  89. package/src/components/ui/Toast.tsx +80 -14
  90. package/src/env.d.ts +16 -0
  91. package/src/env.ts +20 -0
  92. package/src/index.ts +0 -1
  93. package/src/layouts/AdminLayout.astro +164 -170
  94. package/src/layouts/AuthLayout.astro +23 -6
  95. package/src/lib/MediaService.ts +541 -0
  96. package/src/lib/api.ts +163 -0
  97. package/src/lib/auth/sqlite-adapter.ts +319 -0
  98. package/src/lib/config.ts +23 -7
  99. package/src/lib/dataStore.ts +188 -73
  100. package/src/lib/date-utils.ts +69 -0
  101. package/src/lib/db/adapter.ts +54 -0
  102. package/src/lib/db/drizzle-mysql-adapter.ts +194 -0
  103. package/src/lib/db/drizzle-mysql-auth-adapter.ts +327 -0
  104. package/src/lib/db/drizzle-postgres-adapter.ts +202 -0
  105. package/src/lib/db/drizzle-postgres-auth-adapter.ts +304 -0
  106. package/src/lib/db/drizzle-sqlite-adapter.ts +227 -0
  107. package/src/lib/db/drizzle-sqlite-auth-adapter.ts +548 -0
  108. package/src/lib/db/index.ts +449 -0
  109. package/src/lib/db/mongodb-adapter.ts +207 -0
  110. package/src/lib/db/mongodb-auth-adapter.ts +305 -0
  111. package/src/lib/db/schema/mysql-auth.ts +113 -0
  112. package/src/lib/db/schema/mysql-content.ts +20 -0
  113. package/src/lib/db/schema/postgres-auth.ts +116 -0
  114. package/src/lib/db/schema/postgres-content.ts +35 -0
  115. package/src/lib/db/schema/postgres-media.ts +52 -0
  116. package/src/lib/db/schema/postgres-settings.ts +11 -0
  117. package/src/lib/db/schema/sqlite-auth.ts +112 -0
  118. package/src/lib/db/schema/sqlite-content.ts +20 -0
  119. package/src/lib/db/version-adapter.ts +248 -0
  120. package/src/lib/graphql/index.ts +1 -0
  121. package/src/lib/graphql/schema.ts +443 -0
  122. package/src/lib/i18n.tsx +353 -0
  123. package/src/lib/rate-limit.ts +267 -0
  124. package/src/lib/slugify.ts +15 -0
  125. package/src/lib/storage.ts +374 -0
  126. package/src/lib/store.ts +85 -0
  127. package/src/lib/validation.ts +250 -0
  128. package/src/middleware.ts +70 -11
  129. package/src/pages/[collection]/[id].astro +178 -122
  130. package/src/pages/[collection]/index.astro +24 -156
  131. package/src/pages/admin/api-explorer.astro +98 -0
  132. package/src/pages/admin/graphql-explorer.astro +40 -0
  133. package/src/pages/admin/graphql.astro +97 -0
  134. package/src/pages/admin/index.astro +200 -139
  135. package/src/pages/admin/keys.astro +8 -0
  136. package/src/pages/admin/rest-playground.astro +44 -0
  137. package/src/pages/admin/webhooks.astro +8 -0
  138. package/src/pages/api/[collection]/[id]/publish.ts +52 -0
  139. package/src/pages/api/[collection]/[id]/unpublish.ts +42 -0
  140. package/src/pages/api/[collection]/[id]/versions.ts +66 -0
  141. package/src/pages/api/[collection]/[id].ts +114 -159
  142. package/src/pages/api/[collection]/index.ts +150 -230
  143. package/src/pages/api/auth/[id].ts +48 -69
  144. package/src/pages/api/auth/audit-logs.ts +20 -43
  145. package/src/pages/api/auth/login.ts +159 -45
  146. package/src/pages/api/auth/logout.ts +42 -24
  147. package/src/pages/api/auth/refresh.ts +119 -0
  148. package/src/pages/api/auth/register.ts +110 -40
  149. package/src/pages/api/auth/users.ts +22 -97
  150. package/src/pages/api/collections.ts +59 -0
  151. package/src/pages/api/globals/[slug]/test.ts +172 -0
  152. package/src/pages/api/globals/[slug].ts +42 -0
  153. package/src/pages/api/graphql.ts +90 -0
  154. package/src/pages/api/health.ts +417 -40
  155. package/src/pages/api/keys/[id].ts +26 -0
  156. package/src/pages/api/keys/index.ts +75 -0
  157. package/src/pages/api/media/[id].ts +309 -0
  158. package/src/pages/api/media/folders.ts +609 -0
  159. package/src/pages/api/media/index.ts +146 -0
  160. package/src/pages/api/media/resize.ts +267 -0
  161. package/src/pages/api/search.ts +82 -0
  162. package/src/pages/api/slug-availability.ts +70 -0
  163. package/src/pages/api/storage-config.ts +20 -0
  164. package/src/pages/api/storage-status.ts +206 -0
  165. package/src/pages/api/upload.ts +334 -0
  166. package/src/pages/api/webhooks/index.ts +71 -0
  167. package/src/pages/audit/index.astro +2 -104
  168. package/src/pages/login.astro +11 -11
  169. package/src/pages/media.astro +10 -0
  170. package/src/pages/preview/[collection]/[id].astro +178 -0
  171. package/src/pages/register.astro +13 -13
  172. package/src/pages/roles/index.astro +21 -21
  173. package/src/pages/settings/[slug].astro +162 -0
  174. package/src/pages/settings/index.astro +9 -0
  175. package/src/pages/users/[id].astro +29 -21
  176. package/src/pages/users/index.astro +22 -17
  177. package/src/pages/users/new.astro +18 -17
  178. package/src/styles/main.css +563 -128
  179. package/src/components/layout/Sidebar.tsx +0 -497
@@ -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 };