Hi,
I am trying to write a function that uses the Architect Scripting SDK to extract a flow's YAML and Validation Issues. In the self-contained example below I capture these as output
. However, my code has two problems:
- The process is hanging, suggesting I'm not handling a promise properly
- The use of Promises and Callbacks is making it hard to determine how I should be chaining promises
It would be exceedingly helpful if someone with experience of this SDK wouldn't mind casting their discerning eyes over my code, and offering some improvements please? I'm certain even this small amount of the code is committing many sins.
import architectScripting, {
ArchValidationIssue,
ArchValidationResults,
} from 'purecloud-flow-scripting-api-sdk-javascript';
const scriptingSession = architectScripting.environment.archSession;
const flowFactory = architectScripting.factories.archFactoryFlows;
const archEnums = architectScripting.enums.archEnums;
const flowId = process.env.FLOW_ID!;
const region = process.env.GENESYSCLOUD_REGION!;
const oAuthClientId = process.env.GENESYSCLOUD_OAUTHCLIENT_ID!;
const oAuthClientSecret = process.env.GENESYSCLOUD_OAUTHCLIENT_SECRET!;
// In the production code I want to continue to process the `output` variable,
// so do not want the process to be terminated
scriptingSession.endTerminatesProcess = false;
(async () => {
const output: {
issues?: ArchValidationIssue[];
flowYaml?: { content: string; filename: string };
} = {};
await scriptingSession.startWithClientIdAndSecret(
region,
() => {
return flowFactory.loadFlowByFlowIdAsync(
flowId,
archEnums.FLOW_TYPES.inboundShortMessage,
'latest',
(flow) => {
return Promise.all([
flow.exportToObjectAsync(
(exportObject) => (output.flowYaml = exportObject),
archEnums.FLOW_FORMAT_TYPES.yaml,
),
flow.validateAsync().then((a: ArchValidationResults) => {
output.issues = a.issues;
}),
]);
},
);
},
oAuthClientId,
oAuthClientSecret,
void 0,
true,
);
return output;
})()
.then(
// In production version I will be processing the result of the async above
console.dir
)
.catch(console.error);
Many thanks!