@statezero/core 0.1.46 → 0.1.48

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,6 +25,7 @@ export class FileObject {
25
25
  get status(): "failed" | "uploading" | "uploaded" | "pending";
26
26
  get filePath(): any;
27
27
  get fileUrl(): any;
28
+ serialize(): any;
28
29
  _initializeAndStartUpload(file: any, options: any): any;
29
30
  /**
30
31
  * Fast upload using S3 presigned URLs with multipart support
@@ -82,6 +82,25 @@ export class FileObject {
82
82
  }
83
83
  return configInstance.buildFileUrl(this.uploadResult.file_url, this.constructor.configKey);
84
84
  }
85
+ serialize() {
86
+ const status = this.status;
87
+ if (status === "uploaded" && this.filePath) {
88
+ return this.filePath;
89
+ }
90
+ else if (status === "failed") {
91
+ // Fix: use "failed" instead of "error"
92
+ throw new Error(`Cannot use FileObject in query - upload failed: ${this.uploadError}`);
93
+ }
94
+ else if (status === "uploading") {
95
+ throw new Error(`Cannot use FileObject in query - file is still uploading. Wait for upload to complete before executing the query.`);
96
+ }
97
+ else if (status === "pending") {
98
+ throw new Error(`Cannot use FileObject in query - file upload has not started yet.`);
99
+ }
100
+ else {
101
+ throw new Error(`Cannot use FileObject in query - unexpected status: ${status}`);
102
+ }
103
+ }
85
104
  async _initializeAndStartUpload(file, options) {
86
105
  const config = configInstance.getConfig();
87
106
  const backend = config.backendConfigs?.[this.constructor.configKey];
@@ -37,51 +37,6 @@ 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
- }
85
40
  /**
86
41
  * Makes an API call to the backend with the given QuerySet.
87
42
  * Automatically handles FileObject replacement with file paths for write operations.
@@ -129,15 +84,6 @@ export async function makeApiCall(querySet, operationType, args = {}, operationI
129
84
  "get_or_create", "update_or_create"
130
85
  ];
131
86
  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
- }
141
87
  const baseUrl = backend.API_URL.replace(/\/+$/, "");
142
88
  const finalUrl = `${baseUrl}/${ModelClass.modelName}/`;
143
89
  const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {};
@@ -13,6 +13,7 @@ import { FileObject } from './files.js';
13
13
  import { configInstance } from "../../config.js";
14
14
  import { parseStateZeroError, MultipleObjectsReturned, DoesNotExist, } from "./errors.js";
15
15
  import axios from "axios";
16
+ import { typeOf } from "mathjs";
16
17
  /**
17
18
  * A constructor for a Model.
18
19
  *
@@ -210,6 +211,13 @@ export class Model {
210
211
  // Let DateParsingHelpers.serializeDate throw if it fails
211
212
  return DateParsingHelpers.serializeDate(value, field, ModelClass.schema);
212
213
  }
214
+ // FileObjects need special
215
+ const fileFormats = ["file-path", "image-path"];
216
+ if (ModelClass.schema &&
217
+ fileFormats.includes(ModelClass.schema.properties[field]?.format)) {
218
+ value = this.getField(field);
219
+ return value ? value.serialize() : null;
220
+ }
213
221
  return value;
214
222
  }
215
223
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
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",