@statezero/core 0.1.50 → 0.1.51

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.
@@ -11,7 +11,13 @@ export class FileObject {
11
11
  lastModified: number | null;
12
12
  uploaded: boolean;
13
13
  uploading: boolean;
14
- uploadResult: any;
14
+ uploadResult: {
15
+ file_path: any;
16
+ file_name: any;
17
+ file_url: any;
18
+ size: any;
19
+ mime_type: any;
20
+ } | null;
15
21
  uploadError: any;
16
22
  fileData: any;
17
23
  uploadType: string | null;
@@ -34,11 +40,23 @@ export class FileObject {
34
40
  /**
35
41
  * Handle single file upload
36
42
  */
37
- _singleUpload(file: any, uploadData: any, options: any): Promise<any>;
43
+ _singleUpload(file: any, uploadData: any, options: any): Promise<{
44
+ file_path: any;
45
+ file_name: any;
46
+ file_url: any;
47
+ size: any;
48
+ mime_type: any;
49
+ } | null>;
38
50
  /**
39
51
  * Handle multipart upload with concurrency using p-queue
40
52
  */
41
- _multipartUpload(file: any, uploadData: any, options: any): Promise<any>;
53
+ _multipartUpload(file: any, uploadData: any, options: any): Promise<{
54
+ file_path: any;
55
+ file_name: any;
56
+ file_url: any;
57
+ size: any;
58
+ mime_type: any;
59
+ } | null>;
42
60
  /**
43
61
  * Create file chunks for multipart upload
44
62
  */
@@ -46,7 +64,13 @@ export class FileObject {
46
64
  /**
47
65
  * Complete the upload (both single and multipart)
48
66
  */
49
- _completeUpload(filePath: any, originalName: any, uploadId?: null, parts?: null): Promise<any>;
67
+ _completeUpload(filePath: any, originalName: any, uploadId?: null, parts?: null): Promise<{
68
+ file_path: any;
69
+ file_name: any;
70
+ file_url: any;
71
+ size: any;
72
+ mime_type: any;
73
+ } | null>;
50
74
  /**
51
75
  * Direct upload to Django backend (original method)
52
76
  */
@@ -60,24 +84,11 @@ export class FileObject {
60
84
  */
61
85
  getBlob(): Blob;
62
86
  waitForUpload(): Promise<any>;
63
- /**
64
- * Returns a static version of this FileObject that can be stored in IndexedDB
65
- * Creates a new FileObject using the stored file constructor path
66
- */
67
- asStatic(): any;
68
87
  toJSON(): {
69
- name: any;
70
- size: any;
71
- type: any;
72
- status: string;
73
- uploaded: boolean;
74
88
  filePath: any;
89
+ fileName: any;
75
90
  fileUrl: any;
76
- uploadResult: any;
77
- uploadError: string | null;
78
- uploadType: string | null;
79
- uploadId: any;
80
- totalChunks: number;
81
- completedChunks: number;
91
+ size: any;
92
+ mimeType: any;
82
93
  };
83
94
  }
@@ -10,17 +10,24 @@ export class FileObject {
10
10
  // Handle stored file data (from API)
11
11
  if (file &&
12
12
  typeof file === "object" &&
13
- file.file_path &&
13
+ (file.file_path || file.filePath) && // Handle both formats
14
14
  !(file instanceof File)) {
15
- // This is stored file data from the backend
16
- this.name = file.file_name;
15
+ // Handle both snake_case (from backend) and camelCase (from toJSON)
16
+ this.name = file.file_name || file.fileName;
17
17
  this.size = file.size;
18
- this.type = file.mime_type; // Now coming from backend
18
+ this.type = file.mime_type || file.mimeType;
19
19
  this.lastModified = null;
20
20
  // Mark as already uploaded
21
21
  this.uploaded = true;
22
22
  this.uploading = false;
23
- this.uploadResult = file; // Store the entire response
23
+ // Store the entire response, normalizing format
24
+ this.uploadResult = {
25
+ file_path: file.file_path || file.filePath,
26
+ file_name: file.file_name || file.fileName,
27
+ file_url: file.file_url || file.fileUrl,
28
+ size: file.size,
29
+ mime_type: file.mime_type || file.mimeType,
30
+ };
24
31
  this.uploadError = null;
25
32
  this.fileData = null;
26
33
  // No upload properties needed
@@ -30,7 +37,7 @@ export class FileObject {
30
37
  this.completedChunks = 0;
31
38
  this.chunkSize = null;
32
39
  this.maxConcurrency = null;
33
- this.uploadPromise = null;
40
+ this.uploadPromise = Promise.resolve(this.uploadResult);
34
41
  return;
35
42
  }
36
43
  // Handle File objects (for upload) - existing code
@@ -59,6 +66,13 @@ export class FileObject {
59
66
  `Provided: ${this.chunkSize / (1024 * 1024)}MB`);
60
67
  }
61
68
  this.maxConcurrency = options.maxConcurrency || 3;
69
+ // This is to make the fileObject serializable by IDB in the cache
70
+ Object.defineProperty(this, "uploadPromise", {
71
+ value: Promise.resolve(this.uploadResult),
72
+ writable: true,
73
+ enumerable: false,
74
+ configurable: true,
75
+ });
62
76
  this.uploadPromise = this._initializeAndStartUpload(file, options);
63
77
  }
64
78
  get isStoredFile() {
@@ -88,7 +102,6 @@ export class FileObject {
88
102
  return this.filePath;
89
103
  }
90
104
  else if (status === "failed") {
91
- // Fix: use "failed" instead of "error"
92
105
  throw new Error(`Cannot use FileObject in query - upload failed: ${this.uploadError}`);
93
106
  }
94
107
  else if (status === "uploading") {
@@ -367,39 +380,15 @@ export class FileObject {
367
380
  return new Blob([this.fileData], { type: this.type });
368
381
  }
369
382
  async waitForUpload() {
370
- if (this.uploaded && this.uploadResult && !this.uploadPromise) {
371
- return this.uploadResult;
372
- }
373
- if (this.uploadPromise) {
374
- return this.uploadPromise;
375
- }
376
- throw new Error("No upload in progress and file not uploaded");
377
- }
378
- /**
379
- * Returns a static version of this FileObject that can be stored in IndexedDB
380
- * Creates a new FileObject using the stored file constructor path
381
- */
382
- asStatic() {
383
- if (!this.uploaded || !this.uploadResult) {
384
- throw new Error("Cannot create static version of file that hasn't been uploaded");
385
- }
386
- return new this.constructor(this.uploadResult);
383
+ return this.uploadPromise;
387
384
  }
388
385
  toJSON() {
389
386
  return {
390
- name: this.name,
391
- size: this.size,
392
- type: this.type,
393
- status: this.status,
394
- uploaded: this.uploaded,
395
387
  filePath: this.filePath,
388
+ fileName: this.name,
396
389
  fileUrl: this.fileUrl,
397
- uploadResult: this.uploadResult,
398
- uploadError: this.uploadError ? String(this.uploadError) : null,
399
- uploadType: this.uploadType,
400
- uploadId: this.uploadId,
401
- totalChunks: this.totalChunks,
402
- completedChunks: this.completedChunks,
390
+ size: this.size,
391
+ mimeType: this.type,
403
392
  };
404
393
  }
405
394
  }
@@ -101,16 +101,15 @@ export class Model {
101
101
  // Let DateParsingHelpers.parseDate throw if it fails
102
102
  return DateParsingHelpers.parseDate(value, field, ModelClass.schema);
103
103
  }
104
- // File/Image fields need special handling - wrap as FileObject
105
104
  const fileFormats = ["file-path", "image-path"];
106
105
  if (ModelClass.schema &&
107
106
  fileFormats.includes(ModelClass.schema.properties[field]?.format) &&
108
107
  value) {
109
- // Check if it's already a FileObject
108
+ // If it's already a FileObject, serialize it
110
109
  if (value instanceof FileObject) {
111
- return value.asStatic();
110
+ return value.toJSON();
112
111
  }
113
- // If it's stored file data from API, wrap it as FileObject
112
+ // If it's stored file data from API, create FileObject and serialize
114
113
  if (typeof value === "object" && value.file_path) {
115
114
  // Create anonymous subclass with correct configKey
116
115
  const BackendFileObject = (_a = class extends FileObject {
@@ -118,7 +117,12 @@ export class Model {
118
117
  __setFunctionName(_a, "BackendFileObject"),
119
118
  _a.configKey = ModelClass.configKey,
120
119
  _a);
121
- return new BackendFileObject(value).asStatic();
120
+ const fileObj = new BackendFileObject(value);
121
+ return fileObj.toJSON(); // Get the computed fileUrl, etc.
122
+ }
123
+ // If it's just a file path string, return as-is
124
+ if (typeof value === "string") {
125
+ return value;
122
126
  }
123
127
  }
124
128
  // relationship fields need special handling
@@ -211,13 +215,6 @@ export class Model {
211
215
  // Let DateParsingHelpers.serializeDate throw if it fails
212
216
  return DateParsingHelpers.serializeDate(value, field, ModelClass.schema);
213
217
  }
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
- }
221
218
  return value;
222
219
  }
223
220
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
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",