mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
Merge branch 'develop' into feature/enterprise
This commit is contained in:
commit
83aca3e5db
3 changed files with 83 additions and 43 deletions
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "gitako",
|
||||
"version": "1.3.2",
|
||||
"version": "1.3.4",
|
||||
"description": "File tree for GitHub, and more than that.",
|
||||
"repository": "https://github.com/EnixCoda/Gitako",
|
||||
"author": "EnixCoda",
|
||||
|
|
|
|||
|
|
@ -27,33 +27,36 @@ async function request(
|
|||
if (accessToken) {
|
||||
headers.Authorization = `token ${accessToken}`
|
||||
}
|
||||
|
||||
let res: Response
|
||||
try {
|
||||
const res = await fetch(url, { headers })
|
||||
const contentType = res.headers.get('Content-Type') || res.headers.get('content-type')
|
||||
const isJson = contentType?.includes('application/json')
|
||||
// About res.ok:
|
||||
// True if res.status between 200~299
|
||||
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
|
||||
if (res.ok) {
|
||||
if (isJson) return res.json()
|
||||
throw new Error(`Response content type is "${contentType}"`)
|
||||
} else {
|
||||
if (res.status === 404 || res.status === 401) throw new Error(errors.NOT_FOUND)
|
||||
else if (res.status === 403) throw new Error(errors.API_RATE_LIMIT)
|
||||
else if (res.status === 500) throw new Error(errors.SERVER_FAULT)
|
||||
else if (isJson) {
|
||||
const content = await res.json()
|
||||
if (apiRateLimitExceeded(content)) throw new Error(errors.API_RATE_LIMIT)
|
||||
if (isEmptyProject(content)) throw new Error(errors.EMPTY_PROJECT)
|
||||
if (isBlockedProject(content)) throw new Error(errors.BLOCKED_PROJECT)
|
||||
throw new Error(`Unknown message content "${content?.message}"`)
|
||||
} else {
|
||||
throw new Error(`Response content type is "${contentType}"`)
|
||||
}
|
||||
}
|
||||
res = await fetch(url, { headers })
|
||||
} catch (err) {
|
||||
throw new Error(errors.CONNECTION_BLOCKED)
|
||||
}
|
||||
|
||||
const contentType = res.headers.get('Content-Type') || res.headers.get('content-type')
|
||||
const isJson = contentType?.includes('application/json')
|
||||
// About res.ok:
|
||||
// True if res.status between 200~299
|
||||
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
|
||||
if (res.ok) {
|
||||
if (isJson) return res.json()
|
||||
throw new Error(`Response content type is "${contentType}"`)
|
||||
} else {
|
||||
if (res.status === 404 || res.status === 401) throw new Error(errors.NOT_FOUND)
|
||||
else if (res.status === 403) throw new Error(errors.API_RATE_LIMIT)
|
||||
else if (res.status === 500) throw new Error(errors.SERVER_FAULT)
|
||||
else if (isJson) {
|
||||
const content = await res.json()
|
||||
if (apiRateLimitExceeded(content)) throw new Error(errors.API_RATE_LIMIT)
|
||||
if (isEmptyProject(content)) throw new Error(errors.EMPTY_PROJECT)
|
||||
if (isBlockedProject(content)) throw new Error(errors.BLOCKED_PROJECT)
|
||||
throw new Error(`Unknown message content "${content?.message}"`)
|
||||
} else {
|
||||
throw new Error(`Response content type is "${contentType}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const API_ENDPOINT = isEnterprise() ? `${window.location.host}/api/v3` : 'api.github.com'
|
||||
|
|
|
|||
|
|
@ -49,40 +49,77 @@ type Storage = {
|
|||
|
||||
// separate different platform configs to simplify interactions with browser storage API
|
||||
// e.g.
|
||||
// platform_GitHub?: Config
|
||||
// platform_github.com?: Config
|
||||
}
|
||||
|
||||
async function migrateConfig() {
|
||||
// not referencing to enum above to prevent migrate future configs
|
||||
const config = await storageHelper.get<Config | Storage>([
|
||||
'configVersion',
|
||||
'sideBarWidth',
|
||||
'shortcut',
|
||||
'access_token',
|
||||
'compressSingletonFolder',
|
||||
'copyFileButton',
|
||||
'copySnippetButton',
|
||||
'intelligentToggle',
|
||||
'icons',
|
||||
])
|
||||
if (!config || !('configVersion' in config) || config.configVersion < '1.0.1') {
|
||||
await storageHelper.set({ platform_GitHub: config, configVersion: '1.0.1' })
|
||||
const migrations: {
|
||||
version: string
|
||||
migrate(version: string): Promise<void>
|
||||
}[] = [
|
||||
{
|
||||
version: '1.0.1',
|
||||
async migrate(version) {
|
||||
const config: any | void = await storageHelper.get<Config | Storage>([
|
||||
'configVersion',
|
||||
'sideBarWidth',
|
||||
'shortcut',
|
||||
'access_token',
|
||||
'compressSingletonFolder',
|
||||
'copyFileButton',
|
||||
'copySnippetButton',
|
||||
'intelligentToggle',
|
||||
'icons',
|
||||
])
|
||||
if (config && (!('configVersion' in config) || config.configVersion < version)) {
|
||||
await storageHelper.set({ platform_GitHub: config, configVersion: version })
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
version: '1.3.4',
|
||||
async migrate(version) {
|
||||
const config: any | void = await storageHelper.get<Config | Storage>([
|
||||
'configVersion',
|
||||
'platform_undefined', // this was a mistake :(
|
||||
'platform_GitHub',
|
||||
'platform_github.com',
|
||||
])
|
||||
if (
|
||||
config &&
|
||||
'configVersion' in config &&
|
||||
config.configVersion < version &&
|
||||
(config.platform_GitHub || config.platform_undefined) &&
|
||||
!config['platform_github.com']
|
||||
) {
|
||||
await storageHelper.set({
|
||||
['platform_github.com']: config.platform_GitHub || config.platform_undefined,
|
||||
configVersion: version,
|
||||
})
|
||||
}
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
for (const { version, migrate } of migrations) {
|
||||
await migrate(version)
|
||||
}
|
||||
}
|
||||
|
||||
let platformName: string
|
||||
// do NOT use platform name
|
||||
const platformStorageKey = `platform_` + window.location.host.toLowerCase()
|
||||
const prepareConfig = new Promise(async resolve => {
|
||||
await migrateConfig()
|
||||
platformName = `platform_` + platformName
|
||||
resolve()
|
||||
})
|
||||
|
||||
export async function get(): Promise<Config> {
|
||||
await prepareConfig
|
||||
const config = await storageHelper.get<Record<string, Config>>([platformName])
|
||||
return applyDefaultConfigs((config && config[platformName]) || {})
|
||||
const config = await storageHelper.get<Record<string, Config>>([platformStorageKey])
|
||||
return applyDefaultConfigs((config && config[platformStorageKey]) || {})
|
||||
}
|
||||
|
||||
export async function set(config: Config) {
|
||||
return await storageHelper.set({ [platformName]: config })
|
||||
return await storageHelper.set({ [platformStorageKey]: config })
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue