@statezero/core 0.1.90 → 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.
|
@@ -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