-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathroute.ts
More file actions
55 lines (46 loc) · 1.75 KB
/
Copy pathroute.ts
File metadata and controls
55 lines (46 loc) · 1.75 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
import { apiHandler } from "@/lib/apiHandler";
import { buildLinkHeader } from "@/lib/pagination";
import { listReposQueryParamsSchema } from "@/lib/schemas";
import { queryParamsSchemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { NextRequest } from "next/server";
import { listRepos } from "./listReposApi";
// eslint-disable-next-line authz/require-auth-wrapper -- delegates to listRepos() which calls withOptionalAuth
export const GET = apiHandler(async (request: NextRequest) => {
const rawParams = Object.fromEntries(
Object.keys(listReposQueryParamsSchema.shape).map(key => [
key,
request.nextUrl.searchParams.get(key) ?? undefined
])
);
const parseResult = listReposQueryParamsSchema.safeParse(rawParams);
if (!parseResult.success) {
return serviceErrorResponse(queryParamsSchemaValidationError(parseResult.error));
}
const { page, perPage, sort, direction, query } = parseResult.data;
const response = await listRepos({
page,
perPage,
sort,
direction,
query,
})
if (isServiceError(response)) {
return serviceErrorResponse(response);
}
const { data, totalCount } = response;
const headers = new Headers({ 'Content-Type': 'application/json' });
headers.set('X-Total-Count', totalCount.toString());
const linkHeader = buildLinkHeader(request, {
page,
perPage,
totalCount,
extraParams: {
sort,
direction,
...(query ? { query } : {}),
},
});
if (linkHeader) headers.set('Link', linkHeader);
return new Response(JSON.stringify(data), { status: 200, headers });
});