@flatfile/plugin-constraints
Version:
A plugin for extending blueprint with external constraints
8 lines (6 loc) • 1.46 kB
JavaScript
import { FlatfileClient } from '@flatfile/api';
import { bulkRecordHook } from '@flatfile/plugin-record-hook';
function p(r,o){return r.fields.reduce((t,e)=>(e.constraints?.forEach(i=>{i.type==="external"&&i.validator===o&&t.push([e,i]);}),t),[])}function m(r,o){return r.constraints?.filter(t=>t.type==="external"&&t.validator===o)||[]}var h=new FlatfileClient,P=(r,o)=>t=>{t.on("commit:created",async e=>{await e.cache.init("sheet-schema",async()=>(await h.sheets.get(e.context.sheetId)).data.config);}),t.use(bulkRecordHook("**",async(e,i)=>{let l=i.cache.get("sheet-schema"),f=p(l,r);for(let[n,s]of f)for(let a of e)try{await o(a.get(n.key),n.key,{config:s.config,record:a,property:n,event:i});}catch(c){a.addError(n.key,String(c));}}));};var C=new FlatfileClient,H=(r,o)=>t=>{t.on("commit:created",async e=>{await e.cache.init("sheet-schema",async()=>(await C.sheets.get(e.context.sheetId)).data.config);}),t.use(bulkRecordHook("**",async(e,i)=>{let l=i.cache.get("sheet-schema"),f=m(l,r);for(let n of f){let s=n.fields||[];for(let a of e)try{await o(R(a,s),n.fields,{config:n.config,record:a,properties:x(l,s),event:i});}catch(c){s.forEach(d=>{a.addError(d,String(c));});}}}));};function x(r,o=[]){return r.fields.filter(t=>o.includes(t.key))}function R(r,o){return o.reduce((t,e)=>(t[e]=r.get(e),t),{})}
export { P as externalConstraint, H as externalSheetConstraint };
//# sourceMappingURL=index.browser.js.map
//# sourceMappingURL=index.browser.js.map