All files / app/service api.ts

51.11% Statements 23/45
100% Branches 3/3
22.22% Functions 2/9
51.11% Lines 23/45

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 9778x           78x   78x           78x   77x           77x             77x             77x                 77x           77x                             80x 3x 3x   3x 3x   3x               3x 1x 2x 1x     1x 1x 1x              
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import {
  CancelFriendRequestReq,
  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,
  };
}