opacacms 0.1.7 → 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 (46) hide show
  1. package/dist/admin/index.js +255 -20
  2. package/dist/admin/webcomponent.js +291 -46
  3. package/dist/cli/index.js +3638 -30
  4. package/dist/client.js +126 -5
  5. package/dist/db/bun-sqlite.js +790 -21
  6. package/dist/db/d1.js +788 -19
  7. package/dist/db/index.js +53 -4
  8. package/dist/db/postgres.js +792 -23
  9. package/dist/db/sqlite.js +788 -19
  10. package/dist/index.js +456 -8
  11. package/dist/runtimes/bun.js +1909 -8
  12. package/dist/runtimes/cloudflare-workers.js +1910 -9
  13. package/dist/runtimes/next.js +1908 -7
  14. package/dist/runtimes/node.js +1909 -8
  15. package/dist/schema/collection.d.ts +3 -7
  16. package/dist/schema/fields/index.d.ts +24 -25
  17. package/dist/schema/global.d.ts +9 -9
  18. package/dist/schema/index.d.ts +30 -4
  19. package/dist/schema/index.js +546 -1
  20. package/dist/server.js +2246 -17
  21. package/dist/storage/index.js +40 -1
  22. package/package.json +1 -1
  23. package/dist/chunk-16vgcf3k.js +0 -88
  24. package/dist/chunk-2yz1nsxs.js +0 -126
  25. package/dist/chunk-5gvbp2qa.js +0 -167
  26. package/dist/chunk-62ev8gnc.js +0 -41
  27. package/dist/chunk-6ew02s0c.js +0 -472
  28. package/dist/chunk-7a9kn0np.js +0 -116
  29. package/dist/chunk-8sqjbsgt.js +0 -42
  30. package/dist/chunk-9kxpbcb1.js +0 -85
  31. package/dist/chunk-cvdd4eqh.js +0 -110
  32. package/dist/chunk-d3ffeqp9.js +0 -87
  33. package/dist/chunk-fa5mg0hr.js +0 -96
  34. package/dist/chunk-j4d50hrx.js +0 -20
  35. package/dist/chunk-jwjk85ze.js +0 -15
  36. package/dist/chunk-m09hahe2.js +0 -250
  37. package/dist/chunk-s8mqwnm1.js +0 -14
  38. package/dist/chunk-srsac177.js +0 -85
  39. package/dist/chunk-v521d72w.js +0 -10
  40. package/dist/chunk-vtvqfhgy.js +0 -2442
  41. package/dist/chunk-xa7rjsn2.js +0 -20
  42. package/dist/chunk-xg35h5a3.js +0 -15
  43. package/dist/chunk-y8hc6nm4.js +0 -17
  44. package/dist/chunk-ybbbqj63.js +0 -130
  45. package/dist/chunk-yr32cp7h.js +0 -1603
  46. package/dist/chunk-zvwb67nd.js +0 -332
