2023-06-29 08:00:46 -04:00
|
|
|
import axios, { AxiosResponse } from 'axios'
|
|
|
|
|
|
|
|
interface IConfigMap {
|
2024-04-09 03:03:32 -04:00
|
|
|
config?: Partial<IImgurConfig>
|
2023-06-29 08:00:46 -04:00
|
|
|
hash?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IConfig {
|
|
|
|
headers: {
|
|
|
|
Authorization: string
|
|
|
|
}
|
|
|
|
timeout: number
|
|
|
|
}
|
2023-02-15 10:36:47 -05:00
|
|
|
|
|
|
|
export default class ImgurApi {
|
2023-08-29 05:12:32 -04:00
|
|
|
static baseUrl = 'https://api.imgur.com/3'
|
2023-06-29 08:00:46 -04:00
|
|
|
private static async makeRequest (
|
|
|
|
method: 'delete',
|
|
|
|
url: string,
|
2023-08-30 09:10:28 -04:00
|
|
|
config: IConfig
|
2023-06-29 08:00:46 -04:00
|
|
|
): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
const response: AxiosResponse = await axios[method](url, config)
|
|
|
|
return response.status === 200
|
|
|
|
} catch (error) {
|
2023-08-30 09:10:28 -04:00
|
|
|
console.error(error)
|
2023-06-29 08:00:46 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async delete (configMap: IConfigMap): Promise<boolean> {
|
2023-08-10 08:30:46 -04:00
|
|
|
const {
|
|
|
|
config: { clientId = '', username = '', accessToken = '' } = {},
|
|
|
|
hash = ''
|
|
|
|
} = configMap
|
|
|
|
let Authorization: string, apiUrl: string
|
|
|
|
|
2023-04-15 09:51:30 -04:00
|
|
|
if (username && accessToken) {
|
|
|
|
Authorization = `Bearer ${accessToken}`
|
2023-08-10 08:30:46 -04:00
|
|
|
apiUrl = `${ImgurApi.baseUrl}/account/${username}/image/${hash}`
|
2023-04-15 09:51:30 -04:00
|
|
|
} else if (clientId) {
|
|
|
|
Authorization = `Client-ID ${clientId}`
|
2023-08-10 08:30:46 -04:00
|
|
|
apiUrl = `${ImgurApi.baseUrl}/image/${hash}`
|
2023-04-15 09:51:30 -04:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2023-06-29 08:00:46 -04:00
|
|
|
const requestConfig: IConfig = {
|
2023-08-10 08:30:46 -04:00
|
|
|
headers: { Authorization },
|
2023-06-29 08:00:46 -04:00
|
|
|
timeout: 30000
|
2023-02-15 10:36:47 -05:00
|
|
|
}
|
2023-06-29 08:00:46 -04:00
|
|
|
return ImgurApi.makeRequest('delete', apiUrl, requestConfig)
|
2023-02-15 10:36:47 -05:00
|
|
|
}
|
|
|
|
}
|