Skip to content

Commit

Permalink
cli: fix error fetching more bots (#2925)
Browse files Browse the repository at this point in the history
## Description

Add await when call getMoreBots
Remove duplicated call to getBots inside deployBotFromFlag function
Add generic type T to apiGet function
  • Loading branch information
Iru89 authored Oct 29, 2024
1 parent 5dcf9e8 commit 5245e67
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions packages/botonic-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ npm install -g @botonic/cli
$ botonic COMMAND
running command...
$ botonic (-v|--version|version)
@botonic/cli/0.30.1 darwin-arm64 node-v20.11.1
@botonic/cli/0.30.2-alpha.0 darwin-arm64 node-v20.11.1
$ botonic --help [COMMAND]
USAGE
$ botonic COMMAND
Expand Down Expand Up @@ -66,7 +66,7 @@ EXAMPLES
Deploying to AWS...
```

_See code: [lib/commands/deploy.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/deploy.js)_
_See code: [lib/commands/deploy.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/deploy.js)_

## `botonic destroy [PROVIDER]`

Expand All @@ -81,7 +81,7 @@ EXAMPLE
Destroying AWS stack...
```

_See code: [lib/commands/destroy.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/destroy.js)_
_See code: [lib/commands/destroy.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/destroy.js)_

## `botonic help [COMMAND]`

Expand Down Expand Up @@ -112,7 +112,7 @@ OPTIONS
-p, --path=path Path to botonic project. Defaults to current dir.
```

_See code: [lib/commands/login.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/login.js)_
_See code: [lib/commands/login.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/login.js)_

## `botonic logout`

Expand All @@ -126,7 +126,7 @@ OPTIONS
-p, --path=path Path to botonic project. Defaults to current dir.
```

_See code: [lib/commands/logout.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/logout.js)_
_See code: [lib/commands/logout.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/logout.js)_

## `botonic new NAME [PROJECTNAME]`

Expand All @@ -146,7 +146,7 @@ EXAMPLE
✨ test_bot was successfully created!
```

_See code: [lib/commands/new.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/new.js)_
_See code: [lib/commands/new.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/new.js)_

## `botonic serve`

Expand All @@ -164,7 +164,7 @@ EXAMPLE
> Project is running at http://localhost:8080/
```

_See code: [lib/commands/serve.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/serve.js)_
_See code: [lib/commands/serve.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/serve.js)_

## `botonic test`

Expand All @@ -191,5 +191,5 @@ EXAMPLE
Ran all test suites.
```

_See code: [lib/commands/test.js](https://github.com/hubtype/botonic/blob/v0.30.1/lib/commands/test.js)_
_See code: [lib/commands/test.js](https://github.com/hubtype/botonic/blob/v0.30.2-alpha.0/lib/commands/test.js)_
<!-- commandsstop -->
2 changes: 1 addition & 1 deletion packages/botonic-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@botonic/cli",
"description": "Build Chatbots Using React",
"version": "0.30.1",
"version": "0.30.2-alpha.0",
"license": "MIT",
"bin": {
"botonic": "./bin/run"
Expand Down
49 changes: 28 additions & 21 deletions packages/botonic-cli/src/botonic-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
AnalyticsInfo,
BotDetail,
BotListItem,
BotsList,
Me,
OAuth,
PaginatedResponse,
} from './interfaces'
import { BotConfigJSON } from './util/bot-config'
import { pathExists } from './util/file-system'
Expand Down Expand Up @@ -177,26 +177,30 @@ export class BotonicAPIService {
if (pathExists(pathToCredentials)) unlinkSync(pathToCredentials)
}

private async apiPost({
private async apiPost<T>({
apiVersion = 'v1',
path,
body,
headers,
params,
}: RequestArgs): Promise<any> {
return this.apiClient.post(`${this.baseUrl}/${apiVersion}/${path}`, body, {
headers: headers || this.headers,
params,
})
}: RequestArgs): Promise<AxiosResponse<T, any>> {
return this.apiClient.post<T>(
`${this.baseUrl}/${apiVersion}/${path}`,
body,
{
headers: headers || this.headers,
params,
}
)
}

private async apiGet({
private async apiGet<T>({
apiVersion = 'v1',
path,
headers,
params,
}: RequestArgs): Promise<any> {
return this.apiClient.get(`${this.baseUrl}/${apiVersion}/${path}`, {
}: RequestArgs): Promise<AxiosResponse<T, any>> {
return this.apiClient.get<T>(`${this.baseUrl}/${apiVersion}/${path}`, {
headers: headers || this.headers,
params,
})
Expand Down Expand Up @@ -244,7 +248,7 @@ export class BotonicAPIService {
const accessToken = this.getOauth().access_token
this.setHeaders(accessToken)

const meResponse = await this.apiGet({ path: 'users/me' })
const meResponse = await this.apiGet<Me>({ path: 'users/me' })
if (meResponse) {
this.me = meResponse.data
}
Expand Down Expand Up @@ -278,29 +282,32 @@ export class BotonicAPIService {
return this.apiGet({ path: 'users/me/' })
}

async getBots(): AxiosPromise<BotsList> {
const botsResponse = await this.apiGet({ apiVersion: 'v2', path: 'bots/' })
async getBots(): Promise<AxiosResponse<PaginatedResponse<BotListItem>, any>> {
const botsResponse = await this.apiGet<PaginatedResponse<BotListItem>>({
apiVersion: 'v2',
path: 'bots/?page_size=100',
})

if (botsResponse.data.next) {
this.getMoreBots(botsResponse.data.results, botsResponse.data.next)
await this.getMoreBots(botsResponse.data.results, botsResponse.data.next)
}

return botsResponse
}

private async getMoreBots(bots: BotListItem[], nextBots?: string) {
if (!nextBots) {
private async getMoreBots(bots: BotListItem[], nextUrl: string | null) {
if (!nextUrl) {
return bots
}

const resp = await this.apiGet({
const resp = await this.apiGet<PaginatedResponse<BotListItem>>({
apiVersion: 'v2',
path: nextBots.split(`${this.baseUrl}/v2/`)[1],
path: nextUrl.split(`${this.baseUrl}/v2/`)[1],
})
resp.data.results.map(b => bots.push(b))
nextBots = resp.data.next
resp.data.results.forEach(bot => bots.push(bot))
nextUrl = resp.data.next

return this.getMoreBots(bots, nextBots)
return this.getMoreBots(bots, nextUrl)
}

async getProviders(): Promise<AxiosPromise> {
Expand Down
13 changes: 7 additions & 6 deletions packages/botonic-cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ Deploying to AWS...
}

async deployBotFromFlag(botName: string): Promise<void | undefined> {
const resp = await this.botonicApiService.getBots()
const bots = resp.data.results
const bots = await this.getAvailableBots()

const bot = bots.filter(b => b.name === botName)[0]
if (bot === undefined && !botName) {
Expand All @@ -111,7 +110,6 @@ Deploying to AWS...
bots.map(b => console.log(` > ${String(b.name)}`))
return undefined
} else if (botName) {
const bots = await this.getAvailableBots()
const botByBotName = bots.find(bot => bot.name === botName)
if (botByBotName) {
this.botonicApiService.setCurrentBot(bot)
Expand Down Expand Up @@ -178,13 +176,16 @@ Deploying to AWS...
}

async deployBotFlow(): Promise<void> {
if (this.botName) return this.deployBotFromFlag(this.botName)
if (this.botName) {
return this.deployBotFromFlag(this.botName)
}

if (
!this.botonicApiService.bot ||
!Object.keys(this.botonicApiService.bot).length
)
) {
return this.newBotFlow()
else {
} else {
const resp = await this.botonicApiService.getBots()
const bots = resp.data.results

Expand Down
9 changes: 4 additions & 5 deletions packages/botonic-cli/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ export interface GlobalCredentials {
me?: Me
analytics: AnalyticsInfo
}

export interface BotsList {
export interface PaginatedResponse<T> {
count: number
next: string
previous: string
results: BotListItem[]
next: string | null
previous: string | null
results: T[]
}

export interface BotListItem {
Expand Down

0 comments on commit 5245e67

Please sign in to comment.