@statezero/core 0.1.45 → 0.1.46

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.
@@ -37,6 +37,51 @@ export function processIncludedEntities(modelStoreRegistry, included, ModelClass
37
37
  throw new Error(`Failed to process included entities: ${error.message}`);
38
38
  }
39
39
  }
40
+ /**
41
+ * Recursively processes an object to replace FileObject instances with their file paths.
42
+ * Throws an error if any FileObject is not yet uploaded.
43
+ *
44
+ * @param {any} obj - The object to process
45
+ * @returns {any} The processed object with FileObjects replaced by paths
46
+ */
47
+ function processFileObjects(obj) {
48
+ if (obj === null || obj === undefined) {
49
+ return obj;
50
+ }
51
+ // Handle FileObject instances
52
+ if (obj instanceof FileObject) {
53
+ const status = obj.status;
54
+ if (status === 'uploaded' && obj.filePath) {
55
+ return obj.filePath;
56
+ }
57
+ else if (status === 'error') {
58
+ throw new Error(`Cannot use FileObject in query - upload failed: ${obj.uploadError}`);
59
+ }
60
+ else if (status === 'uploading') {
61
+ throw new Error(`Cannot use FileObject in query - file is still uploading. Wait for upload to complete before executing the query.`);
62
+ }
63
+ else if (status === 'pending') {
64
+ throw new Error(`Cannot use FileObject in query - file upload has not started yet.`);
65
+ }
66
+ else {
67
+ throw new Error(`Cannot use FileObject in query - unexpected status: ${status}`);
68
+ }
69
+ }
70
+ // Handle arrays
71
+ if (Array.isArray(obj)) {
72
+ return obj.map(item => processFileObjects(item));
73
+ }
74
+ // Handle plain objects
75
+ if (typeof obj === 'object' && obj.constructor === Object) {
76
+ const processedObj = {};
77
+ for (const [key, value] of Object.entries(obj)) {
78
+ processedObj[key] = processFileObjects(value);
79
+ }
80
+ return processedObj;
81
+ }
82
+ // Return primitive values as-is
83
+ return obj;
84
+ }
40
85
  /**
41
86
  * Makes an API call to the backend with the given QuerySet.
42
87
  * Automatically handles FileObject replacement with file paths for write operations.
@@ -84,6 +129,15 @@ export async function makeApiCall(querySet, operationType, args = {}, operationI
84
129
  "get_or_create", "update_or_create"
85
130
  ];
86
131
  const isWriteOperation = writeOperations.includes(operationType);
132
+ // Process FileObjects for write operations
133
+ if (isWriteOperation) {
134
+ try {
135
+ payload = processFileObjects(payload);
136
+ }
137
+ catch (error) {
138
+ throw new Error(`Failed to process file uploads: ${error.message}`);
139
+ }
140
+ }
87
141
  const baseUrl = backend.API_URL.replace(/\/+$/, "");
88
142
  const finalUrl = `${baseUrl}/${ModelClass.modelName}/`;
89
143
  const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {};
@@ -192,28 +192,15 @@ export class Model {
192
192
  */
193
193
  serializeField(field) {
194
194
  const ModelClass = this.constructor;
195
- const value = this.getField(field);
196
- // File/Image fields need special handling - extract file_path from FileObject
197
- const fileFormats = ["file-path", "image-path"];
198
- if (ModelClass.schema &&
199
- fileFormats.includes(ModelClass.schema.properties[field]?.format) &&
200
- value instanceof FileObject) {
201
- const status = value.status;
202
- if (status === "uploaded" && value.filePath) {
203
- return value.filePath;
204
- }
205
- else if (status === "error") {
206
- throw new Error(`Cannot serialize FileObject for field '${field}' - upload failed: ${value.uploadError}`);
207
- }
208
- else if (status === "uploading") {
209
- throw new Error(`Cannot serialize FileObject for field '${field}' - file is still uploading`);
210
- }
211
- else if (status === "pending") {
212
- throw new Error(`Cannot serialize FileObject for field '${field}' - file upload has not started`);
213
- }
214
- else {
215
- throw new Error(`Cannot serialize FileObject for field '${field}' - unexpected status: ${status}`);
216
- }
195
+ if (ModelClass.primaryKeyField === field)
196
+ return this._pk;
197
+ // check local overrides
198
+ let value = this._data[field];
199
+ // if it's not been overridden, get it from the store
200
+ if (value === undefined && !isNil(this._pk)) {
201
+ let storedValue = modelStoreRegistry.getEntity(ModelClass, this._pk);
202
+ if (storedValue)
203
+ value = storedValue[field];
217
204
  }
218
205
  // Date/DateTime fields need special handling - convert Date objects to strings for API
219
206
  const dateFormats = ["date", "date-time"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.45",
3
+ "version": "0.1.46",
4
4
  "type": "module",
5
5
  "module": "ESNext",
6
6
  "description": "The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate",