All files / src/api googleCalendarApi.ts

100% Statements 10/10
100% Branches 3/3
100% Functions 7/7
100% Lines 10/10

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                7x 1x   6x       7x                 1x                   2x 2x       2x           1x                       1x    
import { API_BASE_URL } from "../const";
type CalendarSelectionPayload = {
  id: string;
  summary?: string;
  primary?: boolean;
};
 
function requireApiBaseUrl(): string {
  if (!API_BASE_URL) {
    throw new Error("API_BASE_URL is missing (check .env + app.config.ts)");
  }
  return API_BASE_URL;
}
 
function googleFetch(path: string, init?: RequestInit): Promise<Response> {
  return fetch(`${requireApiBaseUrl()}${path}`, {
    ...init,
    credentials: "include",
  });
}
 
export function requestGoogleOAuthExchange(
  serverAuthCode: string,
): Promise<Response> {
  return googleFetch("/api/google/oauth/exchange", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ serverAuthCode }),
  });
}
 
export function requestGoogleState(
  includeCalendars = false,
): Promise<Response> {
  const path = `/api/google/state?days=7&timeZone=${encodeURIComponent("America/Montreal")}&includeCalendars=${includeCalendars}`;
  return googleFetch(path);
}
 
export function requestGoogleCalendars(): Promise<Response> {
  return googleFetch("/api/google/calendars");
}
 
export function requestSetGoogleSelectedCalendar(
  calendar: CalendarSelectionPayload,
): Promise<Response> {
  return googleFetch("/api/google/selected-calendar", {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      id: calendar.id,
      summary: calendar.summary,
      primary: !!calendar.primary,
    }),
  });
}
 
export function requestGoogleLogout(): Promise<Response> {
  return googleFetch("/api/google/oauth/logout", { method: "POST" });
}