Skip to content

Commit

Permalink
add fallback to clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Jan 10, 2024
1 parent 0eae727 commit d042fc8
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,57 @@ import { GithubObject, TezosDocumentation, TezosAPIStorage } from './models'
import { capitalizeFirstLetter } from './helper'

export const fetchGithubObjects = async () => {
const cache = localStorage.getItem('tezos_api_storage')
try {
const cache = localStorage.getItem('tezos_api_storage')

if (cache) {
const storage = JSON.parse(cache) as TezosAPIStorage
if (Date.now() > storage.timestamp + 1000 * 60 * 60 * 24) {
return storage.tezosNodes
if (cache) {
const storage = JSON.parse(cache) as TezosAPIStorage
if (Date.now() > storage.timestamp + 1000 * 60 * 60 * 24) {
return storage.tezosNodes
}
}
}

const apiUrl =
'https://api.github.com/repos/tezos/tezos-mirror/contents/docs/api'
const githubResponse = await fetch(apiUrl)
const githubObjects = (await githubResponse.json()) as GithubObject[]
const tezosNodes = githubObjects
.filter((file) => file.name.includes('openapi.json'))
.map((file) => {
const args = file.name.split('-')
if (args[0] === 'rpc') {
return {
network: 'all',
type: 'general',
url: file.download_url,
}
} else if (args[1] === 'mempool') {
return {
network: args[0] + 'net',
type: args[1],
url: file.download_url,
const apiUrl =
'https://api.github.com/repos/tezos/tezos-mirror/contents/docs/api'
const githubResponse = await fetch(apiUrl)
const githubObjects = (await githubResponse.json()) as GithubObject[]
const tezosNodes = githubObjects
.filter((file) => file.name.includes('openapi.json'))
.map((file) => {
const args = file.name.split('-')
if (args[0] === 'rpc') {
return {
network: 'all',
type: 'general',
url: file.download_url,
}
} else if (args[1] === 'mempool') {
return {
network: args[0] + 'net',
type: args[1],
url: file.download_url,
}
} else {
return {
network: args[0] + 'net',
type: 'blocks',
url: file.download_url,
}
}
} else {
return {
network: args[0] + 'net',
type: 'blocks',
url: file.download_url,
}
}
})
})

const storage: TezosAPIStorage = {
tezosNodes,
timestamp: Date.now(),
}
const storage: TezosAPIStorage = {
tezosNodes,
timestamp: Date.now(),
}

localStorage.setItem('tezos_api_storage', JSON.stringify(storage))
return tezosNodes as TezosDocumentation[]
localStorage.setItem('tezos_api_storage', JSON.stringify(storage))
return tezosNodes as TezosDocumentation[]
} catch (e) {
console.log(e)
localStorage.removeItem('tezos_api_storage')
return []
}
}

export const fetchDocumentation = async (
Expand Down

0 comments on commit d042fc8

Please sign in to comment.