-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
routes.ts
46 lines (36 loc) · 1.28 KB
/
routes.ts
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
import { TaskType } from "src/types/Task";
export type RouteQuery = Record<string, string | number | boolean | undefined>;
export const stringifyQuery = (query: RouteQuery | undefined) => {
if (!query) {
return "";
}
const filteredQuery = Object.fromEntries(Object.entries(query).filter(([, value]) => value !== undefined)) as Record<
string,
string
>;
return new URLSearchParams(filteredQuery).toString();
};
const createRoute = (path: string, query?: RouteQuery) => {
if (!query) {
return path;
}
return `${path}?${stringifyQuery(query)}`;
};
export const ROUTES = {
ADMIN_MESSAGE_DETAIL: (id: string) => `/admin/messages/${id}`,
MESSAGE_DETAIL: (id: string) => `/messages/${id}`,
};
export type QueryWithLang<T extends RouteQuery | undefined = undefined> = T extends undefined
? { lang: string }
: T & { lang: string };
const withLang =
<T extends RouteQuery | undefined = undefined>(path: string, q?: T) =>
(query: QueryWithLang<T>) => {
return createRoute(path, { ...q, ...query });
};
export const API_ROUTES = {
NEW_TASK: (type: TaskType, query: QueryWithLang) => createRoute(`/api/new_task/${type}`, query),
UPDATE_TASK: "/api/update_task",
AVAILABLE_TASK: withLang("/api/available_tasks"),
RECENT_MESSAGES: withLang("/api/messages"),
};