adding headers on the backend

This commit is contained in:
2024-09-09 22:00:11 -06:00
parent b7eaae0ea4
commit 029e3ff7eb
4 changed files with 28 additions and 25 deletions

View File

@@ -1,11 +1,15 @@
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";
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();
if (!isServer) {
console.log("not on the server, setting up interceptor");
axiosClient.interceptors.request.use((config) => {
if (
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(
(response) => response,
(error: AxiosError) => {

View File

@@ -7,6 +7,7 @@ const baseCanvasUrl = "https://snow.instructure.com/api/v1";
export const canvasPageService = {
async getAll(courseId: number): Promise<CanvasPage[]> {
console.log("requesting pages");
const url = `${baseCanvasUrl}/courses/${courseId}/pages`;
const pages = await canvasServiceUtils.paginatedRequest<CanvasPage[]>({
url,

View File

@@ -2,11 +2,6 @@ import { axiosClient } from "../axiosUtils";
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 rateLimitSleepInterval = 1000;
@@ -44,7 +39,6 @@ const rateLimitAwarePost = async (
method: "POST",
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
@@ -81,8 +75,6 @@ const recursiveDelete = async (
...options,
method: "DELETE",
headers: {
...options.headers,
Authorization: `Bearer ${token}`,
},
});
@@ -113,20 +105,12 @@ const recursiveDelete = async (
};
export const webRequestor = {
getMany: async <T>(url: string, options: FetchOptions = {}) => {
const response = await axiosClient.get<T[]>(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const response = await axiosClient.get<T[]>(url);
return { data: response.data, response };
},
get: async <T>(url: string) => {
const response = await axiosClient.get<T>(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const response = await axiosClient.get<T>(url);
return { data: response.data, response };
},
@@ -144,7 +128,6 @@ export const webRequestor = {
method: "PUT",
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});
@@ -156,7 +139,6 @@ export const webRequestor = {
body: JSON.stringify(body),
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
});