term dropdown populating

This commit is contained in:
2024-09-11 09:21:05 -06:00
parent dd983982d8
commit 72dcb2f54b
13 changed files with 266 additions and 32 deletions

View File

@@ -1,12 +1,46 @@
import { NextRequest, NextResponse } from "next/server";
import { axiosClient } from "@/services/axiosUtils";
import { withErrorHandling } from "@/services/withErrorHandling";
import { AxiosResponseHeaders, RawAxiosResponseHeaders } from "axios";
const getUrl = (params: { rest: string[] }) => {
const { rest } = params;
const path = rest.join("/");
const newUrl = `https://snow.instructure.com/api/v1/${path}`;
return newUrl;
return new URL(newUrl);
};
const getNextUrl = (
headers: AxiosResponseHeaders | RawAxiosResponseHeaders
): string | undefined => {
const linkHeader: string | undefined =
typeof headers.get === "function"
? (headers.get("link") as string)
: ((headers as RawAxiosResponseHeaders)["link"] as string);
if (!linkHeader) {
console.log("could not find link header in the response");
return undefined;
}
const links = linkHeader.split(",").map((link) => link.trim());
const nextLink = links.find((link) => link.includes('rel="next"'));
if (!nextLink) {
console.log("could not find next url in link header, reached end of pagination");
return undefined;
}
const nextUrl = nextLink.split(";")[0].trim().slice(1, -1);
return nextUrl;
};
const proxyResponseHeaders = (response: any) => {
const headers = new Headers();
Object.entries(response.headers).forEach(([key, value]) => {
headers.set(key, value as string);
});
return headers;
};
export async function GET(
@@ -16,18 +50,42 @@ export async function GET(
return withErrorHandling(async () => {
try {
const url = getUrl(params);
const response = await axiosClient.get(url, {
headers: {
// Include other headers from the incoming request if needed:
// 'Content-Type': req.headers.get('content-type') || 'application/json',
"Content-Type": "application/json",
},
});
// const response = await axiosClient.get(url, {
// headers: {
// // Include other headers from the incoming request if needed:
// "Content-Type": "application/json",
// },
// });
return NextResponse.json(response.data);
var requestCount = 1;
url.searchParams.set("per_page", "100");
const { data: firstData, headers: firstHeaders } = await axiosClient.get(
url.toString()
);
var returnData = firstData ? [firstData] : [];
var nextUrl = getNextUrl(firstHeaders);
while (nextUrl) {
requestCount += 1;
const { data, headers } = await axiosClient.get(nextUrl);
if (data) {
returnData = [...returnData, data];
}
nextUrl = getNextUrl(headers);
}
if (requestCount > 1) {
console.log(
`Requesting ${typeof returnData} took ${requestCount} requests`
);
}
return NextResponse.json(returnData);
} catch (error: any) {
return new NextResponse(
JSON.stringify({ error: error.message || "Canvas get request failed" }),
JSON.stringify({ error: error.message || "Canvas GET request failed" }),
{ status: error.response?.status || 500 }
);
}
@@ -43,11 +101,13 @@ export async function POST(
const url = getUrl(params);
const body = await req.json();
const response = await axiosClient.post(url, body);
return NextResponse.json(response.data);
const headers = proxyResponseHeaders(response);
return new NextResponse(JSON.stringify(response.data), { headers });
} catch (error: any) {
return new NextResponse(
JSON.stringify({
error: error.message || "Canvas post request failed",
error: error.message || "Canvas POST request failed",
}),
{ status: error.response?.status || 500 }
);
@@ -64,15 +124,18 @@ export async function PUT(
const url = getUrl(params);
const body = await req.json();
const response = await axiosClient.put(url, body);
return NextResponse.json(response.data);
const headers = proxyResponseHeaders(response);
return new NextResponse(JSON.stringify(response.data), { headers });
} catch (error: any) {
return new NextResponse(
JSON.stringify({ error: error.message || "Canvas put request failed" }),
JSON.stringify({ error: error.message || "Canvas PUT request failed" }),
{ status: error.response?.status || 500 }
);
}
});
}
export async function DELETE(
req: NextRequest,
{ params }: { params: { rest: string[] } }
@@ -81,11 +144,13 @@ export async function DELETE(
try {
const url = getUrl(params);
const response = await axiosClient.delete(url);
return NextResponse.json(response.data);
const headers = proxyResponseHeaders(response);
return new NextResponse(JSON.stringify(response.data), { headers });
} catch (error: any) {
return new NextResponse(
JSON.stringify({
error: error.message || "Canvas delete request failed",
error: error.message || "Canvas DELETE request failed",
}),
{ status: error.response?.status || 500 }
);