Skip to content

Commit

Permalink
Merge pull request #92 from seamapi/action-attempt-poll-fix
Browse files Browse the repository at this point in the history
Fix action attempt poll logic
  • Loading branch information
andrii-balitskyi authored Feb 23, 2024
2 parents d6f00eb + 7942a2d commit 40da3a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
24 changes: 7 additions & 17 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ContextHelpers } from "./lib/types"
import { version } from "./package.json"
import { interactForUseRemoteApiDefs } from "./lib/interact-for-use-remote-api-defs"
import { randomBytes } from "node:crypto"
import { interactForActionAttemptPoll } from "./lib/interact-for-action-attempt-poll"

const sections = [
{
Expand Down Expand Up @@ -245,23 +246,12 @@ async function cli(args: ParsedArgs) {
}
}

if ("action_attempt" in response.data) {
const { poll_for_action_attempt } = await prompts({
name: "poll_for_action_attempt",
message: "Would you like to poll the action attempt until it's ready?",
type: "toggle",
initial: true,
active: "yes",
inactive: "no",
})

if (poll_for_action_attempt) {
const { action_attempt_id } = response.data.action_attempt
await seam.actionAttempts.get(
{ action_attempt_id },
{ waitForActionAttempt: { pollingInterval: 240, timeout: 10_000 } }
)
}
if (
response.data &&
typeof response.data === "object" &&
"action_attempt" in response.data
) {
interactForActionAttemptPoll(response.data.action_attempt)
}
}

Expand Down
30 changes: 30 additions & 0 deletions lib/interact-for-action-attempt-poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { ActionAttemptsGetResponse } from "@seamapi/http/connect"

export const interactForActionAttemptPoll = async (
action_attempt: ActionAttemptsGetResponse["action_attempt"]
) => {
if (action_attempt.status === "pending") {
const { poll_for_action_attempt } = await prompts({
name: "poll_for_action_attempt",
message: "Would you like to poll the action attempt until it's ready?",
type: "toggle",
initial: true,
active: "yes",
inactive: "no",
})

if (poll_for_action_attempt) {
const seam = await getSeam()
const { action_attempt_id } = action_attempt

const updated_action_attempt = await seam.actionAttempts.get(
{ action_attempt_id },
{ waitForActionAttempt: { pollingInterval: 240, timeout: 10_000 } }
)

console.dir(updated_action_attempt, { depth: null })
}
}
}

0 comments on commit 40da3a4

Please sign in to comment.