diff --git a/src/client/kient.ts b/src/client/kient.ts index 31fc6e4..1ffb038 100644 --- a/src/client/kient.ts +++ b/src/client/kient.ts @@ -22,6 +22,7 @@ export class Kient extends Emitter { private _endpoints: Endpoints private _wsClient: WsClient private _wsHandlers: { chatroom: ChatroomSocket } + public autenticated = false private async init() { this._apiClient = await ApiClient.create(this) diff --git a/src/endpoints/authentication/authentication.endpoint.ts b/src/endpoints/authentication/authentication.endpoint.ts index 74cb4e7..0f4ca44 100644 --- a/src/endpoints/authentication/authentication.endpoint.ts +++ b/src/endpoints/authentication/authentication.endpoint.ts @@ -42,26 +42,6 @@ export class AuthenticationEndpoint extends BaseEndpoint { } }) - - if (response.status === 200) { - const responseBody = deserialize(response.body) - if (responseBody.token) { - this._apiClient.setBearerToken(responseBody.token) - return - } - let errorMessage = '' - if (responseBody.otp_required) { - errorMessage = 'Login requires one time code from email' - } - if (responseBody['2fa_required']) { - errorMessage = 'Login requires authenticator app token' - } - throw new KientAuthenticationError({ - name: '2FA_REQUIRED', - message: errorMessage - }) - } - if (response.status === 422) { const responseBody = deserialize(response.body) if (responseBody.message === 'Username or password is not correct') { @@ -87,6 +67,27 @@ export class AuthenticationEndpoint extends BaseEndpoint { }) } } + + if (response.status === 200) { + const responseBody = deserialize(response.body) + if (responseBody.token) { + this._apiClient.setBearerToken(responseBody.token) + this._client.autenticated = true + return + } + let errorMessage = '' + if (responseBody.otp_required) { + errorMessage = 'Login requires one time code from email' + } + if (responseBody['2fa_required']) { + errorMessage = 'Login requires authenticator app token' + } + throw new KientAuthenticationError({ + name: '2FA_REQUIRED', + message: errorMessage + }) + } + throw new KientApiError({ name: 'SOMETHING_WENT_WRONG', message: 'Unknown authentication error when attemping login' @@ -94,13 +95,13 @@ export class AuthenticationEndpoint extends BaseEndpoint { } public async currentUser() { + if (!this._client.autenticated) { + throw new KientApiError({ name: 'UNAUTHENTICATED' }) + } const response = await this._apiClient.callKickApi({ endpoint: 'api/v1/user' }) const deserializedBody = deserialize(response.body) if (!deserializedBody.id) { - throw new KientApiError({ - name: 'UNAUTHENTICATED', - message: 'Not authenticated' - }) + throw new KientApiError({ name: 'UNAUTHENTICATED' }) } return deserialize(response.body) } diff --git a/src/endpoints/chat/chat.endpoint.ts b/src/endpoints/chat/chat.endpoint.ts index 7436381..5270928 100644 --- a/src/endpoints/chat/chat.endpoint.ts +++ b/src/endpoints/chat/chat.endpoint.ts @@ -2,9 +2,14 @@ import { deserialize } from '@deepkit/type' import { BaseEndpoint } from '../endpoint.base' import { SendMessageResponse } from './dto/send-message.response' import { KientApiError } from '../api.error' +import { GenericApiResponse } from '@/utils/generic-api.response' export class ChatEndpoint extends BaseEndpoint { public async sendMessage(chatroomId: string | number, message: string) { + if (!this._client.autenticated) { + throw new KientApiError({ name: 'UNAUTHENTICATED' }) + } + const body = { content: message, type: 'message' @@ -16,6 +21,10 @@ export class ChatEndpoint extends BaseEndpoint { body: JSON.stringify(body) } }) + if (response.status !== 200) { + throw new KientApiError({ name: 'SOMETHING_WENT_WRONG', cause: response }) + } + const deserializedResponse = deserialize(response.body) if (deserializedResponse.status.code === 401) { throw new KientApiError({ name: 'UNAUTHENTICATED' }) @@ -23,7 +32,35 @@ export class ChatEndpoint extends BaseEndpoint { if (deserializedResponse.status.code !== 200) { throw new KientApiError({ name: 'SOMETHING_WENT_WRONG', - message: deserializedResponse.status.message + message: deserializedResponse.status.message, + cause: response + }) + } + return deserializedResponse + } + + public async deleteMessage(chatroomId: string | number, messageId: string) { + if (!this._client.autenticated) { + throw new KientApiError({ name: 'UNAUTHENTICATED' }) + } + + const response = await this._apiClient.callKickApi({ + endpoint: `api/v2/chatrooms/${chatroomId}/messages/${messageId}`, + method: 'delete' + }) + if (response.status !== 200) { + throw new KientApiError({ name: 'SOMETHING_WENT_WRONG', cause: response }) + } + + const deserializedResponse = deserialize>(response.body) + if (deserializedResponse.status.code === 401) { + throw new KientApiError({ name: 'UNAUTHENTICATED' }) + } + if (deserializedResponse.status.code !== 200) { + throw new KientApiError({ + name: 'SOMETHING_WENT_WRONG', + message: deserializedResponse.status.message, + cause: response }) } return deserializedResponse