Skip to content

Commit

Permalink
linting + move node-fetch to dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcastel50 committed May 31, 2024
1 parent 70a0f23 commit 5602e8c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"material-ui-popup-state": "5.0.4",
"matrix-widget-api": "1.1.1",
"msw": "0.47.4",
"node-fetch": "2",
"normalize.css": "8.0.1",
"postprocessing": "6.29.3",
"prop-types": "15.8.1",
Expand Down Expand Up @@ -144,7 +143,8 @@
"identity-obj-proxy": "3.0.0",
"jest": "29.3.1",
"jest-environment-jsdom": "29.3.1",
"jsdom": "20.0.3"
"jsdom": "20.0.3",
"node-fetch": "2"
},
"engines": {
"node": ">=18"
Expand Down
120 changes: 60 additions & 60 deletions src/net/github/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,84 @@ const httpCacheApiAvailable = ('caches' in window)
*
* @return {Promise<Cache | object>} The HTTP cache object.
*/
async function getCache() {
if (!httpCache) {
httpCache = await openCache()
}
return httpCache
async function getCache() {
if (!httpCache) {
httpCache = await openCache()
}
return httpCache
}

/**
* Opens the HTTP cache if the Cache API is available.
*
* @return {Promise<Cache | object>} A Cache object if the Cache API is available, otherwise an empty object.
*/
async function openCache() {
if (httpCacheApiAvailable) {
/**
* Opens the HTTP cache if the Cache API is available.
*
* @return {Promise<Cache | object>} A Cache object if the Cache API is available, otherwise an empty object.
*/
async function openCache() {
if (httpCacheApiAvailable) {
return await caches.open('bldrs-github-api-cache')
}
// fallback to caching only on current page, won't survive page reloads
return {}
}
// fallback to caching only on current page, won't survive page reloads
return {}
}

/**
* Converts a cached response to an Octokit response format.
*
* @param {Response|null} cachedResponse - The cached response to convert.
* @return {Promise<object | null>} A structured object mimicking an Octokit response, or null if the response is invalid.
*/
async function convertToOctokitResponse(cachedResponse) {
if (!cachedResponse) {
return null
}
/**
* Converts a cached response to an Octokit response format.
*
* @param {Response|null} cachedResponse The cached response to convert.
* @return {Promise<object | null>} A structured object mimicking an Octokit response, or null if the response is invalid.
*/
async function convertToOctokitResponse(cachedResponse) {
if (!cachedResponse) {
return null
}

const data = await cachedResponse.json()
const headers = cachedResponse.headers
const status = cachedResponse.status
const data = await cachedResponse.json()
const headers = cachedResponse.headers
const status = cachedResponse.status

// Create a structure that mimics an Octokit response
const octokitResponse = {
data: data,
status: status,
headers: {},
url: cachedResponse.url,
}
// Create a structure that mimics an Octokit response
const octokitResponse = {
data: data,
status: status,
headers: {},
url: cachedResponse.url,
}

// Iterate over headers and add them to the response object
headers.forEach((value, key) => {
octokitResponse.headers[key] = value
})
// Iterate over headers and add them to the response object
headers.forEach((value, key) => {
octokitResponse.headers[key] = value
})

return octokitResponse
}
return octokitResponse
}

/**
* Checks the cache for a specific key and converts the response to an Octokit response format.
*
* @param {string} key - The key to search for in the cache.
* @return {Promise<object | null>} The cached response in Octokit format, or null if not found or an error occurs.
*/
export async function checkCache(key) {
try {
if (httpCacheApiAvailable) {
const _httpCache = await getCache()
const response = await _httpCache.match(key)
return await convertToOctokitResponse(response)
} else {
return httpCache[key]
}
} catch (error) {
return null
/**
* Checks the cache for a specific key and converts the response to an Octokit response format.
*
* @param {string} key The key to search for in the cache.
* @return {Promise<object | null>} The cached response in Octokit format, or null if not found or an error occurs.
*/
export async function checkCache(key) {
try {
if (httpCacheApiAvailable) {
const _httpCache = await getCache()
const response = await _httpCache.match(key)
return await convertToOctokitResponse(response)
} else {
return httpCache[key]
}
} catch (error) {
return null
}
}


/**
* Updates the cache entry for a given key with the response received.
* The cache will only be updated if the response headers contain an ETag.
*
* @param {string} key - The cache key associated with the request.
* @param {object} response - The HTTP response object from Octokit which includes headers and data.
* @param {string} key The cache key associated with the request.
* @param {object} response The HTTP response object from Octokit which includes headers and data.
*/
export async function updateCache(key, response) {
if (response.headers.etag) {
Expand Down

0 comments on commit 5602e8c

Please sign in to comment.