package/dist/index.js CHANGED
@@ -1,11 +1,459 @@
1
- import {
2
- OpacaError,
3
- createClient
4
- } from "./chunk-d3ffeqp9.js";
5
- import {
6
- defineConfig
7
- } from "./chunk-zvwb67nd.js";
8
- import"./chunk-8sqjbsgt.js";
1
+ import { createRequire } from "node:module";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ function __accessProp(key) {
7
+ return this[key];
8
+ }
9
+ var __toCommonJS = (from) => {
10
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
11
+ if (entry)
12
+ return entry;
13
+ entry = __defProp({}, "__esModule", { value: true });
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (var key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(entry, key))
17
+ __defProp(entry, key, {
18
+ get: __accessProp.bind(from, key),
19
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
20
+ });
21
+ }
22
+ __moduleCache.set(from, entry);
23
+ return entry;
24
+ };
25
+ var __moduleCache;
26
+ var __returnValue = (v) => v;
27
+ function __exportSetter(name, newValue) {
28
+ this[name] = __returnValue.bind(null, newValue);
29
+ }
30
+ var __export = (target, all) => {
31
+ for (var name in all)
32
+ __defProp(target, name, {
33
+ get: all[name],
34
+ enumerable: true,
35
+ configurable: true,
36
+ set: __exportSetter.bind(all, name)
37
+ });
38
+ };
39
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
40
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
41
+
42
+ // src/client.ts
43
+ class OpacaError extends Error {
44
+ status;
45
+ data;
46
+ constructor(message, status, data = {}) {
47
+ super(message);
48
+ this.name = "OpacaError";
49
+ this.status = status;
50
+ this.data = data;
51
+ }
52
+ }
53
+ function createClient(configOrOptions) {
54
+ const options = configOrOptions.serverURL ? { baseURL: configOrOptions.serverURL } : configOrOptions;
55
+ const baseURL = options.baseURL.replace(/\/$/, "");
56
+ const getHeaders = () => {
57
+ const headers = {
58
+ "Content-Type": "application/json"
59
+ };
60
+ if (options.token) {
61
+ headers.Authorization = `Bearer ${options.token}`;
62
+ }
63
+ return headers;
64
+ };
65
+ const fetcher = async (path, init) => {
66
+ const res = await fetch(`${baseURL}${path}`, {
67
+ ...init,
68
+ credentials: "include",
69
+ headers: {
70
+ ...getHeaders(),
71
+ ...init?.headers
72
+ }
73
+ });
74
+ if (!res.ok) {
75
+ const errorData = await res.json().catch(() => ({}));
76
+ throw new OpacaError(errorData.message || `API Error: ${res.status}`, res.status, errorData);
77
+ }
78
+ return res.json();
79
+ };
80
+ const client = {
81
+ getMetadata: () => fetcher("/api/__admin/metadata"),
82
+ getCollections: () => fetcher("/api/__admin/collections"),
83
+ getSetupStatus: () => fetcher("/api/__admin/setup"),
84
+ collections: new Proxy({}, {
85
+ get(_, collectionSlug) {
86
+ return {
87
+ find: (query) => {
88
+ const searchParams = new URLSearchParams;
89
+ if (query) {
90
+ Object.entries(query).forEach(([key, value]) => {
91
+ searchParams.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
92
+ });
93
+ }
94
+ const queryString = searchParams.toString();
95
+ return fetcher(`/api/${String(collectionSlug)}${queryString ? `?${queryString}` : ""}`);
96
+ },
97
+ findOne: (id) => fetcher(`/api/${String(collectionSlug)}/${id}`),
98
+ list: () => fetcher(`/api/${String(collectionSlug)}`),
99
+ create: (data) => fetcher(`/api/${String(collectionSlug)}`, {
100
+ method: "POST",
101
+ body: JSON.stringify(data)
102
+ }),
103
+ update: (id, data) => fetcher(`/api/${String(collectionSlug)}/${id}`, {
104
+ method: "PATCH",
105
+ body: JSON.stringify(data)
106
+ }),
107
+ delete: (id) => fetcher(`/api/${String(collectionSlug)}/${id}`, {
108
+ method: "DELETE"
109
+ })
110
+ };
111
+ }
112
+ }),
113
+ globals: new Proxy({}, {
114
+ get(_, globalSlug) {
115
+ return {
116
+ get: () => fetcher(`/api/globals/${String(globalSlug)}`),
117
+ update: (data) => fetcher(`/api/globals/${String(globalSlug)}`, {
118
+ method: "PATCH",
119
+ body: JSON.stringify(data)
120
+ })
121
+ };
122
+ }
123
+ })
124
+ };
125
+ return client;
126
+ }
127
+ // src/validation.ts
128
+ import { z } from "zod";
129
+ var FieldTypeSchema = z.enum([
130
+ "text",
131
+ "slug",
132
+ "textarea",
133
+ "number",
134
+ "richtext",
135
+ "relationship",
136
+ "select",
137
+ "radio",
138
+ "date",
139
+ "boolean",
140
+ "json",
141
+ "file",
142
+ "blocks",
143
+ "group",
144
+ "row",
145
+ "collapsible",
146
+ "tabs",
147
+ "join",
148
+ "array",
149
+ "virtual"
150
+ ]);
151
+ var BaseFieldSchema = z.object({
152
+ name: z.string(),
153
+ label: z.string().optional(),
154
+ required: z.boolean().optional().default(false),
155
+ unique: z.boolean().optional().default(false),
156
+ localized: z.boolean().optional(),
157
+ defaultValue: z.any().optional(),
158
+ validate: z.function().optional(),
159
+ admin: z.object({
160
+ description: z.string().optional(),
161
+ condition: z.function().optional(),
162
+ hidden: z.boolean().optional(),
163
+ readOnly: z.boolean().optional(),
164
+ width: z.string().optional(),
165
+ style: z.any().optional(),
166
+ className: z.string().optional(),
167
+ components: z.object({
168
+ Field: z.string().optional(),
169
+ Cell: z.string().optional()
170
+ }).optional()
171
+ }).optional()
172
+ });
173
+ var FieldSchema = z.lazy(() => z.discriminatedUnion("type", [
174
+ BaseFieldSchema.extend({ type: z.literal("text") }),
175
+ BaseFieldSchema.extend({
176
+ type: z.literal("slug"),
177
+ from: z.string(),
178
+ format: z.function().optional()
179
+ }),
180
+ BaseFieldSchema.extend({ type: z.literal("textarea") }),
181
+ BaseFieldSchema.extend({ type: z.literal("number") }),
182
+ BaseFieldSchema.extend({ type: z.literal("date") }),
183
+ BaseFieldSchema.extend({
184
+ type: z.literal("boolean"),
185
+ options: z.object({
186
+ display: z.enum(["switch", "toggle", "checkbox", "select"]).optional().default("switch")
187
+ }).loose().optional()
188
+ }),
189
+ BaseFieldSchema.extend({
190
+ type: z.literal("richtext"),
191
+ options: z.object({
192
+ defaultMode: z.enum(["simple", "notion"]).optional()
193
+ }).loose().optional()
194
+ }),
195
+ BaseFieldSchema.extend({
196
+ type: z.literal("relationship"),
197
+ relationTo: z.string(),
198
+ hasMany: z.boolean().optional(),
199
+ displayField: z.string().optional(),
200
+ references: z.object({
201
+ table: z.string(),
202
+ column: z.string(),
203
+ onDelete: z.enum(["cascade", "set null", "restrict", "no action"]).optional()
204
+ }).optional()
205
+ }),
206
+ BaseFieldSchema.extend({
207
+ type: z.literal("select"),
208
+ options: z.object({
209
+ choices: z.array(z.union([z.string(), z.object({ label: z.string(), value: z.string() })]))
210
+ }).loose().optional()
211
+ }),
212
+ BaseFieldSchema.extend({
213
+ type: z.literal("radio"),
214
+ options: z.object({
215
+ choices: z.array(z.union([z.string(), z.object({ label: z.string(), value: z.string() })]))
216
+ }).loose()
217
+ }),
218
+ BaseFieldSchema.extend({
219
+ type: z.literal("file"),
220
+ bucket: z.string().optional(),
221
+ options: z.object({
222
+ allowedmime_types: z.array(z.string()).optional(),
223
+ maxFileSize: z.number().optional(),
224
+ metaFields: z.array(z.lazy(() => FieldSchema)).optional()
225
+ }).loose().optional()
226
+ }),
227
+ BaseFieldSchema.extend({
228
+ type: z.literal("json"),
229
+ fields: z.array(z.lazy(() => FieldSchema)).optional()
230
+ }),
231
+ BaseFieldSchema.extend({
232
+ type: z.literal("blocks"),
233
+ blocks: z.array(z.object({
234
+ slug: z.string(),
235
+ fields: z.array(z.lazy(() => FieldSchema)),
236
+ label: z.string().optional()
237
+ }))
238
+ }),
239
+ BaseFieldSchema.extend({
240
+ type: z.literal("group"),
241
+ fields: z.array(z.lazy(() => FieldSchema))
242
+ }),
243
+ z.object({
244
+ name: z.string().optional(),
245
+ type: z.literal("row"),
246
+ fields: z.array(z.lazy(() => FieldSchema)),
247
+ label: z.string().optional()
248
+ }),
249
+ z.object({
250
+ name: z.string().optional(),
251
+ type: z.literal("collapsible"),
252
+ fields: z.array(z.lazy(() => FieldSchema)),
253
+ label: z.string().optional(),
254
+ options: z.object({
255
+ initiallyCollapsed: z.boolean().optional()
256
+ }).loose().optional()
257
+ }),
258
+ z.object({
259
+ name: z.string().optional(),
260
+ type: z.literal("tabs"),
261
+ tabs: z.array(z.object({
262
+ label: z.string(),
263
+ fields: z.array(z.lazy(() => FieldSchema))
264
+ }))
265
+ }),
266
+ z.object({
267
+ name: z.string().optional().refine((val) => val === undefined, {
268
+ message: "Join fields are virtual and should not have a 'name' property. They are mapped dynamically via the API."
269
+ }).or(z.string()),
270
+ type: z.literal("join"),
271
+ collection: z.string(),
272
+ on: z.string(),
273
+ label: z.string().optional()
274
+ }),
275
+ BaseFieldSchema.extend({
276
+ type: z.literal("array"),
277
+ fields: z.array(z.lazy(() => FieldSchema)).optional()
278
+ }),
279
+ BaseFieldSchema.extend({
280
+ type: z.literal("virtual"),
281
+ resolve: z.function(),
282
+ returnType: z.string().optional()
283
+ })
284
+ ]));
285
+ var AccessConfigSchema = z.object({
286
+ read: z.union([z.boolean(), z.function()]).optional(),
287
+ create: z.union([z.boolean(), z.function()]).optional(),
288
+ update: z.union([z.boolean(), z.function()]).optional(),
289
+ delete: z.union([z.boolean(), z.function()]).optional(),
290
+ requireApiKey: z.boolean().optional()
291
+ });
292
+ var CollectionHooksSchema = z.object({
293
+ beforeCreate: z.function().optional(),
294
+ afterCreate: z.function().optional(),
295
+ beforeUpdate: z.function().optional(),
296
+ afterUpdate: z.function().optional(),
297
+ beforeDelete: z.function().optional(),
298
+ afterDelete: z.function().optional()
299
+ });
300
+ var CollectionSchema = z.object({
301
+ slug: z.string(),
302
+ label: z.string().optional(),
303
+ icon: z.string().optional(),
304
+ apiPath: z.string().optional(),
305
+ fields: z.array(FieldSchema),
306
+ timestamps: z.union([
307
+ z.boolean(),
308
+ z.object({
309
+ createdAt: z.string().optional(),
310
+ updatedAt: z.string().optional()
311
+ })
312
+ ]).optional(),
313
+ auth: z.boolean().optional(),
314
+ hooks: CollectionHooksSchema.optional(),
315
+ access: AccessConfigSchema.optional(),
316
+ versions: z.object({
317
+ drafts: z.boolean().optional(),
318
+ maxRevisions: z.number().optional(),
319
+ autosave: z.boolean().optional()
320
+ }).optional(),
321
+ webhooks: z.array(z.object({
322
+ events: z.array(z.string()),
323
+ url: z.string(),
324
+ headers: z.record(z.string(), z.string()).optional()
325
+ })).optional(),
326
+ admin: z.union([
327
+ z.boolean(),
328
+ z.object({
329
+ defaultColumns: z.array(z.string()).optional(),
330
+ useAsTitle: z.string().optional(),
331
+ hidden: z.boolean().optional(),
332
+ disableAdmin: z.boolean().optional(),
333
+ views: z.array(z.object({
334
+ name: z.string(),
335
+ filter: z.record(z.string(), z.any()),
336
+ defaultColumns: z.array(z.string()).optional()
337
+ })).optional(),
338
+ bulkActions: z.array(z.object({
339
+ label: z.string(),
340
+ action: z.function()
341
+ })).optional()
342
+ })
343
+ ]).optional(),
344
+ hidden: z.boolean().optional()
345
+ });
346
+ var GlobalSchema = z.object({
347
+ slug: z.string(),
348
+ label: z.string().optional(),
349
+ icon: z.string().optional(),
350
+ fields: z.array(FieldSchema),
351
+ timestamps: z.union([
352
+ z.boolean(),
353
+ z.object({
354
+ createdAt: z.string().optional(),
355
+ updatedAt: z.string().optional()
356
+ })
357
+ ]).optional(),
358
+ access: AccessConfigSchema.optional()
359
+ });
360
+ var AdminConfigSchema = z.object({
361
+ userCollection: z.string().optional(),
362
+ disableAdmin: z.boolean().optional(),
363
+ route: z.string().optional()
364
+ });
365
+ var OpacaAuthConfigSchema = z.object({
366
+ strategies: z.object({
367
+ emailPassword: z.boolean().optional(),
368
+ magicLink: z.object({
369
+ enabled: z.boolean(),
370
+ sendEmail: z.function()
371
+ }).optional()
372
+ }).optional(),
373
+ socialProviders: z.object({
374
+ github: z.object({
375
+ clientId: z.string(),
376
+ clientSecret: z.string()
377
+ }).optional(),
378
+ google: z.object({
379
+ clientId: z.string(),
380
+ clientSecret: z.string()
381
+ }).optional()
382
+ }).optional(),
383
+ session: z.object({
384
+ expiresInDays: z.number().optional(),
385
+ updateAgeSeconds: z.number().optional()
386
+ }).optional(),
387
+ features: z.object({
388
+ apiKeys: z.object({
389
+ enabled: z.boolean()
390
+ }).optional(),
391
+ mfa: z.object({
392
+ enabled: z.boolean(),
393
+ issuer: z.string()
394
+ }).optional()
395
+ }).optional()
396
+ });
397
+ var ApiConfigSchema = z.object({
398
+ maxLimit: z.number().optional(),
399
+ rateLimit: z.object({
400
+ windowMs: z.number().optional(),
401
+ limit: z.number().optional(),
402
+ enabled: z.boolean().optional(),
403
+ store: z.any().optional(),
404
+ provider: z.function().optional(),
405
+ keyGenerator: z.function().optional()
406
+ }).optional()
407
+ });
408
+ var OpacaConfigSchema = z.object({
409
+ collections: z.array(CollectionSchema),
410
+ globals: z.array(GlobalSchema).optional(),
411
+ db: z.any(),
412
+ storages: z.record(z.string(), z.any()).optional(),
413
+ runMigrationsOnStartup: z.boolean().optional(),
414
+ admin: AdminConfigSchema.optional(),
415
+ serverURL: z.string().optional(),
416
+ appName: z.string().optional(),
417
+ secret: z.string().optional(),
418
+ trustedOrigins: z.union([z.array(z.string()), z.function()]).optional(),
419
+ auth: OpacaAuthConfigSchema.optional(),
420
+ api: ApiConfigSchema.optional(),
421
+ access: z.object({
422
+ roles: z.record(z.string(), z.record(z.string(), z.array(z.string()))).optional()
423
+ }).optional(),
424
+ i18n: z.object({
425
+ locales: z.array(z.string()),
426
+ defaultLocale: z.string()
427
+ }).optional()
428
+ });
429
+
430
+ // src/config.ts
431
+ function recursivelyBuild(obj) {
432
+ if (!obj || typeof obj !== "object")
433
+ return obj;
434
+ if (typeof obj.build === "function") {
435
+ return recursivelyBuild(obj.build());
436
+ }
437
+ if (Array.isArray(obj)) {
438
+ return obj.map(recursivelyBuild);
439
+ }
440
+ if (obj.constructor !== Object) {
441
+ return obj;
442
+ }
443
+ const built = {};
444
+ for (const [key, value] of Object.entries(obj)) {
445
+ built[key] = recursivelyBuild(value);
446
+ }
447
+ return built;
448
+ }
449
+ function defineConfig(config) {
450
+ const builtConfig = recursivelyBuild(config);
451
+ const result = OpacaConfigSchema.safeParse(builtConfig);
452
+ if (!result.success) {
453
+ throw new Error(`Invalid OpacaCMS Configuration: ${result.error.message}`);
454
+ }
455
+ return builtConfig;
456
+ }
9
457
  export {
10
458
  defineConfig,
11
459
  createClient,