@stackone/core 1.14.4 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.mjs CHANGED
@@ -1 +1 @@
1
- import{HttpClient as e}from"@stackone/transport";import t from"jsonpath";import*as r from"date-fns";const s=(e,t="")=>{if("object"===e.type&&e.properties){const r=""===t||t.endsWith(".")?t:`${t}.`;return Object.entries(e.properties).flatMap((([e,t])=>s(t,`${r}${e}`)))}return"array"===e.type&&e.items?s(e.items,`${t}[].`):[{name:t.endsWith(".")?t.slice(0,-1):t,type:e.type,description:e.description}]};class n{constructor({version:t="1",key:r,name:n,services:o={},httpOperations:i={},auth:a,proxy:u,httpClient:c=new e}){this.listOperations=()=>Object.keys(this.httpOperations||{}),this.getOperation=e=>{const t=this.httpOperations?.[e];if(!t)throw new Error(`Operation ${e} not found`);return t},this.getOperationResponseFieldsMetadata=e=>{const t=this.getOperation(e).responses;if(!t)return null;const r=(e=>{const t=Object.entries(e).find((([e])=>e.startsWith("2")));return t?t[1]:null})(t);return r?.content["application/json"]?.schema?s(r.content["application/json"]?.schema):null},this.callOperation=async({operationId:e,params:t,credentials:r})=>{const s=this.httpOperations?.[e];if(!s)throw new Error(`Operation ${e} not found`);const n=(({params:e,httpOperation:t})=>{const{url:r,method:s}=t;let n=r;const o=[],i=[],a={};let u;Object.entries(e||{}).forEach((([e,r])=>{const s=t.parameters?.[e];if(s)switch(s.in){case"path":n=n.replace(`{${e}}`,r).replace(`:${e}`,r);break;case"query":o.push(`${e}=${r}`);break;case"header":a[e]=r;break;case"cookie":i.push(`${e}=${r}; `);break;case"body":u=r}}));const c=o.length>0?`?${o.join("&")}`:"";return i.length>0&&(a.cookie=i.join("")),{url:`${n}${c}`,method:s,headers:Object.keys(a).length>0?a:void 0,body:u||void 0}})({params:t,httpOperation:s}),{method:o,url:i,headers:a,body:u}=n,{apiKey:c}=r;return{connector:this,input:{operationId:e,params:t},response:await this.httpClient.request({method:o,url:i,headers:{...a,authorization:c},payload:u}),credentials:r}},this.version=t,this.key=r,this.name=n,this.services=o,this.httpOperations=i,this.auth=a,this.proxy=u,this.httpClient=c}}const o="request_body";var i;!function(e){e.MAP="map",e.TYPECAST="typecast",e.MAP_FIELDS="map_fields"}(i||(i={}));const a=async({blockSteps:e,block:t})=>e.reduce((async(e,t)=>t({block:await e})),Promise.resolve(t)),u=async({block:e})=>{const t=e?.fieldConfigs,r=[];if(!t||"disabled"===e?.debug?.custom_mappings)return e;let s;const n=e.providerData,o=e.context.childResource??e.context.subResource??e.context.resource;if(Array.isArray(e.result))s=e.result.map((e=>{const s=n?.[o]?.items;if(!s)return e;const i=c(e.id,e.remote_id,n),a=l(e,t,i);return r.push(...a.errors||[]),a.record}));else{const o=l(e.result,t,n);s=o.record,r.push(...o.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(r).length>0?r:void 0}},c=(e,t,r)=>r?Object.keys(r).reduce(((s,n)=>{const o=r[n];if(!o.items)return s;const i=o.items.find((r=>r.id===e||r.id===t));return i?{...s,[n]:i}:s}),{}):{},l=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},o=[],i={...e};for(const r of t){const{error:t,value:a}=d(s,e.id,r);t?o.push(t):r.isUnified?i[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...i,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:o.length>0?o:void 0}},d=(e,r,s)=>{const{expression:n,targetFieldKey:o}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:o}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:o}}}const i=n&&t.query(e,n)[0];return i?{value:i}:{error:{message:"Expression returned no value",id:r,targetField:o}}},p=e=>null!=e,y=e=>"string"==typeof e,f=e=>e instanceof Date,h="yyyy-MM-dd",m=["yyyy-MM-dd","yyyy/MM/dd","dd/MM/yyyy","dd-MM-yyyy","yyyy-dd-MM","yyyy/dd/MM","MM/dd/yyyy","MM-dd-yyyy"],g=({value:e})=>{const t=r.parseISO(e);if(r.isValid(t))return t;const s=new Date;for(const t of m){const n=r.parse(e,t,s);if(r.isValid(n))return n.setUTCHours(0,0,0,0),n}return null},b=({value:e,format:t=h})=>{const s=(({value:e,format:t=h})=>{if(f(e))return e;if(!y(e))return null;try{const s=r.parse(e,t,new Date);return r.isValid(s)?s:g({value:e})}catch(e){return null}})({value:e,format:t});return s?s.toISOString():null};var v;!function(e){e.String="string",e.Number="number",e.Boolean="boolean",e.DateTimeString="datetime_string"}(v||(v={}));const O=({value:e,type:t,format:r})=>{if((e=>null==e||""===e)(e))return null;switch(t){case v.String:return(({value:e})=>y(e)?e:f(e)?e.toISOString():(e=>"object"==typeof e&&null!==e)(e)?JSON.stringify(e):String(e))({value:e});case v.Number:return(({value:e})=>{if((e=>"number"==typeof e)(e))return e;if(f(e))return null;const t=Number(e);return isNaN(t)?null:t})({value:e});case v.Boolean:return(({value:e})=>(e=>"boolean"==typeof e)(e)?e:"false"!==e&&"0"!==e&&0!==e&&Boolean(e))({value:e});case v.DateTimeString:return b({value:e,format:r});default:return e}},M=async({block:e})=>{const t=e?.fieldConfigs;if(!t||"disabled"===e?.debug?.custom_mappings)return e;let r;return r=Array.isArray(e.result)?e.result.map((e=>k(e,t))):k(e.result,t),{...e,result:r}},k=(e,t)=>{const r={...e};return t.forEach((t=>{const{targetFieldKey:s,type:n}=t;(e=>Object.values(v).includes(e))(n)&&(t.isUnified&&p(r[s])?r[s]=O({value:e[s],type:n}):r.unified_custom_fields&&p(r.unified_custom_fields?.[s])&&(r.unified_custom_fields[s]=O({value:e.unified_custom_fields?.[s],type:n})))})),{...r}},j=async({block:e})=>{const t=e?.fieldConfigs,r=[],s=e.steps;if(!t||"disabled"===e?.debug?.custom_mappings||!s)return e;let n;if(Array.isArray(e.result)){const o=e.result.length;n=e.result.map(((e,n)=>{const i=_(s,o,n),a=S(e,t,i);return r.push(...a.errors||[]),a.record}))}else{const o=S(e.result,t,s);n=o.record,r.push(...o.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(r).length>0?r:void 0}},_=(e,t,r)=>Object.entries(e).reduce(((e,[s,n])=>{const o=n.output.data;return Array.isArray(o)&&o.length===t?e[s]={output:{data:o[r]}}:e[s]=n,e}),{}),S=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},o=[],i={...e};for(const r of t){const{error:t,value:a}=A(s,e.id,r);t?o.push(t):r.isUnified?i[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...i,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:o.length>0?o:void 0}},A=(e,r,s)=>{const{expression:n,targetFieldKey:o}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:o}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:o}}}const i=n&&t.query(e,n)[0];return void 0===i?{error:{message:"Expression returned no value",id:r,targetField:o}}:{value:i}},E={build({type:e}){switch(e){case i.MAP:return u;case i.TYPECAST:return M;case i.MAP_FIELDS:return j;default:throw new Error(`Unknown block step: ${e}`)}}},F=({fieldConfigs:e})=>{const t=[];return e&&t.push(E.build({type:i.MAP}),E.build({type:i.TYPECAST})),t};export{n as BaseConnector,i as BlockStepType,E as BlockStepsFactory,o as REQUEST_BODY_PARAM,a as blockPipe,F as getBlockPipeStepsFromConfigs};
1
+ import{HttpClient as e}from"@stackone/transport";import t from"jsonpath";import{isDate as r,isString as s,isBoolean as n,isNumber as i,isObject as o,isMissing as a,notMissing as u}from"@stackone/utils";import*as c from"date-fns";const l=(e,t="")=>{if("object"===e.type&&e.properties){const r=""===t||t.endsWith(".")?t:`${t}.`;return Object.entries(e.properties).flatMap((([e,t])=>l(t,`${r}${e}`)))}return"array"===e.type&&e.items?l(e.items,`${t}[].`):[{name:t.endsWith(".")?t.slice(0,-1):t,type:e.type,description:e.description}]};class d{constructor({version:t="1",key:r,name:s,services:n={},httpOperations:i={},auth:o,proxy:a,httpClient:u=new e}){this.listOperations=()=>Object.keys(this.httpOperations||{}),this.getOperation=e=>{const t=this.httpOperations?.[e];if(!t)throw new Error(`Operation ${e} not found`);return t},this.getOperationResponseFieldsMetadata=e=>{const t=this.getOperation(e).responses;if(!t)return null;const r=(e=>{const t=Object.entries(e).find((([e])=>e.startsWith("2")));return t?t[1]:null})(t);return r?.content["application/json"]?.schema?l(r.content["application/json"]?.schema):null},this.getOperationRequestFieldsMetadata=e=>this.getOperation(e).parameters,this.callOperation=async({operationId:e,params:t,credentials:r})=>{const s=this.httpOperations?.[e];if(!s)throw new Error(`Operation ${e} not found`);const n=(({params:e,httpOperation:t})=>{const{url:r,method:s}=t;let n=r;const i=[],o=[],a={};let u;Object.entries(e||{}).forEach((([e,r])=>{const s=t.parameters?.[e];if(s)switch(s.in){case"path":n=n.replace(`{${e}}`,r).replace(`:${e}`,r);break;case"query":i.push(`${e}=${r}`);break;case"header":a[e]=r;break;case"cookie":o.push(`${e}=${r}; `);break;case"body":u=r}}));const c=i.length>0?`?${i.join("&")}`:"";return o.length>0&&(a.cookie=o.join("")),{url:`${n}${c}`,method:s,headers:Object.keys(a).length>0?a:void 0,body:u||void 0}})({params:t,httpOperation:s}),{method:i,url:o,headers:a,body:u}=n,{apiKey:c}=r;return{connector:this,input:{operationId:e,params:t},response:await this.httpClient.request({method:i,url:o,headers:{...a,authorization:c},payload:u}),credentials:r}},this.version=t,this.key=r,this.name=s,this.services=n,this.httpOperations=i,this.auth=o,this.proxy=a,this.httpClient=u}}const p="request_body";var y;!function(e){e.MAP="map",e.TYPECAST="typecast",e.MAP_FIELDS="map_fields"}(y||(y={}));const h=async({blockSteps:e,block:t})=>e.reduce((async(e,t)=>t({block:await e})),Promise.resolve(t)),f=async({block:e})=>{const t=e?.fieldConfigs,r=[];if(!t||"disabled"===e?.debug?.custom_mappings)return e;let s;const n=e.providerData,i=e.context.childResource??e.context.subResource??e.context.resource;if(Array.isArray(e.result))s=e.result.map((e=>{const s=n?.[i]?.items;if(!s)return e;const o=m(e.id,e.remote_id,n),a=g(e,t,o);return r.push(...a.errors||[]),a.record}));else{const i=g(e.result,t,n);s=i.record,r.push(...i.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(r).length>0?r:void 0}},m=(e,t,r)=>r?Object.keys(r).reduce(((s,n)=>{const i=r[n];if(!i.items)return s;const o=i.items.find((r=>r.id===e||r.id===t));return o?{...s,[n]:o}:s}),{}):{},g=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},i=[],o={...e};for(const r of t){const{error:t,value:a}=b(s,e.id,r);t?i.push(t):r.isUnified?o[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},b=(e,r,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:i}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:i}}}const o=n&&t.query(e,n)[0];return o?{value:o}:{error:{message:"Expression returned no value",id:r,targetField:i}}},v=async({block:e})=>{const t=e?.fieldConfigs,r=[],s=e.steps;if(!t||"disabled"===e?.debug?.custom_mappings||!s)return e;let n;if(Array.isArray(e.result)){const i=e.result.length;n=e.result.map(((e,n)=>{const o=O(s,i,n),a=M(e,t,o);return r.push(...a.errors||[]),a.record}))}else{const i=M(e.result,t,s);n=i.record,r.push(...i.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(r).length>0?r:void 0}},O=(e,t,r)=>Object.entries(e).reduce(((e,[s,n])=>{const i=n.output.data;return Array.isArray(i)&&i.length===t?e[s]={output:{data:i[r]}}:e[s]=n,e}),{}),M=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},i=[],o={...e};for(const r of t){const{error:t,value:a}=k(s,e.id,r);t?i.push(t):r.isUnified?o[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},k=(e,r,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:i}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:i}}}const o=n&&t.query(e,n)[0];return void 0===o?{error:{message:"Expression returned no value",id:r,targetField:i}}:{value:o}},_="yyyy-MM-dd",j=["yyyy-MM-dd","yyyy/MM/dd","dd/MM/yyyy","dd-MM-yyyy","yyyy-dd-MM","yyyy/dd/MM","MM/dd/yyyy","MM-dd-yyyy"],S=({value:e})=>{const t=c.parseISO(e);if(c.isValid(t))return t;const r=new Date;for(const t of j){const s=c.parse(e,t,r);if(c.isValid(s))return s.setUTCHours(0,0,0,0),s}return null},F=({value:e,format:t=_})=>{const n=(({value:e,format:t=_})=>{if(r(e))return e;if(!s(e))return null;try{const r=c.parse(e,t,new Date);return c.isValid(r)?r:S({value:e})}catch(e){return null}})({value:e,format:t});return n?n.toISOString():null};var A;!function(e){e.String="string",e.Number="number",e.Boolean="boolean",e.DateTimeString="datetime_string"}(A||(A={}));const E=({value:e,type:t,format:u})=>{if(a(e))return null;switch(t){case A.String:return(({value:e})=>s(e)?e:r(e)?e.toISOString():o(e)?JSON.stringify(e):String(e))({value:e});case A.Number:return(({value:e})=>{if(i(e))return e;if(r(e))return null;const t=Number(e);return isNaN(t)?null:t})({value:e});case A.Boolean:return(({value:e})=>n(e)?e:"false"!==e&&"0"!==e&&0!==e&&Boolean(e))({value:e});case A.DateTimeString:return F({value:e,format:u});default:return e}},$=async({block:e})=>{const t=e?.fieldConfigs;if(!t||"disabled"===e?.debug?.custom_mappings)return e;let r;return r=Array.isArray(e.result)?e.result.map((e=>w(e,t))):w(e.result,t),{...e,result:r}},w=(e,t)=>{const r={...e};return t.forEach((t=>{const{targetFieldKey:s,type:n}=t;(e=>Object.values(A).includes(e))(n)&&(t.isUnified&&u(r[s])?r[s]=E({value:e[s],type:n}):r.unified_custom_fields&&u(r.unified_custom_fields?.[s])&&(r.unified_custom_fields[s]=E({value:e.unified_custom_fields?.[s],type:n})))})),{...r}},x={build({type:e}){switch(e){case y.MAP:return f;case y.TYPECAST:return $;case y.MAP_FIELDS:return v;default:throw new Error(`Unknown block step: ${e}`)}}},C=({fieldConfigs:e})=>{const t=[];return e&&t.push(x.build({type:y.MAP}),x.build({type:y.TYPECAST})),t};export{d as BaseConnector,y as BlockStepType,x as BlockStepsFactory,p as REQUEST_BODY_PARAM,h as blockPipe,C as getBlockPipeStepsFromConfigs};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("@stackone/transport"),t=require("jsonpath");function r(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var s=r(require("date-fns"));const n=(e,t="")=>{if("object"===e.type&&e.properties){const r=""===t||t.endsWith(".")?t:`${t}.`;return Object.entries(e.properties).flatMap((([e,t])=>n(t,`${r}${e}`)))}return"array"===e.type&&e.items?n(e.items,`${t}[].`):[{name:t.endsWith(".")?t.slice(0,-1):t,type:e.type,description:e.description}]};var o;exports.BlockStepType=void 0,(o=exports.BlockStepType||(exports.BlockStepType={})).MAP="map",o.TYPECAST="typecast",o.MAP_FIELDS="map_fields";const i=async({block:e})=>{const t=e?.fieldConfigs,r=[];if(!t||"disabled"===e?.debug?.custom_mappings)return e;let s;const n=e.providerData,o=e.context.childResource??e.context.subResource??e.context.resource;if(Array.isArray(e.result))s=e.result.map((e=>{const s=n?.[o]?.items;if(!s)return e;const i=a(e.id,e.remote_id,n),c=u(e,t,i);return r.push(...c.errors||[]),c.record}));else{const o=u(e.result,t,n);s=o.record,r.push(...o.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(r).length>0?r:void 0}},a=(e,t,r)=>r?Object.keys(r).reduce(((s,n)=>{const o=r[n];if(!o.items)return s;const i=o.items.find((r=>r.id===e||r.id===t));return i?{...s,[n]:i}:s}),{}):{},u=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},o=[],i={...e};for(const r of t){const{error:t,value:a}=c(s,e.id,r);t?o.push(t):r.isUnified?i[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...i,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:o.length>0?o:void 0}},c=(e,r,s)=>{const{expression:n,targetFieldKey:o}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:o}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:o}}}const i=n&&t.query(e,n)[0];return i?{value:i}:{error:{message:"Expression returned no value",id:r,targetField:o}}},l=e=>null!=e,p=e=>"string"==typeof e,d=e=>e instanceof Date,y="yyyy-MM-dd",f=["yyyy-MM-dd","yyyy/MM/dd","dd/MM/yyyy","dd-MM-yyyy","yyyy-dd-MM","yyyy/dd/MM","MM/dd/yyyy","MM-dd-yyyy"],h=({value:e})=>{const t=s.parseISO(e);if(s.isValid(t))return t;const r=new Date;for(const t of f){const n=s.parse(e,t,r);if(s.isValid(n))return n.setUTCHours(0,0,0,0),n}return null},m=({value:e,format:t=y})=>{const r=(({value:e,format:t=y})=>{if(d(e))return e;if(!p(e))return null;try{const r=s.parse(e,t,new Date);return s.isValid(r)?r:h({value:e})}catch(e){return null}})({value:e,format:t});return r?r.toISOString():null};var g;!function(e){e.String="string",e.Number="number",e.Boolean="boolean",e.DateTimeString="datetime_string"}(g||(g={}));const b=({value:e,type:t,format:r})=>{if((e=>null==e||""===e)(e))return null;switch(t){case g.String:return(({value:e})=>p(e)?e:d(e)?e.toISOString():(e=>"object"==typeof e&&null!==e)(e)?JSON.stringify(e):String(e))({value:e});case g.Number:return(({value:e})=>{if((e=>"number"==typeof e)(e))return e;if(d(e))return null;const t=Number(e);return isNaN(t)?null:t})({value:e});case g.Boolean:return(({value:e})=>(e=>"boolean"==typeof e)(e)?e:"false"!==e&&"0"!==e&&0!==e&&Boolean(e))({value:e});case g.DateTimeString:return m({value:e,format:r});default:return e}},v=async({block:e})=>{const t=e?.fieldConfigs;if(!t||"disabled"===e?.debug?.custom_mappings)return e;let r;return r=Array.isArray(e.result)?e.result.map((e=>O(e,t))):O(e.result,t),{...e,result:r}},O=(e,t)=>{const r={...e};return t.forEach((t=>{const{targetFieldKey:s,type:n}=t;(e=>Object.values(g).includes(e))(n)&&(t.isUnified&&l(r[s])?r[s]=b({value:e[s],type:n}):r.unified_custom_fields&&l(r.unified_custom_fields?.[s])&&(r.unified_custom_fields[s]=b({value:e.unified_custom_fields?.[s],type:n})))})),{...r}},k=async({block:e})=>{const t=e?.fieldConfigs,r=[],s=e.steps;if(!t||"disabled"===e?.debug?.custom_mappings||!s)return e;let n;if(Array.isArray(e.result)){const o=e.result.length;n=e.result.map(((e,n)=>{const i=S(s,o,n),a=M(e,t,i);return r.push(...a.errors||[]),a.record}))}else{const o=M(e.result,t,s);n=o.record,r.push(...o.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(r).length>0?r:void 0}},S=(e,t,r)=>Object.entries(e).reduce(((e,[s,n])=>{const o=n.output.data;return Array.isArray(o)&&o.length===t?e[s]={output:{data:o[r]}}:e[s]=n,e}),{}),M=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},o=[],i={...e};for(const r of t){const{error:t,value:a}=j(s,e.id,r);t?o.push(t):r.isUnified?i[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...i,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:o.length>0?o:void 0}},j=(e,r,s)=>{const{expression:n,targetFieldKey:o}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:o}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:o}}}const i=n&&t.query(e,n)[0];return void 0===i?{error:{message:"Expression returned no value",id:r,targetField:o}}:{value:i}},x={build({type:e}){switch(e){case exports.BlockStepType.MAP:return i;case exports.BlockStepType.TYPECAST:return v;case exports.BlockStepType.MAP_FIELDS:return k;default:throw new Error(`Unknown block step: ${e}`)}}};exports.BaseConnector=class{constructor({version:t="1",key:r,name:s,services:o={},httpOperations:i={},auth:a,proxy:u,httpClient:c=new e.HttpClient}){this.listOperations=()=>Object.keys(this.httpOperations||{}),this.getOperation=e=>{const t=this.httpOperations?.[e];if(!t)throw new Error(`Operation ${e} not found`);return t},this.getOperationResponseFieldsMetadata=e=>{const t=this.getOperation(e).responses;if(!t)return null;const r=(e=>{const t=Object.entries(e).find((([e])=>e.startsWith("2")));return t?t[1]:null})(t);return r?.content["application/json"]?.schema?n(r.content["application/json"]?.schema):null},this.callOperation=async({operationId:e,params:t,credentials:r})=>{const s=this.httpOperations?.[e];if(!s)throw new Error(`Operation ${e} not found`);const n=(({params:e,httpOperation:t})=>{const{url:r,method:s}=t;let n=r;const o=[],i=[],a={};let u;Object.entries(e||{}).forEach((([e,r])=>{const s=t.parameters?.[e];if(s)switch(s.in){case"path":n=n.replace(`{${e}}`,r).replace(`:${e}`,r);break;case"query":o.push(`${e}=${r}`);break;case"header":a[e]=r;break;case"cookie":i.push(`${e}=${r}; `);break;case"body":u=r}}));const c=o.length>0?`?${o.join("&")}`:"";return i.length>0&&(a.cookie=i.join("")),{url:`${n}${c}`,method:s,headers:Object.keys(a).length>0?a:void 0,body:u||void 0}})({params:t,httpOperation:s}),{method:o,url:i,headers:a,body:u}=n,{apiKey:c}=r;return{connector:this,input:{operationId:e,params:t},response:await this.httpClient.request({method:o,url:i,headers:{...a,authorization:c},payload:u}),credentials:r}},this.version=t,this.key=r,this.name=s,this.services=o,this.httpOperations=i,this.auth=a,this.proxy=u,this.httpClient=c}},exports.BlockStepsFactory=x,exports.REQUEST_BODY_PARAM="request_body",exports.blockPipe=async({blockSteps:e,block:t})=>e.reduce((async(e,t)=>t({block:await e})),Promise.resolve(t)),exports.getBlockPipeStepsFromConfigs=({fieldConfigs:e})=>{const t=[];return e&&t.push(x.build({type:exports.BlockStepType.MAP}),x.build({type:exports.BlockStepType.TYPECAST})),t};
1
+ "use strict";var e=require("@stackone/transport"),t=require("jsonpath"),r=require("@stackone/utils");function s(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=s(require("date-fns"));const i=(e,t="")=>{if("object"===e.type&&e.properties){const r=""===t||t.endsWith(".")?t:`${t}.`;return Object.entries(e.properties).flatMap((([e,t])=>i(t,`${r}${e}`)))}return"array"===e.type&&e.items?i(e.items,`${t}[].`):[{name:t.endsWith(".")?t.slice(0,-1):t,type:e.type,description:e.description}]};var o;exports.BlockStepType=void 0,(o=exports.BlockStepType||(exports.BlockStepType={})).MAP="map",o.TYPECAST="typecast",o.MAP_FIELDS="map_fields";const a=async({block:e})=>{const t=e?.fieldConfigs,r=[];if(!t||"disabled"===e?.debug?.custom_mappings)return e;let s;const n=e.providerData,i=e.context.childResource??e.context.subResource??e.context.resource;if(Array.isArray(e.result))s=e.result.map((e=>{const s=n?.[i]?.items;if(!s)return e;const o=u(e.id,e.remote_id,n),a=c(e,t,o);return r.push(...a.errors||[]),a.record}));else{const i=c(e.result,t,n);s=i.record,r.push(...i.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(r).length>0?r:void 0}},u=(e,t,r)=>r?Object.keys(r).reduce(((s,n)=>{const i=r[n];if(!i.items)return s;const o=i.items.find((r=>r.id===e||r.id===t));return o?{...s,[n]:o}:s}),{}):{},c=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},i=[],o={...e};for(const r of t){const{error:t,value:a}=l(s,e.id,r);t?i.push(t):r.isUnified?o[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},l=(e,r,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:i}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:i}}}const o=n&&t.query(e,n)[0];return o?{value:o}:{error:{message:"Expression returned no value",id:r,targetField:i}}},p=async({block:e})=>{const t=e?.fieldConfigs,r=[],s=e.steps;if(!t||"disabled"===e?.debug?.custom_mappings||!s)return e;let n;if(Array.isArray(e.result)){const i=e.result.length;n=e.result.map(((e,n)=>{const o=d(s,i,n),a=y(e,t,o);return r.push(...a.errors||[]),a.record}))}else{const i=y(e.result,t,s);n=i.record,r.push(...i.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(r).length>0?r:void 0}},d=(e,t,r)=>Object.entries(e).reduce(((e,[s,n])=>{const i=n.output.data;return Array.isArray(i)&&i.length===t?e[s]={output:{data:i[r]}}:e[s]=n,e}),{}),y=(e,t,r)=>{if(!e||!r)return{record:e};const s={unified:{...e},..."object"==typeof r?r:{}},n={},i=[],o={...e};for(const r of t){const{error:t,value:a}=f(s,e.id,r);t?i.push(t):r.isUnified?o[r.targetFieldKey]=a:n[r.targetFieldKey]=a}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},f=(e,r,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:r,targetField:i}};try{t.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:r,targetField:i}}}const o=n&&t.query(e,n)[0];return void 0===o?{error:{message:"Expression returned no value",id:r,targetField:i}}:{value:o}},h="yyyy-MM-dd",g=["yyyy-MM-dd","yyyy/MM/dd","dd/MM/yyyy","dd-MM-yyyy","yyyy-dd-MM","yyyy/dd/MM","MM/dd/yyyy","MM-dd-yyyy"],m=({value:e})=>{const t=n.parseISO(e);if(n.isValid(t))return t;const r=new Date;for(const t of g){const s=n.parse(e,t,r);if(n.isValid(s))return s.setUTCHours(0,0,0,0),s}return null},b=({value:e,format:t=h})=>{const s=(({value:e,format:t=h})=>{if(r.isDate(e))return e;if(!r.isString(e))return null;try{const r=n.parse(e,t,new Date);return n.isValid(r)?r:m({value:e})}catch(e){return null}})({value:e,format:t});return s?s.toISOString():null};var v;!function(e){e.String="string",e.Number="number",e.Boolean="boolean",e.DateTimeString="datetime_string"}(v||(v={}));const O=({value:e,type:t,format:s})=>{if(r.isMissing(e))return null;switch(t){case v.String:return(({value:e})=>r.isString(e)?e:r.isDate(e)?e.toISOString():r.isObject(e)?JSON.stringify(e):String(e))({value:e});case v.Number:return(({value:e})=>{if(r.isNumber(e))return e;if(r.isDate(e))return null;const t=Number(e);return isNaN(t)?null:t})({value:e});case v.Boolean:return(({value:e})=>r.isBoolean(e)?e:"false"!==e&&"0"!==e&&0!==e&&Boolean(e))({value:e});case v.DateTimeString:return b({value:e,format:s});default:return e}},k=async({block:e})=>{const t=e?.fieldConfigs;if(!t||"disabled"===e?.debug?.custom_mappings)return e;let r;return r=Array.isArray(e.result)?e.result.map((e=>M(e,t))):M(e.result,t),{...e,result:r}},M=(e,t)=>{const s={...e};return t.forEach((t=>{const{targetFieldKey:n,type:i}=t;(e=>Object.values(v).includes(e))(i)&&(t.isUnified&&r.notMissing(s[n])?s[n]=O({value:e[n],type:i}):s.unified_custom_fields&&r.notMissing(s.unified_custom_fields?.[n])&&(s.unified_custom_fields[n]=O({value:e.unified_custom_fields?.[n],type:i})))})),{...s}},S={build({type:e}){switch(e){case exports.BlockStepType.MAP:return a;case exports.BlockStepType.TYPECAST:return k;case exports.BlockStepType.MAP_FIELDS:return p;default:throw new Error(`Unknown block step: ${e}`)}}};exports.BaseConnector=class{constructor({version:t="1",key:r,name:s,services:n={},httpOperations:o={},auth:a,proxy:u,httpClient:c=new e.HttpClient}){this.listOperations=()=>Object.keys(this.httpOperations||{}),this.getOperation=e=>{const t=this.httpOperations?.[e];if(!t)throw new Error(`Operation ${e} not found`);return t},this.getOperationResponseFieldsMetadata=e=>{const t=this.getOperation(e).responses;if(!t)return null;const r=(e=>{const t=Object.entries(e).find((([e])=>e.startsWith("2")));return t?t[1]:null})(t);return r?.content["application/json"]?.schema?i(r.content["application/json"]?.schema):null},this.getOperationRequestFieldsMetadata=e=>this.getOperation(e).parameters,this.callOperation=async({operationId:e,params:t,credentials:r})=>{const s=this.httpOperations?.[e];if(!s)throw new Error(`Operation ${e} not found`);const n=(({params:e,httpOperation:t})=>{const{url:r,method:s}=t;let n=r;const i=[],o=[],a={};let u;Object.entries(e||{}).forEach((([e,r])=>{const s=t.parameters?.[e];if(s)switch(s.in){case"path":n=n.replace(`{${e}}`,r).replace(`:${e}`,r);break;case"query":i.push(`${e}=${r}`);break;case"header":a[e]=r;break;case"cookie":o.push(`${e}=${r}; `);break;case"body":u=r}}));const c=i.length>0?`?${i.join("&")}`:"";return o.length>0&&(a.cookie=o.join("")),{url:`${n}${c}`,method:s,headers:Object.keys(a).length>0?a:void 0,body:u||void 0}})({params:t,httpOperation:s}),{method:i,url:o,headers:a,body:u}=n,{apiKey:c}=r;return{connector:this,input:{operationId:e,params:t},response:await this.httpClient.request({method:i,url:o,headers:{...a,authorization:c},payload:u}),credentials:r}},this.version=t,this.key=r,this.name=s,this.services=n,this.httpOperations=o,this.auth=a,this.proxy=u,this.httpClient=c}},exports.BlockStepsFactory=S,exports.REQUEST_BODY_PARAM="request_body",exports.blockPipe=async({blockSteps:e,block:t})=>e.reduce((async(e,t)=>t({block:await e})),Promise.resolve(t)),exports.getBlockPipeStepsFromConfigs=({fieldConfigs:e})=>{const t=[];return e&&t.push(S.build({type:exports.BlockStepType.MAP}),S.build({type:exports.BlockStepType.TYPECAST})),t};
@@ -1,5 +1,5 @@
1
1
  import { IHttpClient } from '@stackone/transport';
