UNPKG

@flatfile/plugin-space-configure

Version:
27 lines (25 loc) 2.76 kB
import { FlatfileClient } from '@flatfile/api'; import { jobHandler } from '@flatfile/plugin-job-handler'; var p=new FlatfileClient,m=async e=>{let{data:a}=await p.workbooks.list({spaceId:e}),i=`<div class="my-doc"> `;for(let t of a){if(t.labels.includes("file")&&t.name.startsWith("[file]")){console.log(`Skipping file workbook \`${t.name}\``);continue}let{data:f}=await p.sheets.list({workbookId:t.id});i+=` <h2>${t.name}</h2>`;for(let o of f){let s=o.config.fields.map(d=>{let k=d.constraints?.map(b=>`<span style="color: #d97a71; background-color: #fff0ef; padding: 2px 6px; border-radius: 5px; margin-left: 8px;">${b.type}</span>`).join("");return ` <tr> <td style="padding:8px; border-bottom:1px solid #f3f2f2;">${d.label}${k??""}</td> <td style="padding:8px; border-bottom:1px solid #f3f2f2;"><span style="color: #2e424b; background-color: #f0f0f0; padding: 2px 6px; border-radius: 5px;">${d.type}</span></td> <td style="padding:8px; border-bottom:1px solid #f3f2f2;">${d.description||""}</td> </tr>`}).join("");i+=` <h3>${o.name}</h3> <table style="width:100%; border-collapse: collapse;"> <thead> <tr> <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd; width: 25%;">Field Name</th> <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd; width: 10%;">Data Type</th> <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd; width: 65%;">Description</th> </tr> </thead> <tbody>${s} </tbody> </table>`;}}i+=` </div>`;let{data:r}=await p.documents.list(e),l=r.find(t=>t.title==="Data Checklist");l?await p.documents.update(e,l.id,{title:"Data Checklist",body:i}):await p.documents.create(e,{title:"Data Checklist",body:i});};function C(){return function(e){e.on(["workbook:created","workbook:updated"],async a=>{let{spaceId:i}=a.context;console.log(`\`dataChecklist\` firing on \`${a.topic}\` for space \`${i}\``),await m(i);});}}var c=new FlatfileClient;function v(e,a){return function(i){i.use(jobHandler("space:configure",async(r,l)=>{let{spaceId:t,environmentId:f}=r.context,o=typeof e=="function"?await e(r):e,n=await Promise.all(o.workbooks.map(async s=>(await c.workbooks.create({spaceId:t,environmentId:f,name:"Workbook",...s})).data.id));if(await l(50,"Workbook created"),o.config?.maintainWorkbookOrder&&(o.space||(o.space={}),o.space.settings={sidebarConfig:{workbookSidebarOrder:n}}),await c.spaces.update(t,{environmentId:f,primaryWorkbookId:n&&n.length>0?n[0]:"",...o.space}),o.documents)for(let s of o.documents)await c.documents.create(t,s);return a&&await a(r,n,l),{info:"Space configured"}}));}} export { v as configureSpace, m as dataChecklist, C as dataChecklistPlugin }; //# sourceMappingURL=index.browser.js.map //# sourceMappingURL=index.browser.js.map