diff --git a/src/commands/login.ts b/src/commands/login.ts index 7c58053..b35c208 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -1,8 +1,9 @@ -import axios from 'axios'; +import axios, { AxiosError } from 'axios'; import { defineCommand } from 'citty'; import consola from 'consola'; import { API_URL } from '../config'; import usersService from '../services/users'; +import { getMessageFromUnknownError } from '../utils/error'; import { passwordPrompt, prompt } from '../utils/prompt'; import userConfig from '../utils/userConfig'; @@ -57,9 +58,12 @@ export default defineCommand({ consola.success(`Successfully signed in.`); } catch (error) { userConfig.write({}); - consola.error( - 'Invalid token. Please provide a valid token. You can create a token at https://cloud.capawesome.io/settings/tokens.', - ); + let message = getMessageFromUnknownError(error); + if (error instanceof AxiosError && error.response?.status === 401) { + message = + 'Invalid token. Please provide a valid token. You can create a token at https://cloud.capawesome.io/settings/tokens.'; + } + consola.error(message); } } }, diff --git a/src/services/app-bundles.ts b/src/services/app-bundles.ts index 9cc5d13..1d13ed8 100644 --- a/src/services/app-bundles.ts +++ b/src/services/app-bundles.ts @@ -22,9 +22,6 @@ class AppBundlesServiceImpl implements AppBundlesService { ...data.formData.getHeaders(), }, }); - if (!response.success) { - throw response.error; - } return response.data; } @@ -34,21 +31,15 @@ class AppBundlesServiceImpl implements AppBundlesService { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } return response.data; } async delete(data: DeleteAppBundleDto): Promise { - const response = await this.httpClient.delete(`/apps/${data.appId}/bundles/${data.bundleId}`, { + await this.httpClient.delete(`/apps/${data.appId}/bundles/${data.bundleId}`, { headers: { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } } } diff --git a/src/services/app-channels.ts b/src/services/app-channels.ts index 1f9effe..b7f0975 100644 --- a/src/services/app-channels.ts +++ b/src/services/app-channels.ts @@ -20,14 +20,11 @@ class AppChannelsServiceImpl implements AppChannelsService { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } return response.data; } async delete(data: DeleteAppChannelDto): Promise { - const response = await this.httpClient.delete(`/apps/${data.appId}/channels`, { + await this.httpClient.delete(`/apps/${data.appId}/channels`, { headers: { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, @@ -35,9 +32,6 @@ class AppChannelsServiceImpl implements AppChannelsService { name: data.name, }, }); - if (!response.success) { - throw response.error; - } } } diff --git a/src/services/app-devices.ts b/src/services/app-devices.ts index 6b602e8..55b0111 100644 --- a/src/services/app-devices.ts +++ b/src/services/app-devices.ts @@ -14,14 +14,11 @@ class AppDevicesServiceImpl implements AppDevicesService { } async delete(data: DeleteAppDeviceDto): Promise { - const res = await this.httpClient.delete(`/apps/${data.appId}/devices/${data.deviceId}`, { + await this.httpClient.delete(`/apps/${data.appId}/devices/${data.deviceId}`, { headers: { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!res.success) { - throw res.error; - } } } diff --git a/src/services/apps.ts b/src/services/apps.ts index 2ddaf0d..0df6081 100644 --- a/src/services/apps.ts +++ b/src/services/apps.ts @@ -21,21 +21,15 @@ class AppsServiceImpl implements AppsService { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } return response.data; } async delete(dto: DeleteAppDto): Promise { - const response = await this.httpClient.delete(`/apps/${dto.id}`, { + await this.httpClient.delete(`/apps/${dto.id}`, { headers: { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } } async findAll(): Promise { @@ -44,9 +38,6 @@ class AppsServiceImpl implements AppsService { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } return response.data; } } diff --git a/src/services/update.ts b/src/services/update.ts index d1b6915..000539e 100644 --- a/src/services/update.ts +++ b/src/services/update.ts @@ -16,15 +16,16 @@ class UpdateServiceImpl implements UpdateService { } async checkForUpdate(): Promise { - const response = await this.httpClient.get(`https://registry.npmjs.org/${pkg.name}/latest`); - if (!response.success) { - throw response.error; - } - const latestVersion = response.data.version; - if (semver.gt(latestVersion, pkg.version)) { - consola.warn( - `New version of Capawesome CLI available: ${pkg.name}@${latestVersion}. Please update to receive the latest features and bug fixes.`, - ); + try { + const response = await this.httpClient.get(`https://registry.npmjs.org/${pkg.name}/latest`); + const latestVersion = response.data.version; + if (semver.gt(latestVersion, pkg.version)) { + consola.warn( + `New version of Capawesome CLI available: ${pkg.name}@${latestVersion}. Please update to receive the latest features and bug fixes.`, + ); + } + } catch (error) { + consola.error('Failed to check for updates.'); } } } diff --git a/src/services/users.ts b/src/services/users.ts index 258cdc7..d00d350 100644 --- a/src/services/users.ts +++ b/src/services/users.ts @@ -19,9 +19,6 @@ class UsersServiceImpl implements UsersService { Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, }, }); - if (!response.success) { - throw response.error; - } return response.data; } } diff --git a/src/utils/http-client.ts b/src/utils/http-client.ts index be4791a..3364844 100644 --- a/src/utils/http-client.ts +++ b/src/utils/http-client.ts @@ -1,98 +1,32 @@ -import axios, { AxiosRequestConfig } from 'axios'; +import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; import { API_URL } from '../config'; -interface SuccessHttpResponse { - success: true; - status: number; - data: T; -} - -interface FailureHttpResponse { - success: false; - status: number; - error: any; -} - -type HttpResponse = SuccessHttpResponse | FailureHttpResponse; - export interface HttpClient { - delete(url: string, config?: AxiosRequestConfig | undefined): Promise>; - get(url: string, config?: AxiosRequestConfig | undefined): Promise>; - patch(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise>; - post(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise>; + delete(url: string, config?: AxiosRequestConfig | undefined): Promise>; + get(url: string, config?: AxiosRequestConfig | undefined): Promise>; + patch(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise>; + post(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise>; } class HttpClientImpl implements HttpClient { - async delete(url: string, config?: AxiosRequestConfig | undefined): Promise> { - try { - const urlWithHost = url.startsWith('http') ? url : API_URL + url; - const res = await axios.delete(urlWithHost, config); - return { - success: true, - status: res.status, - data: res.data, - }; - } catch (e: any) { - return { - success: false, - status: e.response.status, - error: e, - }; - } + delete(url: string, config?: AxiosRequestConfig | undefined): Promise> { + const urlWithHost = url.startsWith('http') ? url : API_URL + url; + return axios.delete(urlWithHost, config); } - async get(url: string, config?: AxiosRequestConfig | undefined): Promise> { - try { - const urlWithHost = url.startsWith('http') ? url : API_URL + url; - const res = await axios.get(urlWithHost, config); - return { - success: true, - status: res.status, - data: res.data, - }; - } catch (e: any) { - return { - success: false, - status: e.response.status, - error: e, - }; - } + async get(url: string, config?: AxiosRequestConfig | undefined): Promise> { + const urlWithHost = url.startsWith('http') ? url : API_URL + url; + return axios.get(urlWithHost, config); } - async patch(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise> { - try { - const urlWithHost = url.startsWith('http') ? url : API_URL + url; - const res = await axios.patch(urlWithHost, data, config); - return { - success: true, - status: res.status, - data: res.data, - }; - } catch (e: any) { - return { - success: false, - status: e.response.status, - error: e, - }; - } + async patch(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise> { + const urlWithHost = url.startsWith('http') ? url : API_URL + url; + return axios.patch(urlWithHost, data, config); } - async post(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise> { - try { - const urlWithHost = url.startsWith('http') ? url : API_URL + url; - const res = await axios.post(urlWithHost, data, config); - return { - success: true, - status: res.status, - data: res.data, - }; - } catch (e: any) { - return { - success: false, - status: e.response.status, - error: e, - }; - } + async post(url: string, data?: any, config?: AxiosRequestConfig | undefined): Promise> { + const urlWithHost = url.startsWith('http') ? url : API_URL + url; + return axios.post(urlWithHost, data, config); } }