From b45ae1fee5103f3cc3b58aa60b8e61857418eae4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chif=20Gerg=C5=91?= <chif.gergo@cloud.bme.hu>
Date: Fri, 28 Aug 2020 20:45:36 +0200
Subject: [PATCH] Wrap axios query methods inside a hook

---
 src/hooks/useRestQueries.ts | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 src/hooks/useRestQueries.ts

diff --git a/src/hooks/useRestQueries.ts b/src/hooks/useRestQueries.ts
new file mode 100644
index 0000000..1201e5c
--- /dev/null
+++ b/src/hooks/useRestQueries.ts
@@ -0,0 +1,20 @@
+import axios, { AxiosRequestConfig } from 'axios';
+import { ApiRequest, RequestParams } from './types';
+
+function userRestQueries(
+  // When a request needs more config like CancelToken, etc.
+  config?: AxiosRequestConfig,
+): Record<string, <Data>(path: string) => ApiRequest<Data>> {
+  return {
+    get: <Data>(path: string) => (params: RequestParams) =>
+      axios.get<Data>(path, { ...config, ...params }),
+    post: <Data>(path: string) => (params: RequestParams) =>
+      axios.post<Data>(path, params.body ?? {}, { ...config, ...params }),
+    put: <Data>(path: string) => (params: RequestParams) =>
+      axios.put<Data>(path, params.body ?? {}, { ...config, ...params }),
+    delete: (path: string) => (params: RequestParams) =>
+      axios.delete(path, { ...config, ...params }),
+  };
+}
+
+export default userRestQueries;
-- 
GitLab