Open History to see the last commit
1import type {
2 CreateProjectInput,
3 ProjectMutationResponse,
4 ProjectsPageResponse,
5 ToggleProjectFavoriteInput,
6} from './types'
7β
8const DEFAULT_API_PORT = 4184
9const configuredApiPort = Number.parseInt(
10 import.meta.env.VITE_PAGINATION_API_PORT ?? String(DEFAULT_API_PORT),
11 10,
12)
13const API_PORT = Number.isInteger(configuredApiPort)
14 ? configuredApiPort
15 : DEFAULT_API_PORT
16const API_BASE_URL = `http://127.0.0.1:${API_PORT}`
17β
18function buildProjectsUrl(
19 page: number,
20 delayMs: number,
21 forceError: boolean,
22): URL {
23 const url = new URL('/api/projects', API_BASE_URL)
24 url.searchParams.set('page', String(page))
25β
26 if (delayMs > 0) {
27 url.searchParams.set('delay', String(delayMs))
28 }
29β
30 if (forceError) {
31 url.searchParams.set('error', 'true')
32 }
33β
34 return url
35}
36β
37async function readJsonOrThrow<T>(
38 response: Response,
39 fallbackMessage: string,
40): Promise<T> {
41 if (response.ok) {
42 return (await response.json()) as T
43 }
44β
45 const payload = (await response.json().catch(() => null)) as {
46 error?: string
47 } | null
48β
49 throw new Error(
50 payload && typeof payload === 'object' && 'error' in payload
51 ? String(payload.error ?? fallbackMessage)
52 : fallbackMessage,
53 )
54}
55β
56async function requestJson<T>(
57 url: URL,
58 init: RequestInit,
59 fallbackMessage: string,
60): Promise<T> {
61 const response = await fetch(url, init)
62 return readJsonOrThrow<T>(response, fallbackMessage)
63}
64β
65export const projectsAPI = {
66 getProjects: async (
67 page: number,
68 delayMs: number,
69 forceError: boolean,
70 ): Promise<ProjectsPageResponse> => {
71 const response = await fetch(buildProjectsUrl(page, delayMs, forceError))
72 return readJsonOrThrow<ProjectsPageResponse>(
73 response,
74 `Failed to fetch projects page ${page}`,
75 )
76 },
77 createProject: async (
78 input: CreateProjectInput,
79 ): Promise<ProjectMutationResponse> => {
80 return requestJson<ProjectMutationResponse>(
81 new URL('/api/projects', API_BASE_URL),
82 {
83 method: 'POST',
84 headers: {
85 'content-type': 'application/json',
86 },
87 body: JSON.stringify(input),
88 },
89 'Failed to create project',
90 )
91 },
92 updateProjectFavorite: async (
93 input: ToggleProjectFavoriteInput,
94 ): Promise<ProjectMutationResponse> => {
95 return requestJson<ProjectMutationResponse>(
96 new URL(`/api/projects/${input.id}`, API_BASE_URL),
97 {
98 method: 'PATCH',
99 headers: {
100 'content-type': 'application/json',
101 },
102 body: JSON.stringify({ isFavorite: input.isFavorite }),
103 },
104 `Failed to update project ${input.id}`,
105 )
106 },
107}
108β
109export async function armNextProjectMutationFailureOnServer(): Promise<void> {
110 await requestJson<{ ok: true }>(
111 new URL('/api/testing/fail-next-mutation', API_BASE_URL),
112 {
113 method: 'POST',
114 },
115 'Failed to arm next mutation failure',
116 )
117}
118β
119export async function resetProjectsApiState(): Promise<void> {
120 await requestJson<{ ok: true }>(
121 new URL('/api/reset', API_BASE_URL),
122 {
123 method: 'POST',
124 },
125 'Failed to reset API state',
126 )
127}