-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiImpl.ts
More file actions
69 lines (58 loc) · 1.82 KB
/
apiImpl.ts
File metadata and controls
69 lines (58 loc) · 1.82 KB
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
import { ProjectData, UserData } from "./types";
export interface ProjectsResponse {
projects: ProjectData[];
hasMoreResults: boolean;
}
const PROJECTS_REQUEST_TIMEOUT_MS = 1000;
export const PROJECTS_REQUEST_TIMEOUT_MESSAGE =
"Request timed out. Possible infinite loop in api/pagination.py.";
export class ProjectsRequestTimeoutError extends Error {
constructor(message: string = PROJECTS_REQUEST_TIMEOUT_MESSAGE) {
super(message);
this.name = "ProjectsRequestTimeoutError";
}
}
function isAbortError(error: unknown): boolean {
return (
typeof error === "object" &&
error !== null &&
"name" in error &&
error.name === "AbortError"
);
}
class DefaultServer {
async getUsers(): Promise<UserData[]> {
const response = await fetch('http://127.0.0.1:5000/api/users');
return response.json();
}
async getProjects(options?: {
userId?: string;
startAfter?: ProjectData;
pageSize?: number;
}): Promise<ProjectsResponse> {
const url = new URL('http://127.0.0.1:5000/api/projects');
if (options?.userId) {
url.searchParams.append('userId', options.userId);
}
if (options?.startAfter?.id) {
url.searchParams.append('startAfterId', options.startAfter.id.toString());
}
if (options?.pageSize) {
url.searchParams.append('pageSize', options.pageSize.toString());
}
const controller = new AbortController();
const timeoutId = window.setTimeout(() => controller.abort(), PROJECTS_REQUEST_TIMEOUT_MS);
try {
const response = await fetch(url, { signal: controller.signal });
return response.json();
} catch (error) {
if (isAbortError(error)) {
throw new ProjectsRequestTimeoutError();
}
throw error;
} finally {
window.clearTimeout(timeoutId);
}
}
}
export const SERVER = new DefaultServer();