Skip to content

Commit

Permalink
Merge pull request #68 from seamapi/issue-#43
Browse files Browse the repository at this point in the history
Support for adding array parameters e.g. access_codes/list editing parameter access_code_ids
  • Loading branch information
mastafit authored Jan 11, 2024
2 parents 4a4c367 + 5524b53 commit ba19c67
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
61 changes: 61 additions & 0 deletions lib/interact-for-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import prompts from "prompts"

export const interactForArray = async (
array: string[],
message: string
): Promise<string[]> => {
let updatedArray = [...array]

const displayList = () => {
console.log(`${message} Current list:`)
if (updatedArray.length > 0) {
updatedArray.forEach((item, index) => {
console.log(`${index + 1}: ${item}`)
})
} else {
console.log("The list is currently empty.")
}
}

let action: string
do {
displayList()

const response = await prompts({
type: "select",
name: "action",
message: "Choose an action:",
choices: [
{ title: "Add an item", value: "add" },
{ title: "Remove an item", value: "remove" },
{ title: "Finish editing", value: "done" },
],
})

action = response.action

if (action === "add") {
const { newItem } = await prompts({
type: "text",
name: "newItem",
message: "Enter the new item:",
})
if (newItem) {
updatedArray.push(newItem)
}
} else if (action === "remove") {
const { index } = await prompts({
type: "number",
name: "index",
message: "Enter the index of the item to remove:",
validate: (value) =>
value > 0 && value <= updatedArray.length ? true : "Invalid index",
})
if (index) {
updatedArray.splice(index - 1, 1)
}
}
} while (action !== "done")

return updatedArray
}
18 changes: 6 additions & 12 deletions lib/interact-for-open-api-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { interactForCredentialPool } from "./interact-for-credential-pool"
import { ContextHelpers } from "./types"
import { interactForAcsEntrance } from "./interact-for-acs-entrance"
import { ellipsis } from "./util/ellipsis"
import { interactForArray } from "./interact-for-array"

const ergonomicPropOrder = [
"name",
Expand Down Expand Up @@ -214,18 +215,11 @@ export const interactForOpenApiObject = async (
).value
args.params[paramToEdit] = value
return interactForOpenApiObject(args, ctx)
} else if (
prop.type === "array" &&
(prop as any)?.items?.type === "string"
) {
const value = (
await prompts({
name: "value",
message: `${paramToEdit}:`,
type: "text",
})
).value
args.params[paramToEdit] = [value]
} else if (prop.type === "array") {
args.params[paramToEdit] = await interactForArray(
args.params[paramToEdit] || [],
`Edit the list for ${paramToEdit}`
)
return interactForOpenApiObject(args, ctx)
} else if (prop.type === "object") {
args.params[paramToEdit] = await interactForOpenApiObject(
Expand Down

0 comments on commit ba19c67

Please sign in to comment.