Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-ways-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smartthings/cli": patch
---

remove some unnecessary calls to the API
5 changes: 3 additions & 2 deletions src/__tests__/lib/command/util/util-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ describe('createChooseFn', () => {
it('uses listItems from createChooseFn by default', async () => {
expect(await chooseSimpleType(command)).toBe('selected-simple-type-id')

expect(itemListMock).toHaveBeenCalledExactlyOnceWith(command)
// The list function should not be called until it's actually used.
expect(itemListMock).toHaveBeenCalledTimes(0)

const listItems = selectFromListMock.mock.calls[0][2].listItems

Expand All @@ -183,7 +184,7 @@ describe('createChooseFn', () => {
expect(await chooseSimpleType(command, undefined, { listItems: overridingListItemsMock }))
.toBe('selected-simple-type-id')

expect(overridingListItemsMock).toHaveBeenCalledExactlyOnceWith(command)
expect(overridingListItemsMock).toHaveBeenCalledTimes(0)
expect(itemListMock).not.toHaveBeenCalled()

const listItems = selectFromListMock.mock.calls[0][2].listItems
Expand Down
19 changes: 12 additions & 7 deletions src/lib/command/util/util-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type CreateChooseFunctionOptions<T extends object> = {

export type ChooseFunction<T extends object> = (
command: APICommand<SelectFromListFlags>,
itemIdOrNameFromArg?: string,
itemIdOrIndexFromArg?: string,
options?: Partial<ChooseOptions<T>>) => Promise<string>

export const createChooseFn = <T extends object>(
Expand All @@ -55,20 +55,25 @@ export const createChooseFn = <T extends object>(
): ChooseFunction<T> =>
async (
command: APICommand<SelectFromListFlags>,
itemIdOrNameFromArg?: string,
itemIdOrIndexFromArg?: string,
options?: Partial<ChooseOptions<T>>,
): Promise<string> => {
const opts = chooseOptionsWithDefaults(options)

// Listing items usually makes an API call which we only want to happen once so we do it
// now and just use stub functions that return these items later as needed.
const items = await (opts.listItems ?? listItems)(command)
const filteredItems = opts.listFilter ? items.filter(opts.listFilter) : items
const listItemsWrapper = async (): Promise<T[]> => filteredItems
let items: T[] | undefined = undefined
const listItemsWrapper = async (): Promise<T[]> => {
if (!items) {
items = await (opts.listItems ?? listItems)(command)
}
const filteredItems = opts.listFilter ? items.filter(opts.listFilter) : items
return filteredItems
}

const preselectedId = opts.allowIndex
? await stringTranslateToId(config, itemIdOrNameFromArg, listItemsWrapper)
: itemIdOrNameFromArg
? await stringTranslateToId(config, itemIdOrIndexFromArg, listItemsWrapper)
: itemIdOrIndexFromArg

const selectOptions: SelectOptions<T> = {
preselectedId,
Expand Down