Skip to content

Commit

Permalink
fix: add loader (nanospinner)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewuu committed Feb 20, 2025
1 parent e316d74 commit 222a162
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 34 deletions.
10 changes: 6 additions & 4 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,10 +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",
type: "autocomplete",
Expand Down
6 changes: 4 additions & 2 deletions lib/interact-for-acs-entrance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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",
type: "autocomplete",
Expand Down
6 changes: 4 additions & 2 deletions lib/interact-for-acs-system.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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",
type: "autocomplete",
Expand Down
10 changes: 6 additions & 4 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,10 +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",
type: "autocomplete",
Expand Down
12 changes: 8 additions & 4 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,11 +20,14 @@ 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
7 changes: 5 additions & 2 deletions lib/interact-for-connected-account.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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",
type: "autocomplete",
Expand Down
13 changes: 8 additions & 5 deletions lib/interact-for-credential-pool.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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()

const acs_system_id = await interactForAcsSystem(
"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",
type: "autocomplete",
Expand Down
6 changes: 4 additions & 2 deletions lib/interact-for-device.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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",
type: "autocomplete",
Expand Down
6 changes: 4 additions & 2 deletions lib/interact-for-user-identity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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",
type: "autocomplete",
Expand Down
6 changes: 4 additions & 2 deletions lib/interact-for-workspace-id.ts
Original file line number Diff line number Diff line change
@@ -1,13 +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",
type: "select",
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
17 changes: 17 additions & 0 deletions lib/util/with-loading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createSpinner } from "nanospinner"

export const withLoading = async <T>(
message: string,
fn: () => Promise<T>
): Promise<T> => {
const spinner = createSpinner(message).start()
try {
await new Promise((resolve) => setTimeout(resolve, 5000))
const result = await fn()
spinner.success()
return result
} catch (error) {
spinner.error()
throw error
}
}
Loading

0 comments on commit 222a162

Please sign in to comment.