core/utils/Fetch.ts
2021-03-23 22:53:11 +09:00

25 lines
623 B
TypeScript

import { ResponseProps } from '@types'
import { KoreanbotsEndPoints } from './Constants'
const Fetch = async <T>(endpoint: string, options?: RequestInit, rawEndpoint=false): Promise<ResponseProps<T>> => {
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