@statezero/core 0.1.89 → 0.1.91

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.
@@ -220,9 +220,10 @@ function createOperatorFromLookup(field, lookup, value, isRelationship, ModelCla
220
220
  if (isRelationship) {
221
221
  // For relationship fields with lookups, we need special handling
222
222
  if (lookup === 'isnull') {
223
+ // Check for both undefined and null values
223
224
  return {
224
225
  field,
225
- operator: value ? { $exists: false } : { $exists: true }
226
+ operator: value ? { $in: [null, undefined] } : { $nin: [null, undefined] }
226
227
  };
227
228
  }
228
229
  else if (lookup === 'in') {
@@ -244,9 +245,10 @@ function createOperatorFromLookup(field, lookup, value, isRelationship, ModelCla
244
245
  }
245
246
  // Regular field lookups (same as in the original code)
246
247
  if (lookup === 'isnull') {
248
+ // Check for both undefined and null values
247
249
  return {
248
250
  field,
249
- operator: value ? { $exists: false } : { $exists: true }
251
+ operator: value ? { $in: [null, undefined] } : { $nin: [null, undefined] }
250
252
  };
251
253
  }
252
254
  else if (lookup === 'exact') {
@@ -43,6 +43,16 @@ export class Model {
43
43
  * @returns {Promise<boolean>} Promise that resolves to true if valid, throws error if invalid
44
44
  */
45
45
  static validate(data: Object, validateType?: string, partial?: boolean): Promise<boolean>;
46
+ /**
47
+ * Get field permissions for the current user (cached on the class)
48
+ * @param {boolean} refresh - Force refresh the cached permissions
49
+ * @returns {Promise<{visible_fields: string[], creatable_fields: string[], editable_fields: string[]}>}
50
+ */
51
+ static getFieldPermissions(refresh?: boolean): Promise<{
52
+ visible_fields: string[];
53
+ creatable_fields: string[];
54
+ editable_fields: string[];
55
+ }>;
46
56
  constructor(data?: {});
47
57
  serializer: ModelSerializer;
48
58
  _data: {};
@@ -299,6 +299,50 @@ export class Model {
299
299
  throw new Error(`Validation failed: ${error.message}`);
300
300
  }
301
301
  }
302
+ /**
303
+ * Get field permissions for the current user (cached on the class)
304
+ * @param {boolean} refresh - Force refresh the cached permissions
305
+ * @returns {Promise<{visible_fields: string[], creatable_fields: string[], editable_fields: string[]}>}
306
+ */
307
+ static async getFieldPermissions(refresh = false) {
308
+ const ModelClass = this;
309
+ // Return cached permissions if available and not forcing refresh
310
+ if (!refresh && ModelClass._fieldPermissionsCache) {
311
+ return ModelClass._fieldPermissionsCache;
312
+ }
313
+ // Get backend config and check if it exists
314
+ const config = configInstance.getConfig();
315
+ const backend = config.backendConfigs[ModelClass.configKey];
316
+ if (!backend) {
317
+ throw new Error(`No backend configuration found for key: ${ModelClass.configKey}`);
318
+ }
319
+ // Build URL for field permissions endpoint
320
+ const baseUrl = backend.API_URL.replace(/\/+$/, "");
321
+ const url = `${baseUrl}/${ModelClass.modelName}/field-permissions/`;
322
+ // Prepare headers
323
+ const headers = {
324
+ "Content-Type": "application/json",
325
+ ...(backend.getAuthHeaders ? backend.getAuthHeaders() : {}),
326
+ };
327
+ // Make direct API call to field permissions endpoint
328
+ try {
329
+ const response = await axios.get(url, { headers });
330
+ // Cache the permissions on the class
331
+ ModelClass._fieldPermissionsCache = response.data;
332
+ // Backend returns {visible_fields: [], creatable_fields: [], editable_fields: []}
333
+ return response.data;
334
+ }
335
+ catch (error) {
336
+ if (error.response && error.response.data) {
337
+ const parsedError = parseStateZeroError(error.response.data);
338
+ if (Error.captureStackTrace) {
339
+ Error.captureStackTrace(parsedError, ModelClass.getFieldPermissions);
340
+ }
341
+ throw parsedError;
342
+ }
343
+ throw new Error(`Failed to get field permissions: ${error.message}`);
344
+ }
345
+ }
302
346
  }
303
347
  /**
304
348
  * Creates a new Model instance.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.89",
3
+ "version": "0.1.91",
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",