2022-04-30 01:09:53 +03:00
|
|
|
import { ChatMessage } from '../interfaces/chat-message.model';
|
|
|
|
const ENDPOINT = `http://localhost:8080/api/chat`;
|
2022-04-27 00:04:35 +03:00
|
|
|
const URL_CHAT_REGISTRATION = `http://localhost:8080/api/chat/register`;
|
|
|
|
|
|
|
|
interface UserRegistrationResponse {
|
|
|
|
id: string;
|
|
|
|
accessToken: string;
|
|
|
|
displayName: string;
|
|
|
|
}
|
|
|
|
|
2022-04-30 01:09:53 +03:00
|
|
|
class ChatService {
|
|
|
|
public static async getChatHistory(accessToken: string): Promise<ChatMessage[]> {
|
|
|
|
const response = await fetch(`${ENDPOINT}?accessToken=${accessToken}`);
|
|
|
|
const status = await response.json();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async registerUser(username: string): Promise<UserRegistrationResponse> {
|
2022-04-27 00:04:35 +03:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ displayName: username }),
|
|
|
|
};
|
2022-04-28 09:19:20 +03:00
|
|
|
|
2022-04-27 00:04:35 +03:00
|
|
|
try {
|
|
|
|
const response = await fetch(URL_CHAT_REGISTRATION, options);
|
|
|
|
const result = await response.json();
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-28 09:19:20 +03:00
|
|
|
}
|
2022-04-30 01:09:53 +03:00
|
|
|
|
|
|
|
export default ChatService;
|