@flatfile/util-extractor
Version:
A library containing common utilities and helpers for extractors.
12 lines (9 loc) • 3.39 kB
JavaScript
;
var api = require('@flatfile/api');
var utilCommon = require('@flatfile/util-common');
var utilFileBuffer = require('@flatfile/util-file-buffer');
var o=new api.FlatfileClient,P=3e3,M=(t,a,e,i)=>c=>{c.on("file:created",async s=>{let{fileId:l,spaceId:d}=s.context,{data:f}=await o.files.get(l);if(f.mode==="export"||typeof t=="string"&&!f.name.endsWith(t)||t instanceof RegExp&&!t.test(f.name))return;let w=await o.jobs.create({type:api.Flatfile.JobType.File,operation:`extract-plugin-${a}`,status:api.Flatfile.JobStatus.Ready,source:l});await o.jobs.execute(w.data.id);}),c.on("job:ready",{operation:`extract-plugin-${a}`},async s=>{let{fileId:l,jobId:d,spaceId:f}=s.context,{chunkSize:w,parallel:K,debug:h}={chunkSize:5e3,parallel:1,debug:!1,...i},u=async(n,p)=>{await o.jobs.ack(d,{progress:n,info:p}),h&&console.log(`Job progress: ${n}, Info: ${p}`);};try{await u(1,"plugins.extraction.retrieveFile");let{data:n}=await o.files.get(l),{data:p}=await o.entitlements.list({resourceId:f}),x=!!p.find(r=>r.key==="headerSelection"),F=!!p.find(r=>r.key==="sourceEditor"),C=await utilFileBuffer.getFileBuffer(s),R=async(r,y)=>{try{let{data:m}=await o.files.detectHeader({options:{...r},data:y});return m}catch(m){return console.dir(m,{depth:null}),{skip:0,header:[]}}};await u(3,"plugins.extraction.parseSheets");let b=await e(C,{...i,fileId:l,fileExt:n.ext,headerSelectionEnabled:x,getHeaders:R});await u(5,"plugins.extraction.createWorkbook");let g=await O(s.context.environmentId,n,b,F);if(await o.files.update(n.id,{workbookId:g.id}),!g.sheets||g.sheets.length===0)throw new Error("plugins.extraction.noSheets");await u(10,"plugins.extraction.addingRecords"),await new Promise(r=>{setTimeout(r,P);});for(let r of g.sheets)b[r.name]&&await utilCommon.createAllRecords(r.id,b[r.name].data.map(A),async(y,m,S)=>{await u(Math.min(99,Math.round(10+90*(m/S))),"plugins.extraction.addingRecords");});x&&await T(g,b),await o.files.update(n.id,{status:"complete"}),await o.jobs.complete(d,{info:"files.uploadFile.popovers.extractionCompleted",outcome:{message:"plugins.extraction.extractedFile"}});}catch(n){h&&console.log(`Extractor error: ${n.message}`),await o.files.update(l,{status:"failed"}),await o.jobs.fail(d,{info:"plugins.extraction.extractionFailed",outcome:{message:n.message}});}});};async function O(t,a,e,i){let c=$(a.name,a.spaceId,t,e,i),{data:s}=await o.workbooks.create(c);return s}function $(t,a,e,i,c){let s=Object.entries(i).map(([l,d])=>z(l,d,c));return {name:`[file] ${t}`,labels:["file"],spaceId:a,environmentId:e,sheets:s,treatments:[api.Flatfile.WorkbookTreatments.ExtractedFromSource]}}function z(t,{headers:a,descriptions:e},i){return utilCommon.normalizeSheetConfig({name:t,slug:utilCommon.slugify(t),fields:L({keys:a,descriptions:e}),allowAdditionalFields:i})}function D(t){return t.trim().replace(/%/g,"_PERCENT_").replace(/\$/g,"_DOLLAR_")}function A(t){let a={};for(let e in t)t.hasOwnProperty(e)&&(a[D(e)]=t[e]);return a}function L({keys:t,descriptions:a={}}){return t.map(e=>(e||(e=""),typeof e!="string"&&(e=String(e)),e=e.trim(),{key:e,label:e,description:a?.[e]||"",type:"string"}))}async function T(t,a){await Promise.all(t.sheets.map(async e=>{let{metadata:i}=a[e.name];await o.sheets.updateSheet(e.id,{metadata:i});}));}
exports.Extractor = M;
exports.keysToFields = L;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map