@stackone/core 1.16.0 → 1.17.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.
Files changed (29) hide show
  1. package/dist/index.es.mjs +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/types/blocks/types.d.ts +7 -29
  4. package/dist/types/index.d.ts +6 -8
  5. package/dist/types/stepFunctions/factory.d.ts +7 -0
  6. package/dist/types/stepFunctions/getFieldMappingStepFunctions.d.ts +5 -0
  7. package/dist/types/stepFunctions/map/mapStepFunction.d.ts +2 -0
  8. package/dist/types/stepFunctions/mapFields/mapFieldsStepFunction.d.ts +2 -0
  9. package/dist/types/stepFunctions/typecast/typecastStepFunction.d.ts +2 -0
  10. package/dist/types/stepFunctions/types.d.ts +13 -0
  11. package/dist/types/steps/stepsRunners.d.ts +6 -0
  12. package/dist/types/steps/types.d.ts +13 -0
  13. package/package.json +1 -1
  14. package/dist/types/baseConnector.d.ts +0 -43
  15. package/dist/types/blocks/blockPipe.d.ts +0 -5
  16. package/dist/types/blocks/blockStepsConfigs.d.ts +0 -4
  17. package/dist/types/blocks/blockStepsFactory.d.ts +0 -7
  18. package/dist/types/blocks/steps/map/mapBlockStep.d.ts +0 -2
  19. package/dist/types/blocks/steps/mapFields/mapFieldsBlockStep.d.ts +0 -2
  20. package/dist/types/blocks/steps/stepsDirectoryTypes.d.ts +0 -5
  21. package/dist/types/blocks/steps/typecast/typecastBlockStep.d.ts +0 -2
  22. package/dist/types/constants.d.ts +0 -1
  23. package/dist/types/mappers.d.ts +0 -11
  24. package/dist/types/responseMetaData.d.ts +0 -3
  25. package/dist/types/types.d.ts +0 -90
  26. /package/dist/types/{blocks/steps → stepFunctions}/typecast/constants.d.ts +0 -0
  27. /package/dist/types/{blocks/steps → stepFunctions}/typecast/parsers.d.ts +0 -0
  28. /package/dist/types/{blocks/steps → stepFunctions}/typecast/typecast.d.ts +0 -0
  29. /package/dist/types/{blocks/steps → stepFunctions}/typecast/types.d.ts +0 -0
