Setting the file. One moment.
Provision Repeater Cms · Wix Headless Replatform · wix/skills · Skills Docs
ContentsBack to the top of the page 28.10
Workflow
scripts/provision-repeater-cms.mjs
scripts/ provision-repeater-cms.mjs
JavaScript · 25 lines · 3 KB
throw
new
Error
(
"--out is required"
);
8 const plan = JSON . parse ( await readFile (path. join (args.out, "docs" , "site-clone" , "repeater-cms.json" ), "utf8" ));
9 if ( ! args.execute) return console. log ( JSON . stringify ({ dryRun: true , collections: plan.collections. map (( c ) => ({ id: c.id, rows: c.items. length })) }, null , 2 ));
10 const token = process.env. WIX_AUTH_TOKEN ; const siteId = process.env. WIX_SITE_ID ;
11 if ( ! token || ! siteId) throw new Error ( "--execute requires WIX_AUTH_TOKEN and WIX_SITE_ID" );
12 for ( const collection of plan.collections) {
13 const definition = { id: collection.id, displayName: collection.displayName, fields: fields (), permissions: permissions () };
14 const created = await send ( "/wix-data/v2/collections" , { collection: definition }, token, siteId, [ 409 ]);
15 const existing = created?.collection || ( await send ( `/wix-data/v2/collections/${ collection . id }` , undefined , token, siteId, [], "GET" ))?.collection;
16 const collectionId = existing?.id || existing?._id || collection.id;
17 if (existing) await send ( "/wix-data/v2/collections" , { collection: { ... existing, permissions: permissions () } }, token, siteId, [], "PUT" );
18 await send ( "/wix-data/v1/permissions" , { dataCollectionId: collectionId, dataPermissions: { id: collectionId, itemRead: "ANYONE" , itemInsert: "PRIVILEGED" , itemUpdate: "PRIVILEGED" , itemRemove: "PRIVILEGED" } }, token, siteId);
19 for ( const data of collection.items) await send ( "/wix-data/v2/items" , { dataCollectionId: collectionId, dataItem: { id: data.sourceKey, data: { ... data, _id: data.sourceKey } } }, token, siteId, [ 409 ]);
20 }
21 }
22 function fields () { return [[ "sourceKey" , "TEXT" ],[ "label" , "TEXT" ],[ "body" , "TEXT" ],[ "imagesJson" , "TEXT" ],[ "linksJson" , "TEXT" ],[ "sortOrder" , "NUMBER" ],[ "initialExpanded" , "BOOLEAN" ],[ "itemStructure" , "TEXT" ]]. map (([ key , type ]) => ({ key, displayName: key, type })); }
23 function permissions () { return { read: "ANYONE" , insert: "ADMIN" , update: "ADMIN" , remove: "ADMIN" }; }
24 async function send ( endpoint , body , token , siteId , allowed = [], method = "POST" ) { const value = String (token). trim (); const authorization = / ^ Bearer \s / i . test (value) || / ^ IST \. / . test (value) ? value : `Bearer ${ value }` ; const res = await fetch ( `${ API }${ endpoint }` , { method, headers: { Authorization: authorization, "wix-site-id" : siteId, "Content-Type" : "application/json" }, ... (body === undefined ? {} : { body: JSON . stringify (body) }) }); if ( ! res.ok && ! allowed. includes (res.status)) throw new Error ( `${ endpoint }: ${ res . status } ${ await res . text () }` ); return res.ok ? res. json () : null ; }
25 main (). catch (( error ) => { console. error (error.stack || error.message); process. exit ( 1 ); });