2
- import { Bundle, HttpOperation, HttpOperations, IConnector, Operation, ResponseFieldMetaData, Service } from './types';
2
+ import { Bundle, HttpOperation, HttpOperations, HttpParameter, IConnector, Operation, ResponseFieldMetaData, Service } from './types';
3
3
  export declare abstract class BaseConnector implements IConnector {
4
4
  protected httpClient: IHttpClient;
5
5
  version: string;
@@ -32,9 +32,12 @@ export declare abstract class BaseConnector implements IConnector {
32
32
  listOperations: () => string[];
33
33
  getOperation: (operationId: string) => HttpOperation;
34
34
  getOperationResponseFieldsMetadata: (operationId: string) => ResponseFieldMetaData[] | null;
35
+ getOperationRequestFieldsMetadata: (operationId: string) => {
36
+ [key: string]: HttpParameter;
37
+ } | undefined;
35
38
  callOperation: ({ operationId, params, credentials, }: {
36
39
  operationId: string;
37
- params?: Record<string, unknown> | undefined;
40
+ params?: Record<string, unknown>;
38
41
  credentials?: unknown;
39
42
  }) => Promise<Bundle>;
40
43
  }
@@ -1,4 +1,4 @@
1
1
  import type { BlockFieldConfig, BlockStep } from './types';
2
2
  export declare const getBlockPipeStepsFromConfigs: ({ fieldConfigs, }: {
3
- fieldConfigs?: BlockFieldConfig[] | undefined;
3
+ fieldConfigs?: BlockFieldConfig[];
4
4
  }) => BlockStep[];
@@ -9,9 +9,9 @@ export declare const safeParseToBoolean: ({ value, }: {
9
9
  }) => boolean | null;
10
10
  export declare const safeParseToDateTime: ({ value, format, }: {
11
11
  value: unknown;
12
- format?: string | undefined;
12
+ format?: string;
13
13
  }) => Date | null;
14
14
  export declare const safeParseToDateTimeString: ({ value, format, }: {
15
15
  value: unknown;
16
- format?: string | undefined;
16
+ format?: string;
17
17
  }) => string | null;
@@ -1,6 +1,6 @@
1
1
  export declare const typecast: ({ value, type, format, }: {
2
2
  value: unknown;
3
3
  type: string;
4
- format?: string | undefined;
4
+ format?: string;
5
5
  }) => unknown;
6
6
  export declare const isFieldSupportedType: (type: string) => boolean;
@@ -1,11 +1,11 @@
1
1
  import { HttpMethod } from '@stackone/transport';
2
2
  import { HttpOperation } from './types';
3
3
  export declare const mapParametersToHttpRequest: ({ params, httpOperation, }: {
4
- params?: Record<string, unknown> | undefined;
4
+ params?: Record<string, unknown>;
5
5
  httpOperation: HttpOperation;
6
6
  }) => {
7
7
  url: string;
8
8
  method: HttpMethod;
9
- headers?: Record<string, unknown> | undefined;
10
- body?: Record<string, unknown> | undefined;
9
+ headers?: Record<string, unknown>;
10
+ body?: Record<string, unknown>;
11
11
  };
@@ -1,4 +1,4 @@
1
- import { IHttpClient, HttpResponse, HttpMethod } from '@stackone/transport';
1
+ import { HttpMethod, HttpResponse, IHttpClient } from '@stackone/transport';
2
2
  export type RequestParameters = {
3
3
  id?: string | number;
4
4
  body?: Record<string, unknown>;
@@ -37,6 +37,7 @@ export interface HttpParameter {
37
37
  in: HttpParameterLocation;
38
38
  type: HttpParameterType;
39
39
  isArray?: boolean;
40
+ required?: boolean;
40
41
  }
41
42
  export type HttpResponses = {
42
43
  [key: string]: HttpSchemaResponse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackone/core",
3
- "version": "1.14.4",
3
+ "version": "1.16.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -16,8 +16,14 @@
16
16
  "build": "rollup -c --environment NODE_ENV:production",
17
17
  "prebuild:dev": "npm run clean",
18
18
  "build:dev": "rollup -c --environment NODE_ENV:development",
19
- "lint": "tsc --project tsconfig.json && eslint \"src/**/*.ts\"",
20
- "lint:fix": "npm run lint -- --fix",
19
+ "code:format": "biome format ./src ./*.mjs",
20
+ "code:format:fix": "biome format --write ./src ./*.mjs",
21
+ "code:lint": "biome lint --error-on-warnings ./src ./*.mjs",
22
+ "code:lint:fix": "biome lint --write ./src ./*.mjs",
23
+ "code:check": "biome check ./src ./*.mjs",
24
+ "code:check:fix": "biome check --write ./src ./*.mjs",
25
+ "lint": "npm run code:check",
26
+ "lint:fix": "npm run code:check:fix",
21
27
  "test": "vitest run --silent",
22
28
  "test:watch": "vitest watch --silent",
23
29
  "publish-release": "npm publish --access=public"
@@ -27,6 +33,7 @@
27
33
  "license": "ISC",
28
34
  "dependencies": {
29
35
  "@stackone/transport": "*",
36
+ "@stackone/utils": "*",
30
37
  "date-fns": "^3.4.0",
31
38
  "jsonpath": "^1.1.1"
32
39
  },
@@ -1,7 +0,0 @@
1
- export declare const notMissing: <T>(value: T | null | undefined) => value is T;
2
- export declare const isMissing: <T>(value: T | null | undefined) => boolean;
3
- export declare const isString: (value: unknown) => value is string;
4
- export declare const isNumber: (value: unknown) => value is number;
5
- export declare const isBoolean: (value: unknown) => value is boolean;
6
- export declare const isDate: (value: unknown) => value is Date;
7
- export declare const isObject: (value: unknown) => value is Record<string, unknown>;