mirror of
https://github.com/owncast/owncast.git
synced 2024-12-18 07:12:33 +03:00
17d433749c
I found two issues: 1. This `options` object is being passed down to fetchData, where it is deconstructed by the "data" attribute, not body. 2. the `data` object is being transformed into JSON downstream, thus the stringification done here makes for a string json object only Signed-off-by: auyer <rafael@rcpassos.me>
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { createContext } from 'react';
|
|
import { ChatMessage } from '../interfaces/chat-message.model';
|
|
import { getUnauthedData } from '../utils/apis';
|
|
|
|
const ENDPOINT = `/api/chat`;
|
|
const URL_CHAT_REGISTRATION = `/api/chat/register`;
|
|
|
|
export interface UserRegistrationResponse {
|
|
id: string;
|
|
accessToken: string;
|
|
displayName: string;
|
|
displayColor: number;
|
|
}
|
|
|
|
export interface ChatStaticService {
|
|
getChatHistory(accessToken: string): Promise<ChatMessage[]>;
|
|
registerUser(username: string): Promise<UserRegistrationResponse>;
|
|
}
|
|
|
|
class ChatService {
|
|
public static async getChatHistory(accessToken: string): Promise<ChatMessage[]> {
|
|
try {
|
|
const response = await getUnauthedData(`${ENDPOINT}?accessToken=${accessToken}`);
|
|
return response;
|
|
} catch (e) {
|
|
console.error(e);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public static async registerUser(username: string): Promise<UserRegistrationResponse> {
|
|
const options = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
data: { displayName: username },
|
|
};
|
|
|
|
const response = await getUnauthedData(URL_CHAT_REGISTRATION, options);
|
|
return response;
|
|
}
|
|
}
|
|
|
|
export const ChatServiceContext = createContext<ChatStaticService>(ChatService);
|