feat: added Fetch function

This commit is contained in:
원더 2021-02-08 22:40:41 +09:00
parent 9fa0e2000f
commit e5b576fec7

20
utils/Fetch.ts Normal file
View File

@ -0,0 +1,20 @@
import { ResponseProps } from '@types'
import { KoreanbotsEndPoints } from './Constants'
const Fetch = async <T>(endpoint: string, options?: RequestInit):Promise<ResponseProps<T>> => {
const url = KoreanbotsEndPoints.baseAPI + ( endpoint.startsWith('/') ? endpoint : '/' + endpoint)
const res = await fetch(url, options ?? { method: 'GET' })
let json = {}
try {
json = await res.json()
} catch {
json = { code: 500, message: (await res.text()) }
}
return json
}
export default Fetch