Setting the file. One moment.
Cms Collection Request · Wix Replatform · wix/skills · Skills Docs
ContentsBack to the top of the page lib/ cms-collection-request.js
JavaScript · 84 lines · 4 KB
freeze
({ read:
'ADMIN'
, insert:
'ADMIN'
, update:
'ADMIN'
, remove:
'ADMIN'
}),
10 });
11
12 function valueKind ( value ) {
13 if (Array. isArray (value)) return 'array' ;
14 if (value === null ) return 'null' ;
15 return typeof value;
16 }
17
18 function invalidCollectionRequest ( path , errors ) {
19 const message = `CMS collection request invalid: ${ errors . join ( '; ' ) }` ;
20 return Object. assign ( new Error (message), {
21 code: 'CMS_COLLECTION_REQUEST_INVALID' ,
22 blockerCode: 'CMS_COLLECTION_REQUEST_INVALID' ,
23 recommendedAction: 'Regenerate the CMS collection requirement with an object-shaped permissions and field definition, then re-run setup.' ,
24 fields: errors. map (( error ) => `${ path || 'collection'}: ${ error }` ),
25 });
26 }
27
28 function normalizePermissions ( value , path ) {
29 if ( typeof value === 'string' ) {
30 const preset = CMS_PERMISSION_PRESETS [value];
31 if (preset) return { ... preset };
32 throw invalidCollectionRequest (path, [ `${ path }: expected an object or known preset "migration-internal", received string "${ value }"` ]);
33 }
34 if ( ! value || typeof value !== 'object' || Array. isArray (value)) {
35 throw invalidCollectionRequest (path, [ `${ path }: expected an object with read, insert, update and remove, received ${ valueKind ( value ) }` ]);
36 }
37 const missing = COLLECTION_PERMISSION_KEYS . filter (( key ) => typeof value[key] !== 'string' || ! value[key]. trim ());
38 if (missing. length ) {
39 throw invalidCollectionRequest (path, [ `${ path }: missing or invalid permission field(s): ${ missing . join ( ', ' ) }` ]);
40 }
41 return Object. fromEntries ( COLLECTION_PERMISSION_KEYS . map (( key ) => [key, value[key]]));
42 }
43
44 function normalizeField ( field , path ) {
45 if ( ! field || typeof field !== 'object' || Array. isArray (field)) {
46 throw invalidCollectionRequest (path, [ `${ path }: expected an object, received ${ valueKind ( field ) }` ]);
47 }
48 if ( typeof field.key !== 'string' || ! field.key. trim ()) {
49 throw invalidCollectionRequest (path, [ `${ path }.key: expected a non-empty string` ]);
50 }
51 if ( typeof field.type !== 'string' || ! field.type. trim ()) {
52 throw invalidCollectionRequest (path, [ `${ path }.type: expected a non-empty string` ]);
53 }
54 if (field.displayName !== undefined && typeof field.displayName !== 'string' ) {
55 throw invalidCollectionRequest (path, [ `${ path }.displayName: expected a string, received ${ valueKind ( field . displayName ) }` ]);
56 }
57 if (field.required !== undefined && typeof field.required !== 'boolean' ) {
58 throw invalidCollectionRequest (path, [ `${ path }.required: expected a boolean, received ${ valueKind ( field . required ) }` ]);
59 }
60 if (field.typeMetadata !== undefined && ( ! field.typeMetadata || typeof field.typeMetadata !== 'object' || Array. isArray (field.typeMetadata))) {
61 throw invalidCollectionRequest (path, [ `${ path }.typeMetadata: expected an object, received ${ valueKind ( field . typeMetadata ) }` ]);
62 }
63 return { ... field };
64 }
65
66 function normalizeCmsCollectionDefinition ( collection , { path = 'collection' } = {}) {
67 if ( ! collection || typeof collection !== 'object' || Array. isArray (collection)) {
68 throw invalidCollectionRequest (path, [ `${ path }: expected an object, received ${ valueKind ( collection ) }` ]);
69 }
70 if ( typeof collection.id !== 'string' || ! collection.id. trim ()) {
71 throw invalidCollectionRequest (path, [ `${ path }.id: expected a non-empty string` ]);
72 }
73 if (collection.displayName !== undefined && typeof collection.displayName !== 'string' ) {
74 throw invalidCollectionRequest (path, [ `${ path }.displayName: expected a string, received ${ valueKind ( collection . displayName ) }` ]);
75 }
76 if ( ! Array. isArray (collection.fields)) {
77 throw invalidCollectionRequest (path, [ `${ path }.fields: expected an array, received ${ valueKind ( collection . fields ) }` ]);
78 }
79 const fields = collection.fields. map (( field , index ) => normalizeField (field, `${ path }.fields[${ index }]` ));
80 const permissions = normalizePermissions (collection.permissions, `${ path }.permissions` );
81 return { ... collection, fields, permissions };
82 }
83
84 module . exports = { CMS_PERMISSION_PRESETS, normalizeCmsCollectionDefinition };