-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrouter.ts
194 lines (173 loc) · 7.49 KB
/
router.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import GAME_VERSIONS from "@app/utils/config/game-versions";
import {
MAX_SEARCH_LIMIT,
categoryFilterParamNamespace,
defaultSearchLimit,
defaultSortBy,
gameVersionFilterParamNamespace,
licenseFilterParamNamespace,
loaderFilterParamNamespace,
pageOffsetParamNamespace,
searchLimitParamNamespace,
sortByParamNamespace,
} from "@app/utils/config/search";
import { getProjectTypeFromName } from "@app/utils/convertors";
import { getAllLoaderCategories, getValidProjectCategories } from "@app/utils/project";
import { SearchResultSortMethod, TagHeaderType } from "@app/utils/types";
import { type Context, Hono } from "hono";
import { applyCacheHeaders } from "~/middleware/cache";
import { searchReqRateLimiter } from "~/middleware/rate-limit/get-req";
import { isNumber } from "~/utils";
import { HTTP_STATUS, serverErrorResponse } from "~/utils/http";
import { searchProjects } from "./controllers";
const searchRouter = new Hono();
searchRouter.use(searchReqRateLimiter);
searchRouter.use(applyCacheHeaders({ maxAge: 0, sMaxAge: 1800 }));
searchRouter.get("/", search_get);
searchRouter.get("/filters/sort-by", sortByFilters_get);
searchRouter.get("/filters/loaders", loaders_get);
searchRouter.get("/filters/game-versions", gameVersions_get);
searchRouter.get("/filters/categories", categories_get);
searchRouter.get("/filters/features", features_get);
searchRouter.get("/filters/resolutions", resolutions_get);
searchRouter.get("/filters/performance-impact", performanceImpacts_get);
searchRouter.get("/filters/license", licenses_get);
async function search_get(ctx: Context) {
try {
const query = ctx.req.query("q") || "";
const categories = ctx.req.queries(categoryFilterParamNamespace) || [];
const loaders = ctx.req.queries(loaderFilterParamNamespace) || [];
const gameVersions = ctx.req.queries(gameVersionFilterParamNamespace) || [];
const pageStr = ctx.req.query(pageOffsetParamNamespace) || "";
const offsetStr = ctx.req.query("offset") || "";
const limitStr = ctx.req.query(searchLimitParamNamespace) || `${defaultSearchLimit}`;
const environments = ctx.req.queries("e") || [];
const openSourceOnly = ctx.req.query(licenseFilterParamNamespace) === "oss";
const sortBy = ctx.req.query(sortByParamNamespace) || defaultSortBy;
const typeStr = ctx.req.query("type");
const type = typeStr ? getProjectTypeFromName(typeStr) : undefined;
let limit = Number.parseInt(limitStr);
if (!isNumber(limit)) limit = defaultSearchLimit;
else if (limit > MAX_SEARCH_LIMIT) limit = MAX_SEARCH_LIMIT;
else if (limit <= 0) limit = 1;
const page = Number.parseInt(pageStr);
let offset = Number.parseInt(offsetStr);
if (!isNumber(offset)) {
if (isNumber(page)) offset = (page - 1) * limit;
else offset = 0;
}
const res = await searchProjects({
query,
loaders,
gameVersions,
categories,
environments,
openSourceOnly,
sortBy: sortBy as SearchResultSortMethod,
offset: offset,
limit: limit,
type: type,
});
return ctx.json(res.data, res.status);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function sortByFilters_get(ctx: Context) {
try {
const list = [
SearchResultSortMethod.RELEVANCE,
SearchResultSortMethod.DOWNLOADS,
SearchResultSortMethod.FOLLOW_COUNT,
SearchResultSortMethod.RECENTLY_UPDATED,
SearchResultSortMethod.RECENTLY_PUBLISHED,
];
return ctx.json({ success: true, queryKey: sortByParamNamespace, default: defaultSortBy, list: list }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function loaders_get(ctx: Context) {
try {
const projectType = getProjectTypeFromName(ctx.req.query("type") || "");
if (!projectType) {
return ctx.json({ success: false, message: "Invalid project type" }, HTTP_STATUS.BAD_REQUEST);
}
const loaderFilters = getAllLoaderCategories(projectType);
return ctx.json({ success: true, queryKey: loaderFilterParamNamespace, list: loaderFilters }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function gameVersions_get(ctx: Context) {
try {
return ctx.json({ success: true, queryKey: gameVersionFilterParamNamespace, list: GAME_VERSIONS }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function categories_get(ctx: Context) {
try {
const projectType = getProjectTypeFromName(ctx.req.query("type") || "");
if (!projectType) {
return ctx.json({ success: false, message: "Invalid project type" }, HTTP_STATUS.BAD_REQUEST);
}
const categories = getValidProjectCategories([projectType], TagHeaderType.CATEGORY).map((category) => category.name);
return ctx.json({ success: true, queryKey: categoryFilterParamNamespace, list: categories }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function features_get(ctx: Context) {
try {
const projectType = getProjectTypeFromName(ctx.req.query("type") || "");
if (!projectType) {
return ctx.json({ success: false, message: "Invalid project type" }, HTTP_STATUS.BAD_REQUEST);
}
const categories = getValidProjectCategories([projectType], TagHeaderType.FEATURE).map((category) => category.name);
return ctx.json({ success: true, queryKey: categoryFilterParamNamespace, list: categories }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function resolutions_get(ctx: Context) {
try {
const projectType = getProjectTypeFromName(ctx.req.query("type") || "");
if (!projectType) {
return ctx.json({ success: false, message: "Invalid project type" }, HTTP_STATUS.BAD_REQUEST);
}
const categories = getValidProjectCategories([projectType], TagHeaderType.RESOLUTION).map((category) => category.name);
return ctx.json({ success: true, queryKey: categoryFilterParamNamespace, list: categories }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function performanceImpacts_get(ctx: Context) {
try {
const projectType = getProjectTypeFromName(ctx.req.query("type") || "");
if (!projectType) {
return ctx.json({ success: false, message: "Invalid project type" }, HTTP_STATUS.BAD_REQUEST);
}
const categories = getValidProjectCategories([projectType], TagHeaderType.PERFORMANCE_IMPACT).map((category) => category.name);
return ctx.json({ success: true, queryKey: categoryFilterParamNamespace, list: categories }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
async function licenses_get(ctx: Context) {
try {
return ctx.json({ success: true, queryKey: licenseFilterParamNamespace, list: ["oss"] }, HTTP_STATUS.OK);
} catch (error) {
console.error(error);
return serverErrorResponse(ctx);
}
}
export default searchRouter;