From 8a70975cda8f954b91a956324ad90c95dadb831e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E5=93=92=E8=B5=AB=E8=90=9D?= Date: Thu, 29 Jun 2023 05:00:46 -0700 Subject: [PATCH] :hammer: Refactor: refactor of some apis --- src/renderer/apis/aliyun.ts | 34 +++++++++++++---------- src/renderer/apis/allApi.ts | 8 +++--- src/renderer/apis/github.ts | 43 +++++++++++++++++++---------- src/renderer/apis/imgur.ts | 54 ++++++++++++++++++++++++++++--------- src/renderer/apis/qiniu.ts | 22 +++++++++------ src/renderer/apis/smms.ts | 25 ++++++++++++----- src/renderer/apis/tcyun.ts | 27 ++++++++++++++----- 7 files changed, 147 insertions(+), 66 deletions(-) diff --git a/src/renderer/apis/aliyun.ts b/src/renderer/apis/aliyun.ts index 39617a7..576402f 100644 --- a/src/renderer/apis/aliyun.ts +++ b/src/renderer/apis/aliyun.ts @@ -12,25 +12,31 @@ interface IConfigMap { } export default class AliyunApi { + private static createClient (config: IConfigMap['config']): OSS { + const { accessKeyId, accessKeySecret, bucket, area } = config + return new OSS({ + accessKeyId, + accessKeySecret, + bucket, + region: area + }) + } + + private static getKey (fileName: string, path?: string): string { + return path && path !== '/' + ? `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}` + : fileName + } + static async delete (configMap: IConfigMap): Promise { - const { fileName, config: { accessKeyId, accessKeySecret, bucket, area, path } } = configMap + const { fileName, config } = configMap try { - const client = new OSS({ - accessKeyId, - accessKeySecret, - bucket, - region: area - }) - let key - if (path === '/' || !path) { - key = fileName - } else { - key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}` - } + const client = AliyunApi.createClient(config) + const key = AliyunApi.getKey(fileName, config.path) const result = await client.delete(key) as any return result.res.status === 204 } catch (error) { - console.log(error) + console.error(error) return false } } diff --git a/src/renderer/apis/allApi.ts b/src/renderer/apis/allApi.ts index 7626277..79234f1 100644 --- a/src/renderer/apis/allApi.ts +++ b/src/renderer/apis/allApi.ts @@ -22,10 +22,10 @@ const apiMap: IStringKeyMap = { export default class ALLApi { static async delete (configMap: IStringKeyMap): Promise { - if (apiMap[configMap.type] !== undefined) { - return await apiMap[configMap.type].delete(configMap) - } else { - return false + const api = apiMap[configMap.type] + if (api) { + return await api.delete(configMap) } + return false } } diff --git a/src/renderer/apis/github.ts b/src/renderer/apis/github.ts index b46c994..0a6a82e 100644 --- a/src/renderer/apis/github.ts +++ b/src/renderer/apis/github.ts @@ -1,21 +1,36 @@ import { Octokit } from '@octokit/rest' +interface IConfigMap { + fileName: string + hash: string + config: { + repo: string + token: string + branch: string + path?: string + } +} + export default class GithubApi { - static async delete (configMap: IStringKeyMap): Promise { - const { fileName, hash, config: { repo, token, branch, path } } = configMap - const owner = repo.split('/')[0] - const repoName = repo.split('/')[1] - const octokit = new Octokit({ + private static createOctokit (token: string) { + return new Octokit({ auth: token }) - let key - if (path === '/' || !path) { - key = fileName - } else { - key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}` - } + } + + private static createKey (path: string | undefined, fileName: string): string { + return path && path !== '/' + ? `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}` + : fileName + } + + static async delete (configMap: IConfigMap): Promise { + const { fileName, hash, config: { repo, token, branch, path } } = configMap + const [owner, repoName] = repo.split('/') + const octokit = GithubApi.createOctokit(token) + const key = GithubApi.createKey(path, fileName) try { - const result = await octokit.rest.repos.deleteFile({ + const { status } = await octokit.rest.repos.deleteFile({ owner, repo: repoName, path: key, @@ -23,9 +38,9 @@ export default class GithubApi { sha: hash, branch }) - return result.status === 200 + return status === 200 } catch (error) { - console.log(error) + console.error(error) return false } } diff --git a/src/renderer/apis/imgur.ts b/src/renderer/apis/imgur.ts index 16218f5..3497b95 100644 --- a/src/renderer/apis/imgur.ts +++ b/src/renderer/apis/imgur.ts @@ -1,12 +1,45 @@ -import axios from 'axios' +import axios, { AxiosResponse } from 'axios' + +interface IConfigMap { + config?: { + clientId?: string + username?: string + accessToken?: string + } + hash?: string +} + +interface IConfig { + headers: { + Authorization: string + } + timeout: number +} export default class ImgurApi { - static async delete (configMap: IStringKeyMap): Promise { + private static async makeRequest ( + method: 'delete', + url: string, + config: IConfig, + logError: boolean = true + ): Promise { + try { + const response: AxiosResponse = await axios[method](url, config) + return response.status === 200 + } catch (error) { + if (logError) { + console.error(error) + } + return false + } + } + + static async delete (configMap: IConfigMap): Promise { const { config = {}, hash = '' } = configMap || {} const { clientId = '', username = '', accessToken = '' } = config const baseUrl = 'https://api.imgur.com/3' - let Authorization - let apiUrl + let Authorization: string + let apiUrl: string if (username && accessToken) { Authorization = `Bearer ${accessToken}` apiUrl = `${baseUrl}/account/${username}/image/${hash}` @@ -19,15 +52,10 @@ export default class ImgurApi { const headers = { Authorization } - try { - const res = await axios.delete(apiUrl, { - headers, - timeout: 30000 - }) - return res.status === 200 - } catch (error) { - console.log(error) - return false + const requestConfig: IConfig = { + headers, + timeout: 30000 } + return ImgurApi.makeRequest('delete', apiUrl, requestConfig) } } diff --git a/src/renderer/apis/qiniu.ts b/src/renderer/apis/qiniu.ts index c678bb6..abb27a1 100644 --- a/src/renderer/apis/qiniu.ts +++ b/src/renderer/apis/qiniu.ts @@ -1,18 +1,24 @@ import Qiniu from 'qiniu' +interface IConfigMap { + fileName: string; + config: { + accessKey: string; + secretKey: string; + bucket: string; + path?: string; + } +} + export default class QiniuApi { - static async delete (configMap: IStringKeyMap): Promise { + static async delete (configMap: IConfigMap): Promise { const { fileName, config: { accessKey, secretKey, bucket, path } } = configMap const mac = new Qiniu.auth.digest.Mac(accessKey, secretKey) const qiniuConfig = new Qiniu.conf.Config() try { const bucketManager = new Qiniu.rs.BucketManager(mac, qiniuConfig) - let key = '' - if (path === '/' || !path) { - key = fileName - } else { - key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}` - } + const formattedPath = path?.replace(/^\//, '').replace(/\/$/, '') || '' + const key = path === '/' || !path ? fileName : `${formattedPath}/${fileName}` const res = await new Promise((resolve, reject) => { bucketManager.delete(bucket, key, (err, respBody, respInfo) => { if (err) { @@ -27,7 +33,7 @@ export default class QiniuApi { }) as any return res && res.respInfo.statusCode === 200 } catch (error) { - console.log(error) + console.error(error) return false } } diff --git a/src/renderer/apis/smms.ts b/src/renderer/apis/smms.ts index d154316..4f3a565 100644 --- a/src/renderer/apis/smms.ts +++ b/src/renderer/apis/smms.ts @@ -1,15 +1,26 @@ -import axios from 'axios' +import axios, { AxiosResponse } from 'axios' + +interface IConfigMap { + hash?: string + config?: { + token?: string + } +} export default class SmmsApi { private static readonly baseUrl = 'https://smms.app/api/v2' - static async delete (configMap: IStringKeyMap): Promise { - const { hash, config: { token } } = configMap - if (!hash || !token) { + static async delete (configMap: IConfigMap): Promise { + const { hash, config } = configMap + if (!hash || !config || !config.token) { + console.error('SmmsApi.delete: invalid params') return false } + + const { token } = config + try { - const res = await axios.get( + const response: AxiosResponse = await axios.get( `${SmmsApi.baseUrl}/delete/${hash}`, { headers: { Authorization: token @@ -20,9 +31,9 @@ export default class SmmsApi { }, timeout: 30000 }) - return res.status === 200 + return response.status === 200 } catch (error) { - console.log(error) + console.error(error) return false } } diff --git a/src/renderer/apis/tcyun.ts b/src/renderer/apis/tcyun.ts index ac99bdc..fef09f3 100644 --- a/src/renderer/apis/tcyun.ts +++ b/src/renderer/apis/tcyun.ts @@ -1,13 +1,28 @@ import COS from 'cos-nodejs-sdk-v5' +interface IConfigMap { + fileName: string; + config: { + secretId: string; + secretKey: string; + bucket: string; + area: string; + path?: string; + }; +} + export default class TcyunApi { - static async delete (configMap: IStringKeyMap): Promise { + private static createCOS (SecretId: string, SecretKey: string): COS { + return new COS({ + SecretId, + SecretKey + }) + } + + static async delete (configMap: IConfigMap): Promise { const { fileName, config: { secretId, secretKey, bucket, area, path } } = configMap try { - const cos = new COS({ - SecretId: secretId, - SecretKey: secretKey - }) + const cos = TcyunApi.createCOS(secretId, secretKey) let key if (path === '/' || !path) { key = `/${fileName}` @@ -21,7 +36,7 @@ export default class TcyunApi { }) return result.statusCode === 204 } catch (error) { - console.log(error) + console.error(error) return false } }