@integration-app/sdk
Advanced tools
Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "@integration-app/sdk", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "JavaScript SDK for Integration.app", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -0,0 +0,0 @@ import commonjs from 'rollup-plugin-commonjs' |
@@ -0,0 +0,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
import Pusher, { Channel } from 'pusher-js' | ||
import Axios, { AxiosRequestConfig } from 'axios' | ||
@@ -21,2 +22,17 @@ import { openIframe } from './iframe' | ||
type FlowRun = { | ||
id: string | ||
state: FlowRunState | ||
nodeRuns: FlowNodeRun[] | ||
} & { | ||
[prop: string]: any | ||
} | ||
type FlowNodeRun = { | ||
id: string | ||
state: FlowNodeRunState | ||
} & { | ||
[prop: string]: any | ||
} | ||
export interface IntegrationAppClientOptions { | ||
@@ -35,2 +51,3 @@ accessToken?: string | ||
private accessToken = '' | ||
private pusher: Pusher | undefined | ||
@@ -57,4 +74,5 @@ constructor(options: IntegrationAppClientOptions) { | ||
public async findFlows(options: FindFlowsOptions) { | ||
const queryParams: Record<string, string> = { | ||
blueprintKey: options.blueprintKey, | ||
const queryParams: Record<string, string> = {} | ||
if (options.blueprintKey) { | ||
queryParams.blueprintKey = options.blueprintKey | ||
} | ||
@@ -138,22 +156,51 @@ if (options.parameters) { | ||
/** | ||
* TODO: Probably change `flowId` to `flow` | ||
* or support both `flow` and `flowId` | ||
* */ | ||
public async runFlow(flowId: string, options: RunFlowOptions = {}) { | ||
const { id: flowRunId } = await this.post('flow-runs', { flowId: flowId }) | ||
let flowRun: Record<string, any> | ||
const pusher = await this.getPusherInstance() | ||
const pusherChannelName = `private-flow-run-updates-${flowRunId}` | ||
const pusherEventNameFlowRun = 'flow-run-update' | ||
const pusherEventNameFlowNodeRuns = 'flow-node-run-update' | ||
let pusherChannel: Channel | ||
let flowRun: FlowRun | ||
let checkFlowRunInterval: ReturnType<typeof setInterval> | ||
let checkFlowNodeRunsInterval: ReturnType<typeof setInterval> | ||
const thisGet = this.get.bind(this) | ||
const fetchFlowRun = async () => this.get(`flow-runs/${flowRunId}`) | ||
const fetchFlowNodeRuns = async () => | ||
this.get(`flow-node-runs?filter[flowRunId]=${flowRunId}`) | ||
const fetchFlowNodeRun = async (flowNodeRunId: string) => | ||
this.get(`flow-node-runs/${flowNodeRunId}`) | ||
function updateFlowRun(nextFlowRun: Record<string, any>) { | ||
function updateFlowRun(nextFlowRun: FlowRun) { | ||
flowRun = { ...flowRun, ...nextFlowRun } | ||
} | ||
function updateFlowNodeRuns(nextFlowNodeRuns: any[]) { | ||
flowRun = { ...flowRun, nodeRuns: nextFlowNodeRuns } | ||
function updateSingleFlowNodeRun(nextFlowNodeRun: FlowNodeRun) { | ||
const nodeRuns = flowRun?.nodeRuns ?? [] | ||
const idx = nodeRuns.findIndex((r) => r.id == nextFlowNodeRun.id) | ||
if (idx == -1) { | ||
nodeRuns.push(nextFlowNodeRun) | ||
} else { | ||
nodeRuns[idx] = nextFlowNodeRun | ||
} | ||
updateFlowNodeRuns(nodeRuns) | ||
} | ||
function updateFlowNodeRuns(flowNodeRunsNext: FlowNodeRun[]) { | ||
flowRun = { ...flowRun, nodeRuns: flowNodeRunsNext } | ||
} | ||
function cleanup() { | ||
checkFlowRunInterval && clearInterval(checkFlowRunInterval) | ||
checkFlowNodeRunsInterval && clearInterval(checkFlowNodeRunsInterval) | ||
pusherChannel && pusherChannel.unbind(pusherEventNameFlowRun) | ||
pusherChannel && pusherChannel.unbind(pusherEventNameFlowNodeRuns) | ||
pusher && pusher.unsubscribe(pusherChannelName) | ||
} | ||
@@ -163,15 +210,13 @@ | ||
if (!flowRun?.nodeRuns) { | ||
return | ||
const latestNodeRuns = await fetchFlowNodeRuns() | ||
updateFlowNodeRuns(latestNodeRuns) | ||
} | ||
const flowNodeRunFetches = flowRun.nodeRuns.map( | ||
(nodeRun: Record<string, any>) => { | ||
if (nodeRun?.state === FlowNodeRunState.COMPLETED) { | ||
return nodeRun | ||
} | ||
return thisGet(`flow-node-runs/${nodeRun.id}`) | ||
}, | ||
) | ||
const flowNodeRunFetches = flowRun.nodeRuns.map((nodeRun) => { | ||
return nodeRun?.state === FlowNodeRunState.COMPLETED | ||
? nodeRun | ||
: fetchFlowNodeRun(nodeRun.id) | ||
}) | ||
const flowNodeRuns = await Promise.all([...flowNodeRunFetches]) | ||
const flowNodeRuns = await Promise.all(flowNodeRunFetches) | ||
updateFlowNodeRuns(flowNodeRuns) | ||
@@ -181,2 +226,17 @@ } | ||
return new Promise((resolve, reject) => { | ||
async function handleFlowRunUpdate(nextFlowRun: FlowRun) { | ||
updateFlowRun(nextFlowRun) | ||
switch (nextFlowRun.state) { | ||
case FlowRunState.COMPLETED: | ||
await onSuccess() | ||
break | ||
case FlowRunState.FAILED: | ||
await onFailure() | ||
break | ||
default: | ||
onUpdate() | ||
} | ||
} | ||
function onUpdate() { | ||
@@ -201,29 +261,29 @@ options.onUpdate && options.onUpdate(flowRun) | ||
/* POLLING */ | ||
checkFlowNodeRunsInterval = setInterval(async () => { | ||
const flowNodeRunsNext = await this.get( | ||
`flow-node-runs?filter[flowRunId]=${flowRunId}`, | ||
) | ||
updateFlowNodeRuns(flowNodeRunsNext) | ||
checkFlowRunInterval = setInterval(async () => { | ||
const nextFlowRun = await fetchFlowRun() | ||
await handleFlowRunUpdate(nextFlowRun) | ||
}, 1000) | ||
checkFlowRunInterval = setInterval(async () => { | ||
const flowRunLatest = await thisGet(`flow-runs/${flowRunId}`) | ||
updateFlowRun(flowRunLatest) | ||
switch (flowRunLatest.state) { | ||
case FlowRunState.COMPLETED: | ||
await onSuccess() | ||
break | ||
case FlowRunState.FAILED: | ||
await onFailure() | ||
break | ||
default: | ||
onUpdate() | ||
} | ||
checkFlowNodeRunsInterval = setInterval(async () => { | ||
const nextFlowNodeRuns = await fetchFlowNodeRuns() | ||
updateFlowNodeRuns(nextFlowNodeRuns) | ||
}, 1000) | ||
/* PUSHER */ | ||
if (pusher) { | ||
pusherChannel = pusher.subscribe(pusherChannelName) | ||
pusherChannel.bind(pusherEventNameFlowRun, handleFlowRunUpdate) | ||
pusherChannel.bind(pusherEventNameFlowNodeRuns, updateSingleFlowNodeRun) | ||
} | ||
}) | ||
} | ||
public async findFlowRuns(flowId: string) { | ||
return this.get(`flow-runs?filter[flowId]=${flowId}`) | ||
} | ||
/** | ||
* TODO: Probably change `flowNodeRunId` to `flowNodeRun` | ||
* or support both `flowNodeRun` and `flowNodeRunId` | ||
* */ | ||
public async getFlowNodeRunOutput(flowNodeRunId: string) { | ||
@@ -261,2 +321,14 @@ return this.get(`flow-node-runs/${flowNodeRunId}/output`) | ||
private async getPusherInstance(): Promise<Pusher> { | ||
if (!this.pusher) { | ||
const { key, cluster } = await this.get('updates/pusher-config') | ||
this.pusher = new Pusher(key, { | ||
cluster: cluster, | ||
authEndpoint: this.apiUri + '/updates/auth', | ||
auth: { headers: { Authorization: `Bearer ${this.accessToken}` } }, | ||
}) | ||
} | ||
return this.pusher | ||
} | ||
public async get(uri: string) { | ||
@@ -329,4 +401,4 @@ return this.makeApiRequest('GET', { url: uri }) | ||
export interface FindFlowsOptions { | ||
blueprintKey: string | ||
blueprintKey?: string | ||
parameters?: Record<string, any> | ||
} |
@@ -0,0 +0,0 @@ import { connectToChild } from 'penpal' |
@@ -0,0 +0,0 @@ import { IntegrationEngineClient } from './client' |
@@ -0,0 +0,0 @@ { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
16762
7
494
2