diff --git a/src/plugins/api.js b/src/plugins/api.js index e73372c..0267b29 100644 --- a/src/plugins/api.js +++ b/src/plugins/api.js @@ -2,7 +2,7 @@ import { Repository } from "./classes.js"; import axios from "axios" -import { BASE_URL_REPO, BASE_URL_USER, HEADERS } from "./apiConfig/config.js" +import { BASE_URL_REPO, BASE_URL_USER, getHeaders } from "./apiConfig/config.js" let repoOwner = ""; let repoName = ""; @@ -21,7 +21,7 @@ export function setInfosForApi(currRepoOwner, currRepoName, currBranch) { * @returns {Promise} the array of emails with attributes 'email', 'primary', 'verified', 'visibility' */ export async function getUserEmail() { - const headers = HEADERS; + const headers = getHeaders(); return axios .get(`${BASE_URL_USER}/public_emails`, { headers }) .then((response) => response.data) @@ -37,7 +37,7 @@ export async function getUserEmail() { * @returns {Promise} the array of informations with attributes 'login', 'name', etc. */ export async function getUserName() { - const headers = HEADERS + const headers = getHeaders() return axios.get(BASE_URL_USER, { headers }) .then((response) => response.data) .catch((err) => { @@ -52,7 +52,7 @@ export async function getUserName() { * @returns {Promise} informations about the branch with attributes 'commit: { sha }', etc. */ export async function getCommitSha() { - const headers = HEADERS + const headers = getHeaders() return axios.get(`${BASE_URL_REPO}/${repoOwner}/${repoName}/branches/${branch}`, { headers }) .then((response) => response.data) .catch((err) => { @@ -68,7 +68,7 @@ export async function getCommitSha() { * @returns {Promise} informations about the newly created file with attributes 'sha', etc. */ export async function createBlobs(file) { - const headers = HEADERS; + const headers = getHeaders(); const copyHeader = { ...headers }; // Spread syntax to create a shallow copy copyHeader["Accept"] = "application/vnd.github+json"; return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/blobs`, @@ -92,7 +92,7 @@ export async function createBlobs(file) { * @returns {Promise} informations about the newly created tree with attributes 'sha', etc. */ export async function createFileTree(lastCommitSha, folderTree) { - const headers = HEADERS + const headers = getHeaders() return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/trees`, { base_tree: lastCommitSha, tree: folderTree @@ -116,7 +116,7 @@ export async function createFileTree(lastCommitSha, folderTree) { * @returns {Promise} informations about the created commit with attributes 'sha', etc. */ export async function createCommit(commitMessage, authorInfos, lastCommitSha, treeSha) { - const headers = HEADERS + const headers = getHeaders() return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/commits`, { message: commitMessage, author: authorInfos, @@ -139,7 +139,7 @@ export async function createCommit(commitMessage, authorInfos, lastCommitSha, tr * @returns {Promise} informations about the reference. */ export async function pushToGitHub(newCommitSha) { - const headers = HEADERS + const headers = getHeaders() return axios.post(`${BASE_URL_REPO}/${repoOwner}/${repoName}/git/refs/heads/${branch}`, { ref: "refs/heads/" + branch, sha: newCommitSha @@ -164,10 +164,8 @@ export async function pushToGitHub(newCommitSha) { */ export async function loadRepositoryList(searchText = "", page = 1, perPage = 5) { - const token = localStorage.getItem("authId"); try { - const headers = HEADERS const userId = localStorage.getItem('user'); const body = { @@ -188,7 +186,9 @@ export async function loadRepositoryList(searchText = "", page = 1, perPage = 5) }` }; - const response = await axios.post('https://api.github.com/graphql', body, { headers }); + const response = await axios.post('https://api.github.com/graphql', body, { + headers: getHeaders() + }); if (!response?.data) { return []; @@ -260,7 +260,7 @@ export async function searchRepositoryList(searchString, maxResults = 2, searchR */ export async function loadFileTreeOfRepository(repoFullName, branch) { - const headers = HEADERS + const headers = getHeaders() return axios.get(`${BASE_URL_REPO}/${repoFullName}/git/trees/${branch}?recursive=1`, { headers }) .then((response) => response.data) @@ -279,7 +279,7 @@ export async function loadFileTreeOfRepository(repoFullName, branch) { * @returns {Promise<{ name : string }[]>} the fetched array of branches */ export async function loadBranchesName(repoName, username) { - const headers = HEADERS + const headers = getHeaders() return axios .get(`${BASE_URL_REPO}/${username}/${repoName}/branches?per_page=999`, { headers }) .then((response) => response.data) @@ -299,7 +299,7 @@ export async function loadBranchesName(repoName, username) { * @returns {Promise} a promise with the raw content of the specified file */ export async function loadRawFile(repoFullName, branch, filePath) { - const headers = HEADERS + const headers = getHeaders() if (typeof branch !== "string" || typeof branch != "string") { console.log( diff --git a/src/plugins/apiConfig/config.js b/src/plugins/apiConfig/config.js index c946226..0587c79 100644 --- a/src/plugins/apiConfig/config.js +++ b/src/plugins/apiConfig/config.js @@ -1,9 +1,11 @@ const BASE_URL_USER = "https://api.github.com/user" const BASE_URL_REPO = "https://api.github.com/repos" -const HEADERS = { - Authorization: `Bearer ${localStorage.getItem("authId") - }` + +const getHeaders = () => { + return { + Authorization: `Bearer ${localStorage?.getItem("authId")}` + } } -export { BASE_URL_REPO, BASE_URL_USER, HEADERS } +export { BASE_URL_REPO, BASE_URL_USER, getHeaders } diff --git a/tests/api-searchRepositoryList.test.js b/tests/api-searchRepositoryList.test.js index cb73c9f..8396b9e 100644 --- a/tests/api-searchRepositoryList.test.js +++ b/tests/api-searchRepositoryList.test.js @@ -7,6 +7,7 @@ global.localStorage = { return "abc...."; } }; +console.log("###### print local storage #######", localStorage.getItem("authId")) import { searchRepositoryList } from "../src/plugins/api.js"; import { searchTermRepoPairs } from "./constants.js"; @@ -21,6 +22,7 @@ for (let i = 0; i < searchTermRepoPairs.length; i++) { let expectedResult = searchTermRepoPairs[i].results; test("Test Searching Repos with list in parameter. Searching for " + searchTerm, async () => { + console.log(localStorage.getItem("authId")) let list = []; axios.post.mockResolvedValue({