From e5b576fec71df446ea8e58762c5f70525f2c05ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Mon, 8 Feb 2021 22:40:41 +0900 Subject: [PATCH] feat: added Fetch function --- utils/Fetch.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 utils/Fetch.ts diff --git a/utils/Fetch.ts b/utils/Fetch.ts new file mode 100644 index 0000000..47f5d2d --- /dev/null +++ b/utils/Fetch.ts @@ -0,0 +1,20 @@ +import { ResponseProps } from '@types' +import { KoreanbotsEndPoints } from './Constants' + +const Fetch = async (endpoint: string, options?: RequestInit):Promise> => { + 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 \ No newline at end of file