v1.0.0-beta
This commit is contained in:
+45
-36
@@ -1,5 +1,4 @@
|
||||
import firebase from 'firebase/app';
|
||||
import 'firebase/auth';
|
||||
/*global chrome*/
|
||||
|
||||
const API_HOST = window.location.href.startsWith('http://localhost')
|
||||
? 'http://localhost:5001/watch-party-crx/us-central1/widgets'
|
||||
@@ -26,70 +25,80 @@ export const getPlayerSource = () => {
|
||||
if (HREF.startsWith(PLAYER_SOURCE.LOCAL)) return PLAYER_SOURCE.LOCAL;
|
||||
};
|
||||
|
||||
export const isAuthed = () => true && firebase.auth().currentUser;
|
||||
|
||||
export const doAuth = () => firebase.auth().signInAnonymously();
|
||||
|
||||
export const fetcher = async ({ ENDPOINT, METHOD, ...args }) => {
|
||||
const idToken = await firebase.auth().currentUser.getIdToken();
|
||||
return fetch(`${API_HOST}/${ENDPOINT}`, {
|
||||
method: METHOD,
|
||||
headers: {
|
||||
Authorization: `Token ${idToken}:${firebase.auth().currentUser.uid}`,
|
||||
},
|
||||
...args,
|
||||
export const getUserUid = async () =>
|
||||
new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(
|
||||
{
|
||||
type: 'GET_AUTH',
|
||||
},
|
||||
(response) => {
|
||||
const { uid } = response;
|
||||
window.localStorage.setItem('watchPartyUserUid', uid);
|
||||
resolve(uid);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
export const fetcher = async ({ endpoint, method, ...args }) => {
|
||||
const url = `${API_HOST}/${endpoint}`;
|
||||
return new Promise((resolve, reject) =>
|
||||
chrome.runtime.sendMessage(
|
||||
{
|
||||
type: 'API_CALL',
|
||||
url,
|
||||
method,
|
||||
...args,
|
||||
},
|
||||
(response) => {
|
||||
if (response.status > 299) reject(response);
|
||||
else resolve({ json: () => response });
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export const CreateRoomApi = async (data) => {
|
||||
if (!isAuthed()) await doAuth();
|
||||
return fetcher({
|
||||
ENDPOINT: 'create',
|
||||
METHOD: 'POST',
|
||||
endpoint: 'create',
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
|
||||
export const JoinRoomApi = async (data) => {
|
||||
if (!isAuthed()) await doAuth();
|
||||
return fetcher({
|
||||
ENDPOINT: 'join',
|
||||
METHOD: 'POST',
|
||||
endpoint: 'join',
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
|
||||
export const SendMessageApi = async (data) => {
|
||||
if (!isAuthed()) await doAuth();
|
||||
return fetcher({
|
||||
ENDPOINT: 'message',
|
||||
METHOD: 'POST',
|
||||
endpoint: 'message',
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
|
||||
export const LeaveRoomApi = async (data) => {
|
||||
if (!isAuthed()) await doAuth();
|
||||
return fetcher({
|
||||
ENDPOINT: `leave/${data.roomId}/users/${data.uid}`,
|
||||
METHOD: 'DELETE',
|
||||
endpoint: `leave/${data.roomId}/users/${data.uid}`,
|
||||
method: 'DELETE',
|
||||
});
|
||||
};
|
||||
|
||||
export const DeleteRoomApi = async (data) => {
|
||||
if (!isAuthed()) await doAuth();
|
||||
return fetcher({
|
||||
ENDPOINT: `delete/${data.roomId}`,
|
||||
METHOD: 'DELETE',
|
||||
endpoint: `delete/${data.roomId}`,
|
||||
method: 'DELETE',
|
||||
});
|
||||
};
|
||||
|
||||
export const UpdatePlayerApi = async (data) => {
|
||||
if (isAuthed()) {
|
||||
return fetcher({
|
||||
ENDPOINT: 'updatePlayer',
|
||||
METHOD: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
return fetcher({
|
||||
endpoint: 'updatePlayer',
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user