UNPKG

@flatfile/util-extractor

Version:

A library containing common utilities and helpers for extractors.

12 lines (9 loc) 3.41 kB
'use strict'; var api = require('@flatfile/api'); var utilCommon = require('@flatfile/util-common'); var utilFileBuffer = require('@flatfile/util-file-buffer'); var o=new api.FlatfileClient,_=3e3,v=(t,a,e,r)=>l=>{l.on("file:created",async s=>{let{fileId:c,spaceId:d}=s.context,{data:u}=await o.files.get(c);if(u.mode==="export"||typeof t=="string"&&!u.name.endsWith(t)||t instanceof RegExp&&!t.test(u.name))return;let w=await o.jobs.create({type:api.Flatfile.JobType.File,operation:`extract-plugin-${a}`,status:api.Flatfile.JobStatus.Ready,source:c});await o.jobs.execute(w.data.id);}),l.on("job:ready",{operation:`extract-plugin-${a}`},async s=>{let{fileId:c,jobId:d,spaceId:u}=s.context,{chunkSize:w,parallel:H,debug:h}={chunkSize:5e3,parallel:1,debug:!1,...r},p=async(n,f)=>{await o.jobs.ack(d,{progress:n,info:f}),h&&console.log(`Job progress: ${n}, Info: ${f}`);};try{await p(1,"plugins.extraction.retrieveFile");let{data:n}=await o.files.get(c),{data:f}=await o.entitlements.list({resourceId:u}),x=!!f.find(i=>i.key==="headerSelection"),F=!!f.find(i=>i.key==="sourceEditor"),S=f.find(i=>i.key==="customRecordsPageSize"),R=await utilFileBuffer.getFileBuffer(s),C=async(i,y)=>{try{let{data:m}=await o.files.detectHeader({options:{...i},data:y});return m}catch(m){return console.dir(m,{depth:null}),{skip:0,header:[]}}};await p(3,"plugins.extraction.parseSheets");let b=await e(R,{...r,fileId:c,fileExt:n.ext,headerSelectionEnabled:x,getHeaders:C});await p(5,"plugins.extraction.createWorkbook");let g=await $(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 p(10,"plugins.extraction.addingRecords"),await new Promise(i=>{setTimeout(i,_);});for(let i of g.sheets)b[i.name]&&await utilCommon.createAllRecords(i.id,b[i.name].data.map(K),async(y,m,W)=>{await p(Math.min(99,Math.round(10+90*(m/W))),"plugins.extraction.addingRecords");},{pageSize:S?.metadata?.pageSize});x&&await B(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(c,{status:"failed"}),await o.jobs.fail(d,{info:"plugins.extraction.extractionFailed",outcome:{message:n.message}});}});};async function $(t,a,e,r){let l=D(a.name,a.spaceId,t,e,r),{data:s}=await o.workbooks.create(l);return s}function D(t,a,e,r,l){let s=Object.entries(r).map(([c,d])=>A(c,d,l));return {name:`[file] ${t}`,labels:["file"],spaceId:a,environmentId:e,sheets:s,treatments:[api.Flatfile.WorkbookTreatments.ExtractedFromSource]}}function A(t,{headers:a,descriptions:e},r){return utilCommon.normalizeSheetConfig({name:t,slug:utilCommon.slugify(t),fields:T({keys:a,descriptions:e}),allowAdditionalFields:r})}function K(t){let a={};for(let e in t)Object.hasOwn(t,e)&&(a[utilCommon.normalizeKey(e)]=t[e]);return a}function T({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 B(t,a){await Promise.all(t.sheets.map(async e=>{let{metadata:r}=a[e.name];await o.sheets.updateSheet(e.id,{metadata:r});}));} exports.Extractor = v; exports.keysToFields = T; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map