Skip to content

Commit

Permalink
feat: add chat message deletion
Browse files Browse the repository at this point in the history
- improve error handling
  • Loading branch information
zSoulweaver committed Nov 13, 2023
1 parent c5b923f commit 2378f4b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/client/kient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class Kient extends Emitter<KientEvents> {
private _endpoints: Endpoints
private _wsClient: WsClient
private _wsHandlers: { chatroom: ChatroomSocket }
public autenticated = false

private async init() {
this._apiClient = await ApiClient.create(this)
Expand Down
49 changes: 25 additions & 24 deletions src/endpoints/authentication/authentication.endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ export class AuthenticationEndpoint extends BaseEndpoint {
}
})


if (response.status === 200) {
const responseBody = deserialize<LoginResponse>(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<LoginErrorResponse>(response.body)
if (responseBody.message === 'Username or password is not correct') {
Expand All @@ -87,20 +67,41 @@ export class AuthenticationEndpoint extends BaseEndpoint {
})
}
}

if (response.status === 200) {
const responseBody = deserialize<LoginResponse>(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'
})
}

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<UserResponse>(response.body)
if (!deserializedBody.id) {
throw new KientApiError({
name: 'UNAUTHENTICATED',
message: 'Not authenticated'
})
throw new KientApiError({ name: 'UNAUTHENTICATED' })
}
return deserialize<UserResponse>(response.body)
}
Expand Down
39 changes: 38 additions & 1 deletion src/endpoints/chat/chat.endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -16,14 +21,46 @@ 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<SendMessageResponse>(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
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<GenericApiResponse<null>>(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
Expand Down

0 comments on commit 2378f4b

Please sign in to comment.