package/dist/index.es.mjs CHANGED
@@ -1 +1 @@
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};
1
+ import e from"jsonpath";import{isDate as r,isString as t,isBoolean as s,isNumber as n,isObject as i,isMissing as o,notMissing as u}from"@stackone/utils";import*as a from"date-fns";var l;!function(e){e.MAP="map",e.TYPECAST="typecast",e.MAP_FIELDS="map_fields"}(l||(l={}));const d=async({stepFunctions:e,block:r})=>e.reduce((async(e,r)=>r({block:await e})),Promise.resolve(r)),c=async({block:e})=>{const r=e?.fieldConfigs,t=[];if(!r||"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=y(e.id,e.remote_id,n),u=f(e,r,o);return t.push(...u.errors||[]),u.record}));else{const i=f(e.result,r,n);s=i.record,t.push(...i.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(t).length>0?t:void 0}},y=(e,r,t)=>t?Object.keys(t).reduce(((s,n)=>{const i=t[n];if(!i.items)return s;const o=i.items.find((t=>t.id===e||t.id===r));return o?{...s,[n]:o}:s}),{}):{},f=(e,r,t)=>{if(!e||!t)return{record:e};const s={unified:{...e},..."object"==typeof t?t:{}},n={},i=[],o={...e};for(const t of r){const{error:r,value:u}=p(s,e.id,t);r?i.push(r):t.isUnified?o[t.targetFieldKey]=u:n[t.targetFieldKey]=u}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},p=(r,t,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:t,targetField:i}};try{e.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:t,targetField:i}}}const o=n&&e.query(r,n)[0];return o?{value:o}:{error:{message:"Expression returned no value",id:t,targetField:i}}},m=async({block:e})=>{const r=e?.fieldConfigs,t=[],s=e.steps;if(!r||"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=g(s,i,n),u=v(e,r,o);return t.push(...u.errors||[]),u.record}))}else{const i=v(e.result,r,s);n=i.record,t.push(...i.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(t).length>0?t:void 0}},g=(e,r,t)=>Object.entries(e).reduce(((e,[s,n])=>{const i=n.output.data;return Array.isArray(i)&&i.length===r?e[s]={output:{data:i[t]}}:e[s]=n,e}),{}),v=(e,r,t)=>{if(!e||!t)return{record:e};const s={unified:{...e},..."object"==typeof t?t:{}},n={},i=[],o={...e};for(const t of r){const{error:r,value:u}=b(s,e.id,t);r?i.push(r):t.isUnified?o[t.targetFieldKey]=u:n[t.targetFieldKey]=u}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},b=(r,t,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:t,targetField:i}};try{e.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:t,targetField:i}}}const o=n&&e.query(r,n)[0];return void 0===o?{error:{message:"Expression returned no value",id:t,targetField:i}}:{value:o}},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"],_=({value:e})=>{const r=a.parseISO(e);if(a.isValid(r))return r;const t=new Date;for(const r of M){const s=a.parse(e,r,t);if(a.isValid(s))return s.setUTCHours(0,0,0,0),s}return null},S=({value:e,format:s=h})=>{const n=(({value:e,format:s=h})=>{if(r(e))return e;if(!t(e))return null;try{const r=a.parse(e,s,new Date);return a.isValid(r)?r:_({value:e})}catch(e){return null}})({value:e,format:s});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 F=({value:e,type:u,format:a})=>{if(o(e))return null;switch(u){case A.String:return(({value:e})=>t(e)?e:r(e)?e.toISOString():i(e)?JSON.stringify(e):String(e))({value:e});case A.Number:return(({value:e})=>{if(n(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})=>s(e)?e:"false"!==e&&"0"!==e&&0!==e&&Boolean(e))({value:e});case A.DateTimeString:return S({value:e,format:a});default:return e}},E=async({block:e})=>{const r=e?.fieldConfigs;if(!r||"disabled"===e?.debug?.custom_mappings)return e;let t;return t=Array.isArray(e.result)?e.result.map((e=>O(e,r))):O(e.result,r),{...e,result:t}},O=(e,r)=>{const t={...e};return r.forEach((r=>{const{targetFieldKey:s,type:n}=r;(e=>Object.values(A).includes(e))(n)&&(r.isUnified&&u(t[s])?t[s]=F({value:e[s],type:n}):t.unified_custom_fields&&u(t.unified_custom_fields?.[s])&&(t.unified_custom_fields[s]=F({value:e.unified_custom_fields?.[s],type:n})))})),{...t}},k={build({functionName:e}){switch(e){case l.MAP:return c;case l.TYPECAST:return E;case l.MAP_FIELDS:return m;default:throw new Error(`Unknown step function: ${e}`)}}},x=({fieldConfigs:e})=>{const r=[];return e&&r.push(k.build({functionName:l.MAP}),k.build({functionName:l.TYPECAST})),r};export{l as StepFunctionName,k as StepFunctionsFactory,x as getFieldMappingStepFunctions,d as stepsBasicPipeRunner};
package/dist/index.js CHANGED
@@ -1 +1 @@
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
+ "use strict";var e=require("jsonpath"),r=require("@stackone/utils");function t(e){var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var s=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,s.get?s:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var s,n=t(require("date-fns"));exports.StepFunctionName=void 0,(s=exports.StepFunctionName||(exports.StepFunctionName={})).MAP="map",s.TYPECAST="typecast",s.MAP_FIELDS="map_fields";const i=async({block:e})=>{const r=e?.fieldConfigs,t=[];if(!r||"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 a=o(e.id,e.remote_id,n),c=u(e,r,a);return t.push(...c.errors||[]),c.record}));else{const i=u(e.result,r,n);s=i.record,t.push(...i.errors||[])}return{...e,result:s,customMappingErrors:Object.keys(t).length>0?t:void 0}},o=(e,r,t)=>t?Object.keys(t).reduce(((s,n)=>{const i=t[n];if(!i.items)return s;const o=i.items.find((t=>t.id===e||t.id===r));return o?{...s,[n]:o}:s}),{}):{},u=(e,r,t)=>{if(!e||!t)return{record:e};const s={unified:{...e},..."object"==typeof t?t:{}},n={},i=[],o={...e};for(const t of r){const{error:r,value:u}=a(s,e.id,t);r?i.push(r):t.isUnified?o[t.targetFieldKey]=u:n[t.targetFieldKey]=u}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},a=(r,t,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:t,targetField:i}};try{e.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:t,targetField:i}}}const o=n&&e.query(r,n)[0];return o?{value:o}:{error:{message:"Expression returned no value",id:t,targetField:i}}},c=async({block:e})=>{const r=e?.fieldConfigs,t=[],s=e.steps;if(!r||"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=l(s,i,n),u=d(e,r,o);return t.push(...u.errors||[]),u.record}))}else{const i=d(e.result,r,s);n=i.record,t.push(...i.errors||[])}return{...e,result:n,customMappingErrors:Object.keys(t).length>0?t:void 0}},l=(e,r,t)=>Object.entries(e).reduce(((e,[s,n])=>{const i=n.output.data;return Array.isArray(i)&&i.length===r?e[s]={output:{data:i[t]}}:e[s]=n,e}),{}),d=(e,r,t)=>{if(!e||!t)return{record:e};const s={unified:{...e},..."object"==typeof t?t:{}},n={},i=[],o={...e};for(const t of r){const{error:r,value:u}=p(s,e.id,t);r?i.push(r):t.isUnified?o[t.targetFieldKey]=u:n[t.targetFieldKey]=u}return{record:{...o,unified_custom_fields:Object.keys(n).length>0?n:void 0},errors:i.length>0?i:void 0}},p=(r,t,s)=>{const{expression:n,targetFieldKey:i}=s;if(!n)return{error:{message:"Expression is empty",id:t,targetField:i}};try{e.parse(n)}catch(e){return{error:{message:"Invalid JSON path expression",id:t,targetField:i}}}const o=n&&e.query(r,n)[0];return void 0===o?{error:{message:"Expression returned no value",id:t,targetField:i}}:{value:o}},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"],g=({value:e})=>{const r=n.parseISO(e);if(n.isValid(r))return r;const t=new Date;for(const r of f){const s=n.parse(e,r,t);if(n.isValid(s))return s.setUTCHours(0,0,0,0),s}return null},m=({value:e,format:t=y})=>{const s=(({value:e,format:t=y})=>{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: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 b=({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 m({value:e,format:s});default:return e}},S=async({block:e})=>{const r=e?.fieldConfigs;if(!r||"disabled"===e?.debug?.custom_mappings)return e;let t;return t=Array.isArray(e.result)?e.result.map((e=>M(e,r))):M(e.result,r),{...e,result:t}},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]=b({value:e[n],type:i}):s.unified_custom_fields&&r.notMissing(s.unified_custom_fields?.[n])&&(s.unified_custom_fields[n]=b({value:e.unified_custom_fields?.[n],type:i})))})),{...s}},F={build({functionName:e}){switch(e){case exports.StepFunctionName.MAP:return i;case exports.StepFunctionName.TYPECAST:return S;case exports.StepFunctionName.MAP_FIELDS:return c;default:throw new Error(`Unknown step function: ${e}`)}}};exports.StepFunctionsFactory=F,exports.getFieldMappingStepFunctions=({fieldConfigs:e})=>{const r=[];return e&&r.push(F.build({functionName:exports.StepFunctionName.MAP}),F.build({functionName:exports.StepFunctionName.TYPECAST})),r},exports.stepsBasicPipeRunner=async({stepFunctions:e,block:r})=>e.reduce((async(e,r)=>r({block:await e})),Promise.resolve(r));
@@ -1,11 +1,11 @@
1
- import { HttpMethod } from '@stackone/transport';
2
- import { BlockStepType } from './steps/stepsDirectoryTypes';
1
+ import type { HttpMethod } from '@stackone/transport';
2
+ import type { StepsSnapshots } from '../steps/types';
3
3
  export type Block = {
4
- fieldConfigs?: BlockFieldConfig[];
4
+ fieldConfigs?: FieldConfig[];
5
5
  result?: BlockIndexedRecord[] | BlockIndexedRecord;
6
6
  context: BlockContext;
7
- debug?: BlockDebugParams;
8
- steps?: BlockStepSnapshots;
7
+ debug?: DebugParams;
8
+ steps?: StepsSnapshots;
9
9
  customMappingErrors?: FieldMappingError[];
10
10
  providerData?: ProviderData;
11
11
  };
