Преглед изворни кода

feat: axios request | react-query wrapper | API routes

Oleksandr Honcharov пре 1 година
родитељ
комит
d159445fd6
7 измењених фајлова са 40 додато и 0 уклоњено
  1. 11 0
      src/api/query/index.ts
  2. 1 0
      src/constants/index.ts
  3. 9 0
      src/storage/async-storage.ts
  4. 11 0
      src/types/api.ts
  5. 1 0
      src/types/index.ts
  6. 1 0
      src/utils/index.ts
  7. 6 0
      src/utils/request.ts

+ 11 - 0
src/api/query/index.ts

@@ -0,0 +1,11 @@
+import { useQuery } from '@tanstack/react-query';
+import { request } from '../../utils';
+import { API } from '../../types';
+
+export const apiCall = (key: API, data?: unknown, enabled?: boolean) => {
+  return useQuery({
+    enabled: false,
+    queryKey: [key],
+    queryFn: () => request.postForm(key, data ?? {})
+  });
+};

+ 1 - 0
src/constants/index.ts

@@ -0,0 +1 @@
+export * from './secrets';

+ 9 - 0
src/storage/async-storage.ts

@@ -0,0 +1,9 @@
+import AsyncStorage from '@react-native-async-storage/async-storage';
+
+export const get = (key: string) => {
+  return AsyncStorage.getItem(key);
+};
+
+export const set = (key: string, value: string) => {
+  AsyncStorage.setItem(key, value).then();
+};

+ 11 - 0
src/types/api.ts

@@ -0,0 +1,11 @@
+export enum API_ROUTE {
+  USER = 'user'
+}
+
+export enum API_ENDPOINT {
+  LOGIN = 'login'
+}
+
+export enum API {
+  LOGIN = `${API_ROUTE.USER}/${API_ENDPOINT.LOGIN}`
+}

+ 1 - 0
src/types/index.ts

@@ -1 +1,2 @@
 export * from './navigation';
+export * from './api';

+ 1 - 0
src/utils/index.ts

@@ -0,0 +1 @@
+export * from './request';

+ 6 - 0
src/utils/request.ts

@@ -0,0 +1,6 @@
+import axios from 'axios';
+import { API_URL } from '../constants';
+
+export const request = axios.create({
+  baseURL: API_URL
+});