mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
canvas crud on pages
This commit is contained in:
@@ -1,28 +1,94 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { axiosClient } from "@/services/axiosUtils";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
const getUrl = (params: { rest: string[] }) => {
|
||||
const { rest } = params;
|
||||
const path = rest.join("/");
|
||||
const newUrl = `https://snow.instructure.com/api/v1/${path}`;
|
||||
return newUrl;
|
||||
};
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { rest: string[] } }
|
||||
) {
|
||||
const { rest } = params;
|
||||
const path = rest.join("/");
|
||||
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",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const newUrl = `https://snow.instructure.com/api/v1/${path}`;
|
||||
const response = await axiosClient.get(newUrl, {
|
||||
headers: {
|
||||
// Include other headers from the incoming request if needed:
|
||||
// 'Content-Type': req.headers.get('content-type') || 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
return new NextResponse(
|
||||
JSON.stringify({ error: error.message || "Request failed" }),
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
return new NextResponse(
|
||||
JSON.stringify({ error: error.message || "Canvas get request failed" }),
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { rest: string[] } }
|
||||
) {
|
||||
return withErrorHandling(async () => {
|
||||
try {
|
||||
const url = getUrl(params);
|
||||
const body = await req.json();
|
||||
const response = await axiosClient.post(url, body);
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
return new NextResponse(
|
||||
JSON.stringify({
|
||||
error: error.message || "Canvas post request failed",
|
||||
}),
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { rest: string[] } }
|
||||
) {
|
||||
return withErrorHandling(async () => {
|
||||
try {
|
||||
const url = getUrl(params);
|
||||
const body = await req.json();
|
||||
const response = await axiosClient.put(url, body);
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
return new NextResponse(
|
||||
JSON.stringify({ error: error.message || "Canvas put request failed" }),
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
export async function DELETE(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { rest: string[] } }
|
||||
) {
|
||||
return withErrorHandling(async () => {
|
||||
try {
|
||||
const url = getUrl(params);
|
||||
const response = await axiosClient.delete(url);
|
||||
return NextResponse.json(response.data);
|
||||
} catch (error: any) {
|
||||
return new NextResponse(
|
||||
JSON.stringify({
|
||||
error: error.message || "Canvas delete request failed",
|
||||
}),
|
||||
{ status: error.response?.status || 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user