@statezero/core 0.1.7 → 0.1.9

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.
package/dist/config.d.ts CHANGED
@@ -29,6 +29,10 @@ export function initializeAllEventReceivers(): Object;
29
29
  * @param {Function} getterFn - The getModelClass function imported from model-registry.js
30
30
  */
31
31
  export function registerModelGetter(getterFn: Function): void;
32
+ /**
33
+ * Helper function to build file URLs
34
+ */
35
+ export function buildFileUrl(fileUrl: any, backendKey?: string): any;
32
36
  /**
33
37
  * Get a model class by name using the registered getter.
34
38
  *
@@ -47,6 +51,7 @@ export namespace configInstance {
47
51
  export { initializeEventReceiver };
48
52
  export { registerModelGetter };
49
53
  export { getModelClass };
54
+ export { buildFileUrl };
50
55
  }
51
56
  export default configInstance;
52
57
  import { PusherEventReceiver } from './core/eventReceivers.js';
package/dist/config.js CHANGED
@@ -47,6 +47,7 @@ const backendSchema = z.object({
47
47
  API_URL: z.string().url('API_URL must be a valid URL'),
48
48
  GENERATED_TYPES_DIR: z.string({ required_error: 'GENERATED_TYPES_DIR is required' }),
49
49
  BACKEND_TZ: z.string().optional(),
50
+ fileRootURL: z.string().url('fileRootURL must be a valid URL').optional(),
50
51
  fileUploadMode: z.enum(['server', 's3']).default('server'),
51
52
  getAuthHeaders: z.function().optional()
52
53
  .refine((fn) => fn === undefined || typeof fn === 'function', 'getAuthHeaders must be a function if provided'),
@@ -212,6 +213,26 @@ export function registerModelGetter(getterFn) {
212
213
  }
213
214
  modelGetter = getterFn;
214
215
  }
216
+ /**
217
+ * Helper function to build file URLs
218
+ */
219
+ export function buildFileUrl(fileUrl, backendKey = 'default') {
220
+ // If URL is already absolute (cloud storage), use it as-is
221
+ if (fileUrl && fileUrl.startsWith('http')) {
222
+ return fileUrl;
223
+ }
224
+ const cfg = getConfig();
225
+ const backend = cfg.backendConfigs[backendKey];
226
+ if (!backend) {
227
+ throw new ConfigError(`Backend "${backendKey}" not found in configuration.`);
228
+ }
229
+ // If no fileRootURL provided, return relative URL as-is
230
+ if (!backend.fileRootURL) {
231
+ return fileUrl;
232
+ }
233
+ // Construct full URL
234
+ return backend.fileRootURL.replace(/\/$/, '') + fileUrl;
235
+ }
215
236
  /**
216
237
  * Get a model class by name using the registered getter.
217
238
  *
@@ -237,6 +258,7 @@ export const configInstance = {
237
258
  setBackendConfig,
238
259
  initializeEventReceiver,
239
260
  registerModelGetter,
240
- getModelClass
261
+ getModelClass,
262
+ buildFileUrl
241
263
  };
242
264
  export default configInstance;
@@ -5,10 +5,10 @@ export class FileObject {
5
5
  static configKey: string;
6
6
  static MIN_CHUNK_SIZE: number;
7
7
  constructor(file: any, options?: {});
8
- name: string;
9
- size: number;
10
- type: string;
11
- lastModified: number;
8
+ name: any;
9
+ size: any;
10
+ type: any;
11
+ lastModified: number | null;
12
12
  uploaded: boolean;
13
13
  uploading: boolean;
14
14
  uploadResult: any;
@@ -21,6 +21,7 @@ export class FileObject {
21
21
  chunkSize: any;
22
22
  maxConcurrency: any;
23
23
  uploadPromise: any;
24
+ get isStoredFile(): any;
24
25
  get status(): "failed" | "uploading" | "uploaded" | "pending";
25
26
  get filePath(): any;
26
27
  get fileUrl(): any;
@@ -59,9 +60,9 @@ export class FileObject {
59
60
  getBlob(): Blob;
60
61
  waitForUpload(): Promise<any>;
61
62
  toJSON(): {
62
- name: string;
63
- size: number;
64
- type: string;
63
+ name: any;
64
+ size: any;
65
+ type: any;
65
66
  status: string;
66
67
  uploaded: boolean;
67
68
  filePath: any;
@@ -5,9 +5,37 @@ import PQueue from "p-queue";
5
5
  * FileObject - A file wrapper that handles uploads to StateZero backend
6
6
  */
