@stackone/connect-sdk 1.12.1 → 1.12.2

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{StepFunctionsFactory as t,StepFunctionName as e,AUTHENTICATION_SCHEMA as n}from"@stackone/core";import{safeEvaluate as o}from"@stackone/expressions";import{notMissing as r}from"@stackone/utils";import{parse as i}from"yaml";import{z as s}from"zod";import{HttpClientManager as a}from"@stackone/transport";import{match as c}from"path-to-regexp";const p=async({block:n,stepFunctionName:o,params:r,buildStepFunction:i=t.build})=>{const s=i({functionName:o}).fn,a=await s({block:n,params:r}),c=o===e.MAP_FIELDS?{[e.MAP_FIELDS.toString()]:{output:{data:a.block.result},errors:a.errors,successful:a.successful}}:{};return{...a.block,steps:{...a?.block?.steps??{},...c}}},u=async({block:e,buildStepFunction:n=t.build})=>{const r=e.operation?.steps||[];let i={...e};const s=Object.keys(r);for(const t of s){const e=r[t].stepFunction,o=n({functionName:e.functionName,version:e.version,validateSchemas:!0}).fn,s=await o({block:i,params:e.params});i={...s.block,steps:{...s.block.steps,[t]:{successful:s.successful,errors:s.errors,output:s.output}}}}const a=e.operation?.result?o(e.operation.result,i):void 0;return{...i,outputs:a}},l=s.object({key:s.string(),label:s.string(),type:s.string(),required:s.boolean().optional(),secret:s.boolean().optional(),placeholder:s.string().optional(),description:s.string().optional(),tooltip:s.string().optional()}),d=s.object({operationId:s.string(),categories:s.string().array(),operationType:s.string(),schema:s.string().optional(),entrypointUrl:s.string().optional(),entrypointHttpMethod:s.string().optional(),description:s.string(),inputs:s.object({name:s.string(),type:s.string(),required:s.boolean(),description:s.string(),in:s.string()}).array().optional(),fieldConfigs:s.object({targetFieldKey:s.string(),expression:s.string(),type:s.string(),isUnified:s.boolean()}).array(),steps:s.object({stepId:s.string(),description:s.string(),stepFunction:s.object({functionName:s.string(),version:s.string().optional(),parameters:s.record(s.string(),s.unknown())})}).array(),result:s.string()}),g=s.object({StackOne:s.string(),info:s.object({title:s.string(),version:s.string(),key:s.string(),description:s.string().optional()}),baseUrl:s.string(),authentication:s.record(s.string(),s.object({type:s.string(),label:s.string(),authorization:n,environments:s.object({key:s.string(),name:s.string()}).array(),support:s.object({link:s.string(),description:s.string().optional()}),configFields:l.array().optional(),setupFields:l.array().optional(),testOperationsIds:s.string().array().optional()})).array().optional(),operations:d.array().optional()});function y(t){try{const e=i(t),n=g.parse(e),o={title:n.info.title,version:n.info.version,key:n.info.key,description:n.info.description},s=b(n),a={baseUrl:n.baseUrl,authentication:f(s)},c=h(n,a);return o.operations=c,o.authentication=s,r(c)&&(o.categories=m(Object.values(c))),o}catch(t){throw new Error(`Error parsing YAML file: ${t.message}`)}}const f=t=>{const e={};for(const[n,o]of Object.entries(t))if(o&&"object"==typeof o){const{setupFields:t,configFields:r,support:i,testOperationsIds:s,...a}=o;e[n]=f(a)}else e[n]=o;return e},m=t=>{const e=t.reduce(((t,e)=>{for(const n of e.categories)t.add(n);return t}),new Set);return Array.from(e)},b=t=>{const e={};for(const n of t.authentication??[]){const[t]=Object.keys(n),o=n[t].environments.reduce(((e,o)=>{const{key:r,name:i}=o,{environments:s,...a}=n[t];return e[r]={...a,envKey:r,envName:i},e}),{});e[t]=o}return e},h=(t,e)=>{const n=t.operations?.reduce(((t,n)=>{const o=(t=>t.entrypointUrl&&t.entrypointHttpMethod?`${t.entrypointHttpMethod.toUpperCase()} ${t.entrypointUrl}`:void 0)(n),r=(t=>"list"===t.operationType?`GET /${t.schema}`:`GET /${t.schema}/:id`)(n),i=(t=>"list"===t.operationType?`/${t.schema}`:`/${t.schema}/:id`)(n);return t[o??r]={id:n.operationId,categories:n.categories,description:n.description,operationType:n.operationType,entrypointUrl:n.entrypointUrl??i,entrypointHttpMethod:n.entrypointHttpMethod??"get",inputs:n.inputs,steps:n.steps.reduce(((t,o)=>(t[o.stepId]={id:o.stepId,description:o.description,stepFunction:{functionName:o.stepFunction.functionName,version:o.stepFunction.version,params:{..."request"===o.stepFunction.functionName?e:{},..."map_fields"===o.stepFunction.functionName||"typecast"===o.stepFunction.functionName?{fields:n.fieldConfigs}:{},...o.stepFunction.parameters}}},t)),{}),result:n.result},t}),{});return n},k=(t,e)=>{if(!t.inputs)return{};const n=v(t.inputs).parse(e);return{...n.headers??{},...n.query??{},...n.path??{},...n.body??{}}},j=t=>{const e={};return t.forEach((t=>{let n;switch(t.type.toLowerCase()){case"string":n=s.string();break;case"number":n=s.number();break;case"boolean":n=s.boolean();break;default:n=s.any()}e[t.name]=t.required?n:n.optional()})),s.object(e)},v=t=>{const e=t.filter((t=>"path"===t.in)),n=t.filter((t=>"query"===t.in)),o=t.filter((t=>"body"===t.in)),r=t.filter((t=>"headers"===t.in));return s.object({path:j(e).optional(),query:j(n).optional(),body:j(o).optional(),headers:j(r).optional()})},F=async({inputs:t,category:e,connectorKey:n,connectorVersion:o,authConfigKey:r,environment:i="production",operation:s,accountSecureId:c,projectSecureId:p,credentials:u,logger:l,getHttpClient:d=async()=>a.getInstance()})=>{const g=await d();return{inputs:t,fieldConfigs:[],context:{projectSecureId:p,accountSecureId:c,connectorKey:n,connectorVersion:o,category:e,schema:s.schema?.key,service:"",resource:"",operationType:s.operationType,authenticationType:r,environment:i},operation:s,credentials:u,httpClient:g,logger:l}},I=(t,e,n)=>{const o=n.toUpperCase();if(!t.operations)return;const r=Object.keys(t.operations),i=N(e,o,r);return i?{operation:t.operations?.[i.path],params:i.params}:void 0},S=t=>{const e=t.startsWith("/")?t.slice(1):t;return e.endsWith("/")?e.slice(0,-1):e},N=(t,e,n)=>{const o=S(t);for(const t of n)if(t.startsWith(e)){const n=t.replace(`${e} `,"").trim(),r=c(S(n))(S(o));if(!1!==r)return{path:t,params:r.params}}};export{F as createBlock,p as executeStepFunction,I as getOperationFromUrl,k as parseOperationInputs,y as parseYamlConnector,u as runStepOperation};
1
+ import{StepFunctionsFactory as t,StepFunctionName as e,AUTHENTICATION_SCHEMA as r}from"@stackone/core";import{safeEvaluate as n}from"@stackone/expressions";import{notMissing as o,isString as s,isMissing as i}from"@stackone/utils";import{HttpClientManager as a}from"@stackone/transport";import{match as c}from"path-to-regexp";import{parse as p}from"yaml";import{z as u}from"zod";const l=async({block:r,stepFunctionName:n,params:o,buildStepFunction:s=t.build})=>{const i=s({functionName:n}).fn,a=await i({block:r,params:o}),c=n===e.MAP_FIELDS?{[e.MAP_FIELDS.toString()]:{output:{data:a.block.result},errors:a.errors,successful:a.successful}}:{};return{...a.block,steps:{...a?.block?.steps??{},...c}}},d=async({block:e,buildStepFunction:r=t.build})=>{const o=e.operation?.steps||[];let s={...e};const i=Object.keys(o);for(const t of i){const e=o[t].stepFunction,n=r({functionName:e.functionName,version:e.version,validateSchemas:!0}).fn,i=await n({block:s,params:e.params});s={...i.block,steps:{...i.block.steps,[t]:{successful:i.successful,errors:i.errors,output:i.output}}}}const a=e.operation?.result?n(e.operation.result,s):void 0;return{...s,outputs:a}},g=async({inputs:t,category:e,connectorKey:r,connectorVersion:n,authConfigKey:o,environment:s="production",operation:i,accountSecureId:c,projectSecureId:p,credentials:u,logger:l,getHttpClient:d=async()=>a.getInstance()})=>{const g=await d();return{inputs:t,fieldConfigs:[],context:{projectSecureId:p,accountSecureId:c,connectorKey:r,connectorVersion:n,category:e,schema:i.schema?.key,service:"",resource:"",operationType:i.operationType,authenticationType:o,environment:s},operation:i,credentials:u,httpClient:g,logger:l}},y=(t,e,r)=>{const n=r.toUpperCase();if(!t.operations)return;const o=Object.keys(t.operations),s=f(e,n,o);return s?{operation:t.operations?.[s.path],params:s.params}:void 0},m=t=>{const e=t.startsWith("/")?t.slice(1):t;return e.endsWith("/")?e.slice(0,-1):e},f=(t,e,r)=>{const n=m(t);for(const t of r)if(t.startsWith(e)){const r=t.replace(`${e} `,"").trim(),o=c(m(r))(m(n));if(!1!==o)return{path:t,params:o.params}}},h=u.object({key:u.string(),label:u.string(),type:u.string(),required:u.boolean().optional(),secret:u.boolean().optional(),placeholder:u.string().optional(),description:u.string().optional(),tooltip:u.string().optional()}),b=u.object({operationId:u.string(),categories:u.string().array(),operationType:u.string(),schema:u.string().optional(),entrypointUrl:u.string().optional(),entrypointHttpMethod:u.string().optional(),description:u.string(),inputs:u.object({name:u.string(),type:u.string(),required:u.boolean(),description:u.string(),in:u.string()}).array().optional(),fieldConfigs:u.object({targetFieldKey:u.string(),expression:u.string(),type:u.string(),isUnified:u.boolean()}).array(),steps:u.object({stepId:u.string(),description:u.string(),stepFunction:u.object({functionName:u.string(),version:u.string().optional(),parameters:u.record(u.string(),u.unknown())})}).array(),result:u.string()}),k=u.object({StackOne:u.string(),info:u.object({title:u.string(),version:u.string(),key:u.string(),description:u.string().optional()}),baseUrl:u.string(),authentication:u.record(u.string(),u.object({type:u.string(),label:u.string(),authorization:r,environments:u.object({key:u.string(),name:u.string()}).array(),support:u.object({link:u.string(),description:u.string().optional()}),configFields:h.array().optional(),setupFields:h.array().optional(),testOperationsIds:u.string().array().optional()})).array().optional(),operations:b.array().optional()});function I(t){try{const e=p(t),r=k.parse(e),n={title:r.info.title,version:r.info.version,key:r.info.key,description:r.info.description},s=S(r),i={baseUrl:r.baseUrl,authentication:v(s)},a=j(r,i);return n.operations=a,n.authentication=s,o(a)&&(n.categories=F(Object.values(a))),n}catch(t){throw new Error(`Error parsing YAML file: ${t.message}`)}}const v=t=>{const e={};for(const[r,n]of Object.entries(t))if(n&&"object"==typeof n){const{setupFields:t,configFields:o,support:s,testOperationsIds:i,...a}=n;e[r]=v(a)}else e[r]=n;return e},F=t=>{const e=t.reduce(((t,e)=>{for(const r of e.categories)t.add(r);return t}),new Set);return Array.from(e)},S=t=>{const e={};for(const r of t.authentication??[]){const[t]=Object.keys(r),n=r[t].environments.reduce(((e,n)=>{const{key:o,name:s}=n,{environments:i,...a}=r[t];return e[o]={...a,envKey:o,envName:s},e}),{});e[t]=n}return e},j=(t,e)=>{const r=t.operations?.reduce(((t,r)=>{const n=(t=>t.entrypointUrl&&t.entrypointHttpMethod?`${t.entrypointHttpMethod.toUpperCase()} ${t.entrypointUrl}`:void 0)(r),o=(t=>"list"===t.operationType?`GET /${t.schema}`:`GET /${t.schema}/:id`)(r),s=(t=>"list"===t.operationType?`/${t.schema}`:`/${t.schema}/:id`)(r);return t[n??o]={id:r.operationId,categories:r.categories,description:r.description,operationType:r.operationType,entrypointUrl:r.entrypointUrl??s,entrypointHttpMethod:r.entrypointHttpMethod??"get",inputs:r.inputs,steps:r.steps.reduce(((t,n)=>(t[n.stepId]={id:n.stepId,description:n.description,stepFunction:{functionName:n.stepFunction.functionName,version:n.stepFunction.version,params:{..."request"===n.stepFunction.functionName?e:{},..."map_fields"===n.stepFunction.functionName||"typecast"===n.stepFunction.functionName?{fields:r.fieldConfigs}:{},...n.stepFunction.parameters}}},t)),{}),result:r.result},t}),{});return r},E=(t,e)=>{if(!t.inputs)return{};const r=w(t.inputs).parse(e);return{...r.headers??{},...r.query??{},...r.path??{},...r.body??{}}},O=t=>{const e={};return t.forEach((t=>{let r;switch(t.type.toLowerCase()){case"string":r=u.string();break;case"number":r=u.number();break;case"boolean":r=u.boolean();break;default:r=u.any()}e[t.name]=t.required?r:r.optional()})),u.object(e)},w=t=>{const e=t.filter((t=>"path"===t.in)),r=t.filter((t=>"query"===t.in)),n=t.filter((t=>"body"===t.in)),o=t.filter((t=>"headers"===t.in));return u.object({path:O(e).optional(),query:O(r).optional(),body:O(n).optional(),headers:O(o).optional()})};class N extends Error{constructor(t,e){super(e),this.name="ConnectSDKError",this.errorType=t,Error.captureStackTrace&&Error.captureStackTrace(this,N)}toString(){return`${this.name} [${this.errorType}]: ${this.message}`}}class T extends N{constructor(t){super("CONNECTOR_PARSE_ERROR",t),this.name="ConnectorParseError"}}class C extends N{constructor(t){super("MISSING_OPERATION_ERROR",t),this.name="MissingOperationError"}}class R extends N{constructor(t){super("INVALID_OPERATION_INPUTS_ERROR",t),this.name="InvalidOperationInputsError"}}const U=async({account:t,connector:e,category:r,path:n,method:o="get",queryParams:a,logger:c,parseConnector:p=I,getOperationFromUrlFn:u=y,parseOperationInputsFn:l=E,createBlockFn:m=g,runStepOperationFn:f=d})=>{const h=t.authConfigKey,b=t.environment??"production",k=t.secureId,v=t.projectSecureId,F=t.credentials;let S;try{S=s(e)?p(e):e}catch{throw new T("Error while parsing connector")}const j=u(S,n,o);if(i(j))throw new C("No matching operation found");let O;try{O=l(j.operation,{query:a,path:j.params})}catch{throw new R("Error while parsing operation inputs")}const w=await m({inputs:O,category:r,connectorKey:S.key,connectorVersion:S.version,authConfigKey:h,environment:b,operation:j.operation,accountSecureId:k,projectSecureId:v,credentials:F,logger:c});return await f({block:w})};export{N as ConnectSDKError,g as createBlock,l as executeStepFunction,y as getOperationFromUrl,E as parseOperationInputs,I as parseYamlConnector,U as runConnectorOperation,d as runStepOperation};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("@stackone/core"),e=require("@stackone/expressions"),n=require("@stackone/utils"),o=require("yaml"),r=require("zod"),i=require("@stackone/transport"),s=require("path-to-regexp");const a=r.z.object({key:r.z.string(),label:r.z.string(),type:r.z.string(),required:r.z.boolean().optional(),secret:r.z.boolean().optional(),placeholder:r.z.string().optional(),description:r.z.string().optional(),tooltip:r.z.string().optional()}),c=r.z.object({operationId:r.z.string(),categories:r.z.string().array(),operationType:r.z.string(),schema:r.z.string().optional(),entrypointUrl:r.z.string().optional(),entrypointHttpMethod:r.z.string().optional(),description:r.z.string(),inputs:r.z.object({name:r.z.string(),type:r.z.string(),required:r.z.boolean(),description:r.z.string(),in:r.z.string()}).array().optional(),fieldConfigs:r.z.object({targetFieldKey:r.z.string(),expression:r.z.string(),type:r.z.string(),isUnified:r.z.boolean()}).array(),steps:r.z.object({stepId:r.z.string(),description:r.z.string(),stepFunction:r.z.object({functionName:r.z.string(),version:r.z.string().optional(),parameters:r.z.record(r.z.string(),r.z.unknown())})}).array(),result:r.z.string()}),p=r.z.object({StackOne:r.z.string(),info:r.z.object({title:r.z.string(),version:r.z.string(),key:r.z.string(),description:r.z.string().optional()}),baseUrl:r.z.string(),authentication:r.z.record(r.z.string(),r.z.object({type:r.z.string(),label:r.z.string(),authorization:t.AUTHENTICATION_SCHEMA,environments:r.z.object({key:r.z.string(),name:r.z.string()}).array(),support:r.z.object({link:r.z.string(),description:r.z.string().optional()}),configFields:a.array().optional(),setupFields:a.array().optional(),testOperationsIds:r.z.string().array().optional()})).array().optional(),operations:c.array().optional()});const u=t=>{const e={};for(const[n,o]of Object.entries(t))if(o&&"object"==typeof o){const{setupFields:t,configFields:r,support:i,testOperationsIds:s,...a}=o;e[n]=u(a)}else e[n]=o;return e},l=t=>{const e=t.reduce(((t,e)=>{for(const n of e.categories)t.add(n);return t}),new Set);return Array.from(e)},d=t=>{const e={};for(const n of t.authentication??[]){const[t]=Object.keys(n),o=n[t].environments.reduce(((e,o)=>{const{key:r,name:i}=o,{environments:s,...a}=n[t];return e[r]={...a,envKey:r,envName:i},e}),{});e[t]=o}return e},g=(t,e)=>{const n=t.operations?.reduce(((t,n)=>{const o=(t=>t.entrypointUrl&&t.entrypointHttpMethod?`${t.entrypointHttpMethod.toUpperCase()} ${t.entrypointUrl}`:void 0)(n),r=(t=>"list"===t.operationType?`GET /${t.schema}`:`GET /${t.schema}/:id`)(n),i=(t=>"list"===t.operationType?`/${t.schema}`:`/${t.schema}/:id`)(n);return t[o??r]={id:n.operationId,categories:n.categories,description:n.description,operationType:n.operationType,entrypointUrl:n.entrypointUrl??i,entrypointHttpMethod:n.entrypointHttpMethod??"get",inputs:n.inputs,steps:n.steps.reduce(((t,o)=>(t[o.stepId]={id:o.stepId,description:o.description,stepFunction:{functionName:o.stepFunction.functionName,version:o.stepFunction.version,params:{..."request"===o.stepFunction.functionName?e:{},..."map_fields"===o.stepFunction.functionName||"typecast"===o.stepFunction.functionName?{fields:n.fieldConfigs}:{},...o.stepFunction.parameters}}},t)),{}),result:n.result},t}),{});return n},y=t=>{const e={};return t.forEach((t=>{let n;switch(t.type.toLowerCase()){case"string":n=r.z.string();break;case"number":n=r.z.number();break;case"boolean":n=r.z.boolean();break;default:n=r.z.any()}e[t.name]=t.required?n:n.optional()})),r.z.object(e)},z=t=>{const e=t.filter((t=>"path"===t.in)),n=t.filter((t=>"query"===t.in)),o=t.filter((t=>"body"===t.in)),i=t.filter((t=>"headers"===t.in));return r.z.object({path:y(e).optional(),query:y(n).optional(),body:y(o).optional(),headers:y(i).optional()})},f=t=>{const e=t.startsWith("/")?t.slice(1):t;return e.endsWith("/")?e.slice(0,-1):e},b=(t,e,n)=>{const o=f(t);for(const t of n)if(t.startsWith(e)){const n=t.replace(`${e} `,"").trim(),r=s.match(f(n))(f(o));if(!1!==r)return{path:t,params:r.params}}};exports.createBlock=async({inputs:t,category:e,connectorKey:n,connectorVersion:o,authConfigKey:r,environment:s="production",operation:a,accountSecureId:c,projectSecureId:p,credentials:u,logger:l,getHttpClient:d=async()=>i.HttpClientManager.getInstance()})=>{const g=await d();return{inputs:t,fieldConfigs:[],context:{projectSecureId:p,accountSecureId:c,connectorKey:n,connectorVersion:o,category:e,schema:a.schema?.key,service:"",resource:"",operationType:a.operationType,authenticationType:r,environment:s},operation:a,credentials:u,httpClient:g,logger:l}},exports.executeStepFunction=async({block:e,stepFunctionName:n,params:o,buildStepFunction:r=t.StepFunctionsFactory.build})=>{const i=r({functionName:n}).fn,s=await i({block:e,params:o}),a=n===t.StepFunctionName.MAP_FIELDS?{[t.StepFunctionName.MAP_FIELDS.toString()]:{output:{data:s.block.result},errors:s.errors,successful:s.successful}}:{};return{...s.block,steps:{...s?.block?.steps??{},...a}}},exports.getOperationFromUrl=(t,e,n)=>{const o=n.toUpperCase();if(!t.operations)return;const r=Object.keys(t.operations),i=b(e,o,r);return i?{operation:t.operations?.[i.path],params:i.params}:void 0},exports.parseOperationInputs=(t,e)=>{if(!t.inputs)return{};const n=z(t.inputs).parse(e);return{...n.headers??{},...n.query??{},...n.path??{},...n.body??{}}},exports.parseYamlConnector=function(t){try{const e=o.parse(t),r=p.parse(e),i={title:r.info.title,version:r.info.version,key:r.info.key,description:r.info.description},s=d(r),a={baseUrl:r.baseUrl,authentication:u(s)},c=g(r,a);return i.operations=c,i.authentication=s,n.notMissing(c)&&(i.categories=l(Object.values(c))),i}catch(t){throw new Error(`Error parsing YAML file: ${t.message}`)}},exports.runStepOperation=async({block:n,buildStepFunction:o=t.StepFunctionsFactory.build})=>{const r=n.operation?.steps||[];let i={...n};const s=Object.keys(r);for(const t of s){const e=r[t].stepFunction,n=o({functionName:e.functionName,version:e.version,validateSchemas:!0}).fn,s=await n({block:i,params:e.params});i={...s.block,steps:{...s.block.steps,[t]:{successful:s.successful,errors:s.errors,output:s.output}}}}const a=n.operation?.result?e.safeEvaluate(n.operation.result,i):void 0;return{...i,outputs:a}};
1
+ "use strict";var t=require("@stackone/core"),e=require("@stackone/expressions"),r=require("@stackone/utils"),n=require("@stackone/transport"),o=require("path-to-regexp"),s=require("yaml"),i=require("zod");const a=async({block:r,buildStepFunction:n=t.StepFunctionsFactory.build})=>{const o=r.operation?.steps||[];let s={...r};const i=Object.keys(o);for(const t of i){const e=o[t].stepFunction,r=n({functionName:e.functionName,version:e.version,validateSchemas:!0}).fn,i=await r({block:s,params:e.params});s={...i.block,steps:{...i.block.steps,[t]:{successful:i.successful,errors:i.errors,output:i.output}}}}const a=r.operation?.result?e.safeEvaluate(r.operation.result,s):void 0;return{...s,outputs:a}},c=async({inputs:t,category:e,connectorKey:r,connectorVersion:o,authConfigKey:s,environment:i="production",operation:a,accountSecureId:c,projectSecureId:p,credentials:u,logger:l,getHttpClient:d=async()=>n.HttpClientManager.getInstance()})=>{const g=await d();return{inputs:t,fieldConfigs:[],context:{projectSecureId:p,accountSecureId:c,connectorKey:r,connectorVersion:o,category:e,schema:a.schema?.key,service:"",resource:"",operationType:a.operationType,authenticationType:s,environment:i},operation:a,credentials:u,httpClient:g,logger:l}},p=(t,e,r)=>{const n=r.toUpperCase();if(!t.operations)return;const o=Object.keys(t.operations),s=l(e,n,o);return s?{operation:t.operations?.[s.path],params:s.params}:void 0},u=t=>{const e=t.startsWith("/")?t.slice(1):t;return e.endsWith("/")?e.slice(0,-1):e},l=(t,e,r)=>{const n=u(t);for(const t of r)if(t.startsWith(e)){const r=t.replace(`${e} `,"").trim(),s=o.match(u(r))(u(n));if(!1!==s)return{path:t,params:s.params}}},d=i.z.object({key:i.z.string(),label:i.z.string(),type:i.z.string(),required:i.z.boolean().optional(),secret:i.z.boolean().optional(),placeholder:i.z.string().optional(),description:i.z.string().optional(),tooltip:i.z.string().optional()}),g=i.z.object({operationId:i.z.string(),categories:i.z.string().array(),operationType:i.z.string(),schema:i.z.string().optional(),entrypointUrl:i.z.string().optional(),entrypointHttpMethod:i.z.string().optional(),description:i.z.string(),inputs:i.z.object({name:i.z.string(),type:i.z.string(),required:i.z.boolean(),description:i.z.string(),in:i.z.string()}).array().optional(),fieldConfigs:i.z.object({targetFieldKey:i.z.string(),expression:i.z.string(),type:i.z.string(),isUnified:i.z.boolean()}).array(),steps:i.z.object({stepId:i.z.string(),description:i.z.string(),stepFunction:i.z.object({functionName:i.z.string(),version:i.z.string().optional(),parameters:i.z.record(i.z.string(),i.z.unknown())})}).array(),result:i.z.string()}),y=i.z.object({StackOne:i.z.string(),info:i.z.object({title:i.z.string(),version:i.z.string(),key:i.z.string(),description:i.z.string().optional()}),baseUrl:i.z.string(),authentication:i.z.record(i.z.string(),i.z.object({type:i.z.string(),label:i.z.string(),authorization:t.AUTHENTICATION_SCHEMA,environments:i.z.object({key:i.z.string(),name:i.z.string()}).array(),support:i.z.object({link:i.z.string(),description:i.z.string().optional()}),configFields:d.array().optional(),setupFields:d.array().optional(),testOperationsIds:i.z.string().array().optional()})).array().optional(),operations:g.array().optional()});function z(t){try{const e=s.parse(t),n=y.parse(e),o={title:n.info.title,version:n.info.version,key:n.info.key,description:n.info.description},i=h(n),a={baseUrl:n.baseUrl,authentication:m(i)},c=b(n,a);return o.operations=c,o.authentication=i,r.notMissing(c)&&(o.categories=f(Object.values(c))),o}catch(t){throw new Error(`Error parsing YAML file: ${t.message}`)}}const m=t=>{const e={};for(const[r,n]of Object.entries(t))if(n&&"object"==typeof n){const{setupFields:t,configFields:o,support:s,testOperationsIds:i,...a}=n;e[r]=m(a)}else e[r]=n;return e},f=t=>{const e=t.reduce(((t,e)=>{for(const r of e.categories)t.add(r);return t}),new Set);return Array.from(e)},h=t=>{const e={};for(const r of t.authentication??[]){const[t]=Object.keys(r),n=r[t].environments.reduce(((e,n)=>{const{key:o,name:s}=n,{environments:i,...a}=r[t];return e[o]={...a,envKey:o,envName:s},e}),{});e[t]=n}return e},b=(t,e)=>{const r=t.operations?.reduce(((t,r)=>{const n=(t=>t.entrypointUrl&&t.entrypointHttpMethod?`${t.entrypointHttpMethod.toUpperCase()} ${t.entrypointUrl}`:void 0)(r),o=(t=>"list"===t.operationType?`GET /${t.schema}`:`GET /${t.schema}/:id`)(r),s=(t=>"list"===t.operationType?`/${t.schema}`:`/${t.schema}/:id`)(r);return t[n??o]={id:r.operationId,categories:r.categories,description:r.description,operationType:r.operationType,entrypointUrl:r.entrypointUrl??s,entrypointHttpMethod:r.entrypointHttpMethod??"get",inputs:r.inputs,steps:r.steps.reduce(((t,n)=>(t[n.stepId]={id:n.stepId,description:n.description,stepFunction:{functionName:n.stepFunction.functionName,version:n.stepFunction.version,params:{..."request"===n.stepFunction.functionName?e:{},..."map_fields"===n.stepFunction.functionName||"typecast"===n.stepFunction.functionName?{fields:r.fieldConfigs}:{},...n.stepFunction.parameters}}},t)),{}),result:r.result},t}),{});return r},k=(t,e)=>{if(!t.inputs)return{};const r=F(t.inputs).parse(e);return{...r.headers??{},...r.query??{},...r.path??{},...r.body??{}}},S=t=>{const e={};return t.forEach((t=>{let r;switch(t.type.toLowerCase()){case"string":r=i.z.string();break;case"number":r=i.z.number();break;case"boolean":r=i.z.boolean();break;default:r=i.z.any()}e[t.name]=t.required?r:r.optional()})),i.z.object(e)},F=t=>{const e=t.filter((t=>"path"===t.in)),r=t.filter((t=>"query"===t.in)),n=t.filter((t=>"body"===t.in)),o=t.filter((t=>"headers"===t.in));return i.z.object({path:S(e).optional(),query:S(r).optional(),body:S(n).optional(),headers:S(o).optional()})};class I extends Error{constructor(t,e){super(e),this.name="ConnectSDKError",this.errorType=t,Error.captureStackTrace&&Error.captureStackTrace(this,I)}toString(){return`${this.name} [${this.errorType}]: ${this.message}`}}class v extends I{constructor(t){super("CONNECTOR_PARSE_ERROR",t),this.name="ConnectorParseError"}}class E extends I{constructor(t){super("MISSING_OPERATION_ERROR",t),this.name="MissingOperationError"}}class O extends I{constructor(t){super("INVALID_OPERATION_INPUTS_ERROR",t),this.name="InvalidOperationInputsError"}}exports.ConnectSDKError=I,exports.createBlock=c,exports.executeStepFunction=async({block:e,stepFunctionName:r,params:n,buildStepFunction:o=t.StepFunctionsFactory.build})=>{const s=o({functionName:r}).fn,i=await s({block:e,params:n}),a=r===t.StepFunctionName.MAP_FIELDS?{[t.StepFunctionName.MAP_FIELDS.toString()]:{output:{data:i.block.result},errors:i.errors,successful:i.successful}}:{};return{...i.block,steps:{...i?.block?.steps??{},...a}}},exports.getOperationFromUrl=p,exports.parseOperationInputs=k,exports.parseYamlConnector=z,exports.runConnectorOperation=async({account:t,connector:e,category:n,path:o,method:s="get",queryParams:i,logger:u,parseConnector:l=z,getOperationFromUrlFn:d=p,parseOperationInputsFn:g=k,createBlockFn:y=c,runStepOperationFn:m=a})=>{const f=t.authConfigKey,h=t.environment??"production",b=t.secureId,S=t.projectSecureId,F=t.credentials;let I;try{I=r.isString(e)?l(e):e}catch{throw new v("Error while parsing connector")}const j=d(I,o,s);if(r.isMissing(j))throw new E("No matching operation found");let N;try{N=g(j.operation,{query:i,path:j.params})}catch{throw new O("Error while parsing operation inputs")}const C=await y({inputs:N,category:n,connectorKey:I.key,connectorVersion:I.version,authConfigKey:f,environment:h,operation:j.operation,accountSecureId:b,projectSecureId:S,credentials:F,logger:u});return await m({block:C})},exports.runStepOperation=a;
@@ -1,5 +1,8 @@
1
1
  export { executeStepFunction } from './runners/executeStepFunctions';
2
2
  export { runStepOperation } from './runners/runStepOperation';
3
+ export { runConnectorOperation } from './runners/runConnectorOperation';
3
4
  export { parseYamlConnector, parseOperationInputs } from './connectors/parsers';
4
5
  export { createBlock } from './blocks/createBlock';
5
6
  export { getOperationFromUrl } from './connectors/operations';
7
+ export { ConnectSDKError } from './errors/connectSDKErrors';
8
+ export { ErrorType } from './errors/types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackone/connect-sdk",
3
- "version": "1.12.1",
3
+ "version": "1.12.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.mjs",