Skip to content

Commit

Permalink
Add loading state while seam API is requested (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi authored Nov 4, 2024
1 parent 8b2e218 commit 3378ae7
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 23 deletions.
9 changes: 6 additions & 3 deletions lib/interact-for-access-code.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { interactForDevice } from "./interact-for-device"
import { withLoading } from "./util/with-loading"

export const interactForAccessCode = async ({
device_id,
Expand All @@ -13,9 +14,11 @@ export const interactForAccessCode = async ({
device_id = await interactForDevice()
}

const accessCodes = await seam.accessCodes.list({
device_id,
})
const accessCodes = await withLoading("Fetching access codes...", () =>
seam.accessCodes.list({
device_id,
})
)

const { accessCodeId } = await prompts({
name: "accessCodeId",
Expand Down
5 changes: 4 additions & 1 deletion lib/interact-for-acs-entrance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { withLoading } from "./util/with-loading"

export const interactForAcsEntrance = async () => {
const seam = await getSeam()

const entrances = await seam.acs.entrances.list()
const entrances = await withLoading("Fetching ACS entrances...", () =>
seam.acs.entrances.list()
)

const { acsEntranceId } = await prompts({
name: "acsEntranceId",
Expand Down
5 changes: 4 additions & 1 deletion lib/interact-for-acs-system.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { withLoading } from "./util/with-loading"

export const interactForAcsSystem = async (message?: string) => {
const seam = await getSeam()

const systems = await seam.acs.systems.list()
const systems = await withLoading("Fetching ACS systems...", () =>
seam.acs.systems.list()
)

const { acsSystemId } = await prompts({
name: "acsSystemId",
Expand Down
9 changes: 6 additions & 3 deletions lib/interact-for-acs-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { interactForAcsSystem } from "./interact-for-acs-system"
import { withLoading } from "./util/with-loading"

export const interactForAcsUser = async () => {
const seam = await getSeam()
Expand All @@ -9,9 +10,11 @@ export const interactForAcsUser = async () => {
"What acs_system does the acs_user belong to?"
)

const users = await seam.acs.users.list({
acs_system_id,
})
const users = await withLoading("Fetching ACS users...", () =>
seam.acs.users.list({
acs_system_id,
})
)

const { acsUserId } = await prompts({
name: "acsUserId",
Expand Down
11 changes: 8 additions & 3 deletions lib/interact-for-action-attempt-poll.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { ActionAttemptsGetResponse } from "@seamapi/http/connect"
import { withLoading } from "./util/with-loading"

export const interactForActionAttemptPoll = async (
action_attempt: ActionAttemptsGetResponse["action_attempt"]
Expand All @@ -19,9 +20,13 @@ export const interactForActionAttemptPoll = async (
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 } }
const updated_action_attempt = await withLoading(
"Polling action attempt...",
() =>
seam.actionAttempts.get(
{ action_attempt_id },
{ waitForActionAttempt: { pollingInterval: 240, timeout: 10_000 } }
)
)

console.dir(updated_action_attempt, { depth: null })
Expand Down
6 changes: 5 additions & 1 deletion lib/interact-for-connected-account.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { getConfigStore } from "./get-config-store"
import { withLoading } from "./util/with-loading"
export const interactForConnectedAccount = async () => {
const seam = await getSeam()

const connected_accounts = await seam.connectedAccounts.list()
const connected_accounts = await withLoading(
"Fetching connected accounts...",
() => seam.connectedAccounts.list()
)

const { connectedAccountId } = await prompts({
name: "connectedAccountId",
Expand Down
11 changes: 8 additions & 3 deletions lib/interact-for-credential-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { interactForAcsSystem } from "./interact-for-acs-system"
import { withLoading } from "./util/with-loading"

export const interactForCredentialPool = async () => {
const seam = await getSeam()
Expand All @@ -9,9 +10,13 @@ export const interactForCredentialPool = async () => {
"What acs_system does the credential pool belong to?"
)

const credentialPools = await seam.acs.credentialPools.list({
acs_system_id,
})
const credentialPools = await withLoading(
"Fetching ACS credential pools...",
() =>
seam.acs.credentialPools.list({
acs_system_id,
})
)

const { credentialPoolId } = await prompts({
name: "credentialPoolId",
Expand Down
5 changes: 4 additions & 1 deletion lib/interact-for-device.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { getConfigStore } from "./get-config-store"
import { withLoading } from "./util/with-loading"
export const interactForDevice = async () => {
const seam = await getSeam()

const devices = await seam.devices.list()
const devices = await withLoading("Fetching devices...", () =>
seam.devices.list()
)

const { deviceId } = await prompts({
name: "deviceId",
Expand Down
5 changes: 4 additions & 1 deletion lib/interact-for-user-identity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import prompts from "prompts"
import { getSeam } from "./get-seam"
import { withLoading } from "./util/with-loading"

export const interactForUserIdentity = async () => {
const seam = await getSeam()

const uis = await seam.userIdentities.list()
const uis = await withLoading("Fetching user identities...", () =>
seam.userIdentities.list()
)

const { userIdentityId } = await prompts({
name: "userIdentityId",
Expand Down
5 changes: 4 additions & 1 deletion lib/interact-for-workspace-id.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { getConfigStore } from "./get-config-store"
import prompts from "prompts"
import { getSeam, getSeamMultiWorkspace } from "./get-seam"
import { withLoading } from "./util/with-loading"

export const interactForWorkspaceId = async () => {
const config = getConfigStore()
const seam = await getSeamMultiWorkspace()

const workspaces = await seam.workspaces.list()
const workspaces = await withLoading("Fetching workspaces...", () =>
seam.workspaces.list()
)

const { workspaceId } = await prompts({
name: "workspaceId",
Expand Down
9 changes: 6 additions & 3 deletions lib/util/request-seam-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AxiosResponse } from "axios"
import chalk from "chalk"
import { getSeam } from "../get-seam"
import { withLoading } from "./with-loading"

export const RequestSeamApi = async ({
path,
Expand All @@ -13,9 +14,11 @@ export const RequestSeamApi = async ({

logRequest(path, params)

const response = await seam.client.post(path, params, {
validateStatus: () => true,
})
const response = await withLoading("Making request...", () =>
seam.client.post(path, params, {
validateStatus: () => true,
})
)

logResponse(response)

Expand Down
16 changes: 16 additions & 0 deletions lib/util/with-loading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ora from "ora"

export const withLoading = async <T>(
message: string,
fn: () => Promise<T>
): Promise<T> => {
const spinner = ora(message).start()
try {
const result = await fn()
spinner.succeed()
return result
} catch (error) {
spinner.fail()
throw error
}
}
Loading

0 comments on commit 3378ae7

Please sign in to comment.