-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathroute.ts
More file actions
42 lines (35 loc) · 1.08 KB
/
route.ts
File metadata and controls
42 lines (35 loc) · 1.08 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
'use server';
import { getFileSource } from "@/features/search/fileSourceApi";
import { queryParamsSchemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { NextRequest } from "next/server";
import { z } from "zod";
const querySchema = z.object({
repo: z.string(),
path: z.string(),
ref: z.string().optional(),
});
export const GET = async (request: NextRequest) => {
const rawParams = Object.fromEntries(
Object.keys(querySchema.shape).map(key => [
key,
request.nextUrl.searchParams.get(key) ?? undefined
])
);
const parsed = querySchema.safeParse(rawParams);
if (!parsed.success) {
return serviceErrorResponse(
queryParamsSchemaValidationError(parsed.error)
);
}
const { repo, path, ref } = parsed.data;
const response = await getFileSource({
path,
repo,
ref,
});
if (isServiceError(response)) {
return serviceErrorResponse(response);
}
return Response.json(response);
}