mango-cms 0.2.43 → 0.2.45

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.
@@ -25,7 +25,7 @@
25
25
  "dayjs": "^1.10.7",
26
26
  "express": "^4.18.1",
27
27
  "google-maps": "^4.3.3",
28
- "mango-cms": "^0.2.43",
28
+ "mango-cms": "^0.2.45",
29
29
  "mapbox-gl": "^2.7.0",
30
30
  "sweetalert2": "^11.4.0",
31
31
  "vite": "^6.2.2",
@@ -39,6 +39,54 @@ function getQuery(params) {
39
39
  return query
40
40
  }
41
41
 
42
+ const processFields = (fields, collectionName) => {
43
+ const result = {}
44
+ for (const field of fields) {
45
+ result[field.name] = {
46
+ ...field
47
+ }
48
+
49
+ // Reconstruct validator function from string if it exists
50
+ if (field.validator) {
51
+ try {
52
+ // eslint-disable-next-line no-new-func
53
+ const validatorFn = new Function('return ' + field.validator)()
54
+
55
+ // Wrap in try-catch for runtime safety
56
+ result[field.name].validate = (value) => {
57
+ try {
58
+ return validatorFn(value)
59
+ } catch (e) {
60
+ console.warn(`Validator for ${collectionName}.${field.name} failed (likely due to missing server-side dependencies):`, e.message)
61
+ // Server-only validator - can't validate on client
62
+ return { valid: null, serverOnly: true, response: 'Validation only available server-side' }
63
+ }
64
+ }
65
+ } catch (e) {
66
+ console.warn(`Failed to reconstruct validator for ${collectionName}.${field.name}:`, e)
67
+ // Failed to reconstruct - assume server-only
68
+ result[field.name].validate = () => ({
69
+ valid: null,
70
+ serverOnly: true,
71
+ response: 'Validation only available server-side'
72
+ })
73
+ }
74
+ } else {
75
+ // No validator defined - always valid
76
+ result[field.name].validate = () => ({
77
+ valid: true,
78
+ response: ''
79
+ })
80
+ }
81
+
82
+ // Process nested fields recursively
83
+ if (field.fields && field.fields.length) {
84
+ result[field.name].fields = processFields(field.fields, collectionName)
85
+ }
86
+ }
87
+ return result
88
+ }
89
+
42
90
  const Mango = collections.reduce((a, c) => {
43
91
  let localDB = new LocalDB(c.name, api)
44
92
 
@@ -171,7 +219,7 @@ const Mango = collections.reduce((a, c) => {
171
219
  return new Promise((resolve, reject) => {
172
220
  axios[method](`${api}/${c.name}/${id || ''}`, payload, { headers })
173
221
  .then((response) => resolve(response?.data?.response))
174
- .catch((e) => reject(e))
222
+ .catch((e) => resolve(e?.response?.data))
175
223
  })
176
224
  }
177
225
 
@@ -193,6 +241,10 @@ const Mango = collections.reduce((a, c) => {
193
241
  })
194
242
  }
195
243
 
244
+ let validate = async (data) => {
245
+ return await mangoSave(data, { validate: true })
246
+ }
247
+
196
248
  let sync = () => {
197
249
  let remainingEntries = ref([])
198
250
  let syncedEntries = ref([])
@@ -277,10 +329,15 @@ const Mango = collections.reduce((a, c) => {
277
329
 
278
330
  a[c.name] = runQuery
279
331
  a[c.name]['save'] = save
332
+ a[c.name]['validate'] = validate
280
333
  a[c.name]['delete'] = deleteEntry
281
334
  a[c.name]['subscribe'] = subscribe
335
+ a[c.name]['fields'] = c.fields ? processFields(c.fields, c.name) : {}
336
+
282
337
  a[c.singular] = (id, query) => runQuery({ id, ...query })
283
338
  a[c.singular]['save'] = save
339
+ a[c.singular]['validate'] = validate
340
+ a[c.singular]['fields'] = a[c.name]['fields']
284
341
  a[c.singular]['subscribe'] = (id, callback, message) => {
285
342
  if (!id) return console.error('No id provided')
286
343
  return subscribe(id, message, callback)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mango-cms",
3
- "version": "0.2.43",
3
+ "version": "0.2.45",
4
4
  "main": "./index.js",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -16,7 +16,7 @@
16
16
  "author": "Colton Neifert",
17
17
  "license": "Commercial",
18
18
  "bin": {
19
- "mango": "./cli.js"
19
+ "mango": "cli.js"
20
20
  },
21
21
  "scripts": {
22
22
  "postinstall": "",