core/utils/Fetch.ts
SKINMAKER b421d1ab64
chore: apply prettier (#637)
* chore: apply prettier

* chore: edit ready comment

* chore: move ts comment
2023-11-29 22:04:33 +09:00

32 lines
659 B
TypeScript

import { ResponseProps } from '@types'
import { KoreanbotsEndPoints } from './Constants'
const Fetch = async <T>(
endpoint: string,
options?: RequestInit,
rawEndpoint = false
): Promise<ResponseProps<T>> => {
options = options ?? {}
const url =
(rawEndpoint ? '' : KoreanbotsEndPoints.baseAPI) +
(endpoint.startsWith('/') ? endpoint : '/' + endpoint)
const res = await fetch(url, {
method: 'GET',
headers: { 'content-type': 'application/json', ...options.headers },
...options,
})
let json = {}
try {
json = await res.json()
} catch {
json = { code: 500, message: 'Internal Server Error' }
}
return json
}
export default Fetch