diff options
author | Joseph Ditton <jditton.atomic@gmail.com> | 2021-11-23 14:04:12 -0700 |
---|---|---|
committer | Joseph Ditton <jditton.atomic@gmail.com> | 2021-11-23 14:04:12 -0700 |
commit | 8d0b32f8dfe45291426e58f6bf20cffac8dab6e7 (patch) | |
tree | ec4c1e08e8698d7118641612b67bce940019b3dc /client/utils/use_api.js | |
parent | 4ae4e874689a71e33cdd7a5799fc0c85c4861367 (diff) | |
download | locchat-8d0b32f8dfe45291426e58f6bf20cffac8dab6e7.tar.gz locchat-8d0b32f8dfe45291426e58f6bf20cffac8dab6e7.zip |
adds api, guard, tailwind
Diffstat (limited to 'client/utils/use_api.js')
-rw-r--r-- | client/utils/use_api.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/client/utils/use_api.js b/client/utils/use_api.js new file mode 100644 index 0000000..aab0293 --- /dev/null +++ b/client/utils/use_api.js @@ -0,0 +1,37 @@ +import { useRef } from 'react'; + +export const useApi = (authToken) => { + const apiRef = useRef(new Api()); + apiRef.current.authToken = authToken; + return apiRef.current; +}; + +export class Api { + authToken = null; + + makeRequest(url, method, body) { + return fetch(url, { + method, + headers: { + Authorization: `Bearer ${this.authToken}`, + }, + body, + }).then((res) => res.json()); + } + + get(url) { + return this.makeRequest(url, 'GET'); + } + + post(url, body = {}) { + return this.makeRequest(url, 'POST', body); + } + + put(url, body = {}) { + return this.makeRequest(url, 'PUT', body); + } + + del(url) { + return this.makeRequest(url, 'DELETE'); + } +} |