Skip to content

Commit 11def2e

Browse files
Add health command (#170)
* 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
1 parent 71a208e commit 11def2e

File tree

4 files changed

+75
-53
lines changed

4 files changed

+75
-53
lines changed

cli.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import { interactForCommandSelection } from "./lib/interact-for-command-selectio
66
import parseArgs, { ParsedArgs } from "minimist"
77
import { interactForLogin } from "./lib/interact-for-login"
88
import { interactForWorkspaceId } from "./lib/interact-for-workspace-id"
9-
import { getSeam } from "./lib/get-seam"
109
import chalk from "chalk"
1110
import { interactForServerSelection } from "./lib/interact-for-server-selection"
1211
import { getServer } from "./lib/get-server"
1312
import prompts from "prompts"
14-
import logResponse from "./lib/util/log-response"
1513
import { getApiDefinitions } from "./lib/get-api-definitions"
1614
import commandLineUsage from "command-line-usage"
1715
import { ContextHelpers } from "./lib/types"
1816
import { version } from "./package.json"
1917
import { interactForUseRemoteApiDefs } from "./lib/interact-for-use-remote-api-defs"
2018
import { randomBytes } from "node:crypto"
2119
import { interactForActionAttemptPoll } from "./lib/interact-for-action-attempt-poll"
20+
import { RequestSeamApi } from "./lib/util/request-seam-api"
2221

2322
const sections = [
2423
{
@@ -177,6 +176,13 @@ async function cli(args: ParsedArgs) {
177176
return
178177
}
179178
await interactForServerSelection()
179+
return
180+
} else if (isEqual(selectedCommand, ["health", "get-health"])) {
181+
await RequestSeamApi({
182+
path: "/health/get_health",
183+
params: {},
184+
})
185+
180186
return
181187
}
182188
// TODO - do this using the OpenAPI spec for the command rather than
@@ -209,55 +215,42 @@ async function cli(args: ParsedArgs) {
209215
})
210216
}
211217

212-
const seam = await getSeam()
213-
214218
const apiPath = `/${selectedCommand.join("/").replace(/-/g, "_")}`
215219

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

220-
console.log(`\n\n${chalk.green(apiPath)}`)
221-
console.log(`Request Params:`)
222-
console.log(params)
223-
224-
const response = await seam.client.post(apiPath, params, {
225-
validateStatus: () => true,
224+
const response = await RequestSeamApi({
225+
path: apiPath,
226+
params,
226227
})
227228

228-
logResponse(response)
229+
if (response.data?.connect_webview) {
230+
await handleConnectWebviewResponse(response.data.connect_webview)
231+
}
232+
233+
if (response.data?.action_attempt) {
234+
interactForActionAttemptPoll(response.data.action_attempt)
235+
}
236+
}
229237

230-
if (response.data.connect_webview) {
231-
if (
232-
response.data &&
233-
response.data.connect_webview &&
234-
response.data.connect_webview.url
235-
) {
236-
const url = response.data.connect_webview.url
238+
const handleConnectWebviewResponse = async (connect_webview: any) => {
239+
const url = connect_webview.url
237240

238-
if (process.env.INSIDE_WEB_BROWSER !== "1") {
239-
const { action } = await prompts({
240-
type: "confirm",
241-
name: "action",
242-
message: "Would you like to open the webview in your browser?",
243-
})
241+
if (process.env.INSIDE_WEB_BROWSER !== "1") {
242+
const { action } = await prompts({
243+
type: "confirm",
244+
name: "action",
245+
message: "Would you like to open the webview in your browser?",
246+
})
244247

245-
if (action) {
246-
const { default: open } = await import("open")
247-
await open(url)
248-
}
249-
} else {
250-
//TODO: Figure out how to open the webview in the browser
251-
}
248+
if (action) {
249+
const { default: open } = await import("open")
250+
await open(url)
252251
}
253-
}
254-
255-
if (
256-
response.data &&
257-
typeof response.data === "object" &&
258-
"action_attempt" in response.data
259-
) {
260-
interactForActionAttemptPoll(response.data.action_attempt)
252+
} else {
253+
//TODO: Figure out how to open the webview in the browser
261254
}
262255
}
263256

lib/interact-for-command-selection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function interactForCommandSelection(
2828
["config", "use-remote-api-defs"],
2929
["select", "workspace"],
3030
["select", "server"],
31+
["health", "get-health"],
3132
])
3233

3334
const possibleCommands = uniqBy(
@@ -38,6 +39,7 @@ export async function interactForCommandSelection(
3839
),
3940
(v) => v[commandPath.length]
4041
)
42+
4143
if (possibleCommands.length === 0) {
4244
throw new Error("No possible commands")
4345
}

lib/util/log-response.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/util/request-seam-api.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { AxiosResponse } from "axios"
2+
import chalk from "chalk"
3+
import { getSeam } from "../get-seam"
4+
5+
export const RequestSeamApi = async ({
6+
path,
7+
params,
8+
}: {
9+
path: string
10+
params: Record<string, any>
11+
}) => {
12+
const seam = await getSeam()
13+
14+
logRequest(path, params)
15+
16+
const response = await seam.client.post(path, params, {
17+
validateStatus: () => true,
18+
})
19+
20+
logResponse(response)
21+
22+
return response
23+
}
24+
25+
const logResponse = (response: AxiosResponse) => {
26+
if (response.status >= 400) {
27+
console.log(chalk.red(`\n\n[${response.status}]\n`))
28+
} else {
29+
console.log(chalk.green(`\n\n[${response.status}]`))
30+
}
31+
console.dir(response.data, { depth: null })
32+
console.log("\n")
33+
}
34+
35+
const logRequest = (apiPath: string, params: Record<string, any>) => {
36+
console.log(`\n\n${chalk.green(apiPath)}`)
37+
console.log(`Request Params:`)
38+
console.log(params)
39+
}
40+
41+
export default RequestSeamApi

0 commit comments

Comments
 (0)