diff --git a/package.json b/package.json index 72d3d7f..795c5bd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 0a15427..6e0298a 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -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' diff --git a/src/utils/configHelper.ts b/src/utils/configHelper.ts index 5051a93..d0ac7f8 100644 --- a/src/utils/configHelper.ts +++ b/src/utils/configHelper.ts @@ -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([ - '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 + }[] = [ + { + version: '1.0.1', + async migrate(version) { + const config: any | void = await storageHelper.get([ + '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([ + '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 { await prepareConfig - const config = await storageHelper.get>([platformName]) - return applyDefaultConfigs((config && config[platformName]) || {}) + const config = await storageHelper.get>([platformStorageKey]) + return applyDefaultConfigs((config && config[platformStorageKey]) || {}) } export async function set(config: Config) { - return await storageHelper.set({ [platformName]: config }) + return await storageHelper.set({ [platformStorageKey]: config }) }