mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
adding headers on the backend
This commit is contained in:
@@ -1,12 +1,18 @@
|
|||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
|
|
||||||
|
const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN;
|
||||||
|
if (!token) {
|
||||||
|
throw new Error("CANVAS_TOKEN not in environment");
|
||||||
|
}
|
||||||
|
|
||||||
const nextConfig = {
|
const nextConfig = {
|
||||||
async rewrites() {
|
async rewrites() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
source: '/api/canvas/:rest*',
|
source: "/api/canvas/:rest*",
|
||||||
destination: 'https://snow.instructure.com/api/v1/:rest*',
|
destination: "https://snow.instructure.com/api/v1/:rest*",
|
||||||
},
|
},
|
||||||
]
|
];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
import { isServer } from "@tanstack/react-query";
|
import { isServer } from "@tanstack/react-query";
|
||||||
import axios, { AxiosInstance, AxiosError } from "axios";
|
import axios, { AxiosInstance, AxiosError, AxiosHeaders } from "axios";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
|
const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN;
|
||||||
|
if (!token) {
|
||||||
|
throw new Error("NEXT_PUBLIC_CANVAS_TOKEN not in environment");
|
||||||
|
}
|
||||||
|
|
||||||
export const axiosClient: AxiosInstance = axios.create();
|
export const axiosClient: AxiosInstance = axios.create();
|
||||||
|
|
||||||
if (!isServer) {
|
if (!isServer) {
|
||||||
console.log("not on the server, setting up interceptor");
|
|
||||||
axiosClient.interceptors.request.use((config) => {
|
axiosClient.interceptors.request.use((config) => {
|
||||||
if (
|
if (
|
||||||
config.url &&
|
config.url &&
|
||||||
@@ -21,6 +25,16 @@ if (!isServer) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
axiosClient.interceptors.request.use((config) => {
|
||||||
|
if (
|
||||||
|
config.url &&
|
||||||
|
config.url.startsWith("https://snow.instructure.com/api/v1/")
|
||||||
|
) {
|
||||||
|
config.headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
axiosClient.interceptors.response.use(
|
axiosClient.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error: AxiosError) => {
|
(error: AxiosError) => {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const baseCanvasUrl = "https://snow.instructure.com/api/v1";
|
|||||||
|
|
||||||
export const canvasPageService = {
|
export const canvasPageService = {
|
||||||
async getAll(courseId: number): Promise<CanvasPage[]> {
|
async getAll(courseId: number): Promise<CanvasPage[]> {
|
||||||
|
console.log("requesting pages");
|
||||||
const url = `${baseCanvasUrl}/courses/${courseId}/pages`;
|
const url = `${baseCanvasUrl}/courses/${courseId}/pages`;
|
||||||
const pages = await canvasServiceUtils.paginatedRequest<CanvasPage[]>({
|
const pages = await canvasServiceUtils.paginatedRequest<CanvasPage[]>({
|
||||||
url,
|
url,
|
||||||
|
|||||||
@@ -2,11 +2,6 @@ import { axiosClient } from "../axiosUtils";
|
|||||||
|
|
||||||
type FetchOptions = Omit<RequestInit, "method">;
|
type FetchOptions = Omit<RequestInit, "method">;
|
||||||
|
|
||||||
const token = process.env.NEXT_PUBLIC_CANVAS_TOKEN;
|
|
||||||
if (!token) {
|
|
||||||
throw new Error("CANVAS_TOKEN not in environment");
|
|
||||||
}
|
|
||||||
|
|
||||||
const rateLimitRetryCount = 6;
|
const rateLimitRetryCount = 6;
|
||||||
const rateLimitSleepInterval = 1000;
|
const rateLimitSleepInterval = 1000;
|
||||||
|
|
||||||
@@ -44,7 +39,6 @@ const rateLimitAwarePost = async (
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -81,8 +75,6 @@ const recursiveDelete = async (
|
|||||||
...options,
|
...options,
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
...options.headers,
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -113,20 +105,12 @@ const recursiveDelete = async (
|
|||||||
};
|
};
|
||||||
export const webRequestor = {
|
export const webRequestor = {
|
||||||
getMany: async <T>(url: string, options: FetchOptions = {}) => {
|
getMany: async <T>(url: string, options: FetchOptions = {}) => {
|
||||||
const response = await axiosClient.get<T[]>(url, {
|
const response = await axiosClient.get<T[]>(url);
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return { data: response.data, response };
|
return { data: response.data, response };
|
||||||
},
|
},
|
||||||
|
|
||||||
get: async <T>(url: string) => {
|
get: async <T>(url: string) => {
|
||||||
const response = await axiosClient.get<T>(url, {
|
const response = await axiosClient.get<T>(url);
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return { data: response.data, response };
|
return { data: response.data, response };
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -144,7 +128,6 @@ export const webRequestor = {
|
|||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -156,7 +139,6 @@ export const webRequestor = {
|
|||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user