UNPKG

@flatfile/plugin-convert-json-schema

Version:

A plugin for converting JSON Schema to Flatfile Blueprint and configuring a Flatfile Space.

8 lines (6 loc) 2.4 kB
import { configureSpace } from '@flatfile/plugin-space-configure'; import m from 'cross-fetch'; async function u(t){return {workbooks:await Promise.all(t.workbooks.map(async r=>{let i=await Promise.all(r.sheets.map(async n=>{let o=await g(n.source),s=await d(o);return {name:n?.name||o.title,...o?.description&&{description:o.description},fields:s,...n}}));return {...r,sheets:i}})),space:t.space}}async function g(t){return typeof t=="function"?await t():typeof t=="string"&&y(t)?await p(t):t}function y(t){try{return new URL(t),!0}catch{return !1}}async function d(t){if(!t||!t.properties)return [];let r=(n=>{try{return new URL(n).origin}catch{return ""}})(t.$id);return (await Promise.all(Object.keys(t.properties).map(async n=>await c(t,t.properties[n],n,t.required&&t.required.includes(n)||!1,r)))).flat().filter(Boolean)}async function c(t,e,r="",i=!1,n){if(e.$ref){let a=await w(t,e.$ref,n);return await c(t,a,r,!1,n)}if(e.type==="object"&&e.properties)return (await Promise.all(Object.keys(e.properties).map(async l=>await c(e,e.properties[l],r?`${r}_${l}`:l,e.required&&e.required.includes(l)||!1,n)))).flat();let o={string:{key:r,type:"string"},number:{key:r,type:"number"},integer:{key:r,type:"number"},boolean:{key:r,type:"boolean"},array:{key:r,type:"string-list"},enum:{key:r,type:"enum-list",config:e?.enum?{options:e.enum.map(a=>({value:a,label:String(a)}))}:{options:[]}}};return o[e.type]?[{label:r,...e?.description&&{description:e.description},...i&&{constraints:[{type:"required"}]},...o[e.type]}]:[]}async function w(t,e,r){let i=e.indexOf("#");if(e.startsWith("#/"))return f(t,e);let n=i>=0?e.substring(0,i):e,o=i>=0?e.substring(i):"",s=await p(`${r}${n}`);return o?f(s,o):s}function f(t,e){let r=e.split("/").slice(1).reduce((i,n)=>i&&(i[n]||i.$defs?.[n]||i.definitions?.[n]),t);if(!r)throw new Error(`Cannot resolve reference: ${e}`);return r}async function p(t){try{let e=await m(t);if(!e.ok)throw new Error(`API returned status ${e.status}: ${e.statusText}`);return await e.json()}catch(e){throw new Error(`Error fetching external reference: ${e.message}`)}}function F(t,e){return configureSpace(()=>u(t),e)} export { F as configureSpaceWithJsonSchema, p as fetchExternalReference, d as generateFields, u as generateSetup, c as getPropertyType, f as resolveLocalReference, w as resolveReference }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map