@@ -27,37 +27,15 @@ export type BlockIndexedRecord = {
27
27
  [key: string]: unknown;
28
28
  };
29
29
  };
30
- export type BlockFieldConfig = {
30
+ export type FieldConfig = {
31
31
  expression?: string;
32
32
  targetFieldKey: string;
33
33
  type: string;
34
34
  isUnified?: boolean;
35
35
  };
36
- export type BlockStep = ({ block, params, }: {
37
- block: Readonly<Block>;
38
- params?: BlockStepParams;
39
- }) => Promise<Block>;
40
- export type BlockStepParams = {
41
- [key: string]: unknown;
42
- };
43
- export type BlockDebugParams = {
36
+ export type DebugParams = {
44
37
  custom_mappings?: 'disabled' | 'enabled';
45
38
  };
46
- export type BlockStepDefinition = {
47
- name: BlockStepType;
48
- params?: BlockStepParams;
49
- };
50
- export type BlockStepSnapshot = {
51
- output: {
52
- data: unknown;
53
- matcher?: string;
54
- };
55
- errors?: BlockStepError[];
56
- };
57
- export type BlockStepSnapshots = {
58
- [key: string]: BlockStepSnapshot;
59
- };
60
- export type BlockStepError = FieldMappingError;
61
39
  export type ProviderData = {
62
40
  [key: string]: ProviderDataObject;
63
41
  };
@@ -1,8 +1,6 @@
1
- export { IConnector, HttpOperations, HttpParameter, HttpParameterLocation, HttpParameterType, } from './types';
2
- export { BaseConnector } from './baseConnector';
3
- export { REQUEST_BODY_PARAM } from './constants';
4
- export { Block, BlockStep, BlockIndexedRecord, BlockContext, BlockFieldConfig, BlockStepDefinition, BlockStepParams, BlockStepSnapshots, BlockStepSnapshot, } from './blocks/types';
5
- export { BlockStepType } from './blocks/steps/stepsDirectoryTypes';
6
- export { blockPipe } from './blocks/blockPipe';
7
- export { getBlockPipeStepsFromConfigs } from './blocks/blockStepsConfigs';
8
- export { BlockStepsFactory } from './blocks/blockStepsFactory';
1
+ export { Block, BlockIndexedRecord, BlockContext, FieldConfig, } from './blocks/types';
2
+ export { StepFunction, StepFunctionParams, StepFunctionName } from './stepFunctions/types';
3
+ export { StepsSnapshots } from './steps/types';
4
+ export { stepsBasicPipeRunner } from './steps/stepsRunners';
5
+ export { getFieldMappingStepFunctions } from './stepFunctions/getFieldMappingStepFunctions';
6
+ export { StepFunctionsFactory } from './stepFunctions/factory';
@@ -0,0 +1,7 @@
1
+ import { StepFunctionName } from './types';
2
+ import type { StepFunction } from './types';
3
+ export declare const StepFunctionsFactory: {
4
+ build({ functionName }: {
5
+ functionName: StepFunctionName;
6
+ }): StepFunction;
7
+ };
@@ -0,0 +1,5 @@
1
+ import type { FieldConfig } from '../blocks/types';
2
+ import { type StepFunction } from './types';
3
+ export declare const getFieldMappingStepFunctions: ({ fieldConfigs, }: {
4
+ fieldConfigs?: FieldConfig[];
5
+ }) => StepFunction[];
@@ -0,0 +1,2 @@
1
+ import type { StepFunction } from '../types';
2
+ export declare const mapStepFunction: StepFunction;
@@ -0,0 +1,2 @@
1
+ import type { StepFunction } from '../types';
2
+ export declare const mapFieldsStepFunction: StepFunction;
@@ -0,0 +1,2 @@
1
+ import type { StepFunction } from '../types';
2
+ export declare const typecastStepFunction: StepFunction;
@@ -0,0 +1,13 @@
1
+ import type { Block } from '../blocks/types';
2
+ export declare enum StepFunctionName {
3
+ MAP = "map",
4
+ TYPECAST = "typecast",
5
+ MAP_FIELDS = "map_fields"
6
+ }
7
+ export type StepFunction = ({ block, params, }: {
8
+ block: Readonly<Block>;
9
+ params?: StepFunctionParams;
10
+ }) => Promise<Block>;
11
+ export type StepFunctionParams = {
12
+ [key: string]: unknown;
13
+ };
@@ -0,0 +1,6 @@
1
+ import type { Block } from '../blocks/types';
2
+ import type { StepFunction } from '../stepFunctions/types';
3
+ export declare const stepsBasicPipeRunner: ({ stepFunctions, block, }: {
4
+ stepFunctions: StepFunction[];
5
+ block: Block;
6
+ }) => Promise<Block>;
@@ -0,0 +1,13 @@
1
+ export type StepSnapshot = {
2
+ output: {
3
+ data: unknown;
4
+ matcher?: string;
5
+ };
6
+ errors?: StepError[];
7
+ };
8
+ export type StepsSnapshots = {
9
+ [key: string]: StepSnapshot;
10
+ };
11
+ export type StepError = {
12
+ message: string;
13
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackone/core",
3
- "version": "1.16.0",
3
+ "version": "1.17.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",
@@ -1,43 +0,0 @@
1
- import { IHttpClient } from '@stackone/transport';
2
- import { Bundle, HttpOperation, HttpOperations, HttpParameter, IConnector, Operation, ResponseFieldMetaData, Service } from './types';
3
- export declare abstract class BaseConnector implements IConnector {
4
- protected httpClient: IHttpClient;
5
- version: string;
6
- key: string;
7
- name: string;
8
- services: {
9
- ats?: Service | undefined;
10
- crm?: Service | undefined;
11
- hris?: Service | undefined;
12
- marketing?: Service | undefined;
13
- };
14
- httpOperations?: HttpOperations;
15
- auth?: unknown;
16
- proxy?: Operation;
17
- constructor({ version, key, name, services, httpOperations, auth, proxy, httpClient, }: {
18
- version?: string;
19
- key: string;
20
- name: string;
21
- services?: {
22
- ats?: Service | undefined;
23
- crm?: Service | undefined;
24
- hris?: Service | undefined;
25
- marketing?: Service | undefined;
26
- };
27
- httpOperations?: HttpOperations;
28
- auth?: unknown;
29
- proxy?: Operation;
30
- httpClient?: IHttpClient;
31
- });
32
- listOperations: () => string[];
33
- getOperation: (operationId: string) => HttpOperation;
34
- getOperationResponseFieldsMetadata: (operationId: string) => ResponseFieldMetaData[] | null;
35
- getOperationRequestFieldsMetadata: (operationId: string) => {
36
- [key: string]: HttpParameter;
37
- } | undefined;
38
- callOperation: ({ operationId, params, credentials, }: {
39
- operationId: string;
40
- params?: Record<string, unknown>;
41
- credentials?: unknown;
42
- }) => Promise<Bundle>;
43
- }
@@ -1,5 +0,0 @@
1
- import type { Block, BlockStep } from './types';
2
- export declare const blockPipe: ({ blockSteps, block, }: {
3
- blockSteps: BlockStep[];
4
- block: Block;
5
- }) => Promise<Block>;
@@ -1,4 +0,0 @@
1
- import type { BlockFieldConfig, BlockStep } from './types';
2
- export declare const getBlockPipeStepsFromConfigs: ({ fieldConfigs, }: {
3
- fieldConfigs?: BlockFieldConfig[];
4
- }) => BlockStep[];
@@ -1,7 +0,0 @@
1
- import { BlockStepType } from './steps/stepsDirectoryTypes';
2
- import type { BlockStep } from './types';
3
- export declare const BlockStepsFactory: {
4
- build({ type }: {
5
- type: BlockStepType;
6
- }): BlockStep;
7
- };
@@ -1,2 +0,0 @@
1
- import type { BlockStep } from '../../types';
2
- export declare const mapBlockStep: BlockStep;
@@ -1,2 +0,0 @@
1
- import type { BlockStep } from '../../types';
2
- export declare const mapFieldsBlockStep: BlockStep;
@@ -1,5 +0,0 @@
1
- export declare enum BlockStepType {
2
- MAP = "map",
3
- TYPECAST = "typecast",
4
- MAP_FIELDS = "map_fields"
5
- }
@@ -1,2 +0,0 @@
1
- import type { BlockStep } from '../../types';
2
- export declare const typecastBlockStep: BlockStep;
@@ -1 +0,0 @@
1
- export declare const REQUEST_BODY_PARAM = "request_body";
@@ -1,11 +0,0 @@
1
- import { HttpMethod } from '@stackone/transport';
2
- import { HttpOperation } from './types';
3
- export declare const mapParametersToHttpRequest: ({ params, httpOperation, }: {
4
- params?: Record<string, unknown>;
5
- httpOperation: HttpOperation;
6
- }) => {
7
- url: string;
8
- method: HttpMethod;
9
- headers?: Record<string, unknown>;
10
- body?: Record<string, unknown>;
11
- };
@@ -1,3 +0,0 @@
1
- import { HttpParameterSchema, HttpResponses, HttpSchemaResponse, ResponseFieldMetaData } from './types';
2
- export declare const getSuccessfulResponse: (responses: HttpResponses) => HttpSchemaResponse | null;
3
- export declare const getResponseFieldsMetaData: (schema: HttpParameterSchema, parentFieldKey?: string) => ResponseFieldMetaData[];
@@ -1,90 +0,0 @@
1
- import { HttpMethod, HttpResponse, IHttpClient } from '@stackone/transport';
2
- export type RequestParameters = {
3
- id?: string | number;
4
- body?: Record<string, unknown>;
5
- [key: string]: unknown;
6
- };
7
- export type Operation = ({ input, credentials, getHttpClient, }: {
8
- input: RequestParameters;
9
- credentials?: unknown;
10
- getHttpClient?: () => Promise<IHttpClient>;
11
- }) => Promise<Bundle>;
12
- export interface Resource {
13
- [key: string]: Operation | Resource;
14
- }
15
- export interface Service {
16
- [key: string]: Resource;
17
- }
18
- export type Bundle = {
19
- connector: IConnector;
20
- input: RequestParameters;
21
- credentials: unknown;
22
- url?: string;
23
- response?: HttpResponse;
24
- };
25
- export type AuthenticationConfig = {
26
- [authentication: string]: Environment;
27
- };
28
- export type Environment = {
29
- default: AuthenticationConfig;
30
- production: AuthenticationConfig;
31
- } & {
32
- [environment: string]: AuthenticationConfig;
33
- };
34
- export type HttpParameterLocation = 'path' | 'query' | 'header' | 'cookie' | 'body';
35
- export type HttpParameterType = 'boolean' | 'object' | 'number' | 'string' | 'integer';
36
- export interface HttpParameter {
37
- in: HttpParameterLocation;
38
- type: HttpParameterType;
39
- isArray?: boolean;
40
- required?: boolean;
41
- }
42
- export type HttpResponses = {
43
- [key: string]: HttpSchemaResponse;
44
- };
45
- export type HttpSchemaResponse = {
46
- description: string;
47
- content: {
48
- [key: string]: {
49
- schema: HttpParameterSchema;
50
- };
51
- };
52
- };
53
- export type HttpParameterSchema = {
54
- type: string;
55
- description?: string;
56
- properties?: {
57
- [key: string]: HttpParameterSchema;
58
- };
59
- items?: HttpParameterSchema;
60
- };
61
- export type ResponseFieldMetaData = {
62
- name: string;
63
- type: HttpParameterType;
64
- description?: string;
65
- };
66
- export type HttpOperation = {
67
- url: string;
68
- method: HttpMethod;
69
- parameters?: {
70
- [key: string]: HttpParameter;
71
- };
72
- responses: HttpResponses;
73
- };
74
- export type HttpOperations = {
75
- [key: string]: HttpOperation;
76
- };
77
- export interface IConnector {
78
- version: string;
79
- key: string;
80
- name: string;
81
- services: {
82
- ats?: Service;
83
- crm?: Service;
84
- hris?: Service;
85
- marketing?: Service;
86
- };
87
- httpOperations?: HttpOperations;
88
- auth?: unknown;
89
- proxy?: Operation;
90
- }