7
7
  export class FileObject {
8
+ // Simple changes to FileObject constructor
8
9
  constructor(file, options = {}) {
10
+ // Handle stored file data (from API)
11
+ if (file &&
12
+ typeof file === "object" &&
13
+ file.file_path &&
14
+ !(file instanceof File)) {
15
+ // This is stored file data from the backend
16
+ this.name = file.file_name;
17
+ this.size = file.size;
18
+ this.type = file.mime_type; // Now coming from backend
19
+ this.lastModified = null;
20
+ // Mark as already uploaded
21
+ this.uploaded = true;
22
+ this.uploading = false;
23
+ this.uploadResult = file; // Store the entire response
24
+ this.uploadError = null;
25
+ this.fileData = null;
26
+ // No upload properties needed
27
+ this.uploadType = null;
28
+ this.uploadId = null;
29
+ this.totalChunks = 0;
30
+ this.completedChunks = 0;
31
+ this.chunkSize = null;
32
+ this.maxConcurrency = null;
33
+ this.uploadPromise = Promise.resolve(this.uploadResult);
34
+ return;
35
+ }
36
+ // Handle File objects (for upload) - existing code
9
37
  if (!file || !(file instanceof File)) {
10
- throw new Error("FileObject requires a File object");
38
+ throw new Error("FileObject requires a File object or stored file data");
11
39
  }
12
40
  // Store file metadata directly
13
41
  this.name = file.name;
@@ -33,6 +61,9 @@ export class FileObject {
33
61
  this.maxConcurrency = options.maxConcurrency || 3;
34
62
  this.uploadPromise = this._initializeAndStartUpload(file, options);
35
63
  }
64
+ get isStoredFile() {
65
+ return this.uploaded && !this.fileData && this.uploadResult?.file_path;
66
+ }
36
67
  get status() {
37
68
  if (this.uploadError)
38
69
  return "failed";
@@ -46,7 +77,10 @@ export class FileObject {
46
77
  return this.uploadResult?.file_path;
47
78
  }
48
79
  get fileUrl() {
49
- return this.uploadResult?.file_url;
80
+ if (!this.uploadResult?.file_url) {
81
+ return null;
82
+ }
83
+ return configInstance.buildFileUrl(this.uploadResult.file_url, this.constructor.configKey);
50
84
  }
51
85
  async _initializeAndStartUpload(file, options) {
52
86
  const config = configInstance.getConfig();
@@ -83,6 +83,17 @@ export class Model {
83
83
  if (storedValue)
84
84
  value = storedValue[field]; // if stops null -> undefined
85
85
  }
86
+ // File fields need special handling - wrap as FileObject
87
+ if (ModelClass.fileFields && ModelClass.fileFields.has(field) && value) {
88
+ // Check if it's already a FileObject
89
+ if (value instanceof FileObject) {
90
+ return value;
91
+ }
92
+ // If it's stored file data from API, wrap it as FileObject
93
+ if (typeof value === "object" && value.file_path) {
94
+ return new FileObject(value);
95
+ }
96
+ }
86
97
  // relationship fields need special handling
87
98
  if (ModelClass.relationshipFields.has(field) && value) {
88
99
  // fetch the stored value
@@ -48,8 +48,8 @@ export class MaxStrategy extends MetricCalculationStrategy {
48
48
  * Factory class for creating the appropriate strategy
49
49
  */
50
50
  export class MetricStrategyFactory {
51
- static "__#7@#customStrategies": Map<any, any>;
52
- static "__#7@#defaultStrategies": Map<string, () => CountStrategy>;
51
+ static "__#5@#customStrategies": Map<any, any>;
52
+ static "__#5@#defaultStrategies": Map<string, () => CountStrategy>;
53
53
  /**
54
54
  * Clear all custom strategy overrides
55
55
  */
@@ -61,7 +61,7 @@ export class MetricStrategyFactory {
61
61
  * @param {Function} ModelClass - The model class
62
62
  * @returns {string} A unique key
63
63
  */
64
- private static "__#7@#generateStrategyKey";
64
+ private static "__#5@#generateStrategyKey";
65
65
  /**
66
66
  * Override a strategy for a specific metric type and model class
67
67
  * @param {string} metricType - The type of metric (count, sum, min, max)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
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",