All files / app/service api.ts

44.44% Statements 24/54
60% Branches 3/5
20% Functions 2/10
44.44% Lines 24/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 12278x             78x   78x           78x   77x           77x             77x             77x                 77x           77x                             80x 3x 3x   3x 3x   3x               3x 1x 2x 1x     1x 1x 1x                           77x                                  
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import {
  CancelFriendRequestReq,
  ListBadgeUsersReq,
  PingReq,
  RespondFriendRequestReq,
  SendFriendRequestReq,
} from "proto/api_pb";
 
import client from "./client";
import {
  FETCH_FAILED,
  IMAGE_TOO_LARGE,
  INTERNAL_ERROR,
  SERVER_ERROR,
} from "./constants";
 
export function cancelFriendRequest(friendRequestId: number) {
  const req = new CancelFriendRequestReq();
  req.setFriendRequestId(friendRequestId);
  return client.api.cancelFriendRequest(req);
}
 
export async function listFriends() {
  const req = new Empty();
 
  const response = await client.api.listFriends(req);
  return response.toObject().userIdsList;
}
 
export async function listFriendRequests() {
  const req = new Empty();
 
  const response = await client.api.listFriendRequests(req);
  return response.toObject();
}
 
export function respondFriendRequest(friendRequestId: number, accept: boolean) {
  const req = new RespondFriendRequestReq();
 
  req.setFriendRequestId(friendRequestId);
  req.setAccept(accept);
 
  return client.api.respondFriendRequest(req);
}
 
export function sendFriendRequest(userId: number) {
  const req = new SendFriendRequestReq();
  req.setUserId(userId);
  return client.api.sendFriendRequest(req);
}
 
export async function ping() {
  const req = new PingReq();
  const response = await client.api.ping(req);
 
  return response.toObject();
}
 
export interface ImageInputValues {
  file: File;
  filename: string;
  key: string;
  thumbnail_url: string;
  full_url: string;
}
 
export async function uploadFile(file: File): Promise<ImageInputValues> {
  const urlResponse = await client.api.initiateMediaUpload(new Empty());
  const uploadURL = urlResponse.getUploadUrl();
 
  const requestBody = new FormData();
  requestBody.append("file", file);
 
  const uploadResponse = await fetch(uploadURL, {
    method: "POST",
    body: requestBody,
  }).catch((e) => {
    console.error(e);
    throw new Error(FETCH_FAILED);
  });
 
  if (uploadResponse.status === 413) {
    throw new Error(IMAGE_TOO_LARGE);
  } else if (!uploadResponse.ok) {
    throw new Error(`${SERVER_ERROR}: ${uploadResponse.statusText}`);
  }
 
  const responseJson = await uploadResponse.json().catch((e) => {
    console.error(e);
    throw new Error(`${INTERNAL_ERROR}: ${e.message}`);
  });
  return {
    ...responseJson,
    file: file,
  };
}
 
export interface ListBadgeUsersInput {
  badgeId: string;
  pageSize?: number;
  pageToken?: string;
}
 
export async function listBadgeUsers({
  badgeId,
  pageSize,
  pageToken,
}: ListBadgeUsersInput) {
  const req = new ListBadgeUsersReq();
  req.setBadgeId(badgeId);
  Iif (pageSize) {
    req.setPageSize(pageSize);
  }
  Iif (pageToken) {
    req.setPageToken(pageToken);
  }
 
  const res = await client.api.listBadgeUsers(req);
  return res.toObject();
}