Skip to content

Commit

Permalink
Add health command (#170)
Browse files Browse the repository at this point in the history
* Add command for calling /health/get_health, improve code readability

* Fix get-health command typo causing full command cli input not to work

* Rename makeSeamApiRequest fn
  • Loading branch information
andrii-balitskyi authored Jun 14, 2024
1 parent 71a208e commit 11def2e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 53 deletions.
71 changes: 32 additions & 39 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import { interactForCommandSelection } from "./lib/interact-for-command-selectio
import parseArgs, { ParsedArgs } from "minimist"
import { interactForLogin } from "./lib/interact-for-login"
import { interactForWorkspaceId } from "./lib/interact-for-workspace-id"
import { getSeam } from "./lib/get-seam"
import chalk from "chalk"
import { interactForServerSelection } from "./lib/interact-for-server-selection"
import { getServer } from "./lib/get-server"
import prompts from "prompts"
import logResponse from "./lib/util/log-response"
import { getApiDefinitions } from "./lib/get-api-definitions"
import commandLineUsage from "command-line-usage"
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"
import { RequestSeamApi } from "./lib/util/request-seam-api"

const sections = [
{
Expand Down Expand Up @@ -177,6 +176,13 @@ async function cli(args: ParsedArgs) {
return
}
await interactForServerSelection()
return
} else if (isEqual(selectedCommand, ["health", "get-health"])) {
await RequestSeamApi({
path: "/health/get_health",
params: {},
})

return
}
// TODO - do this using the OpenAPI spec for the command rather than
Expand Down Expand Up @@ -209,55 +215,42 @@ async function cli(args: ParsedArgs) {
})
}

const seam = await getSeam()

const apiPath = `/${selectedCommand.join("/").replace(/-/g, "_")}`

if (apiPath.includes("/events/list") && params.between) {
delete params.since
}

console.log(`\n\n${chalk.green(apiPath)}`)
console.log(`Request Params:`)
console.log(params)

const response = await seam.client.post(apiPath, params, {
validateStatus: () => true,
const response = await RequestSeamApi({
path: apiPath,
params,
})

logResponse(response)
if (response.data?.connect_webview) {
await handleConnectWebviewResponse(response.data.connect_webview)
}

if (response.data?.action_attempt) {
interactForActionAttemptPoll(response.data.action_attempt)
}
}

if (response.data.connect_webview) {
if (
response.data &&
response.data.connect_webview &&
response.data.connect_webview.url
) {
const url = response.data.connect_webview.url
const handleConnectWebviewResponse = async (connect_webview: any) => {
const url = connect_webview.url

if (process.env.INSIDE_WEB_BROWSER !== "1") {
const { action } = await prompts({
type: "confirm",
name: "action",
message: "Would you like to open the webview in your browser?",
})
if (process.env.INSIDE_WEB_BROWSER !== "1") {
const { action } = await prompts({
type: "confirm",
name: "action",
message: "Would you like to open the webview in your browser?",
})

if (action) {
const { default: open } = await import("open")
await open(url)
}
} else {
//TODO: Figure out how to open the webview in the browser
}
if (action) {
const { default: open } = await import("open")
await open(url)
}
}

if (
response.data &&
typeof response.data === "object" &&
"action_attempt" in response.data
) {
interactForActionAttemptPoll(response.data.action_attempt)
} else {
//TODO: Figure out how to open the webview in the browser
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/interact-for-command-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function interactForCommandSelection(
["config", "use-remote-api-defs"],
["select", "workspace"],
["select", "server"],
["health", "get-health"],
])

const possibleCommands = uniqBy(
Expand All @@ -38,6 +39,7 @@ export async function interactForCommandSelection(
),
(v) => v[commandPath.length]
)

if (possibleCommands.length === 0) {
throw new Error("No possible commands")
}
Expand Down
14 changes: 0 additions & 14 deletions lib/util/log-response.ts

This file was deleted.

41 changes: 41 additions & 0 deletions lib/util/request-seam-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { AxiosResponse } from "axios"
import chalk from "chalk"
import { getSeam } from "../get-seam"

export const RequestSeamApi = async ({
path,
params,
}: {
path: string
params: Record<string, any>
}) => {
const seam = await getSeam()

logRequest(path, params)

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

logResponse(response)

return response
}

const logResponse = (response: AxiosResponse) => {
if (response.status >= 400) {
console.log(chalk.red(`\n\n[${response.status}]\n`))
} else {
console.log(chalk.green(`\n\n[${response.status}]`))
}
console.dir(response.data, { depth: null })
console.log("\n")
}

const logRequest = (apiPath: string, params: Record<string, any>) => {
console.log(`\n\n${chalk.green(apiPath)}`)
console.log(`Request Params:`)
console.log(params)
}

export default RequestSeamApi

0 comments on commit 11def2e

Please sign in to comment.